You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by th...@apache.org on 2007/02/08 14:37:46 UTC

svn commit: r504886 - in /webservices/axis2/trunk/java/modules/json: src/org/apache/axis2/json/ test/org/apache/axis2/json/

Author: thilina
Date: Thu Feb  8 05:37:45 2007
New Revision: 504886

URL: http://svn.apache.org/viewvc?view=rev&rev=504886
Log:
Applying the patches given at
https://issues.apache.org/jira/browse/AXIS2-2074
https://issues.apache.org/jira/browse/AXIS2-2050

Thanx Isuru...

Modified:
    webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
    webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java
    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/JSONDataSourceTest.java

Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java?view=diff&rev=504886&r1=504885&r2=504886
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java (original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java Thu Feb  8 05:37:45 2007
@@ -17,15 +17,23 @@
 package org.apache.axis2.json;
 
 import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
+import org.apache.axiom.om.OMDataSource;
 
 import javax.xml.stream.XMLStreamWriter;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 
-public class JSONBadgerfishMessageFormatter extends JSONMessageFormatter{
+public class JSONBadgerfishMessageFormatter extends JSONMessageFormatter {
 
-    protected XMLStreamWriter getJSONWriter(OutputStream outStream){
+    protected XMLStreamWriter getJSONWriter(OutputStream outStream) {
         return new BadgerFishXMLStreamWriter(new OutputStreamWriter(outStream));
     }
 
+    protected String getStringToWrite(OMDataSource dataSource) {
+        if (dataSource instanceof JSONBadgerfishDataSource) {
+            return ((JSONDataSource) dataSource).getCompleteJOSNString();
+        } else {
+            return null;
+        }
+    }
 }

Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java?view=diff&rev=504886&r1=504885&r2=504886
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java (original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java Thu Feb  8 05:37:45 2007
@@ -30,6 +30,7 @@
 import org.apache.axiom.om.OMOutputFormat;
 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
 import org.json.JSONException;
+import org.apache.axis2.AxisFault;
 import org.json.JSONObject;
 import org.json.JSONTokener;
 
@@ -52,7 +53,7 @@
         try {
             writer.flush();
         } catch (IOException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            throw new OMException();
         }
     }
 
@@ -63,7 +64,7 @@
             JSONObject jsonObject = new JSONObject(jsonTokener);
             jsonObject.write(writer);
         } catch (JSONException e) {
-            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+            throw new OMException();
         }
     }
 
@@ -124,23 +125,12 @@
 
     public javax.xml.stream.XMLStreamReader getReader() throws javax.xml.stream.XMLStreamException {
 
-        //todo-badgerfish
-
-//        BadgerFishXMLInputFactory inputFactory = new BadgerFishXMLInputFactory();
-//        return inputFactory.createXMLStreamReader(new JSONTokener("{" + localName + ":" + this.getJSONString()));
-
-        //todo-end
-
-        //todo-mapped
-
         HashMap nstojns = new HashMap();
         nstojns.put("", "");
 
         MappedXMLInputFactory inputFactory = new MappedXMLInputFactory(nstojns);
-        String jsonString = "{" + localName + ":" + this.getJSONString();          
-        return inputFactory.createXMLStreamReader(new JSONTokener(jsonString));
-
-        //todo-end
+        String jsonString = "{" + localName + ":" + this.getJSONString();
+        return inputFactory.createXMLStreamReader(new JSONTokener(jsonString));         
     }
 
     protected String getJSONString() {
@@ -155,7 +145,7 @@
                     temp = (char) jsonInputStream.read();
                 }
             } catch (IOException e) {
-                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+                throw new OMException();
             }
             isRead = true;
             return jsonString;

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=504886&r1=504885&r2=504886
==============================================================================
--- 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 Feb  8 05:37:45 2007
@@ -16,10 +16,11 @@
 
 package org.apache.axis2.json;
 
-import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
+import java.io.*;
 import java.net.URL;
+import java.net.MalformedURLException;
+import java.util.Iterator;
+import java.util.ArrayList;
 
 import javax.xml.stream.FactoryConfigurationError;
 import javax.xml.stream.XMLStreamException;
@@ -27,6 +28,8 @@
 
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.OMDataSource;
+import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.Constants;
 import org.apache.axis2.context.MessageContext;
