use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class ForwardingService method validateResponse.
/**
* Validate if response can be considered as a successful Back-end invocation
*
* @param responseMessage MessageContext of response
* @return true if it is a successful invocation
*/
private boolean validateResponse(MessageContext responseMessage) {
boolean isSuccessful;
String responseSc = ((Axis2MessageContext) responseMessage).getAxis2MessageContext().getProperty(SynapseConstants.HTTP_SC).toString();
// Some events where response code is null (i.e. sender socket timeout
// when there is no response from endpoint)
int sc = 0;
try {
sc = Integer.parseInt(responseSc.trim());
isSuccessful = getHTTPStatusCodeFamily(sc).equals(HTTPStatusCodeFamily.SUCCESSFUL) || isNonRetryErrorCode(responseSc);
} catch (NumberFormatException nfe) {
isSuccessful = false;
}
if (!isSuccessful) {
String statusCode = " ";
if (sc != 0) {
statusCode = Integer.toString(sc);
}
log.info("Message processor [" + this.messageProcessor.getName() + "] received a response with HTTP_SC: " + statusCode + " from backend " + targetEndpoint + ". Message forwarding failed.");
}
return isSuccessful;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class ForwardingService method updateAxis2MessageContext.
/**
* Update Axis2MessageContext before dispatching message
*
* @param messageToDispatch message to be forwarded
*/
private void updateAxis2MessageContext(MessageContext messageToDispatch) {
//
OMElement firstChild = null;
org.apache.axis2.context.MessageContext origAxis2Ctx = ((Axis2MessageContext) messageToDispatch).getAxis2MessageContext();
if (JsonUtil.hasAJsonPayload(origAxis2Ctx)) {
firstChild = origAxis2Ctx.getEnvelope().getBody().getFirstElement();
}
// OMSourcedElemImpl correctly.
if (JsonUtil.hasAJsonPayload(firstChild)) {
//
OMElement clonedFirstElement = messageToDispatch.getEnvelope().getBody().getFirstElement();
if (clonedFirstElement != null) {
clonedFirstElement.detach();
messageToDispatch.getEnvelope().getBody().addChild(firstChild);
}
}
origAxis2Ctx.setProperty(HTTPConstants.NON_ERROR_HTTP_STATUS_CODES, getNonRetryStatusCodes());
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class ConditionalRouterMediator method mediate.
public boolean mediate(MessageContext synCtx) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
Axis2MessageContext axis2smc = (Axis2MessageContext) synCtx;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2smc.getAxis2MessageContext();
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
Map<String, String> evaluatorHeaders = new HashMap<String, String>();
if (headers != null && headers instanceof Map) {
Map headersMap = (Map) headers;
for (Object entryObj : headersMap.entrySet()) {
Map.Entry entry = (Map.Entry) entryObj;
if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
evaluatorHeaders.put((String) entry.getKey(), (String) entry.getValue());
}
}
}
String url = synCtx.getTo().getAddress();
EvaluatorContext context = new EvaluatorContext(url, evaluatorHeaders);
context.setProperties(((Axis2MessageContext) synCtx).getProperties());
context.setMessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext());
try {
for (ConditionalRoute conditionalRoute : conditionalRoutes) {
if (conditionalRoute.getEvaluator().evaluate(context)) {
conditionalRoute.getTarget().mediate(synCtx);
if (conditionalRoute.isBreakRoute()) {
break;
}
}
}
} catch (EvaluatorException ee) {
handleException("Couldn't evaluate the route condition", ee, synCtx);
}
return continueAfter;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class MessageStoreMediator method mediate.
public boolean mediate(MessageContext synCtx) {
boolean produceStatus;
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
if (synCtx != null) {
MessageStore messageStore;
if (messageStoreExp != null) {
messageStore = synCtx.getConfiguration().getMessageStore(messageStoreExp.stringValueOf(synCtx));
} else {
messageStore = synCtx.getConfiguration().getMessageStore(messageStoreName);
}
if (messageStore != null) {
if (messageStore.getParameters().get(PRODUCER_GUARANTEED_DELIVERY) != null) {
isGuaranteedDeliveryEnabled = Boolean.parseBoolean(messageStore.getParameters().get(PRODUCER_GUARANTEED_DELIVERY).toString());
}
if (messageStore.getParameters().get(FAILOVER_MESSAGE_STORE_NAME) != null) {
failoverMessageStoreName = (String) messageStore.getParameters().get(FAILOVER_MESSAGE_STORE_NAME);
}
if (onStoreSequence != null) {
Mediator sequence = synCtx.getSequence(onStoreSequence);
if (sequence != null) {
sequence.mediate(synCtx);
}
}
if (log.isDebugEnabled()) {
log.debug("Message Store mediator storing the message : \n " + synCtx.getEnvelope());
}
// Ensure that the message is fully read
synCtx.getEnvelope().buildWithAttachments();
// Clone the message before sending to the producer
// Fix ESBJAVA-3650
MessageContext newCtx = null;
try {
newCtx = MessageHelper.cloneMessageContext(synCtx);
ContinuationStackManager.clearStack(newCtx);
} catch (AxisFault af) {
handleException("Error when cloning the message context", af, synCtx);
}
synchronized (storeMessageLock) {
produceStatus = messageStore.getProducer().storeMessage(newCtx);
}
if (!produceStatus) {
// Fix ESBJAVA-5011, since connection is already null need to nullify producer also
if (messageStore instanceof JmsStore) {
((JmsStore) messageStore).setProducer(null);
}
if (isGuaranteedDeliveryEnabled && failoverMessageStoreName != null && !failoverMessageStoreName.isEmpty()) {
MessageStore failoverMessageStore = synCtx.getConfiguration().getMessageStore(failoverMessageStoreName);
boolean failoverProduceStatus = failoverMessageStore.getProducer().storeMessage(newCtx);
if (!failoverProduceStatus) {
synCtx.setProperty(NhttpConstants.HTTP_SC, 500);
synCtx.setProperty(NhttpConstants.ERROR_DETAIL, "Failed to store message.");
synCtx.setProperty(NhttpConstants.ERROR_MESSAGE, "Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].");
handleException("Failed to store message [" + synCtx.getMessageID() + "] in failover store [" + failoverMessageStoreName + "].", synCtx);
}
if (shouldTrace(synCtx)) {
trace.error("Message [" + synCtx.getMessageID() + "] store in the failover message store [" + failoverMessageStoreName + "]");
}
} else {
synCtx.setProperty(NhttpConstants.HTTP_SC, 500);
synCtx.setProperty(NhttpConstants.ERROR_DETAIL, "Failed to store message.");
synCtx.setProperty(NhttpConstants.ERROR_MESSAGE, "Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].");
handleException("Failed to store message [" + synCtx.getMessageID() + "] in store [" + messageStore.getName() + "].", synCtx);
}
}
// with the nio transport, this causes the listener not to write a 202
// Accepted response, as this implies that Synapse does not yet know if
// a 202 or 200 response would be written back.
Axis2MessageContext msgCtx = (Axis2MessageContext) synCtx;
if (null != msgCtx.getAxis2MessageContext() && null != msgCtx.getAxis2MessageContext().getOperationContext()) {
msgCtx.getAxis2MessageContext().getOperationContext().setProperty(org.apache.axis2.Constants.RESPONSE_WRITTEN, "SKIP");
}
return true;
} else {
handleException("Message Store does not exist.", synCtx);
}
}
return false;
}
use of org.apache.synapse.core.axis2.Axis2MessageContext in project wso2-synapse by wso2.
the class CalloutMediator method mediate.
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 : Callout mediator");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synCtx.getEnvelope());
}
}
try {
if (!initClientOptions) {
blockingMsgSender.setInitClientOptions(false);
}
if (endpointKey != null) {
endpoint = synCtx.getEndpoint(endpointKey);
}
if (synLog.isTraceOrDebugEnabled()) {
if (!isWrappingEndpointCreated) {
synLog.traceOrDebug("Using the defined endpoint : " + endpoint.getName());
} else {
if (serviceURL != null) {
synLog.traceOrDebug("Using the serviceURL : " + serviceURL);
} else {
synLog.traceOrDebug("Using the To header as the EPR ");
}
if (securityOn) {
synLog.traceOrDebug("Security enabled within the Callout Mediator config");
if (wsSecPolicyKey != null) {
synLog.traceOrDebug("Using security policy key : " + wsSecPolicyKey);
} else {
if (inboundWsSecPolicyKey != null) {
synLog.traceOrDebug("Using inbound security policy key : " + inboundWsSecPolicyKey);
}
if (outboundWsSecPolicyKey != null) {
synLog.traceOrDebug("Using outbound security policy key : " + outboundWsSecPolicyKey);
}
}
}
}
}
if (isWrappingEndpointCreated) {
org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (Constants.VALUE_TRUE.equals(axis2MsgCtx.getProperty(Constants.Configuration.ENABLE_MTOM))) {
((AbstractEndpoint) endpoint).getDefinition().setUseMTOM(true);
}
}
if (this.serviceURL != null && this.serviceURL.contains(DISTRIBUTED_TX_BEGIN_CHECK_STR)) {
try {
initContext(synCtx);
try {
TranscationManger.lookUp(txContext);
} catch (Exception e) {
handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
}
TranscationManger.beginTransaction();
org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION, TranscationManger.getTransaction());
axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION_MANAGER, TranscationManger.getTransactionManager());
} catch (Exception e) {
handleException("Error starting transaction", synCtx);
}
}
MessageContext synapseOutMsgCtx = MessageHelper.cloneMessageContext(synCtx);
// Send the SOAP Header Blocks to support WS-Addressing
setSoapHeaderBlock(synapseOutMsgCtx);
if (!useEnvelopeAsSource && // if the payload is JSON, we do not consider the request (ie. source) path. Instead, we use the complete payload.
!JsonUtil.hasAJsonPayload(((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext())) {
SOAPBody soapBody = synapseOutMsgCtx.getEnvelope().getBody();
for (Iterator itr = soapBody.getChildElements(); itr.hasNext(); ) {
OMElement child = (OMElement) itr.next();
child.detach();
}
soapBody.addChild(getRequestPayload(synCtx));
}
if (action != null) {
synapseOutMsgCtx.setWSAAction(action);
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("About to invoke the service");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Request message payload : " + synapseOutMsgCtx.getEnvelope());
}
}
MessageContext resultMsgCtx = null;
try {
if ("true".equals(synCtx.getProperty(SynapseConstants.OUT_ONLY))) {
blockingMsgSender.send(endpoint, synapseOutMsgCtx);
} else {
resultMsgCtx = blockingMsgSender.send(endpoint, synapseOutMsgCtx);
setResponseHttpSc(resultMsgCtx, synCtx);
if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
handleFault(synCtx, (Exception) resultMsgCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
}
}
} catch (Exception ex) {
handleFault(synCtx, ex);
}
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
}
if (resultMsgCtx != null && resultMsgCtx.getEnvelope() != null) {
org.apache.axis2.context.MessageContext resultAxisMsgCtx = ((Axis2MessageContext) resultMsgCtx).getAxis2MessageContext();
org.apache.axis2.context.MessageContext inAxisMsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (JsonUtil.hasAJsonPayload(resultAxisMsgCtx)) {
JsonUtil.cloneJsonPayload(resultAxisMsgCtx, inAxisMsgCtx);
} else {
if (targetXPath != null) {
Object o = targetXPath.evaluate(synCtx);
OMElement result = resultMsgCtx.getEnvelope().getBody().getFirstElement();
if (o != null && o instanceof OMElement) {
OMNode tgtNode = (OMElement) o;
tgtNode.insertSiblingAfter(result);
tgtNode.detach();
} else if (o != null && o instanceof List && !((List) o).isEmpty()) {
// Always fetches *only* the first
OMNode tgtNode = (OMElement) ((List) o).get(0);
tgtNode.insertSiblingAfter(result);
tgtNode.detach();
} else {
handleException("Evaluation of target XPath expression : " + targetXPath.toString() + " did not yeild an OMNode", synCtx);
}
} else if (targetKey != null) {
OMElement result = resultMsgCtx.getEnvelope().getBody().getFirstElement();
synCtx.setProperty(targetKey, result);
} else {
synCtx.setEnvelope(resultMsgCtx.getEnvelope());
}
}
// Set HTTP Status code
inAxisMsgCtx.setProperty(SynapseConstants.HTTP_SC, resultAxisMsgCtx.getProperty(SynapseConstants.HTTP_SC));
if ("false".equals(synCtx.getProperty(SynapseConstants.BLOCKING_SENDER_PRESERVE_REQ_HEADERS))) {
inAxisMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, resultAxisMsgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
}
} else {
synLog.traceOrDebug("Service returned a null response");
}
} catch (AxisFault e) {
handleException("Error invoking service : " + serviceURL + (action != null ? " with action : " + action : ""), e, synCtx);
} catch (JaxenException e) {
handleException("Error while evaluating the XPath expression: " + targetXPath, e, synCtx);
}
synLog.traceOrDebug("End : Callout mediator");
return true;
}
Aggregations