Search in sources :

Example 21 with Message

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

the class SynchronousCallback method setInMessageContext.

public synchronized void setInMessageContext(MessageContext inMessageContext) throws AxisFault {
    // thread should have activate by the first message.
    if (!isComplete) {
        // this code is invoked only if the code use with axis2 at the client side
        // when axis2 client receive messages it waits in the sending thread until the response comes.
        // so this thread only notify the waiting thread and hence we need to build the message here.
        inMessageContext.getEnvelope().build();
        OperationContext operationContext = outMessageContext.getOperationContext();
        MessageContext msgCtx = operationContext.getMessageContext(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
        if (msgCtx == null) {
            // try to see whether there is a piggy back message context
            if (outMessageContext.getProperty(org.apache.axis2.Constants.PIGGYBACK_MESSAGE) != null) {
                msgCtx = (MessageContext) outMessageContext.getProperty(org.apache.axis2.Constants.PIGGYBACK_MESSAGE);
                msgCtx.setTransportIn(inMessageContext.getTransportIn());
                msgCtx.setTransportOut(inMessageContext.getTransportOut());
                msgCtx.setServerSide(false);
                msgCtx.setProperty(BaseConstants.MAIL_CONTENT_TYPE, inMessageContext.getProperty(BaseConstants.MAIL_CONTENT_TYPE));
                // FIXME: this class must not be transport dependent since it is used by AbstractTransportListener
                msgCtx.setIncomingTransportName(org.apache.axis2.Constants.TRANSPORT_MAIL);
                msgCtx.setEnvelope(inMessageContext.getEnvelope());
            } else {
                inMessageContext.setOperationContext(operationContext);
                inMessageContext.setServiceContext(outMessageContext.getServiceContext());
                if (!operationContext.isComplete()) {
                    operationContext.addMessageContext(inMessageContext);
                }
                AxisOperation axisOp = operationContext.getAxisOperation();
                AxisMessage inMessage = axisOp.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
                inMessageContext.setAxisMessage(inMessage);
                inMessageContext.setServerSide(false);
            }
        } else {
            msgCtx.setOperationContext(operationContext);
            msgCtx.setServiceContext(outMessageContext.getServiceContext());
            AxisOperation axisOp = operationContext.getAxisOperation();
            AxisMessage inMessage = axisOp.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
            msgCtx.setAxisMessage(inMessage);
            msgCtx.setTransportIn(inMessageContext.getTransportIn());
            msgCtx.setTransportOut(inMessageContext.getTransportOut());
            msgCtx.setServerSide(false);
            msgCtx.setProperty(BaseConstants.MAIL_CONTENT_TYPE, inMessageContext.getProperty(BaseConstants.MAIL_CONTENT_TYPE));
            // FIXME: this class must not be transport dependent since it is used by AbstractTransportListener
            msgCtx.setIncomingTransportName(org.apache.axis2.Constants.TRANSPORT_MAIL);
            msgCtx.setEnvelope(inMessageContext.getEnvelope());
        }
        this.inMessageContext = inMessageContext;
        isComplete = true;
        this.notifyAll();
    }
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) AxisOperation(org.apache.axis2.description.AxisOperation) MessageContext(org.apache.axis2.context.MessageContext) AxisMessage(org.apache.axis2.description.AxisMessage)

Example 22 with Message

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

the class MSMQSender method sendOverMSMQ.

private void sendOverMSMQ(MessageContext msgCtx, String queuName, String contentType) throws AxisFault {
    IMSMQClient mqClient = new MSMQCamelClient();
    Message message = null;
    try {
        message = createMSMQMessage(msgCtx, contentType, queuName);
        message.setLabel(contentType);
    } catch (AxisFault axisFault) {
        handleException("Error creaging the MSMQ message from the message context", axisFault);
    }
    // TODO: should we wait for a response, ok we need to support MSMQ
    // request/response scenario
    // get the reply queue name and start to receive from here. See
    // JMSSender.java:166 for more
    // information
    boolean waitForResponse = waitForSynchronousResponse(msgCtx);
    if (waitForResponse) {
        try {
            String correlationId = "";
            if (msgCtx.getRelatesTo() != null) {
                correlationId = msgCtx.getRelatesTo().getValue();
            } else {
                // TODO: if we are having a one way message we don't have this
                correlationId = MSMQConstants.DEFAULT_MSG_CORRELATION_ID;
            }
            message.setCorrelationIdAsString(correlationId);
        } catch (UnsupportedEncodingException e) {
            handleException("Error while setting up message Correlation", e);
        }
    } else {
        message.setCorrelationId(MSMQConstants.DEFAULT_MSG_CORRELATION_ID.getBytes());
    }
    try {
        try {
            // By default we are handling queues without transactional
            mqClient.create(queuName, "MSMQ-WSO2", false);
        } catch (MessageQueueException e) {
            log.warn("Queue " + queuName + "  already there.");
        }
        // TODO: how to handle transactional messages
        mqClient.open(queuName, org.apache.axis2.transport.msmq.util.IMSMQClient.Access.SEND);
    } catch (AxisFault axisFault) {
        log.error("Queue " + queuName + "  already there.");
        handleException("Could not open the queu: " + queuName + " for lisinting", axisFault);
    }
    try {
        if (message != null) {
            mqClient.send(message);
        }
    } catch (AxisFault axisFault) {
        handleException("Cloud not send the message: " + axisFault);
    } finally {
        try {
            mqClient.close();
        } catch (AxisFault axisFault) {
            handleException("Cloud not close the queue: ", axisFault);
        }
    }
    if (waitForResponse) {
    // TODO: logic to be finalized on handling synchronous request/reply
    // for the given MSMQ message
    // MSMQClient replyClient = new MSMQClient();
    }
}
Also used : MSMQCamelClient(org.apache.axis2.transport.msmq.util.MSMQCamelClient) AxisFault(org.apache.axis2.AxisFault) Message(org.apache.axis2.transport.msmq.util.Message) IMSMQClient(org.apache.axis2.transport.msmq.util.IMSMQClient) MessageQueueException(org.apache.axis2.transport.msmq.util.MessageQueueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with Message

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

the class MSMQSender method createMSMQMessage.

/**
 * Generating MSMQ message wrapper in order to communicate with JNI
 * @param msgCtx
 * @param contentTypeProperty
 * @param queuName
 * @return
 * @throws AxisFault
 */
private Message createMSMQMessage(MessageContext msgCtx, String messageContentType, String queuName) throws AxisFault {
    String msgBody = null;
    byte[] msgByteBody = null;
    String msgType = getProperty(msgCtx, MSMQConstants.MSMQ_MESSAGE_TYPE);
    // String msgLable = "L:" + queuName+"["+contentType+"]"; // TODO
    // check the first element of the SOAP body, do we have content wrapped
    // using the
    // default wrapper elements for binary
    // (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP
    // messages
    // for MSMQ but just get the payload in its native format
    String msmqMessageType = guessMessageType(msgCtx);
    if (msmqMessageType == null) {
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgCtx);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgCtx);
        } catch (AxisFault axisFault) {
            handleException("Unable to get the message formatter to use", axisFault);
        }
        String contentType = messageFormatter != null ? messageFormatter.getContentType(msgCtx, format, msgCtx.getSoapAction()) : "";
        boolean useBytesMessage = msgType != null && MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msgType) || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1;
        OutputStream out = null;
        StringWriter sw = null;
        if (useBytesMessage) {
        // TODO: handle MSMQ byte message here
        } else {
            sw = new StringWriter();
            try {
                out = new WriterOutputStream(sw, format.getCharSetEncoding());
            } catch (UnsupportedCharsetException ex) {
                handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
            }
        }
        try {
            if (out != null) {
                messageFormatter.writeTo(msgCtx, format, out, true);
                out.close();
            }
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }
        if (!useBytesMessage) {
            msgBody = sw.toString();
        }
    } else if (MSMQConstants.MSMQ_BYTE_MESSAGE.equals(msmqMessageType)) {
    // TODO. handle .net byte messages here.
    } else if (MSMQConstants.MSMQ_TEXT_MESSAGE.equals(msmqMessageType)) {
        msgBody = msgCtx.getEnvelope().getBody().getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText();
    }
    try {
        // Keep message correlation empty will be deciding later on the process
        Message message = new Message(msgBody != null ? msgBody : "", "", "");
        return message;
    } catch (UnsupportedEncodingException e) {
        log.error("Unsported message has been received: ", e);
        handleEexception("Unsported message has been received: ", e);
    }
    return null;
}
Also used : AxisFault(org.apache.axis2.AxisFault) Message(org.apache.axis2.transport.msmq.util.Message) OutputStream(java.io.OutputStream) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MessageFormatter(org.apache.axis2.transport.MessageFormatter) IOException(java.io.IOException) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) StringWriter(java.io.StringWriter) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 24 with Message

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

