org.apache.axis2.json
Class JSONMessageFormatter

java.lang.Object
  extended by org.apache.axis2.json.JSONMessageFormatter
All Implemented Interfaces:
MessageFormatter
Direct Known Subclasses:
JSONBadgerfishMessageFormatter

public class JSONMessageFormatter
extends java.lang.Object
implements MessageFormatter

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.


Constructor Summary
JSONMessageFormatter()
           
 
Method Summary
 java.lang.String formatSOAPAction(MessageContext msgCtxt, org.apache.axiom.om.OMOutputFormat format, java.lang.String soapActionString)
           
 byte[] getBytes(MessageContext msgCtxt, org.apache.axiom.om.OMOutputFormat format)
          Gives the JSON message as an array of bytes.
 java.lang.String getContentType(MessageContext msgCtxt, org.apache.axiom.om.OMOutputFormat format, java.lang.String soapActionString)
          Different message formats can set their own content types Eg: JSONFormatter can set the content type as application/json
protected  javax.xml.stream.XMLStreamWriter getJSONWriter(java.io.OutputStream outStream)
           
protected  java.lang.String getStringToWrite(org.apache.axiom.om.OMDataSource dataSource)
          If the data source is a "Mapped" formatted data source, gives the JSON string by directly taking from the data source.
 java.net.URL getTargetAddress(MessageContext msgCtxt, org.apache.axiom.om.OMOutputFormat format, java.net.URL targetURL)
          Some message formats may want to alter the target url.
 void writeTo(MessageContext msgCtxt, org.apache.axiom.om.OMOutputFormat format, java.io.OutputStream out, boolean preserve)
          Writes the JSON message to the output stream with the correct convention.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONMessageFormatter

public JSONMessageFormatter()
Method Detail

getContentType

public java.lang.String getContentType(MessageContext msgCtxt,
                                       org.apache.axiom.om.OMOutputFormat format,
                                       java.lang.String soapActionString)
Description copied from interface: MessageFormatter
Different message formats can set their own content types Eg: JSONFormatter can set the content type as application/json

Specified by:
getContentType in interface MessageFormatter

getBytes

public byte[] getBytes(MessageContext msgCtxt,
                       org.apache.axiom.om.OMOutputFormat format)
                throws AxisFault
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.

Specified by:
getBytes in interface MessageFormatter
Parameters:
msgCtxt - Message context which contains the soap envelope to be written
format - format of the message, this is ignored
Returns:
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.

formatSOAPAction

public java.lang.String formatSOAPAction(MessageContext msgCtxt,
                                         org.apache.axiom.om.OMOutputFormat format,
                                         java.lang.String soapActionString)
Specified by:
formatSOAPAction in interface MessageFormatter
Returns:
this only if you want set a transport header for SOAP Action

getJSONWriter

protected javax.xml.stream.XMLStreamWriter getJSONWriter(java.io.OutputStream outStream)

getStringToWrite

protected java.lang.String getStringToWrite(org.apache.axiom.om.OMDataSource dataSource)
If the data source is a "Mapped" formatted data source, gives the JSON string by directly taking from the data source.

Parameters:
dataSource - data source to be checked
Returns:
the JSON string to write

writeTo

public void writeTo(MessageContext msgCtxt,
                    org.apache.axiom.om.OMOutputFormat format,
                    java.io.OutputStream out,
                    boolean preserve)
             throws AxisFault
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. *

Specified by:
writeTo in interface MessageFormatter
Parameters:
msgCtxt - Message context which contains the soap envelope to be written
format - format of the message, this is ignored
out - output stream to be written in to
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.

getTargetAddress

public java.net.URL getTargetAddress(MessageContext msgCtxt,
                                     org.apache.axiom.om.OMOutputFormat format,
                                     java.net.URL targetURL)
                              throws AxisFault
Description copied from interface: MessageFormatter
Some message formats may want to alter the target url.

Specified by:
getTargetAddress in interface MessageFormatter
Returns:
the target URL
Throws:
AxisFault


Copyright © 2007 Apache Web Services Project. All Rights Reserved.