Search in sources :

Example 81 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class JsonFormatterTest method runTest.

private void runTest(String xmlInput, String jsonOut, InputStream inputStream) {
    try {
        MessageFormatter formatter = Util.newJsonFormatter();
        MessageContext messageContext = Util.newMessageContext(xmlInput);
        JsonUtil.getNewJsonPayload(messageContext, inputStream, false, false);
        OutputStream out = Util.newOutputStream();
        formatter.writeTo(messageContext, null, out, false);
        assertTrue(jsonOut.equals(out.toString()));
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
        assertTrue(false);
    } catch (XMLStreamException e) {
        e.printStackTrace();
        assertTrue(false);
    } catch (Exception e) {
        System.out.println(e);
        assertTrue(false);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) XMLStreamException(javax.xml.stream.XMLStreamException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) MessageContext(org.apache.axis2.context.MessageContext) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException)

Example 82 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class JsonStreamingFormatterTest method testGetBytes.

public void testGetBytes() {
    String inputXML = "<test>123</test>";
    String outputJSON = "{\"test\":123}";
    try {
        OMElement omElement = AXIOMUtil.stringToOM(inputXML);
        MessageFormatter formatter = Util.newJsonStreamFormatter();
        MessageContext messageContext = Util.newMessageContext();
        messageContext.getEnvelope().getBody().addChild(omElement);
        byte[] bytes = formatter.getBytes(messageContext, null);
        String result = new String(bytes);
        assertEquals(outputJSON, result);
    } catch (Exception ex) {
        ex.printStackTrace();
        assertTrue(false);
    }
}
Also used : OMElement(org.apache.axiom.om.OMElement) MessageFormatter(org.apache.axis2.transport.MessageFormatter) MessageContext(org.apache.axis2.context.MessageContext)

Example 83 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class JsonStreamingFormatterTest method testCase.

public void testCase() {
    try {
        InputStream inputStream = Util.newInputStream(obj_1.getBytes());
        MessageFormatter formatter = Util.newJsonStreamFormatter();
        MessageContext messageContext = Util.newMessageContext();
        JsonUtil.getNewJsonPayload(messageContext, inputStream, true, true);
        OutputStream out = Util.newOutputStream();
        formatter.writeTo(messageContext, null, out, false);
        assertTrue(obj_1.equals(out.toString()));
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
        assertTrue(false);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) MessageContext(org.apache.axis2.context.MessageContext)

Example 84 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class Util method newMessageContext.

public static MessageContext newMessageContext(String xmlpayload) throws AxisFault, XMLStreamException {
    MessageContext messageContext = new MessageContext();
    SOAPEnvelope envelope = factory.createSOAPEnvelope();
    factory.createSOAPBody(envelope);
    envelope.getBody().addChild(AXIOMUtil.stringToOM(xmlpayload));
    messageContext.setEnvelope(envelope);
    return messageContext;
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope)

Example 85 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class BuilderFormatterTest method runTest.

public void runTest(String jsonIn, String xmlOut) {
    try {
        MessageContext message = Util.newMessageContext();
        InputStream inputStream = Util.newInputStream(jsonIn.getBytes());
        OMElement element = jsonBuilder.processDocument(inputStream, "application/json", message);
        message.getEnvelope().getBody().addChild(element);
        // System.out.println(element.toString());
        assertTrue(xmlOut.equals(element.toString()));
        OutputStream out = Util.newOutputStream();
        formatter.writeTo(message, null, out, false);
        String outStr = new String(((ByteArrayOutputStream) out).toByteArray());
        // System.out.println(outStr);
        assertTrue(jsonIn.equals(outStr));
    } catch (AxisFault axisFault) {
        axisFault.printStackTrace();
        assertTrue(false);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.axis2.context.MessageContext)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)218 AxisFault (org.apache.axis2.AxisFault)126 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)84 EndpointReference (org.apache.axis2.addressing.EndpointReference)79 OMElement (org.apache.axiom.om.OMElement)75 MessageContext (org.apache.synapse.MessageContext)71 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)62 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)56 IOException (java.io.IOException)55 Map (java.util.Map)52 Test (org.junit.Test)45 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)44 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)36 MessageFormatter (org.apache.axis2.transport.MessageFormatter)35 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)34 Options (org.apache.axis2.client.Options)30 HashMap (java.util.HashMap)29 AxisService (org.apache.axis2.description.AxisService)29 QName (javax.xml.namespace.QName)28 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)28