Search in sources :

Example 26 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-axis2-transports by wso2.

the class MqttSender method createMqttMessage.

private MqttMessage createMqttMessage(MessageContext messageContext) {
    OMOutputFormat format = BaseUtils.getOMOutputFormat(messageContext);
    MessageFormatter messageFormatter;
    try {
        messageFormatter = MessageProcessorSelector.getMessageFormatter(messageContext);
    } catch (AxisFault axisFault) {
        throw new AxisMqttException("Unable to get the message formatter to use");
    }
    OutputStream out;
    StringWriter sw = new StringWriter();
    try {
        out = new WriterOutputStream(sw, format.getCharSetEncoding());
    } catch (UnsupportedCharsetException ex) {
        throw new AxisMqttException("Unsupported encoding " + format.getCharSetEncoding(), ex);
    }
    try {
        messageFormatter.writeTo(messageContext, format, out, true);
        out.close();
    } catch (IOException e) {
        throw new AxisMqttException("IO Error while creating BytesMessage", e);
    }
    MqttMessage mqttMessage = new MqttMessage();
    mqttMessage.setPayload(sw.toString().getBytes());
    return mqttMessage;
}
Also used : AxisFault(org.apache.axis2.AxisFault) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream)

Example 27 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.

the class XFormURLEncodedFormatterTest method testGetBytes.

public void testGetBytes() throws AxisFault, XMLStreamException {
    XFormURLEncodedFormatter formatter = new XFormURLEncodedFormatter();
    MessageContext messageContext = Util.newMessageContext("<test><test>123</test></test>");
    byte[] bytes = formatter.getBytes(messageContext, new OMOutputFormat());
    assertEquals("Invalid X-form URL Encoded string", "test=123", new String(bytes));
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 28 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.

the class XFormURLEncodedFormatterTest method testGetContentTypeWithSoapAction.

public void testGetContentTypeWithSoapAction() throws AxisFault, XMLStreamException {
    XFormURLEncodedFormatter formatter = new XFormURLEncodedFormatter();
    MessageContext messageContext = Util.newMessageContext();
    String contentType = formatter.getContentType(messageContext, new OMOutputFormat(), "urn:mediate");
    assertEquals("Invalid content type", "application/x-www-form-urlencoded;action=\"urn:mediate\";", contentType);
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 29 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.

the class XFormURLEncodedFormatterTest method testGetContentType.

public void testGetContentType() throws AxisFault, XMLStreamException {
    XFormURLEncodedFormatter formatter = new XFormURLEncodedFormatter();
    MessageContext messageContext = Util.newMessageContext();
    String contentType = formatter.getContentType(messageContext, new OMOutputFormat(), "");
    assertEquals("Invalid content type", "application/x-www-form-urlencoded", contentType);
}
Also used : MessageContext(org.apache.axis2.context.MessageContext) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 30 with OMOutputFormat

use of org.apache.axiom.om.OMOutputFormat in project wso2-synapse by wso2.

the class JsonDataSourceTest method testSerializeXml.

public void testSerializeXml() throws FileNotFoundException, XMLStreamException {
    JsonDataSource jsonDataSource = createJsonDatasource();
    OutputStream outputStream = Util.newOutputStream();
    OMOutputFormat outputFormat = new OMOutputFormat();
    outputFormat.setContentType("text/xml");
    jsonDataSource.serialize(outputStream, outputFormat);
    assertEquals("Invalid serialization", expectedXML, outputStream.toString());
}
Also used : OutputStream(java.io.OutputStream) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Aggregations

OMOutputFormat (org.apache.axiom.om.OMOutputFormat)64 MessageFormatter (org.apache.axis2.transport.MessageFormatter)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 OutputStream (java.io.OutputStream)18 IOException (java.io.IOException)16 AxisFault (org.apache.axis2.AxisFault)13 MessageContext (org.apache.axis2.context.MessageContext)12 OMElement (org.apache.axiom.om.OMElement)11 DataHandler (javax.activation.DataHandler)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 StringWriter (java.io.StringWriter)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 OMText (org.apache.axiom.om.OMText)5 Map (java.util.Map)4 MultipartBody (org.apache.axiom.mime.MultipartBody)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)4 Collection (java.util.Collection)3