Search in sources :

Example 21 with MessageContext

use of org.apache.axis2.context.MessageContext 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 MessageContext

use of org.apache.axis2.context.MessageContext 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 MessageContext

use of org.apache.axis2.context.MessageContext 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 MessageContext

use of org.apache.axis2.context.MessageContext 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)

Example 25 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.

the class RabbitMQRPCMessageSender method publish.

/**
 * Perform the creation of exchange/queue and the Outputstream
 *
 * @param message    the RabbitMQ AMQP message
 * @param msgContext the Axis2 MessageContext
 */
private void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
    String exchangeName = null;
    AMQP.BasicProperties basicProperties = null;
    byte[] messageBody = null;
    if (dualChannel.isOpen()) {
        String queueName = epProperties.get(RabbitMQConstants.QUEUE_NAME);
        String routeKey = epProperties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
        exchangeName = epProperties.get(RabbitMQConstants.EXCHANGE_NAME);
        String exchangeType = epProperties.get(RabbitMQConstants.EXCHANGE_TYPE);
        String correlationID = epProperties.get(RabbitMQConstants.CORRELATION_ID);
        String replyTo = dualChannel.getReplyToQueue();
        String queueAutoDeclareStr = epProperties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
        String exchangeAutoDeclareStr = epProperties.get(RabbitMQConstants.EXCHANGE_AUTODECLARE);
        boolean queueAutoDeclare = true;
        boolean exchangeAutoDeclare = true;
        if (!StringUtils.isEmpty(queueAutoDeclareStr)) {
            queueAutoDeclare = Boolean.parseBoolean(queueAutoDeclareStr);
        }
        if (!StringUtils.isEmpty(exchangeAutoDeclareStr)) {
            exchangeAutoDeclare = Boolean.parseBoolean(exchangeAutoDeclareStr);
        }
        message.setReplyTo(replyTo);
        if ((!StringUtils.isEmpty(replyTo)) && (StringUtils.isEmpty(correlationID))) {
            // if reply-to is enabled a correlationID must be available. If not specified, use messageID
            correlationID = message.getMessageId();
        }
        if (!StringUtils.isEmpty(correlationID)) {
            message.setCorrelationId(correlationID);
        }
        if (queueName == null || queueName.equals("")) {
            log.info("No queue name is specified");
        }
        if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
            if (queueName == null || queueName.equals("")) {
                log.info("Routing key is not specified");
            } else {
                log.info("Routing key is not specified. Using queue name as the routing key.");
                routeKey = queueName;
            }
        }
        // Declaring the queue
        if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
            // get channel with dualChannel.getChannel() since it will create a new channel if channel is closed
            RabbitMQUtils.declareQueue(dualChannel, queueName, epProperties);
        }
        // Declaring the exchange
        if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
            RabbitMQUtils.declareExchange(dualChannel, exchangeName, epProperties);
            if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
                // Create bind between the queue and exchange with the routeKey
                try {
                    dualChannel.getChannel().queueBind(queueName, exchangeName, routeKey);
                } catch (IOException e) {
                    handleException("Error occurred while creating the bind between the queue: " + queueName + " & exchange: " + exchangeName + " with route-key " + routeKey, e);
                }
            }
        }
        // build basic properties from message
        AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
        String deliveryModeString = epProperties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
        int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
        if (deliveryModeString != null) {
            deliveryMode = Integer.parseInt(deliveryModeString);
        }
        // TODO : override properties from message with ones from transport properties
        // set builder properties from transport properties (overrides current properties)
        builder.deliveryMode(deliveryMode);
        builder.replyTo(replyTo);
        basicProperties = builder.build();
        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            messageFormatter = MessageProcessorSelector.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new AxisRabbitMQException("Unable to get the message formatter to use", axisFault);
        }
        // given. Queue/exchange creation, bindings should be done at the broker
        try {
            // x-consistent-hash type
            if (exchangeType != null && exchangeType.equals("x-consistent-hash")) {
                routeKey = UUID.randomUUID().toString();
            }
        } catch (UnsupportedCharsetException ex) {
            handleException("Unsupported encoding " + format.getCharSetEncoding(), ex);
        }
        try {
            messageFormatter.writeTo(msgContext, format, out, false);
            messageBody = out.toByteArray();
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
        try {
            if (exchangeName != null && exchangeName != "") {
                if (log.isDebugEnabled()) {
                    log.debug("Publishing message to exchange " + exchangeName + " with route key " + routeKey);
                }
                dualChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug("Publishing message with route key " + routeKey);
                }
                dualChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
            }
        } catch (IOException e) {
            handleException("Error while publishing the message", e);
        }
    } else {
        handleException("Channel cannot be created");
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisRabbitMQException(org.apache.axis2.transport.rabbitmq.utils.AxisRabbitMQException) IOException(java.io.IOException) MessageFormatter(org.apache.axis2.transport.MessageFormatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AMQP(com.rabbitmq.client.AMQP) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)35 AxisFault (org.apache.axis2.AxisFault)25 OMElement (org.apache.axiom.om.OMElement)13 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)12 IOException (java.io.IOException)11 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)11 XMLStreamException (javax.xml.stream.XMLStreamException)9 QName (javax.xml.namespace.QName)8 MessageFormatter (org.apache.axis2.transport.MessageFormatter)8 AxisOperation (org.apache.axis2.description.AxisOperation)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)5 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)5 SOAPFactory (org.apache.axiom.soap.SOAPFactory)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 OMFactory (org.apache.axiom.om.OMFactory)4 Options (org.apache.axis2.client.Options)4 Message (org.jivesoftware.smack.packet.Message)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3