Hi,
Please find the source attached. Compile it and add to axis2.xml instead of 
default badgerfish builder.

From: Hans De Clercq [mailto:hans.de.cle...@aristomusic.com]
Sent: Thursday, September 29, 2011 12:43 PM
To: java-user@axis.apache.org
Subject: Re: [AXIS2] Problem: Sending JSON messages to a SOAP Web service using 
Axis2

Hi,

I suppose you mean JSONBadgerfishOMBuilder ?

is it possible to send me a modified jar ?
Or explain how I can modify JSONBadgerfishOMBuilder and what the whole 
processDocument method looks like?

Thanks!

Op 28/09/2011 15:21, Hodchenkov, Paul schreef:
Hi,
According to this [1], you can't use JSON with RPCMessageReceiver.
I modified JSONBadgerfishBuilder to handle this case by converting 
JSON/Badgerfish explicitly to SOAP:
public OMElement processDocument(InputStream inputStream, String contentType,
                                     MessageContext messageContext) throws 
AxisFault {
....
    AbstractXMLInputFactory inputFactory = createFactory();
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(
                    new JSONTokener(IOUtils.toString(reader)));
            OMNodeEx document = (OMNodeEx) new 
StAXOMBuilder(xmlReader).getDocumentElement();
            //removing parent
            document.setParent(null);
            //wrapping document with envelope
            SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
            SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
            SOAPBody body = soapEnvelope.getBody();
            body.addChild(document);

            soapEnvelope.build();

...
}

[1] http://isurues.wordpress.com/2009/10/06/how-to-use-axis2-json/


--
[cid:image001.jpg@01CC7EA8.9EF62A00]
Met vriendelijke groeten I Hans De Clercq I R&D
Aristo Music NV I Interleuvenlaan 15 F I  B - 3001 Heverlee I + 32 498 301007 I 
www.tunify.com<http://www.tunify.com/>

FOLLOW US ON :
[cid:image002.gif@01CC7EA8.9EF62A00]<http://www.twitter.com/tunify>
[cid:image003.gif@01CC7EA8.9EF62A00]<http://www.facebook.com/tunify>

CONFIDENTIALITY: This email (including any attachments) may contain 
confidential, proprietary and privileged information, and unauthorized 
disclosure or use is prohibited. If you received this email in error, please 
notify the sender and delete this email from your system

<<inline: image001.jpg>>

<<inline: image002.gif>>

<<inline: image003.gif>>

package com.format.json;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.OMNodeEx;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
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 org.apache.commons.io.IOUtils;
import org.codehaus.jettison.AbstractXMLInputFactory;
import org.codehaus.jettison.json.JSONTokener;

import javax.xml.stream.XMLStreamReader;
import java.io.*;

/**
 * Base class for JSON implementation. This class solves the namespace problem
 * which present in org.apache.axis2.json.AbstractJSONDataSource. This version 
does not
 * use OMSourcedElementImpl
 *
 */
public lass MyJSONBadgerfishBuilder implements Builder {

    public MyJSONBadgerfishBuilder() {
    }

    /**
     * gives the OMElement 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 OMElement
     * @throws org.apache.axis2.AxisFault
     */

    public OMElement processDocument(InputStream inputStream, String 
contentType,
                                     MessageContext messageContext) throws 
AxisFault {

        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

        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;
            //As the message is received through GET, check for "=" sign and 
consider the second
            //half as the incoming JSON message
            int index = requestURL.indexOf('=');
            if (index > 0) {
                jsonString = requestURL.substring(index + 1);
                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);
            }
        }

        try {
            AbstractXMLInputFactory inputFactory = new 
BadgerFishXMLInputFactory();
            XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(
                    new JSONTokener(IOUtils.toString(reader)));
            OMNodeEx document = (OMNodeEx) new 
StAXOMBuilder(xmlReader).getDocumentElement();
            //removing parent
            document.setParent(null);
            //wrapping document with envelope
            SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
            SOAPEnvelope soapEnvelope = soapFactory.getDefaultEnvelope();
            SOAPBody body = soapEnvelope.getBody();
            body.addChild(document);

            soapEnvelope.build();

            //converting xml structure to soap xml structure,
            //this operation will construct SoapEnvelope,SoapBody,SoapFault 
instead of
            //regular OmElement
            StAXSOAPModelBuilder stAXSOAPModelBuilder = new 
StAXSOAPModelBuilder(soapEnvelope.getXMLStreamReader(), null);

            return stAXSOAPModelBuilder.getSOAPEnvelope();

        } catch (Exception e) {
            throw AxisFault.makeFault(e);
        }

    }

}
---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@axis.apache.org
For additional commands, e-mail: java-user-h...@axis.apache.org

Reply via email to