You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2012/05/02 13:40:47 UTC

svn commit: r1333004 - in /axis/axis2/java/core/trunk/modules/json: src/org/apache/axis2/json/ test-repository/services/POJOService.aar/META-INF/ test/org/apache/axis2/json/

Author: veithen
Date: Wed May  2 11:40:46 2012
New Revision: 1333004

URL: http://svn.apache.org/viewvc?rev=1333004&view=rev
Log:
Added (real) support for mapped JSON. Mapped JSON can now be used with standard data bindings, including POJOs.

Note that there is still an exception thrown in MessageContext#isFault, but the method actually drops the exception silently. Therefore this is not a functional issue, but may be a performance issue.

Added:
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java   (with props)
Modified:
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
    axis/axis2/java/core/trunk/modules/json/test-repository/services/POJOService.aar/META-INF/services.xml
    axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
    axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java Wed May  2 11:40:46 2012
@@ -50,7 +50,7 @@ public abstract class AbstractJSONDataSo
         return getXMLInputFactory().createXMLStreamReader(new JSONTokener(getJSONString()));
     }
 
-    protected abstract AbstractXMLInputFactory getXMLInputFactory();
+    protected abstract AbstractXMLInputFactory getXMLInputFactory() throws XMLStreamException;
     
     public boolean isDestructiveRead() {
         // TODO: for the moment the data source in not destructive (because it reads the entire message into memory before processing it), but this will change...

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java Wed May  2 11:40:46 2012
@@ -102,7 +102,7 @@ public abstract class AbstractJSONMessag
         } else {
             try {
                 ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
-                XMLStreamWriter jsonWriter = getJSONWriter(bytesOut, format);
+                XMLStreamWriter jsonWriter = getJSONWriter(bytesOut, format, msgCtxt);
                 element.serializeAndConsume(jsonWriter);
                 jsonWriter.writeEndDocument();
 
@@ -126,17 +126,17 @@ public abstract class AbstractJSONMessag
         return null;
     }
 
-    private XMLStreamWriter getJSONWriter(OutputStream outStream, OMOutputFormat format)
-            throws AxisFault {
+    private XMLStreamWriter getJSONWriter(OutputStream outStream, OMOutputFormat format, MessageContext messageContext)
+            throws AxisFault, XMLStreamException {
         try {
-            return getJSONWriter(new OutputStreamWriter(outStream, format.getCharSetEncoding()));
+            return getJSONWriter(new OutputStreamWriter(outStream, format.getCharSetEncoding()), messageContext);
         } catch (UnsupportedEncodingException ex) {
             throw AxisFault.makeFault(ex);
         }
     }
     
     //returns the "Mapped" JSON writer
-    protected abstract XMLStreamWriter getJSONWriter(Writer writer);
+    protected abstract XMLStreamWriter getJSONWriter(Writer writer, MessageContext messageContext) throws XMLStreamException;
 
     /**
      * Get the original JSON string from the given element if it is available and if the element has
@@ -184,7 +184,7 @@ public abstract class AbstractJSONMessag
             if (jsonToWrite != null) {
                 out.write(jsonToWrite.getBytes());
             } else {
-                XMLStreamWriter jsonWriter = getJSONWriter(out, format);
+                XMLStreamWriter jsonWriter = getJSONWriter(out, format, msgCtxt);
                 // Jettison v1.2+ relies on writeStartDocument being called (AXIS2-5044)
                 jsonWriter.writeStartDocument();
                 element.serializeAndConsume(jsonWriter);
@@ -194,11 +194,6 @@ public abstract class AbstractJSONMessag
             throw AxisFault.makeFault(e);
         } catch (XMLStreamException e) {
             throw AxisFault.makeFault(e);
-        } catch (IllegalStateException e) {
-            throw new AxisFault(
-                    "Mapped formatted JSON with namespaces are not supported in Axis2. " +
-                            "Make sure that your request doesn't include namespaces or " +
-                            "use the Badgerfish convention");
         }
     }
 
@@ -216,7 +211,7 @@ public abstract class AbstractJSONMessag
                 String jsonString = getStringToWrite(dataOut);
                 if (jsonString == null) {
                     StringWriter out = new StringWriter();
-                    XMLStreamWriter jsonWriter = getJSONWriter(out);
+                    XMLStreamWriter jsonWriter = getJSONWriter(out, msgCtxt);
                     // Jettison v1.2+ relies on writeStartDocument being called (AXIS2-5044)
                     jsonWriter.writeStartDocument();
                     dataOut.serializeAndConsume(jsonWriter);

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java Wed May  2 11:40:46 2012
@@ -103,8 +103,8 @@ public abstract class AbstractJSONOMBuil
             }
         }
 
-        return factory.createOMElement(getDataSource(reader));
+        return factory.createOMElement(getDataSource(reader, messageContext));
     }
 
-    protected abstract AbstractJSONDataSource getDataSource(Reader jsonReader);
+    protected abstract AbstractJSONDataSource getDataSource(Reader jsonReader, MessageContext messageContext);
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java Wed May  2 11:40:46 2012
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.json;
 
+import org.apache.axis2.context.MessageContext;
 import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
 
 import javax.xml.stream.XMLStreamWriter;
@@ -38,7 +39,7 @@ public class JSONBadgerfishMessageFormat
     
     //returns the writer for the badgerfish format
     @Override
-    protected XMLStreamWriter getJSONWriter(Writer writer) {
+    protected XMLStreamWriter getJSONWriter(Writer writer, MessageContext messageContext) {
         return new BadgerFishXMLStreamWriter(writer);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java Wed May  2 11:40:46 2012
@@ -21,6 +21,8 @@ package org.apache.axis2.json;
 
 import java.io.Reader;
 
+import org.apache.axis2.context.MessageContext;
+
 /**
  * Message builder for "Badgerfish" convention. DataSource used here is
  * JSONBadgerfishDataSource which is specific for "Badgerfish"
@@ -28,7 +30,7 @@ import java.io.Reader;
 
 public class JSONBadgerfishOMBuilder extends AbstractJSONOMBuilder {
     @Override
-    protected AbstractJSONDataSource getDataSource(Reader jsonReader) {
+    protected AbstractJSONDataSource getDataSource(Reader jsonReader, MessageContext messageContext) {
         return new JSONBadgerfishDataSource(jsonReader);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java Wed May  2 11:40:46 2012
@@ -19,29 +19,35 @@
 
 package org.apache.axis2.json;
 
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisService;
 import org.codehaus.jettison.AbstractXMLInputFactory;
 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
 
 import java.io.Reader;
-import java.util.HashMap;
+
+import javax.xml.stream.XMLStreamException;
 
 /**
  * JSON data source implementation for the "Mapped" convention.
  */
 
 public class JSONDataSource extends AbstractJSONDataSource {
+    private final MessageContext messageContext;
 
-    public JSONDataSource(Reader jsonReader) {
+    public JSONDataSource(Reader jsonReader, MessageContext messageContext) {
         super(jsonReader);
+        this.messageContext = messageContext;
     }
 
     @Override
-    protected AbstractXMLInputFactory getXMLInputFactory() {
-
-        HashMap XMLToJSNNamespaceMap = new HashMap();
-        XMLToJSNNamespaceMap.put("", "");
+    protected AbstractXMLInputFactory getXMLInputFactory() throws XMLStreamException {
+        AxisService service = messageContext.getAxisService();
+        if (service == null) {
+            throw new XMLStreamException("AxisService not yet set; unable to create namespace map");
+        }
 
         //input factory for "Mapped" convention
-        return new MappedXMLInputFactory(XMLToJSNNamespaceMap);
+        return new MappedXMLInputFactory(JSONUtil.getNS2JNSMap(service));
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java Wed May  2 11:40:46 2012
@@ -19,9 +19,10 @@
 
 package org.apache.axis2.json;
 
-import org.codehaus.jettison.mapped.MappedNamespaceConvention;
-import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
+import org.apache.axis2.context.MessageContext;
+import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
 
+import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.Writer;
 
@@ -44,8 +45,8 @@ public class JSONMessageFormatter extend
     
     //returns the "Mapped" JSON writer
     @Override
-    protected XMLStreamWriter getJSONWriter(Writer writer) {
-        MappedNamespaceConvention mnc = new MappedNamespaceConvention();
-        return new MappedXMLStreamWriter(mnc, writer);
+    protected XMLStreamWriter getJSONWriter(Writer writer, MessageContext messageContext) throws XMLStreamException {
+        return new MappedXMLOutputFactory(JSONUtil.getNS2JNSMap(
+                messageContext.getAxisService())).createXMLStreamWriter(writer);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java Wed May  2 11:40:46 2012
@@ -21,11 +21,13 @@ package org.apache.axis2.json;
 
 import java.io.Reader;
 
+import org.apache.axis2.context.MessageContext;
+
 /** Makes the OMSourcedElement object with the JSONDataSource inside. */
 
 public class JSONOMBuilder extends AbstractJSONOMBuilder {
     @Override
-    protected AbstractJSONDataSource getDataSource(Reader jsonReader) {
-        return new JSONDataSource(jsonReader);
+    protected AbstractJSONDataSource getDataSource(Reader jsonReader, MessageContext messageContext) {
+        return new JSONDataSource(jsonReader, messageContext);
     }
 }

Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java?rev=1333004&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java (added)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java Wed May  2 11:40:46 2012
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axis2.json;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.description.Parameter;
+
+public final class JSONUtil {
+    private JSONUtil() {}
+    
+    public static Map<String,String> getNS2JNSMap(AxisService service) {
+        Map<String,String> ns2jnsMap = new HashMap<String,String>();
+        Parameter param = service.getParameter("JSONNamespaceMap");
+        if (param != null) {
+            for (Iterator it = param.getParameterElement().getChildrenWithName(new QName("mapping")); it.hasNext(); ) {
+                OMElement mapping = (OMElement)it.next();
+                ns2jnsMap.put(mapping.getAttributeValue(new QName("uri")),
+                              mapping.getAttributeValue(new QName("prefix")));
+            }
+        } else {
+            // If no namespace map is defined, use a default map compatible with earlier Axis2 versions
+            ns2jnsMap.put("", "");
+        }
+        return ns2jnsMap;
+    }
+}

Propchange: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: axis/axis2/java/core/trunk/modules/json/test-repository/services/POJOService.aar/META-INF/services.xml
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test-repository/services/POJOService.aar/META-INF/services.xml?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test-repository/services/POJOService.aar/META-INF/services.xml (original)
+++ axis/axis2/java/core/trunk/modules/json/test-repository/services/POJOService.aar/META-INF/services.xml Wed May  2 11:40:46 2012
@@ -25,4 +25,7 @@
         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
     </messageReceivers>
     <parameter name="ServiceClass">org.apache.axis2.json.POJOService</parameter>
+    <parameter name="JSONNamespaceMap">
+        <mapping uri="http://example.org" prefix=""/>
+    </parameter>
 </service>
\ No newline at end of file

Modified: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java (original)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java Wed May  2 11:40:46 2012
@@ -21,6 +21,8 @@ package org.apache.axis2.json;
 
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisService;
 import org.codehaus.jettison.json.JSONException;
 import org.custommonkey.xmlunit.XMLTestCase;
 import org.xml.sax.SAXException;
@@ -107,7 +109,9 @@ public class JSONDataSourceTest extends 
     }
 
     private JSONDataSource getMappedDataSource(String jsonString) {
-        return new JSONDataSource(new StringReader(jsonString));
+        MessageContext messageContext = new MessageContext();
+        messageContext.setAxisService(new AxisService());
+        return new JSONDataSource(new StringReader(jsonString), messageContext);
     }
 
     private String getMappedJSONString() {

Modified: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java?rev=1333004&r1=1333003&r2=1333004&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java (original)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONIntegrationTest.java Wed May  2 11:40:46 2012
@@ -37,7 +37,6 @@ import org.apache.axis2.testutils.PortAl
 import org.apache.axis2.transport.http.SimpleHTTPServer;
 import org.apache.axis2.util.Utils;
 import org.junit.AfterClass;
-import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -155,6 +154,21 @@ public class JSONIntegrationTest impleme
         in.close();
     }
     
+
+    @Test
+    public void testPOJOServiceWithJSONMapped() throws Exception {
+        HttpURLConnection conn = (HttpURLConnection)new URL(pojoUri).openConnection();
+        conn.setDoOutput(true);
+        conn.addRequestProperty("Content-Type", "application/json");
+        Writer out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
+        out.write("{ \"sayHello\" : { \"myName\" : \"Joe\" } }");
+        out.close();
+        assertEquals(200, conn.getResponseCode());
+        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
+        assertEquals("{\"sayHelloResponse\":{\"return\":\"Hello Joe!\"}}", in.readLine());
+        in.close();
+    }
+    
     protected void compareWithCreatedOMText(String response) {
         assertEquals(response, expectedString);
     }