use of org.apache.axis2.context.MessageContext in project wso2-axis2-transports by wso2.
the class RabbitMQMessageSender method publish.
/**
* Perform the creation of exchange/queue and the Outputstream
*
* @param message the RabbitMQ AMQP message
* @param msgContext the Axis2 MessageContext
*/
public void publish(RabbitMQMessage message, MessageContext msgContext) throws AxisRabbitMQException, IOException {
String exchangeName = null;
AMQP.BasicProperties basicProperties = null;
byte[] messageBody = null;
if (rmqChannel.isOpen()) {
String queueName = properties.get(RabbitMQConstants.QUEUE_NAME);
String routeKey = properties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
exchangeName = properties.get(RabbitMQConstants.EXCHANGE_NAME);
String exchangeType = properties.get(RabbitMQConstants.EXCHANGE_TYPE);
String replyTo = properties.get(RabbitMQConstants.REPLY_TO_NAME);
String correlationID = properties.get(RabbitMQConstants.CORRELATION_ID);
String queueAutoDeclareStr = properties.get(RabbitMQConstants.QUEUE_AUTODECLARE);
String exchangeAutoDeclareStr = properties.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(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.debug("No queue name is specified");
}
if (routeKey == null && !"x-consistent-hash".equals(exchangeType)) {
if (queueName == null || queueName.equals("")) {
log.debug("Routing key is not specified");
} else {
log.debug("Routing key is not specified. Using queue name as the routing key.");
routeKey = queueName;
}
}
// read publish properties corr id and route key from message context
Object prRouteKey = msgContext.getProperty(RabbitMQConstants.QUEUE_ROUTING_KEY);
Object prCorrId = msgContext.getProperty(RabbitMQConstants.CORRELATION_ID).toString();
if (prRouteKey != null) {
routeKey = prRouteKey.toString();
log.debug("Specifying routing key from axis2 properties");
}
if (prCorrId != null) {
message.setCorrelationId(prCorrId.toString());
log.debug("Specifying correlation id from axis2 properties");
}
// Declaring the queue
if (queueAutoDeclare && queueName != null && !queueName.equals("")) {
RabbitMQUtils.declareQueue(rmqChannel, queueName, properties);
}
// Declaring the exchange
if (exchangeAutoDeclare && exchangeName != null && !exchangeName.equals("")) {
RabbitMQUtils.declareExchange(rmqChannel, exchangeName, properties);
if (queueName != null && !"x-consistent-hash".equals(exchangeType)) {
// Create bind between the queue and exchange with the routeKey
try {
rmqChannel.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);
}
}
}
AMQP.BasicProperties.Builder builder = buildBasicProperties(message);
String deliveryModeString = properties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE);
int deliveryMode = RabbitMQConstants.DEFAULT_DELIVERY_MODE;
if (deliveryModeString != null) {
deliveryMode = Integer.parseInt(deliveryModeString);
}
builder.deliveryMode(deliveryMode);
if (!StringUtils.isEmpty(replyTo)) {
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 {
/**
* Check for parameter on confirmed delivery in RabbitMq to ensure reliable delivery.
* Can be enabled by setting the parameter "rabbitmq.confirm.delivery" to "true" in broker URL.
*/
boolean isConfirmedDeliveryEnabled = Boolean.parseBoolean(properties.get(RabbitMQConstants.CONFIRMED_DELIVERY));
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().confirmSelect();
}
if (exchangeName != null && exchangeName != "") {
rmqChannel.getChannel().basicPublish(exchangeName, routeKey, basicProperties, messageBody);
} else {
rmqChannel.getChannel().basicPublish("", routeKey, basicProperties, messageBody);
}
if (isConfirmedDeliveryEnabled) {
rmqChannel.getChannel().waitForConfirmsOrDie();
}
} catch (IOException e) {
handleException("Error while publishing the message", e);
} catch (InterruptedException e) {
handleException("InterruptedException while waiting for AMQP message confirm :", e);
}
} else {
handleException("Channel cannot be created");
}
}
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;
}
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");
}
}
use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.
the class PropertyMediator method mediate.
/**
* Sets a property into the current (local) Synapse Context or into the Axis Message Context
* or into Transports Header and removes above properties from the corresponding locations.
*
* @param synCtx the message context
* @return true always
*/
public boolean mediate(MessageContext synCtx) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
SynapseLog synLog = getLog(synCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : Property mediator");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synCtx.getEnvelope());
}
}
if (action == ACTION_SET) {
Object resultValue = getResultValue(synCtx);
// choose part of it
if (resultValue instanceof String && pattern != null) {
resultValue = getMatchedValue((String) resultValue, synLog);
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Setting property : " + name + " at scope : " + (scope == null ? "default" : scope) + " to : " + resultValue + " (i.e. " + (value != null ? "constant : " + value : "result of expression : " + expression) + ")");
}
if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
// Setting property into the Synapse Context
if (resultValue != null && resultValue instanceof OMElement) {
((OMElement) resultValue).build();
}
synCtx.setProperty(name, resultValue);
} else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Setting property into the Axis2 Message Context
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2MessageCtx.setProperty(name, resultValue);
MediatorPropertyUtils.handleSpecialProperties(name, resultValue, axis2MessageCtx);
} else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Setting property into the Axis2 Message Context client options
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2MessageCtx.getOptions().setProperty(name, resultValue);
} else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
/*
* if null is passed as header value at AbstractHTTPSender in Axis2 when header
* value is read causes a null-pointer issue
*/
if (resultValue == null) {
resultValue = "";
}
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
headersMap.put(name, resultValue);
}
if (headers == null) {
Map headersMap = new HashMap();
headersMap.put(name, resultValue);
axis2MessageCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, headersMap);
}
} else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Setting Transport Headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2smc.getAxis2MessageContext().getOperationContext().setProperty(name, resultValue);
} else if (XMLConfigConstants.SCOPE_REGISTRY.equals(scope) && synCtx instanceof Axis2MessageContext) {
String[] args = name.split("@");
String path = "";
String propertyName = "";
// with the property mentioned and the value as its value
if (args.length == 1) {
path = args[0];
} else if (args.length == 2) {
path = args[0];
propertyName = args[1];
}
Registry registry = synCtx.getConfiguration().getRegistry();
registry.newNonEmptyResource(path, false, CONTENT_TYPE, resultValue.toString(), propertyName);
}
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Removing property : " + name + " (scope:" + (scope == null ? "default" : scope) + ")");
}
if (scope == null || XMLConfigConstants.SCOPE_DEFAULT.equals(scope)) {
// Removing property from the Synapse Context
Set pros = synCtx.getPropertyKeySet();
if (pros != null) {
pros.remove(name);
}
} else if (XMLConfigConstants.SCOPE_AXIS2.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Removing property from the Axis2 Message Context
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
axis2MessageCtx.removeProperty(name);
} else if (XMLConfigConstants.SCOPE_CLIENT.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Removing property from the Axis2-client Message Context
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
// Property value is set to null since axis2MessageCtx.getOptions()
// does not have an option to remove properties
axis2MessageCtx.getOptions().setProperty(name, null);
} else if (XMLConfigConstants.SCOPE_TRANSPORT.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Removing transport headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
headersMap.remove(name);
} else {
synLog.traceOrDebug("No transport headers found for the message");
}
} else if (XMLConfigConstants.SCOPE_OPERATION.equals(scope) && synCtx instanceof Axis2MessageContext) {
// Removing operation scope headers
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
OperationContext axis2oc = axis2MessageCtx.getOperationContext();
axis2oc.removeProperty(name);
}
}
synLog.traceOrDebug("End : Property mediator");
return true;
}
use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.
the class FaultMediator method makeSOAPFault.
/**
* Actual transformation of the current message into a fault message
* @param synCtx the current message context
* @param soapVersion SOAP version of the resulting fault desired
* @param synLog the Synapse log to use
*/
private void makeSOAPFault(MessageContext synCtx, int soapVersion, SynapseLog synLog) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Creating a SOAP " + (soapVersion == SOAP11 ? "1.1" : "1.2") + " fault");
}
// get the correct SOAP factory to be used
SOAPFactory factory = (soapVersion == SOAP11 ? OMAbstractFactory.getSOAP11Factory() : OMAbstractFactory.getSOAP12Factory());
// create the SOAP fault document and envelope
OMDocument soapFaultDocument = factory.createOMDocument();
SOAPEnvelope faultEnvelope = factory.getDefaultFaultEnvelope();
soapFaultDocument.addChild(faultEnvelope);
// create the fault element if it is need
SOAPFault fault = faultEnvelope.getBody().getFault();
if (fault == null) {
fault = factory.createSOAPFault();
}
// populate it
setFaultCode(synCtx, factory, fault, soapVersion);
setFaultReason(synCtx, factory, fault, soapVersion);
setFaultNode(factory, fault);
setFaultRole(factory, fault);
setFaultDetail(synCtx, factory, fault);
// set the all headers of original SOAP Envelope to the Fault Envelope
if (synCtx.getEnvelope() != null) {
SOAPHeader soapHeader = synCtx.getEnvelope().getHeader();
if (soapHeader != null) {
for (Iterator iter = soapHeader.examineAllHeaderBlocks(); iter.hasNext(); ) {
Object o = iter.next();
if (o instanceof SOAPHeaderBlock) {
SOAPHeaderBlock header = (SOAPHeaderBlock) o;
faultEnvelope.getHeader().addChild(header);
} else if (o instanceof OMElement) {
faultEnvelope.getHeader().addChild((OMElement) o);
}
}
}
}
if (synLog.isTraceOrDebugEnabled()) {
String msg = "Original SOAP Message : " + synCtx.getEnvelope().toString() + "Fault Message created : " + faultEnvelope.toString();
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace(msg);
}
if (log.isTraceEnabled()) {
log.trace(msg);
}
}
// overwrite current message envelope with new fault envelope
try {
synCtx.setEnvelope(faultEnvelope);
} catch (AxisFault af) {
handleException("Error replacing current SOAP envelope " + "with the fault envelope", af, synCtx);
}
if (synCtx.getFaultTo() != null) {
synCtx.setTo(synCtx.getFaultTo());
} else if (synCtx.getReplyTo() != null) {
synCtx.setTo(synCtx.getReplyTo());
} else {
synCtx.setTo(null);
}
// set original messageID as relatesTo
if (synCtx.getMessageID() != null) {
RelatesTo relatesTo = new RelatesTo(synCtx.getMessageID());
synCtx.setRelatesTo(new RelatesTo[] { relatesTo });
}
synLog.traceOrDebug("End : Fault mediator");
}
Aggregations