Return-Path: Delivered-To: apmail-ws-axis-dev-archive@www.apache.org Received: (qmail 53344 invoked from network); 19 Apr 2009 13:23:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Apr 2009 13:23:13 -0000 Received: (qmail 43110 invoked by uid 500); 19 Apr 2009 13:23:12 -0000 Delivered-To: apmail-ws-axis-dev-archive@ws.apache.org Received: (qmail 42953 invoked by uid 500); 19 Apr 2009 13:23:12 -0000 Mailing-List: contact axis-cvs-help@ws.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list axis-cvs@ws.apache.org Received: (qmail 42944 invoked by uid 500); 19 Apr 2009 13:23:12 -0000 Delivered-To: apmail-ws-axis2-cvs@ws.apache.org Received: (qmail 42941 invoked by uid 99); 19 Apr 2009 13:23:12 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Apr 2009 13:23:12 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 19 Apr 2009 13:23:10 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 99A2C23889D2; Sun, 19 Apr 2009 13:22:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r766465 - /webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/ Date: Sun, 19 Apr 2009 13:22:49 -0000 To: axis2-cvs@ws.apache.org From: veithen@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090419132250.99A2C23889D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: veithen Date: Sun Apr 19 13:22:47 2009 New Revision: 766465 URL: http://svn.apache.org/viewvc?rev=766465&view=rev Log: Fixed the class hierarchy of the JSON related classes: the implemention of the Badgerfish convention should not override the implementation of the mapped convention; instead they should both be derived from common (abstract) base classes. Added: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java - copied, changed from r761927, webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java - copied, changed from r761927, webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java - copied, changed from r761927, webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.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 Copied: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java (from r761927, 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/AbstractJSONDataSource.java?p2=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java&p1=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONDataSource.java&r1=761927&r2=766465&rev=766465&view=diff ============================================================================== --- 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/AbstractJSONDataSource.java Sun Apr 19 13:22:47 2009 @@ -22,8 +22,6 @@ import org.apache.axiom.om.OMDataSource; import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMOutputFormat; -import org.codehaus.jettison.json.JSONTokener; -import org.codehaus.jettison.mapped.MappedXMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; @@ -33,7 +31,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.Writer; -import java.util.HashMap; /** * JSONDataSource keeps the JSON String inside and consumes it when needed. This is to be kept in @@ -41,14 +38,14 @@ * directly without expanding. This uses the "Mapped" JSON convention. */ -public class JSONDataSource implements OMDataSource { +public abstract class AbstractJSONDataSource implements OMDataSource { private InputStream jsonInputStream; private String jsonString; private boolean isRead = false; protected String localName; - public JSONDataSource(InputStream jsonInputStream, String localName) { + public AbstractJSONDataSource(InputStream jsonInputStream, String localName) { this.jsonInputStream = jsonInputStream; this.localName = localName; } @@ -164,16 +161,7 @@ * if there is an error while making the StAX reader. */ - public XMLStreamReader getReader() throws XMLStreamException { - - HashMap XMLToJSNNamespaceMap = new HashMap(); - XMLToJSNNamespaceMap.put("", ""); - - //input factory for "Mapped" convention - MappedXMLInputFactory inputFactory = new MappedXMLInputFactory(XMLToJSNNamespaceMap); - String jsonString = "{" + localName + ":" + this.getJSONString(); - return inputFactory.createXMLStreamReader(new JSONTokener(jsonString)); - } + public abstract XMLStreamReader getReader() throws XMLStreamException; //returns the json string by consuming the JSON input stream. protected String getJSONString() { Copied: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java (from r761927, 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/AbstractJSONMessageFormatter.java?p2=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java&p1=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java&r1=761927&r2=766465&rev=766465&view=diff ============================================================================== --- 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/AbstractJSONMessageFormatter.java Sun Apr 19 13:22:47 2009 @@ -31,8 +31,6 @@ import org.apache.axis2.description.WSDL2Constants; 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; @@ -40,24 +38,16 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; /** - * This JSONMessageFormatter is the formatter for "Mapped" formatted JSON in Axis2. This type of - * JSON strings are really easy to use in Javascript. Eg: <out><in>mapped - * JSON</in></out> is converted to... {"out":{"in":"mapped JSON"}} WARNING: We do not - * support "Mapped" JSON Strings with *namespaces* in Axis2. This convention is supported in Axis2, - * with the aim of making Javascript users' life easy (services written in Javascript). There are - * no namespaces used in Javascript. If you want to use JSON with namespaces, use the - * JSONBadgerfishMessageForatter (for "Badgerfish" formatted JSON) which supports JSON with - * namespaces. + * Base class for JSON message formatters. */ -public class JSONMessageFormatter implements MessageFormatter { +public abstract class AbstractJSONMessageFormatter implements MessageFormatter { public String getContentType(MessageContext msgCtxt, OMOutputFormat format, String soapActionString) { @@ -124,10 +114,7 @@ } //returns the "Mapped" JSON writer - protected XMLStreamWriter getJSONWriter(OutputStream outStream) { - MappedNamespaceConvention mnc = new MappedNamespaceConvention(); - return new MappedXMLStreamWriter(mnc, new OutputStreamWriter(outStream)); - } + protected abstract XMLStreamWriter getJSONWriter(OutputStream outStream); /** * If the data source is a "Mapped" formatted data source, gives the JSON string by directly @@ -136,13 +123,7 @@ * @param dataSource data source to be checked * @return the JSON string to write */ - protected String getStringToWrite(OMDataSource dataSource) { - if (dataSource instanceof JSONDataSource) { - return ((JSONDataSource)dataSource).getCompleteJOSNString(); - } else { - return null; - } - } + protected abstract String getStringToWrite(OMDataSource dataSource); /** * Writes the JSON message to the output stream with the correct convention. If the payload is @@ -164,7 +145,7 @@ 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) { + if (element instanceof SOAPFault) { SOAPFault fault = (SOAPFault)element; OMElement element2 = new OMElementImpl("Fault", null, element.getOMFactory()); element2.setText(fault.toString()); Copied: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java (from r761927, 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/AbstractJSONOMBuilder.java?p2=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java&p1=webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONOMBuilder.java&r1=761927&r2=766465&rev=766465&view=diff ============================================================================== --- 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/AbstractJSONOMBuilder.java Sun Apr 19 13:22:47 2009 @@ -23,7 +23,6 @@ import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; -import org.apache.axiom.om.impl.OMNamespaceImpl; import org.apache.axiom.om.impl.llom.OMSourcedElementImpl; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; @@ -38,10 +37,10 @@ /** Makes the OMSourcedElementImpl object with the JSONDataSource inside. */ -public class JSONOMBuilder implements Builder { +public abstract class AbstractJSONOMBuilder implements Builder { - public JSONOMBuilder() { + public AbstractJSONOMBuilder() { } /** @@ -125,12 +124,10 @@ } catch (IOException e) { throw AxisFault.makeFault(e); } - JSONDataSource jsonDataSource = getDataSource(inputStream, prefix, localName); + AbstractJSONDataSource jsonDataSource = getDataSource(inputStream, prefix, localName); return new OMSourcedElementImpl(localName, ns, factory, jsonDataSource); } - protected JSONDataSource getDataSource(InputStream - jsonInputStream, String prefix, String localName) { - return new JSONDataSource(jsonInputStream, "\"" + prefix + localName + "\""); - } + protected abstract AbstractJSONDataSource getDataSource(InputStream + jsonInputStream, String prefix, String localName); } Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java (original) +++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java Sun Apr 19 13:22:47 2009 @@ -26,12 +26,10 @@ import java.io.InputStream; /** - * JSONDataSource keeps the JSON String inside and consumes it when needed. This is to be kept in - * the OMSourcedElementImpl and can be used either to expand the tree or get the JSON String - * directly without expanding. This uses the "Badgerfish" JSON convention. + * JSON data source implementation for the "Badgerfish" convention. */ -public class JSONBadgerfishDataSource extends JSONDataSource { +public class JSONBadgerfishDataSource extends AbstractJSONDataSource { public JSONBadgerfishDataSource(InputStream jsonInputStream, String localName) { super(jsonInputStream, localName); @@ -44,6 +42,7 @@ * @throws javax.xml.stream.XMLStreamException * if there is an error while making the StAX reader. */ + @Override public javax.xml.stream.XMLStreamReader getReader() throws javax.xml.stream.XMLStreamException { //input factory for "Badgerfish" 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?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- 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 Sun Apr 19 13:22:47 2009 @@ -33,9 +33,10 @@ * namespaces with JSON */ -public class JSONBadgerfishMessageFormatter extends JSONMessageFormatter { +public class JSONBadgerfishMessageFormatter extends AbstractJSONMessageFormatter { //returns the writer for the badgerfish format + @Override protected XMLStreamWriter getJSONWriter(OutputStream outStream) { return new BadgerFishXMLStreamWriter(new OutputStreamWriter(outStream)); } @@ -47,9 +48,10 @@ * @param dataSource data source to be checked * @return the JSON string to write */ + @Override protected String getStringToWrite(OMDataSource dataSource) { if (dataSource instanceof JSONBadgerfishDataSource) { - return ((JSONDataSource)dataSource).getCompleteJOSNString(); + return ((JSONBadgerfishDataSource)dataSource).getCompleteJOSNString(); } else { return null; } Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java (original) +++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/JSONBadgerfishOMBuilder.java Sun Apr 19 13:22:47 2009 @@ -26,9 +26,9 @@ * JSONBadgerfishDataSource which is specific for "Badgerfish" */ -public class JSONBadgerfishOMBuilder extends JSONOMBuilder { - - protected JSONDataSource getDataSource(InputStream jsonInputStream, String prefix, +public class JSONBadgerfishOMBuilder extends AbstractJSONOMBuilder { + @Override + protected AbstractJSONDataSource getDataSource(InputStream jsonInputStream, String prefix, String localName) { return new JSONBadgerfishDataSource(jsonInputStream, "\"" + prefix + localName + "\""); } 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?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- 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 Sun Apr 19 13:22:47 2009 @@ -19,141 +19,22 @@ package org.apache.axis2.json; -import org.apache.axiom.om.OMDataSource; -import org.apache.axiom.om.OMException; -import org.apache.axiom.om.OMOutputFormat; import org.codehaus.jettison.json.JSONTokener; import org.codehaus.jettison.mapped.MappedXMLInputFactory; -import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; -import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; -import java.io.Writer; import java.util.HashMap; /** - * JSONDataSource keeps the JSON String inside and consumes it when needed. This is to be kept in - * the OMSourcedElementImpl and can be used either to expand the tree or get the JSON String - * directly without expanding. This uses the "Mapped" JSON convention. + * JSON data source implementation for the "Mapped" convention. */ -public class JSONDataSource implements OMDataSource { - - private InputStream jsonInputStream; - private String jsonString; - private boolean isRead = false; - protected String localName; +public class JSONDataSource extends AbstractJSONDataSource { public JSONDataSource(InputStream jsonInputStream, String localName) { - this.jsonInputStream = jsonInputStream; - this.localName = localName; - } - - /** - * Writes JSON into the output stream. As this should write JSON, it directly gets the JSON - * string and writes it without expanding the tree. - * - * @param outputStream the stream to be written into - * @param omOutputFormat format of the message, this is ignored. - * @throws javax.xml.stream.XMLStreamException - * if there is an error while writing the message in to the output stream. - */ - public void serialize(OutputStream outputStream, OMOutputFormat omOutputFormat) - throws XMLStreamException { - try { - outputStream.write(getCompleteJOSNString().getBytes()); - } catch (IOException e) { - throw new OMException(); - } - } - - /** - * Writes JSON through the writer. As this should write JSON, it directly gets the JSON string - * and writes it without expanding the tree. - * - * @param writer Writer to be written into - * @param omOutputFormat format of the message, this is ignored. - * @throws javax.xml.stream.XMLStreamException - * if there is an error while writing the message through the writer. - */ - public void serialize(Writer writer, OMOutputFormat omOutputFormat) - throws XMLStreamException { - try { - writer.write(getCompleteJOSNString()); - } catch (IOException e) { - throw new OMException(); - } - } - - /** - * Writes XML through the XMLStreamWriter. As the input data source is JSON, this method needs - * to get a StAX reader from that JSON String. Therefore this uses the getReader() method to get - * the StAX reader writes the events into the XMLStreamWriter. - * - * @param xmlStreamWriter StAX writer to be written into - * @throws javax.xml.stream.XMLStreamException - * if there is an error while writing the message through the StAX writer. - */ - public void serialize(XMLStreamWriter xmlStreamWriter) throws XMLStreamException { - XMLStreamReader reader = getReader(); - xmlStreamWriter.writeStartDocument(); - while (reader.hasNext()) { - int x = reader.next(); - switch (x) { - case XMLStreamConstants.START_ELEMENT: - xmlStreamWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), - reader.getNamespaceURI()); - int namespaceCount = reader.getNamespaceCount(); - for (int i = namespaceCount - 1; i >= 0; i--) { - xmlStreamWriter.writeNamespace(reader.getNamespacePrefix(i), - reader.getNamespaceURI(i)); - } - int attributeCount = reader.getAttributeCount(); - for (int i = 0; i < attributeCount; i++) { - xmlStreamWriter.writeAttribute(reader.getAttributePrefix(i), - reader.getAttributeNamespace(i), - reader.getAttributeLocalName(i), - reader.getAttributeValue(i)); - } - break; - case XMLStreamConstants.START_DOCUMENT: - break; - case XMLStreamConstants.CHARACTERS: - xmlStreamWriter.writeCharacters(reader.getText()); - break; - case XMLStreamConstants.CDATA: - xmlStreamWriter.writeCData(reader.getText()); - break; - case XMLStreamConstants.END_ELEMENT: - xmlStreamWriter.writeEndElement(); - break; - case XMLStreamConstants.END_DOCUMENT: - xmlStreamWriter.writeEndDocument(); - break; - case XMLStreamConstants.SPACE: - break; - case XMLStreamConstants.COMMENT: - xmlStreamWriter.writeComment(reader.getText()); - break; - case XMLStreamConstants.DTD: - xmlStreamWriter.writeDTD(reader.getText()); - break; - case XMLStreamConstants.PROCESSING_INSTRUCTION: - xmlStreamWriter - .writeProcessingInstruction(reader.getPITarget(), reader.getPIData()); - break; - case XMLStreamConstants.ENTITY_REFERENCE: - xmlStreamWriter.writeEntityRef(reader.getLocalName()); - break; - default : - throw new OMException(); - } - } - xmlStreamWriter.writeEndDocument(); + super(jsonInputStream, localName); } /** @@ -163,7 +44,7 @@ * @throws javax.xml.stream.XMLStreamException * if there is an error while making the StAX reader. */ - + @Override public XMLStreamReader getReader() throws XMLStreamException { HashMap XMLToJSNNamespaceMap = new HashMap(); @@ -174,28 +55,4 @@ String jsonString = "{" + localName + ":" + this.getJSONString(); return inputFactory.createXMLStreamReader(new JSONTokener(jsonString)); } - - //returns the json string by consuming the JSON input stream. - protected String getJSONString() { - if (isRead) { - return jsonString; - } else { - try { - char temp = (char)jsonInputStream.read(); - jsonString = ""; - while ((int)temp != 65535) { - jsonString += temp; - temp = (char)jsonInputStream.read(); - } - } catch (IOException e) { - throw new OMException(); - } - isRead = true; - return jsonString; - } - } - - public String getCompleteJOSNString() { - return "{" + localName + ":" + getJSONString(); - } } 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?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- 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 Sun Apr 19 13:22:47 2009 @@ -20,30 +20,12 @@ package org.apache.axis2.json; import org.apache.axiom.om.OMDataSource; -import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMOutputFormat; -import org.apache.axiom.om.impl.llom.OMElementImpl; -import org.apache.axiom.om.impl.llom.OMSourcedElementImpl; -import org.apache.axiom.soap.SOAPFault; -import org.apache.axis2.AxisFault; -import org.apache.axis2.Constants; -import org.apache.axis2.context.MessageContext; -import org.apache.axis2.description.WSDL2Constants; -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.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URL; /** * This JSONMessageFormatter is the formatter for "Mapped" formatted JSON in Axis2. This type of @@ -57,73 +39,10 @@ */ -public class JSONMessageFormatter implements MessageFormatter { - - public String getContentType(MessageContext msgCtxt, OMOutputFormat format, - String soapActionString) { - String contentType = (String)msgCtxt.getProperty(Constants.Configuration.CONTENT_TYPE); - String encoding = format.getCharSetEncoding(); - if (contentType == null) { - contentType = (String)msgCtxt.getProperty(Constants.Configuration.MESSAGE_TYPE); - } - if (encoding != null) { - contentType += "; charset=" + encoding; - } - return contentType; - } - - /** - * Gives the JSON message as an array of bytes. If the payload is an OMSourcedElementImpl and - * it contains a JSONDataSource with a correctly formatted JSON String, gets it directly from - * the DataSource and returns as a byte array. If not, the OM tree is expanded and it is - * serialized into the output stream and byte array is returned. - * - * @param msgCtxt Message context which contains the soap envelope to be written - * @param format format of the message, this is ignored - * @return the payload as a byte array - * @throws AxisFault if there is an error in writing the message using StAX writer or IF THE - * USER TRIES TO SEND A JSON MESSAGE WITH NAMESPACES USING THE "MAPPED" - * CONVENTION. - */ - - public byte[] getBytes(MessageContext msgCtxt, OMOutputFormat format) throws AxisFault { - OMElement element = msgCtxt.getEnvelope().getBody().getFirstElement(); - //if the element is an OMSourcedElementImpl and it contains a JSONDataSource with - //correct convention, directly get the JSON string. - - if (element instanceof OMSourcedElementImpl && - getStringToWrite(((OMSourcedElementImpl)element).getDataSource()) != null) { - String jsonToWrite = getStringToWrite(((OMSourcedElementImpl)element).getDataSource()); - return jsonToWrite.getBytes(); - //otherwise serialize the OM by expanding the tree - } else { - try { - ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); - XMLStreamWriter jsonWriter = getJSONWriter(bytesOut); - element.serializeAndConsume(jsonWriter); - jsonWriter.writeEndDocument(); - - return bytesOut.toByteArray(); - - } catch (XMLStreamException e) { - throw AxisFault.makeFault(e); - } catch (FactoryConfigurationError 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"); - } - } - } - - public String formatSOAPAction(MessageContext msgCtxt, OMOutputFormat format, - String soapActionString) { - return null; - } +public class JSONMessageFormatter extends AbstractJSONMessageFormatter { //returns the "Mapped" JSON writer + @Override protected XMLStreamWriter getJSONWriter(OutputStream outStream) { MappedNamespaceConvention mnc = new MappedNamespaceConvention(); return new MappedXMLStreamWriter(mnc, new OutputStreamWriter(outStream)); @@ -136,6 +55,7 @@ * @param dataSource data source to be checked * @return the JSON string to write */ + @Override protected String getStringToWrite(OMDataSource dataSource) { if (dataSource instanceof JSONDataSource) { return ((JSONDataSource)dataSource).getCompleteJOSNString(); @@ -144,96 +64,4 @@ } } - /** - * Writes the JSON message to the output stream with the correct convention. If the payload is - * an OMSourcedElementImpl and it contains a JSONDataSource with a correctly formatted JSON - * String, gets it directly from the DataSource and writes to the output stream. If not, the OM - * tree is expanded and it is serialized into the output stream. * - * - * @param msgCtxt Message context which contains the soap envelope to be written - * @param format format of the message, this is ignored - * @param out output stream to be written in to - * @param preserve ignored - * @throws AxisFault if there is an error in writing the message using StAX writer or IF THE - * USER TRIES TO SEND A JSON MESSAGE WITH NAMESPACES USING THE "MAPPED" - * CONVENTION. - */ - - public void writeTo(MessageContext msgCtxt, OMOutputFormat format, - 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; - OMElement element2 = new OMElementImpl("Fault", null, element.getOMFactory()); - element2.setText(fault.toString()); - element = element2; - } - 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 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"); - } - } - - public URL getTargetAddress(MessageContext msgCtxt, OMOutputFormat format, URL targetURL) - throws AxisFault { - - String httpMethod = - (String)msgCtxt.getProperty(Constants.Configuration.HTTP_METHOD); - OMElement dataOut = msgCtxt.getEnvelope().getBody().getFirstElement(); - - //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)) { - 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()); - } - 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 AxisFault.makeFault(e); - } catch (XMLStreamException e) { - throw AxisFault.makeFault(e); - } catch (UnsupportedEncodingException e) { - throw AxisFault.makeFault(e); - } - } else { - return targetURL; - } - } - } 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?rev=766465&r1=766464&r2=766465&view=diff ============================================================================== --- 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 Sun Apr 19 13:22:47 2009 @@ -19,117 +19,13 @@ 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 org.apache.axiom.om.impl.OMNamespaceImpl; -import org.apache.axiom.om.impl.llom.OMSourcedElementImpl; -import org.apache.axis2.AxisFault; -import org.apache.axis2.addressing.EndpointReference; -import org.apache.axis2.builder.Builder; -import org.apache.axis2.context.MessageContext; -import org.apache.axis2.transport.http.util.URIEncoderDecoder; - -import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; -import java.io.UnsupportedEncodingException; /** Makes the OMSourcedElementImpl object with the JSONDataSource inside. */ -public class JSONOMBuilder implements Builder { - - - public JSONOMBuilder() { - } - - /** - * gives the OMSourcedElementImpl using the incoming JSON stream - * - * @param inputStream - incoming message as an input stream - * @param contentType - content type of the message (eg: application/json) - * @param messageContext - inflow message context - * @return OMSourcedElementImpl with JSONDataSource inside - * @throws AxisFault - */ - - public OMElement processDocument(InputStream inputStream, String contentType, - MessageContext messageContext) throws AxisFault { - OMFactory factory = OMAbstractFactory.getOMFactory(); - String localName = ""; - String prefix = ""; - OMNamespace ns = factory.createOMNamespace("", ""); - - //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 AxisFault.makeFault(e); - } - - String jsonString; - int index; - //As the message is received through GET, check for "=" sign and consider the second - //half as the incoming JSON message - 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"); - } - } - - /* - Now we have to read the localname and prefix from the input stream - if there is not prefix, message starts like {"foo": - if there is a prefix, message starts like {"prefix:foo": - */ - try { - //read the stream until we find a : symbol - char temp = (char)inputStream.read(); - while (temp != ':') { - if (temp != ' ' && temp != '{') { - localName += temp; - } - temp = (char)inputStream.read(); - } - - //if the part we read ends with ", there is no prefix, otherwise it has a prefix - if (localName.charAt(0) == '"') { - if (localName.charAt(localName.length() - 1) == '"') { - localName = localName.substring(1, localName.length() - 1); - } else { - prefix = localName.substring(1, localName.length()) + ":"; - localName = ""; - //so far we have read only the prefix, now lets read the localname - temp = (char)inputStream.read(); - while (temp != ':') { - if (temp != ' ') { - localName += temp; - } - temp = (char)inputStream.read(); - } - localName = localName.substring(0, localName.length() - 1); - } - } - } catch (IOException e) { - throw AxisFault.makeFault(e); - } - JSONDataSource jsonDataSource = getDataSource(inputStream, prefix, localName); - return new OMSourcedElementImpl(localName, ns, factory, jsonDataSource); - } - - protected JSONDataSource getDataSource(InputStream +public class JSONOMBuilder extends AbstractJSONOMBuilder { + @Override + protected AbstractJSONDataSource getDataSource(InputStream jsonInputStream, String prefix, String localName) { return new JSONDataSource(jsonInputStream, "\"" + prefix + localName + "\""); }