/*
* Copyright (c) 2005-2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.axis2.transport.rabbitmq;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.rabbitmq.utils.RabbitMQConstants;
import org.apache.axis2.transport.rabbitmq.utils.RabbitMQUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This is the RabbitMQ AMQP message receiver which is invoked when a message is received. This processes
* the message through the axis2 engine
*/
public class RabbitMQMessageReceiver {
private static final Log log = LogFactory.getLog(RabbitMQMessageReceiver.class);
private final RabbitMQEndpoint endpoint;
private final RabbitMQListener listener;
private final RabbitMQConnectionFactory rabbitMQConnectionFactory;
/**
* Create a new RabbitMQMessage receiver
*
* @param listener the AMQP transport Listener
* @param rabbitMQConnectionFactory the AMQP connection factory we are associated with
* @param endpoint the RabbitMQEndpoint definition to be used
*/
public RabbitMQMessageReceiver(RabbitMQListener listener,
RabbitMQConnectionFactory rabbitMQConnectionFactory, RabbitMQEndpoint endpoint) {
this.endpoint = endpoint;
this.rabbitMQConnectionFactory = rabbitMQConnectionFactory;
this.listener = listener;
}
/**
* Process a new message received
*
* @param message the RabbitMQ AMQP message received
*/
public boolean onMessage(RabbitMQMessage message) {
boolean successful = false;
try {
successful = processThroughAxisEngine(message);
} catch (AxisFault axisFault) {
log.error("Error while processing message", axisFault);
}
return successful;
}
/**
* 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;
}
}