the class MSMQCamelClient method receive.

public Message receive(int timeOut) throws AxisFault {
    try {
        Message message = new Message("");
        MsmqMessage msmqMessage = new MsmqMessage();
        int initsize = 1;
        boolean cont = true;
        ByteArray recvbuffer = new ByteArray(initsize);
        msmqMessage.setMsgBody(recvbuffer.cast());
        msmqMessage.setBodySize(initsize);
        while (cont) {
            try {
                msmqQueue.receiveMessage(msmqMessage, -1);
                cont = false;
            } catch (RuntimeException ex) {
                if (ex.getMessage().equals("Message body too big")) {
                    initsize += 5;
                    recvbuffer = new ByteArray(initsize);
                    msmqMessage.setMsgBody(recvbuffer.cast());
                    msmqMessage.setBodySize(initsize);
                } else {
                    throw ex;
                }
            }
        }
        Long messageSize = Long.valueOf(msmqMessage.getBodySize());
        byte[] buffer = new byte[messageSize.intValue()];
        for (int i = 0; i < messageSize; ++i) {
            buffer[i] = recvbuffer.getitem(i);
        }
        message.setBody(new String(buffer).getBytes(Message._encoding));
        byte[] cid = new byte[msmq_native_supportConstants.PROPID_M_CORRELATIONID_SIZE];
        message.setCorrelationId(cid);
        message.setLabel(CtypeMapClazz.getCtypeNameById(msmqMessage.getAppSpecific()));
        if (log.isDebugEnabled()) {
            log.info("Message Body size" + msmqMessage.getBodySize());
        }
        return (msmqMessage.getBodySize() == 0 || msmqMessage.getBodySize() == 1) ? null : message;
    } catch (UnsupportedEncodingException e) {
        log.error("Error while Reading the message received via destination", e);
        throw new AxisFault("Error while Reading the message received via destination", e);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) MsmqMessage(org.apache.camel.component.msmq.native_support.MsmqMessage) ByteArray(org.apache.camel.component.msmq.native_support.ByteArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MsmqMessage(org.apache.camel.component.msmq.native_support.MsmqMessage)

Example 25 with Message

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

the class RabbitMQMessageReceiver method processThroughAxisEngine.

/**
 * Process the new message through Axis2
 *
 * @param message the RabbitMQMessage
 * @return true if the caller should commit
 * @throws AxisFault on Axis2 errors
 */
private boolean processThroughAxisEngine(RabbitMQMessage message) throws AxisFault {
    MessageContext msgContext = endpoint.createMessageContext();
    String amqpCorrelationID = message.getCorrelationId();
    if (amqpCorrelationID != null && amqpCorrelationID.length() > 0) {
        msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, amqpCorrelationID);
    } else {
        msgContext.setProperty(RabbitMQConstants.CORRELATION_ID, message.getMessageId());
    }
    String contentType = message.getContentType();
    if (contentType == null) {
        log.warn("Unable to determine content type for message " + msgContext.getMessageID() + " setting to text/plain");
        contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE;
        message.setContentType(contentType);
    }
    msgContext.setProperty(RabbitMQConstants.CONTENT_TYPE, contentType);
    if (message.getContentEncoding() != null) {
        msgContext.setProperty(RabbitMQConstants.CONTENT_ENCODING, message.getContentEncoding());
    }
    String soapAction = message.getSoapAction();
    if (soapAction == null) {
        soapAction = RabbitMQUtils.getSOAPActionHeader(message);
    }
    String replyTo = message.getReplyTo();
    if (replyTo != null) {
        msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, new RabbitMQOutTransportInfo(rabbitMQConnectionFactory, replyTo, contentType));
    }
    RabbitMQUtils.setSOAPEnvelope(message, msgContext, contentType);
    try {
        listener.handleIncomingMessage(msgContext, RabbitMQUtils.getTransportHeaders(message), soapAction, contentType);
    } catch (AxisFault axisFault) {
        log.error("Error when trying to read incoming message ...", axisFault);
        return false;
    }
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) MessageContext(org.apache.axis2.context.MessageContext)

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