Search in sources :

Example 1 with TextMessageBuilderAdapter

use of org.apache.axis2.format.TextMessageBuilderAdapter in project wso2-axis2-transports by wso2.

the class JMSUtils method setSOAPEnvelope.

/**
 * Set the SOAPEnvelope to the Axis2 MessageContext, from the JMS Message passed in
 * @param message the JMS message read
 * @param msgContext the Axis2 MessageContext to be populated
 * @param contentType content type for the message
 * @throws AxisFault
 * @throws JMSException
 */
public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault, JMSException {
    if (contentType == null) {
        if (message instanceof TextMessage) {
            contentType = "text/plain";
            msgContext.setProperty(org.apache.axis2.Constants.Configuration.CONTENT_TYPE, "text/plain");
        } else {
            contentType = "application/octet-stream";
        }
        if (log.isDebugEnabled()) {
            log.debug("No content type specified; assuming " + contentType);
        }
    }
    int index = contentType.indexOf(';');
    String type = index > 0 ? contentType.substring(0, index) : contentType;
    Builder builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
    if (builder == null) {
        if (log.isDebugEnabled()) {
            log.debug("No message builder found for type '" + type + "'. Falling back to SOAP.");
        }
        builder = new SOAPBuilder();
    }
    OMElement documentElement;
    if (message instanceof BytesMessage) {
        // Extract the charset encoding from the content type and
        // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this.
        String charSetEnc = null;
        try {
            if (contentType != null) {
                charSetEnc = new ContentType(contentType).getParameter("charset");
            }
        } catch (ParseException ex) {
        // ignore
        }
        msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
        if (builder instanceof DataSourceMessageBuilder) {
            documentElement = ((DataSourceMessageBuilder) builder).processDocument(new BytesMessageDataSource((BytesMessage) message), contentType, msgContext);
        } else {
            documentElement = builder.processDocument(new BytesMessageInputStream((BytesMessage) message), contentType, msgContext);
        }
    } else if (message instanceof TextMessage) {
        TextMessageBuilder textMessageBuilder;
        if (builder instanceof TextMessageBuilder) {
            textMessageBuilder = (TextMessageBuilder) builder;
        } else {
            textMessageBuilder = new TextMessageBuilderAdapter(builder);
        }
        String content = ((TextMessage) message).getText();
        documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
    } else if (message instanceof MapMessage) {
        documentElement = convertJMSMapToXML((MapMessage) message);
    } else {
        Class msgClass = message.getClass();
        String content = "Unsupported JMS message type : " + (msgClass != null ? msgClass.getName() : "undefined.");
        log.error(content);
        msgContext.setProperty(JMSConstants.SENDING_FAULT, true);
        msgContext.setProperty(JMSConstants.ERROR_MESSAGE, content);
        // ERROR_CODE is not set for msg context since it will suspend the ep if not defined in the configs and
        // under this case we don't need the default behavior to get the endpoint suspended.
        TextMessageBuilder textMessageBuilder = new TextMessageBuilderAdapter(builder);
        documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
    }
    msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Also used : ContentType(javax.mail.internet.ContentType) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder) Builder(org.apache.axis2.builder.Builder) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) MapMessage(javax.jms.MapMessage) OMElement(org.apache.axiom.om.OMElement) BytesMessage(javax.jms.BytesMessage) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) TextMessageBuilderAdapter(org.apache.axis2.format.TextMessageBuilderAdapter) BytesMessageDataSource(org.apache.axis2.transport.jms.iowrappers.BytesMessageDataSource) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) ParseException(javax.mail.internet.ParseException) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) DataSourceMessageBuilder(org.apache.axis2.format.DataSourceMessageBuilder)

Example 2 with TextMessageBuilderAdapter

use of org.apache.axis2.format.TextMessageBuilderAdapter in project wso2-axis2-transports by wso2.

the class MSMQUtil method setSOAPEnvelope.

public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault {
    if (contentType == null) {
        // TODO;we only support text/plain
        contentType = "text/plain";
        if (log.isDebugEnabled()) {
            log.debug("No content type specified; assuming " + contentType);
        }
    }
    int index = contentType.indexOf(';');
    String type = index > 0 ? contentType.substring(0, index) : contentType;
    Builder builder = BuilderUtil.getBuilderFromSelector(type, msgContext);
    String messageBody = null;
    try {
        messageBody = message.getBodyAsString();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (builder == null) {
        if (log.isDebugEnabled()) {
            log.debug("No message builder found for type' " + type + ".Using SOAP builder");
        }
        builder = new SOAPBuilder();
    }
    OMElement documentElement;
    // TODO: we need to handle the message types separately. Assume text message builder format
    TextMessageBuilder textMessageBuilder;
    if (builder instanceof TextMessageBuilder) {
        textMessageBuilder = (TextMessageBuilder) builder;
    } else {
        textMessageBuilder = new TextMessageBuilderAdapter(builder);
    }
    documentElement = textMessageBuilder.processDocument(messageBody, contentType, msgContext);
    msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
Also used : TextMessageBuilderAdapter(org.apache.axis2.format.TextMessageBuilderAdapter) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) Builder(org.apache.axis2.builder.Builder) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SOAPBuilder(org.apache.axis2.builder.SOAPBuilder) OMElement(org.apache.axiom.om.OMElement) TextMessageBuilder(org.apache.axis2.format.TextMessageBuilder)

Aggregations

OMElement (org.apache.axiom.om.OMElement)2 Builder (org.apache.axis2.builder.Builder)2 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)2 TextMessageBuilder (org.apache.axis2.format.TextMessageBuilder)2 TextMessageBuilderAdapter (org.apache.axis2.format.TextMessageBuilderAdapter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BytesMessage (javax.jms.BytesMessage)1 MapMessage (javax.jms.MapMessage)1 TextMessage (javax.jms.TextMessage)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 DataSourceMessageBuilder (org.apache.axis2.format.DataSourceMessageBuilder)1 BytesMessageDataSource (org.apache.axis2.transport.jms.iowrappers.BytesMessageDataSource)1 BytesMessageInputStream (org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream)1