You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ve...@apache.org on 2009/04/19 15:53:39 UTC
svn commit: r766468 - in /webservices/axis2/trunk/java/modules/json:
src/org/apache/axis2/json/ test/org/apache/axis2/json/
Author: veithen
Date: Sun Apr 19 13:53:38 2009
New Revision: 766468
URL: http://svn.apache.org/viewvc?rev=766468&view=rev
Log:
AXIS2-4309: Fixed the charset encoding in JSON support.
Modified:
webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
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
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/AbstractJSONDataSource.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java?rev=766468&r1=766467&r2=766468&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java (original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java Sun Apr 19 13:53:38 2009
@@ -28,8 +28,8 @@
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.Reader;
import java.io.Writer;
/**
@@ -40,13 +40,13 @@
public abstract class AbstractJSONDataSource implements OMDataSource {
- private InputStream jsonInputStream;
+ private Reader jsonReader;
private String jsonString;
private boolean isRead = false;
protected String localName;
- public AbstractJSONDataSource(InputStream jsonInputStream, String localName) {
- this.jsonInputStream = jsonInputStream;
+ public AbstractJSONDataSource(Reader jsonReader, String localName) {
+ this.jsonReader = jsonReader;
this.localName = localName;
}
@@ -169,11 +169,11 @@
return jsonString;
} else {
try {
- char temp = (char)jsonInputStream.read();
+ char temp = (char)jsonReader.read();
jsonString = "";
while ((int)temp != 65535) {
jsonString += temp;
- temp = (char)jsonInputStream.read();
+ temp = (char)jsonReader.read();
}
} catch (IOException e) {
throw new OMException();
Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java?rev=766468&r1=766467&r2=766468&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java (original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java Sun Apr 19 13:53:38 2009
@@ -38,7 +38,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
+import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
@@ -89,7 +92,7 @@
} else {
try {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
- XMLStreamWriter jsonWriter = getJSONWriter(bytesOut);
+ XMLStreamWriter jsonWriter = getJSONWriter(bytesOut, format);
element.serializeAndConsume(jsonWriter);
jsonWriter.writeEndDocument();
@@ -113,8 +116,17 @@
return null;
}
+ private XMLStreamWriter getJSONWriter(OutputStream outStream, OMOutputFormat format)
+ throws AxisFault {
+ try {
+ return getJSONWriter(new OutputStreamWriter(outStream, format.getCharSetEncoding()));
+ } catch (UnsupportedEncodingException ex) {
+ throw AxisFault.makeFault(ex);
+ }
+ }
+
//returns the "Mapped" JSON writer
- protected abstract XMLStreamWriter getJSONWriter(OutputStream outStream);
+ protected abstract XMLStreamWriter getJSONWriter(Writer writer);
/**
* If the data source is a "Mapped" formatted data source, gives the JSON string by directly
@@ -158,7 +170,7 @@
out.write(jsonToWrite.getBytes());
} else {
- XMLStreamWriter jsonWriter = getJSONWriter(out);
+ XMLStreamWriter jsonWriter = getJSONWriter(out, format);
element.serializeAndConsume(jsonWriter);
jsonWriter.writeEndDocument();
}
@@ -191,11 +203,11 @@
jsonString = getStringToWrite(((OMSourcedElementImpl)
dataOut).getDataSource());
} else {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
+ StringWriter out = new StringWriter();
XMLStreamWriter jsonWriter = getJSONWriter(out);
dataOut.serializeAndConsume(jsonWriter);
jsonWriter.writeEndDocument();
- jsonString = new String(out.toByteArray());
+ jsonString = out.toString();
}
jsonString = URIEncoderDecoder.quoteIllegal(jsonString,
WSDL2Constants.LEGAL_CHARACTERS_IN_URL);
Modified: webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java?rev=766468&r1=766467&r2=766468&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java (original)
+++ webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json/AbstractJSONOMBuilder.java Sun Apr 19 13:53:38 2009
@@ -25,14 +25,17 @@
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.llom.OMSourcedElementImpl;
import org.apache.axis2.AxisFault;
+import org.apache.axis2.Constants;
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.InputStreamReader;
+import java.io.Reader;
+import java.io.StringReader;
import java.io.UnsupportedEncodingException;
/** Makes the OMSourcedElementImpl object with the JSONDataSource inside. */
@@ -60,6 +63,8 @@
String prefix = "";
OMNamespace ns = factory.createOMNamespace("", "");
+ Reader reader;
+
//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
@@ -82,10 +87,23 @@
//half as the incoming JSON message
if ((index = requestURL.indexOf("=")) > 0) {
jsonString = requestURL.substring(index + 1);
- inputStream = new ByteArrayInputStream(jsonString.getBytes());
+ reader = new StringReader(jsonString);
} else {
throw new AxisFault("No JSON message received through HTTP GET or POST");
}
+ } else {
+ // Not sure where this is specified, but SOAPBuilder also determines the charset
+ // encoding like that
+ String charSetEncoding = (String)messageContext.getProperty(
+ Constants.Configuration.CHARACTER_SET_ENCODING);
+ if (charSetEncoding == null) {
+ charSetEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
+ }
+ try {
+ reader = new InputStreamReader(inputStream, charSetEncoding);
+ } catch (UnsupportedEncodingException ex) {
+ throw AxisFault.makeFault(ex);
+ }
}
/*
@@ -95,12 +113,12 @@
*/
try {
//read the stream until we find a : symbol
- char temp = (char)inputStream.read();
+ char temp = (char)reader.read();
while (temp != ':') {
if (temp != ' ' && temp != '{') {
localName += temp;
}
- temp = (char)inputStream.read();
+ temp = (char)reader.read();
}
//if the part we read ends with ", there is no prefix, otherwise it has a prefix
@@ -111,12 +129,12 @@
prefix = localName.substring(1, localName.length()) + ":";
localName = "";
//so far we have read only the prefix, now lets read the localname
- temp = (char)inputStream.read();
+ temp = (char)reader.read();
while (temp != ':') {
if (temp != ' ') {
localName += temp;
}
- temp = (char)inputStream.read();
+ temp = (char)reader.read();
}
localName = localName.substring(0, localName.length() - 1);
}
@@ -124,10 +142,10 @@
} catch (IOException e) {
throw AxisFault.makeFault(e);
}
- AbstractJSONDataSource jsonDataSource = getDataSource(inputStream, prefix, localName);
+ AbstractJSONDataSource jsonDataSource = getDataSource(reader, prefix, localName);
return new OMSourcedElementImpl(localName, ns, factory, jsonDataSource);
}
- protected abstract AbstractJSONDataSource getDataSource(InputStream
- jsonInputStream, String prefix, String localName);
+ protected abstract AbstractJSONDataSource getDataSource(Reader
+ jsonReader, 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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -23,7 +23,7 @@
import org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory;
import org.codehaus.jettison.json.JSONTokener;
-import java.io.InputStream;
+import java.io.Reader;
/**
* JSON data source implementation for the "Badgerfish" convention.
@@ -31,8 +31,8 @@
public class JSONBadgerfishDataSource extends AbstractJSONDataSource {
- public JSONBadgerfishDataSource(InputStream jsonInputStream, String localName) {
- super(jsonInputStream, localName);
+ public JSONBadgerfishDataSource(Reader jsonReader, String localName) {
+ super(jsonReader, localName);
}
/**
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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -23,8 +23,7 @@
import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
import javax.xml.stream.XMLStreamWriter;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
+import java.io.Writer;
/**
* This JSONBadgerfishMessageFormatter is the formatter for "Badgerfish" formatted JSON in Axis2.
@@ -37,8 +36,8 @@
//returns the writer for the badgerfish format
@Override
- protected XMLStreamWriter getJSONWriter(OutputStream outStream) {
- return new BadgerFishXMLStreamWriter(new OutputStreamWriter(outStream));
+ protected XMLStreamWriter getJSONWriter(Writer writer) {
+ return new BadgerFishXMLStreamWriter(writer);
}
/**
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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -19,7 +19,7 @@
package org.apache.axis2.json;
-import java.io.InputStream;
+import java.io.Reader;
/**
* Message builder for "Badgerfish" convention. DataSource used here is
@@ -28,8 +28,8 @@
public class JSONBadgerfishOMBuilder extends AbstractJSONOMBuilder {
@Override
- protected AbstractJSONDataSource getDataSource(InputStream jsonInputStream, String prefix,
+ protected AbstractJSONDataSource getDataSource(Reader jsonReader, String prefix,
String localName) {
- return new JSONBadgerfishDataSource(jsonInputStream, "\"" + prefix + localName + "\"");
+ return new JSONBadgerfishDataSource(jsonReader, "\"" + 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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -24,7 +24,7 @@
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamException;
-import java.io.InputStream;
+import java.io.Reader;
import java.util.HashMap;
/**
@@ -33,8 +33,8 @@
public class JSONDataSource extends AbstractJSONDataSource {
- public JSONDataSource(InputStream jsonInputStream, String localName) {
- super(jsonInputStream, localName);
+ public JSONDataSource(Reader jsonReader, String localName) {
+ super(jsonReader, localName);
}
/**
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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -24,8 +24,7 @@
import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
import javax.xml.stream.XMLStreamWriter;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
+import java.io.Writer;
/**
* This JSONMessageFormatter is the formatter for "Mapped" formatted JSON in Axis2. This type of
@@ -43,9 +42,9 @@
//returns the "Mapped" JSON writer
@Override
- protected XMLStreamWriter getJSONWriter(OutputStream outStream) {
+ protected XMLStreamWriter getJSONWriter(Writer writer) {
MappedNamespaceConvention mnc = new MappedNamespaceConvention();
- return new MappedXMLStreamWriter(mnc, new OutputStreamWriter(outStream));
+ return new MappedXMLStreamWriter(mnc, writer);
}
/**
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=766468&r1=766467&r2=766468&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:53:38 2009
@@ -19,14 +19,14 @@
package org.apache.axis2.json;
-import java.io.InputStream;
+import java.io.Reader;
/** Makes the OMSourcedElementImpl object with the JSONDataSource inside. */
public class JSONOMBuilder extends AbstractJSONOMBuilder {
@Override
- protected AbstractJSONDataSource getDataSource(InputStream
- jsonInputStream, String prefix, String localName) {
- return new JSONDataSource(jsonInputStream, "\"" + prefix + localName + "\"");
+ protected AbstractJSONDataSource getDataSource(Reader
+ jsonReader, String prefix, String localName) {
+ return new JSONDataSource(jsonReader, "\"" + prefix + localName + "\"");
}
}
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?rev=766468&r1=766467&r2=766468&view=diff
==============================================================================
--- 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 Sun Apr 19 13:53:38 2009
@@ -27,11 +27,11 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringReader;
public class JSONDataSourceTest extends XMLTestCase {
@@ -98,8 +98,8 @@
}
private JSONBadgerfishDataSource getBadgerfishDataSource(String jsonString) {
- InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
- return new JSONBadgerfishDataSource(readLocalName(jsonInputStream), "\"p\"");
+ Reader jsonReader = new StringReader(jsonString);
+ return new JSONBadgerfishDataSource(readLocalName(jsonReader), "\"p\"");
}
private String getBadgerfishJSONString() {
@@ -107,15 +107,15 @@
}
private JSONDataSource getMappedDataSource(String jsonString) {
- InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes());
- return new JSONDataSource(readLocalName(jsonInputStream), "\"mapping\"");
+ Reader jsonReader = new StringReader(jsonString);
+ return new JSONDataSource(readLocalName(jsonReader), "\"mapping\"");
}
private String getMappedJSONString() {
return "{\"mapping\":{\"inner\":[{\"first\":\"test string one\"},\"test string two\"],\"name\":\"foo\"}}";
}
- private InputStream readLocalName(InputStream in) {
+ private Reader readLocalName(Reader in) {
try {
while ((char)in.read() != ':') {
}