@@ -44,7 +47,6 @@
             contentType = (String) msgCtxt.getProperty(Constants.Configuration.CONTENT_TYPE);
         } else {
             contentType = (String) msgCtxt.getProperty(Constants.Configuration.MESSAGE_TYPE);
-
         }
 
         if (encoding != null) {
@@ -55,18 +57,24 @@
 
     public byte[] getBytes(MessageContext msgCtxt, OMOutputFormat format) throws AxisFault {
         OMElement element = msgCtxt.getEnvelope().getBody().getFirstElement();
-        try {
-            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
-            XMLStreamWriter jsonWriter = getJSONWriter(bytesOut);
-            element.serializeAndConsume(jsonWriter);
-            jsonWriter.writeEndDocument();
-
-            return bytesOut.toByteArray();
-
-        } catch (XMLStreamException e) {
-            throw new AxisFault(e);
-        } catch (FactoryConfigurationError e) {
-            throw new AxisFault(e);
+        if (element instanceof OMSourcedElementImpl && getStringToWrite(((OMSourcedElementImpl) element).getDataSource()) != null)
+        {
+            String jsonToWrite = getStringToWrite(((OMSourcedElementImpl) element).getDataSource());
+            return jsonToWrite.getBytes();
+        } else {
+            try {
+                ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+                XMLStreamWriter jsonWriter = getJSONWriter(bytesOut);
+                element.serializeAndConsume(jsonWriter);
+                jsonWriter.writeEndDocument();
+
+                return bytesOut.toByteArray();
+
+            } catch (XMLStreamException e) {
+                throw new AxisFault(e);
+            } catch (FactoryConfigurationError e) {
+                throw new AxisFault(e);
+            }
         }
     }
 
@@ -79,21 +87,83 @@
         return new MappedXMLStreamWriter(mnc, new OutputStreamWriter(outStream));
     }
 
+    protected String getStringToWrite(OMDataSource dataSource) {
+        if (dataSource instanceof JSONDataSource) {
+            return ((JSONDataSource) dataSource).getCompleteJOSNString();
+        } else {
+            return null;
+        }
+    }
+
     public void writeTo(MessageContext msgCtxt, OMOutputFormat format,
                         OutputStream out, boolean preserve) throws AxisFault {
         OMElement element = msgCtxt.getEnvelope().getBody().getFirstElement();
-        XMLStreamWriter jsonWriter = getJSONWriter(out);
-
         try {
-            element.serializeAndConsume(jsonWriter);
-            jsonWriter.writeEndDocument();
+            if (element instanceof OMSourcedElementImpl && getStringToWrite(((OMSourcedElementImpl) element).getDataSource()) != null)
+            {
+                String jsonToWrite = getStringToWrite(((OMSourcedElementImpl) element).getDataSource());
+                out.write(jsonToWrite.getBytes());
+            } else {
+                XMLStreamWriter jsonWriter = getJSONWriter(out);
+                element.serializeAndConsume(jsonWriter);
+                jsonWriter.writeEndDocument();
+            }
+        } catch (IOException e) {
+            throw new AxisFault(e);
         } catch (XMLStreamException e) {
             throw new AxisFault(e);
         }
     }
 
-    public URL getTargetAddress(MessageContext msgCtxt, OMOutputFormat format, URL targetURL) {
-        return targetURL;
+    public URL getTargetAddress(MessageContext msgCtxt, OMOutputFormat format, URL targetURL) throws AxisFault {
+
+        String httpMethod =
+                (String) msgCtxt.getProperty(Constants.Configuration.HTTP_METHOD);
+
+        if ((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);
+                }
+            } else {
+                return targetURL;
+            }
+        } 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=504886&r1=504885&r2=504886
==============================================================================
--- 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 Feb  8 05:37:45 2007
@@ -33,7 +33,7 @@
     public JSONOMBuilder() {
     }
 
-	public void init(InputStream inputStream) {
+	public void init(InputStream inputStream, String chatSetEncoding) {
 		this.jsonInputStream = inputStream;
 		
 	}
@@ -72,9 +72,4 @@
 	public String getCharsetEncoding() {
 		return "UTF-8";
 	}
-
-	public String getMessageType() {
-		return null;
-	}
-
 }

Modified: webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java?view=diff&rev=504886&r1=504885&r2=504886
==============================================================================
--- webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java (original)
+++ webservices/axis2/trunk/java/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java Thu Feb  8 05:37:45 2007
@@ -30,34 +30,45 @@
 import org.json.JSONException;
 
 public class JSONDataSourceTest extends TestCase {
-   public void testSerialize1() throws XMLStreamException {
-//        String jsonString = "{\"p\":{\"name\":{\"kamal\":{\"$\":\"yes\"},\"$\":\"innername\"},\"@pp\":\"value\"}}";
-//        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
-//        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-//        JSONDataSource source = new JSONDataSource(jsonInputStream);
-//        source.serialize(outStream, null);
-//        assertEquals(jsonString, new String(outStream.toByteArray()));
+
+    public void testSerialize1() throws XMLStreamException {
+        String jsonString = "{\"p\":{\"name\":{\"kamal\":{\"$\":\"yes\"},\"$\":\"innername\"},\"@pp\":\"value\"}}";
+        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        JSONBadgerfishDataSource source = new JSONBadgerfishDataSource(readLocalName(jsonInputStream), "p");
+        source.serialize(outStream, null);
+        assertEquals(jsonString, new String(outStream.toByteArray()));
+    }
+
+    public void testSerialize2() throws XMLStreamException, IOException {
+        String jsonString = "{\"p\":{\"name\":{\"kamal\":[{\"$\":\"yes\"},{\"$\":\"second\"}],\"$\":\"innername\"},\"@pp\":\"value\"}}";
+        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        OutputStreamWriter writer = new OutputStreamWriter(outStream);
+        JSONBadgerfishDataSource source = new JSONBadgerfishDataSource(readLocalName(jsonInputStream), "p");
+        source.serialize(writer, null);
+        writer.flush();
+        assertEquals(jsonString, new String(outStream.toByteArray()));
+    }
+
+    public void testSerialize3() throws XMLStreamException, JSONException {
+        String jsonString = "{\"p\":{\"@xmlns\":{\"bb\":\"http://other.nsb\",\"aa\":\"http://other.ns\",\"$\":\"http://def.ns\"},\"sam\":{\"$\":\"555\", \"@att\":\"lets\"}}}";
+        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
+        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
+        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(outStream);
+        JSONBadgerfishDataSource source = new JSONBadgerfishDataSource(readLocalName(jsonInputStream), "p");
+        source.serialize(writer);
+        writer.flush();
+        assertEquals("<?xml version='1.0' encoding='UTF-8'?><p xmlns=\"http://def.ns\" xmlns:bb=\"http://other.nsb\" xmlns:aa=\"http://other.ns\"><sam att=\"lets\">555</sam></p>", new String(outStream.toByteArray()));
+    }
+
+    private InputStream readLocalName(InputStream in) {
+        try {
+            while ((char) in.read() != ':') {
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return in;
     }
-//
-//    public void testSerialize2() throws XMLStreamException, IOException {
-//        String jsonString = "{\"p\":{\"name\":{\"kamal\":[{\"$\":\"yes\"},{\"$\":\"second\"}],\"$\":\"innername\"},\"@pp\":\"value\"}}";
-//        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
-//        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-//        OutputStreamWriter writer = new OutputStreamWriter(outStream);
-//        JSONDataSource source = new JSONDataSource(jsonInputStream);
-//        source.serialize(writer , null);
-//        writer.flush();
-//        assertEquals(jsonString, new String(outStream.toByteArray()));
-//    }
-//
-//    public void testSerialize3() throws XMLStreamException, JSONException {
-//        String jsonString = "{\"aa:isuru\":{\"@xmlns\":{\"bb\":\"http://other.nsb\",\"aa\":\"http://other.ns\",\"$\":\"http://def.ns\"},\"sam\":{\"$\":\"555\", \"@att\":\"lets\"}}}";
-//        InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
-//        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
-//        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(outStream);
-//        JSONDataSource source = new JSONDataSource(jsonInputStream);
-//        source.serialize(writer);
-//        writer.flush();
-//        assertEquals("<?xml version='1.0' encoding='UTF-8'?><aa:isuru xmlns=\"http://def.ns\" xmlns:bb=\"http://other.nsb\" xmlns:aa=\"http://other.ns\"><sam att=\"lets\">555</sam></aa:isuru>", new String(outStream.toByteArray()));
-//    }
 }



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