You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ke...@apache.org on 2007/04/06 20:51:06 UTC

svn commit: r526248 - in /webservices/axis2/branches/java/1_2/modules/json: src/org/apache/axis2/json/ test/org/apache/axis2/json/

Author: keithc
Date: Fri Apr  6 11:51:06 2007
New Revision: 526248

URL: http://svn.apache.org/viewvc?view=rev&rev=526248
Log:
Applying Axis2-2458 to branch

Modified:
    webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
    webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
    webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/Echo.java
    webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java

Modified: webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java?view=diff&rev=526248&r1=526247&r2=526248
==============================================================================
--- webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java (original)
+++ webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java Fri Apr  6 11:51:06 2007
@@ -24,18 +24,17 @@
 import org.apache.axiom.soap.SOAPFault;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
+import org.apache.axis2.description.WSDL2Constants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.axis2.transport.MessageFormatter;
+import org.apache.axis2.transport.http.util.URIEncoderDecoder;
 import org.codehaus.jettison.mapped.MappedNamespaceConvention;
 import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
 
 import javax.xml.stream.FactoryConfigurationError;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
+import java.io.*;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -158,7 +157,6 @@
                         OutputStream out, boolean preserve) throws AxisFault {
         OMElement element = msgCtxt.getEnvelope().getBody().getFirstElement();
         try {
-
             //Mapped format cannot handle element with namespaces.. So cannot handle Faults
             if (element instanceof SOAPFault && this instanceof JSONMessageFormatter) {
                 SOAPFault fault = (SOAPFault)element;
@@ -170,6 +168,7 @@
                     getStringToWrite(((OMSourcedElementImpl)element).getDataSource()) != null) {
                 String jsonToWrite =
                         getStringToWrite(((OMSourcedElementImpl)element).getDataSource());
+
                 out.write(jsonToWrite.getBytes());
             } else {
                 XMLStreamWriter jsonWriter = getJSONWriter(out);
@@ -192,53 +191,39 @@
 
         String httpMethod =
                 (String)msgCtxt.getProperty(Constants.Configuration.HTTP_METHOD);
+        OMElement dataOut = msgCtxt.getEnvelope().getBody().getFirstElement();
 
-        //if the http method is GET, parameters are attached to the target URL
-        if ((httpMethod != null)
+        //if the http method is GET, send the json string as a parameter
+        if (dataOut != null && (httpMethod != null)
                 && Constants.Configuration.HTTP_METHOD_GET.equalsIgnoreCase(httpMethod)) {
-            String param = getParam(msgCtxt);
-
-            if (param != null && param.length() > 0) {
-                String returnURLFile = targetURL.getFile() + "?" + param;
-                try {
-                    return new URL(targetURL.getProtocol(), targetURL.getHost(),
-                                   targetURL.getPort(), returnURLFile);
-                } catch (MalformedURLException e) {
-                    throw new AxisFault(e);
+            try {
+                String jsonString;
+                if (dataOut instanceof OMSourcedElementImpl && getStringToWrite(((OMSourcedElementImpl) dataOut).getDataSource()) != null)
+                {
+                    jsonString = getStringToWrite(((OMSourcedElementImpl) dataOut).getDataSource());
+                } else {
+                    ByteArrayOutputStream out = new ByteArrayOutputStream();
+                    XMLStreamWriter jsonWriter = getJSONWriter(out);
+                    dataOut.serializeAndConsume(jsonWriter);
+                    jsonWriter.writeEndDocument();
+                    jsonString = new String(out.toByteArray());
                 }
-            } else {
-                return targetURL;
+                jsonString = URIEncoderDecoder.quoteIllegal(jsonString, WSDL2Constants.LEGAL_CHARACTERS_IN_URL);
+                String param = "query=" + jsonString;
+                String returnURLFile = targetURL.getFile() + "?" + param;
+
+
+                return new URL(targetURL.getProtocol(), targetURL.getHost(), targetURL.getPort(), returnURLFile);
+            } catch (MalformedURLException e) {
+                throw new AxisFault(e);
+            } catch (XMLStreamException e) {
+                throw new AxisFault(e);
+            } catch (UnsupportedEncodingException e) {
+                throw new AxisFault(e);
             }
         } else {
             return targetURL;
         }
-    }
-
-    private String getParam(MessageContext msgContext) {
-        OMElement dataOut;
-
-        dataOut = msgContext.getEnvelope().getBody().getFirstElement();
-
-        Iterator iter1 = dataOut.getChildElements();
-        ArrayList paraList = new ArrayList();
-
-        while (iter1.hasNext()) {
-            OMElement ele = (OMElement)iter1.next();
-            String parameter;
-
-            parameter = ele.getLocalName() + "=" + ele.getText();
-            paraList.add(parameter);
-        }
-
-        String paraString = "";
-        int count = paraList.size();
-
-        for (int i = 0; i < count; i++) {
-            String c = (String)paraList.get(i);
-            paraString = "".equals(paraString) ? c : (paraString + "&" + c);
-        }
-
-        return paraString;
     }
 
 }

Modified: webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java?view=diff&rev=526248&r1=526247&r2=526248
==============================================================================
--- webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java (original)
+++ webservices/axis2/branches/java/1_2/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java Fri Apr  6 11:51:06 2007
@@ -16,13 +16,19 @@
 
 package org.apache.axis2.json;
 
-import org.apache.axiom.om.OMAbstractFactory;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMNamespace;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.io.ByteArrayInputStream;
+
+import org.apache.axiom.om.*;
+
 import org.apache.axiom.om.impl.OMNamespaceImpl;
 import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
 import org.apache.axis2.AxisFault;
+import org.apache.axis2.Constants;
+import org.apache.axis2.transport.http.util.URIEncoderDecoder;
+import org.apache.axis2.addressing.EndpointReference;
 import org.apache.axis2.builder.Builder;
 import org.apache.axis2.context.MessageContext;
 
@@ -38,13 +44,40 @@
     }
 
     //returns the OMSourcedElementImpl with JSONDataSource inside
-    public OMElement processDocument(InputStream inputStream, String contentType,
-                                     MessageContext messageContext) throws AxisFault {
+
+    public OMElement processDocument(InputStream inputStream, String contentType, MessageContext messageContext) throws AxisFault {
         String localName = "";
         String prefix = "";
         OMNamespace ns = new OMNamespaceImpl("", "");
 
         OMFactory factory = OMAbstractFactory.getOMFactory();
+
+        //if the input stream is null, then check whether the HTTP method is GET, if so get the JSON String which is received as a parameter, and make it an
+        //input stream
+
+        if (inputStream == null) {
+            EndpointReference endpointReference = messageContext.getTo();
+            if (endpointReference == null) {
+                throw new AxisFault("Cannot create DocumentElement without destination EPR");
+            }
+
+            String requestURL;
+            try {
+                requestURL = URIEncoderDecoder.decode(endpointReference.getAddress());
+            } catch (UnsupportedEncodingException e) {
+                throw new AxisFault(e);
+            }
+
+            String jsonString;
+            int index;
+            if ((index = requestURL.indexOf("=")) > 0) {
+                jsonString = requestURL.substring(index + 1);
+                inputStream = new ByteArrayInputStream(jsonString.getBytes());
+            } else {
+                throw new AxisFault("No JSON message received through HTTP GET or POST");
+            }
+        }
+
         try {
             char temp = (char)inputStream.read();
             while (temp != ':') {
@@ -77,8 +110,7 @@
         return new OMSourcedElementImpl(localName, ns, factory, jsonDataSource);
     }
 
-    protected JSONDataSource getDataSource(InputStream jsonInputStream, String prefix,
-                                           String localName) {
+    protected JSONDataSource getDataSource(InputStream jsonInputStream, String prefix, String localName) {
         return new JSONDataSource(jsonInputStream, "\"" + prefix + localName + "\"");
     }
 }

Modified: webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/Echo.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/Echo.java?view=diff&rev=526248&r1=526247&r2=526248
==============================================================================
--- webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/Echo.java (original)
+++ webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/Echo.java Fri Apr  6 11:51:06 2007
@@ -3,6 +3,8 @@
 import org.apache.axiom.om.OMElement;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
+import org.apache.axis2.wsdl.WSDLConstants;
+import org.apache.axis2.transport.http.HTTPConstants;
 import org.apache.axis2.context.MessageContext;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -14,10 +16,15 @@
     }
 
     public OMElement echoOM(OMElement omEle) throws AxisFault {
-        Object object = MessageContext.getCurrentMessageContext()
-                .getProperty(Constants.Configuration.MESSAGE_TYPE);
-        String messageType = (String)object;
-        if (messageType.indexOf("json") < 0) {
+        MessageContext outMsgCtx = MessageContext.getCurrentMessageContext().getOperationContext().getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
+        Object object = outMsgCtx.getProperty(Constants.Configuration.MESSAGE_TYPE);
+        String messageType = (String) object;
+
+        //if the request is through GET, the message type is application/xml. otherwise don't allow
+        //any non json specific message types
+        if (messageType.equalsIgnoreCase(HTTPConstants.MEDIA_TYPE_APPLICATION_XML)) {
+            outMsgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/json");
+        } else if (messageType.indexOf("json") < 0) {
             throw new AxisFault("Type of the Received Message is not JSON");
         }
         return omEle;

Modified: webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java?view=diff&rev=526248&r1=526247&r2=526248
==============================================================================
--- webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java (original)
+++ webservices/axis2/branches/java/1_2/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java Fri Apr  6 11:51:06 2007
@@ -79,9 +79,9 @@
     }
 
     protected void tearDown() throws Exception {
-        if (count == 2) {
-            server.stop();
-        }
+    	if(count == 3){
+    		server.stop();
+    	}
     }
 
     protected OMElement createEnvelope() throws Exception {
@@ -99,12 +99,13 @@
         return rpcWrapEle;
     }
 
-    private void doEchoOM(String messageType) throws Exception {
-        OMElement payload = createEnvelope();
+    private void doEchoOM(String messageType, String httpMethod) throws Exception{
+    	OMElement payload = createEnvelope();
         Options options = new Options();
         options.setTo(targetEPR);
         options.setProperty(Constants.Configuration.MESSAGE_TYPE, messageType);
         options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
+        options.setProperty(Constants.Configuration.HTTP_METHOD, httpMethod);
 //        ConfigurationContext clientConfigurationContext = ConfigurationContextFactory.createDefaultConfigurationContext();
         ServiceClient sender = new ServiceClient(configurationContext, null);
         options.setAction(null);
@@ -115,12 +116,17 @@
         compareWithCreatedOMText(ele.getText());
     }
 
-    public void testEchoOMWithJSONBadgerfish() throws Exception {
-        doEchoOM("application/json/badgerfish");
+
+    public void testEchoOMWithJSONBadgerfish() throws Exception{
+    	doEchoOM("application/json/badgerfish", Constants.Configuration.HTTP_METHOD_POST);
     }
 
     public void testEchoOMWithJSON() throws Exception {
-        doEchoOM("application/json");
+    	doEchoOM("application/json", Constants.Configuration.HTTP_METHOD_POST);
+    }
+
+    public void testEchoOMWithJSONInGET() throws Exception {
+        doEchoOM("application/json", Constants.Configuration.HTTP_METHOD_GET);
     }
 
     protected void compareWithCreatedOMText(String response) {



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org