Search in sources :

Example 16 with Message

use of org.apache.axis2.transport.msmq.util.Message 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 17 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class JMSRequestResponseClient method sendMessage.

public IncomingMessage<T> sendMessage(ClientOptions options, ContentType contentType, T message) throws Exception {
    String correlationId = doSendMessage(options, contentType, message);
    MessageConsumer consumer = replySession.createConsumer(replyDestination, "JMSCorrelationID = '" + correlationId + "'");
    try {
        Message replyMessage = consumer.receive(8000);
        return new IncomingMessage<T>(new ContentType(replyMessage.getStringProperty("Content-Type")), jmsMessageFactory.parseMessage(replyMessage));
    } finally {
        consumer.close();
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage) Message(javax.jms.Message) ContentType(javax.mail.internet.ContentType) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage)

Example 18 with Message

use of org.apache.axis2.transport.msmq.util.Message 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)

Example 19 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class MailTransportSender method waitForReply.

private void waitForReply(MessageContext msgContext, String mailMessageID) throws AxisFault {
    // message context in asnych model
    if (!(msgContext.getAxisOperation() instanceof OutInAxisOperation) && (msgContext.getProperty(org.apache.axis2.Constants.PIGGYBACK_MESSAGE) == null)) {
        return;
    }
    ConfigurationContext configContext = msgContext.getConfigurationContext();
    // if the mail message listner has not started we need to start it
    if (!configContext.getListenerManager().isListenerRunning(MailConstants.TRANSPORT_NAME)) {
        TransportInDescription mailTo = configContext.getAxisConfiguration().getTransportIn(MailConstants.TRANSPORT_NAME);
        if (mailTo == null) {
            handleException("Could not find the transport receiver for " + MailConstants.TRANSPORT_NAME);
        }
        configContext.getListenerManager().addListener(mailTo, false);
    }
    SynchronousCallback synchronousCallback = new SynchronousCallback(msgContext);
    Map callBackMap = (Map) msgContext.getConfigurationContext().getProperty(BaseConstants.CALLBACK_TABLE);
    callBackMap.put(mailMessageID, synchronousCallback);
    synchronized (synchronousCallback) {
        try {
            synchronousCallback.wait(msgContext.getOptions().getTimeOutInMilliSeconds());
        } catch (InterruptedException e) {
            handleException("Error occured while waiting ..", e);
        }
    }
    if (!synchronousCallback.isComplete()) {
        // when timeout occurs remove this entry.
        callBackMap.remove(mailMessageID);
        handleException("Timeout while waiting for a response");
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 20 with Message

use of org.apache.axis2.transport.msmq.util.Message in project wso2-axis2-transports by wso2.

the class MailRequestResponseClient method waitForReply.

private Message waitForReply(String msgId) throws Exception {
    Thread.yield();
    Thread.sleep(100);
    Message reply = null;
    boolean replyNotFound = true;
    int retryCount = 50;
    while (replyNotFound) {
        log.debug("Checking for response ... with MessageID : " + msgId);
        reply = getMessage(msgId);
        if (reply != null) {
            replyNotFound = false;
        } else {
            if (retryCount-- > 0) {
                Thread.sleep(100);
            } else {
                break;
            }
        }
    }
    return reply;
}
Also used : Message(javax.mail.Message) IncomingMessage(org.apache.axis2.transport.testkit.message.IncomingMessage) MimeMessage(javax.mail.internet.MimeMessage)

Aggregations

AxisFault (org.apache.axis2.AxisFault)25 MessageContext (org.apache.axis2.context.MessageContext)20 IOException (java.io.IOException)9 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)8 MessageFormatter (org.apache.axis2.transport.MessageFormatter)8 ContentType (javax.mail.internet.ContentType)7 OMElement (org.apache.axiom.om.OMElement)7 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 IncomingMessage (org.apache.axis2.transport.testkit.message.IncomingMessage)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 AxisOperation (org.apache.axis2.description.AxisOperation)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 OutputStream (java.io.OutputStream)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 XMPPConnection (org.jivesoftware.smack.XMPPConnection)4 XMPPException (org.jivesoftware.smack.XMPPException)4 Message (org.jivesoftware.smack.packet.Message)4 AMQP (com.rabbitmq.client.AMQP)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3