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 is...@apache.org on 2008/07/10 07:54:33 UTC
svn commit: r675452 - in
/webservices/axis2/trunk/java/modules/json/src/org/apache/axis2/json:
JSONBadgerfishOMBuilder.java JSONDataSource.java JSONMessageFormatter.java
JSONOMBuilder.java
Author: isurues
Date: Wed Jul 9 22:54:32 2008
New Revision: 675452
URL: http://svn.apache.org/viewvc?rev=675452&view=rev
Log:
Some improvements to JSON module code and comments
Modified:
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
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=675452&r1=675451&r2=675452&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 Wed Jul 9 22:54:32 2008
@@ -21,6 +21,10 @@
import java.io.InputStream;
+/**
+ * Message builder for "Badgerfish" convention. DataSource used here is
+ * JSONBadgerfishDataSource which is specific for "Badgerfish"
+ */
public class JSONBadgerfishOMBuilder extends JSONOMBuilder {
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=675452&r1=675451&r2=675452&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 Wed Jul 9 22:54:32 2008
@@ -27,6 +27,8 @@
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;
@@ -61,7 +63,7 @@
* if there is an error while writing the message in to the output stream.
*/
public void serialize(OutputStream outputStream, OMOutputFormat omOutputFormat)
- throws javax.xml.stream.XMLStreamException {
+ throws XMLStreamException {
try {
outputStream.write(getCompleteJOSNString().getBytes());
} catch (IOException e) {
@@ -79,7 +81,7 @@
* if there is an error while writing the message through the writer.
*/
public void serialize(Writer writer, OMOutputFormat omOutputFormat)
- throws javax.xml.stream.XMLStreamException {
+ throws XMLStreamException {
try {
writer.write(getCompleteJOSNString());
} catch (IOException e) {
@@ -96,8 +98,7 @@
* @throws javax.xml.stream.XMLStreamException
* if there is an error while writing the message through the StAX writer.
*/
- public void serialize(javax.xml.stream.XMLStreamWriter xmlStreamWriter)
- throws javax.xml.stream.XMLStreamException {
+ public void serialize(XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
XMLStreamReader reader = getReader();
xmlStreamWriter.writeStartDocument();
while (reader.hasNext()) {
@@ -163,7 +164,7 @@
* if there is an error while making the StAX reader.
*/
- public javax.xml.stream.XMLStreamReader getReader() throws javax.xml.stream.XMLStreamException {
+ public XMLStreamReader getReader() throws XMLStreamException {
HashMap XMLToJSNNamespaceMap = new HashMap();
XMLToJSNNamespaceMap.put("", "");
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=675452&r1=675451&r2=675452&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 Wed Jul 9 22:54:32 2008
@@ -50,8 +50,8 @@
* 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
+ * 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.
*/
@@ -73,10 +73,10 @@
}
/**
- * 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.
+ * 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
@@ -88,8 +88,8 @@
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 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) {
@@ -111,8 +111,9 @@
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");
+ "Mapped formatted JSON with namespaces are not supported in Axis2. " +
+ "Make sure that your request doesn't include namespaces or " +
+ "use the Badgerfish convention");
}
}
}
@@ -186,8 +187,9 @@
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");
+ "Mapped formatted JSON with namespaces are not supported in Axis2. " +
+ "Make sure that your request doesn't include namespaces or " +
+ "use the Badgerfish convention");
}
}
@@ -203,9 +205,10 @@
&& Constants.Configuration.HTTP_METHOD_GET.equalsIgnoreCase(httpMethod)) {
try {
String jsonString;
- if (dataOut instanceof OMSourcedElementImpl && getStringToWrite(((OMSourcedElementImpl) dataOut).getDataSource()) != null)
- {
- jsonString = getStringToWrite(((OMSourcedElementImpl) dataOut).getDataSource());
+ if (dataOut instanceof OMSourcedElementImpl && getStringToWrite(
+ ((OMSourcedElementImpl) dataOut).getDataSource()) != null) {
+ jsonString = getStringToWrite(((OMSourcedElementImpl)
+ dataOut).getDataSource());
} else {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter jsonWriter = getJSONWriter(out);
@@ -213,12 +216,14 @@
jsonWriter.writeEndDocument();
jsonString = new String(out.toByteArray());
}
- jsonString = URIEncoderDecoder.quoteIllegal(jsonString, WSDL2Constants.LEGAL_CHARACTERS_IN_URL);
+ 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);
+ return new URL(targetURL.getProtocol(), targetURL.getHost(),
+ targetURL.getPort(), returnURLFile);
} catch (MalformedURLException e) {
throw AxisFault.makeFault(e);
} catch (XMLStreamException e) {
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=675452&r1=675451&r2=675452&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 Wed Jul 9 22:54:32 2008
@@ -44,17 +44,25 @@
public JSONOMBuilder() {
}
- //returns the OMSourcedElementImpl with JSONDataSource inside
+ /**
+ * 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 {
+ public OMElement processDocument(InputStream inputStream, String contentType,
+ MessageContext messageContext) throws AxisFault {
+ OMFactory factory = OMAbstractFactory.getOMFactory();
String localName = "";
String prefix = "";
- OMNamespace ns = new OMNamespaceImpl("", "");
-
- OMFactory factory = OMAbstractFactory.getOMFactory();
+ 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 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();
@@ -71,6 +79,8 @@
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());
@@ -79,7 +89,13 @@
}
}
+ /*
+ 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 != '{') {
@@ -88,12 +104,14 @@
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 != ' ') {
@@ -111,7 +129,8 @@
return new OMSourcedElementImpl(localName, ns, factory, jsonDataSource);
}
- protected JSONDataSource getDataSource(InputStream jsonInputStream, String prefix, String localName) {
+ protected JSONDataSource getDataSource(InputStream
+ jsonInputStream, String prefix, String localName) {
return new JSONDataSource(jsonInputStream, "\"" + prefix + localName + "\"");
}
}