Search in sources :

Example 1 with BytesMessageInputStream

use of org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream in project wso2-axis2-transports by wso2.

the class MockEchoEndpoint method setUp.

@Setup
@SuppressWarnings("unused")
private void setUp(JMSTestEnvironment env, JMSRequestResponseChannel channel) throws Exception {
    Destination destination = channel.getDestination();
    Destination replyDestination = channel.getReplyDestination();
    connection = env.getConnectionFactory().createConnection();
    connection.setExceptionListener(this);
    connection.start();
    replyConnection = env.getConnectionFactory().createConnection();
    replyConnection.setExceptionListener(this);
    final Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final MessageProducer producer = replySession.createProducer(replyDestination);
    MessageConsumer consumer = connection.createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(destination);
    consumer.setMessageListener(new MessageListener() {

        public void onMessage(Message message) {
            try {
                log.info("Message received: ID = " + message.getJMSMessageID());
                Message reply;
                if (message instanceof BytesMessage) {
                    reply = replySession.createBytesMessage();
                    IOUtils.copy(new BytesMessageInputStream((BytesMessage) message), new BytesMessageOutputStream((BytesMessage) reply));
                } else if (message instanceof TextMessage) {
                    reply = replySession.createTextMessage();
                    ((TextMessage) reply).setText(((TextMessage) message).getText());
                } else {
                    // TODO
                    throw new UnsupportedOperationException("Unsupported message type");
                }
                reply.setJMSCorrelationID(message.getJMSMessageID());
                reply.setStringProperty(BaseConstants.CONTENT_TYPE, message.getStringProperty(BaseConstants.CONTENT_TYPE));
                producer.send(reply);
                log.info("Message sent: ID = " + reply.getJMSMessageID());
            } catch (Throwable ex) {
                fireEndpointError(ex);
            }
        }
    });
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) BytesMessage(javax.jms.BytesMessage) BytesMessageOutputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageOutputStream) MessageProducer(javax.jms.MessageProducer) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Setup(org.apache.axis2.transport.testkit.tests.Setup)

Example 2 with BytesMessageInputStream

use of org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream 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";
        } 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 {
        handleException("Unsupported JMS message type " + message.getClass().getName());
        // Make compiler happy
        return;
    }
    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 3 with BytesMessageInputStream

use of org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream in project wso2-axis2-transports by wso2.

the class LogAspect method beforeSend.

@Before("(call(void javax.jms.MessageProducer.send(javax.jms.Message)) ||" + " call(void javax.jms.TopicPublisher.publish(javax.jms.Message))) && args(message)")
public void beforeSend(Message message) {
    try {
        OutputStream out = LogManager.INSTANCE.createLog("jms");
        try {
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out), false);
            pw.println("Type: " + message.getClass().getName());
            pw.println("JMS message ID: " + message.getJMSMessageID());
            pw.println("JMS correlation ID: " + message.getJMSCorrelationID());
            pw.println("JMS reply to: " + message.getJMSReplyTo());
            for (Enumeration<?> e = message.getPropertyNames(); e.hasMoreElements(); ) {
                String name = (String) e.nextElement();
                pw.print(name);
                pw.print(": ");
                pw.println(message.getStringProperty(name));
            }
            pw.println();
            pw.flush();
            if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                bytesMessage.reset();
                IOUtils.copy(new BytesMessageInputStream(bytesMessage), out);
            } else if (message instanceof TextMessage) {
                pw.print(((TextMessage) message).getText());
                pw.flush();
            }
        } finally {
            out.close();
        }
    } catch (Throwable ex) {
        log.error("Failed to dump JMS message", ex);
    }
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BytesMessage(javax.jms.BytesMessage) BytesMessageInputStream(org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream) TextMessage(javax.jms.TextMessage) PrintWriter(java.io.PrintWriter) Before(org.aspectj.lang.annotation.Before)

Aggregations

BytesMessage (javax.jms.BytesMessage)3 TextMessage (javax.jms.TextMessage)3 BytesMessageInputStream (org.apache.axis2.transport.jms.iowrappers.BytesMessageInputStream)3 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Destination (javax.jms.Destination)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageListener (javax.jms.MessageListener)1 MessageProducer (javax.jms.MessageProducer)1 Session (javax.jms.Session)1 ContentType (javax.mail.internet.ContentType)1 ParseException (javax.mail.internet.ParseException)1 OMElement (org.apache.axiom.om.OMElement)1 Builder (org.apache.axis2.builder.Builder)1 SOAPBuilder (org.apache.axis2.builder.SOAPBuilder)1 DataSourceMessageBuilder (org.apache.axis2.format.DataSourceMessageBuilder)1 TextMessageBuilder (org.apache.axis2.format.TextMessageBuilder)1