axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kei...@apache.org
Subject svn commit: r525793 - in /webservices/axis2/trunk/java/modules: json/src/org/apache/axis2/json/ json/test/org/apache/axis2/json/ kernel/src/org/apache/axis2/transport/http/ samples/yahoojsonsearch/resources/ samples/yahoojsonsearch/src/sample/yahooserv...
Date Thu, 05 Apr 2007 10:45:34 GMT
Author: keithc
Date: Thu Apr  5 03:45:33 2007
New Revision: 525793

URL: http://svn.apache.org/viewvc?view=rev&rev=525793
Log:
Applying patch given in AXIS2-2458.

Modified:
    webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
    webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
    webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/Echo.java
    webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
    webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HTTPWorker.java
    webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/resources/axis2.xml
    webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/src/sample/yahooservices/JSONSearch/JSONSearchModel.java

Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
(original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
Thu Apr  5 03:45:33 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/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
(original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
Thu Apr  5 03:45:33 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/trunk/java/modules/json/test/org/apache/axis2/json/Echo.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/Echo.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/Echo.java (original)
+++ webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/Echo.java Thu Apr
 5 03:45:33 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/trunk/java/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
(original)
+++ webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
Thu Apr  5 03:45:33 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) {

Modified: webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HTTPWorker.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HTTPWorker.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HTTPWorker.java
(original)
+++ webservices/axis2/trunk/java/modules/kernel/src/org/apache/axis2/transport/http/HTTPWorker.java
Thu Apr  5 03:45:33 2007
@@ -193,9 +193,17 @@
             }
 
             outbuffer = copyCommonProperties(msgContext, request);
-
+            String contentType = null;
+            Header[] headers = request.getHeaders(HTTPConstants.HEADER_CONTENT_TYPE);
+            if (headers != null && headers.length > 0) {
+                contentType = headers[0].getValue();
+                int index = contentType.indexOf(';');
+                if (index > 0) {
+                    contentType = contentType.substring(0, index);
+                }
+            }
             // deal with GET request
-            pi = RESTUtil.processURLRequest(msgContext, outbuffer.getOutputStream(), null);
+            pi = RESTUtil.processURLRequest(msgContext, outbuffer.getOutputStream(), contentType);
 
         } else if (method.equals(HTTPConstants.HEADER_POST)) {
             // deal with POST request

Modified: webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/resources/axis2.xml
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/resources/axis2.xml?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/resources/axis2.xml (original)
+++ webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/resources/axis2.xml Thu Apr
 5 03:45:33 2007
@@ -118,6 +118,8 @@
                          class="org.apache.axis2.json.JSONBadgerfishMessageFormatter"/>
         <messageFormatter contentType="text/javascript"
                          class="org.apache.axis2.json.JSONMessageFormatter"/>
+        <messageFormatter contentType="application/x-www-form-urlencoded"
+                         class="org.apache.axis2.transport.http.XFormURLEncodedFormatter"/>
     </messageFormatters>
     
     <!-- ================================================= -->

Modified: webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/src/sample/yahooservices/JSONSearch/JSONSearchModel.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/src/sample/yahooservices/JSONSearch/JSONSearchModel.java?view=diff&rev=525793&r1=525792&r2=525793
==============================================================================
--- webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/src/sample/yahooservices/JSONSearch/JSONSearchModel.java
(original)
+++ webservices/axis2/trunk/java/modules/samples/yahoojsonsearch/src/sample/yahooservices/JSONSearch/JSONSearchModel.java
Thu Apr  5 03:45:33 2007
@@ -20,6 +20,7 @@
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axis2.Constants;
+import org.apache.axis2.transport.http.HTTPConstants;
 import org.apache.axis2.context.ConfigurationContextFactory;
 import org.apache.axis2.context.ConfigurationContext;
 import org.apache.axis2.addressing.EndpointReference;
@@ -60,7 +61,7 @@
             Options options = new Options();
             client.setOptions(options);
             options.setTo(new EndpointReference(epr));
-            options.setProperty(Constants.Configuration.MESSAGE_TYPE, "text/javascript");
+            options.setProperty(Constants.Configuration.MESSAGE_TYPE, HTTPConstants.MEDIA_TYPE_X_WWW_FORM);
             options.setProperty(Constants.Configuration.HTTP_METHOD, Constants.Configuration.HTTP_METHOD_GET);
             OMElement response = client.sendReceive(getPayloadForYahooSearchCall(query, format));
             generateSnippet(response);



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


Mime
View raw message