use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class Aggregate method isComplete.
/**
* Has this aggregation group completed?
*
* @param synLog the Synapse log to use
*
* @return boolean true if aggregation is complete
*/
public synchronized boolean isComplete(SynapseLog synLog) {
if (!completed) {
// if any messages have been collected, check if the completion criteria is met
if (!messages.isEmpty()) {
// get total messages for this group, from the first message we have collected
MessageContext mc = messages.get(0);
Object prop = mc.getProperty(EIPConstants.MESSAGE_SEQUENCE + (aggregateMediator.getId() != null ? "." + aggregateMediator.getId() : ""));
if (prop != null && prop instanceof String) {
String[] msgSequence = prop.toString().split(EIPConstants.MESSAGE_SEQUENCE_DELEMITER);
int total = Integer.parseInt(msgSequence[1]);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug(messages.size() + " messages of " + total + " collected in current aggregation");
}
if (messages.size() >= total) {
synLog.traceOrDebug("Aggregation complete");
return true;
}
}
} else {
synLog.traceOrDebug("No messages collected in current aggregation");
}
// if the minimum number of messages has been reached, its complete
if (minCount > 0 && messages.size() >= minCount) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Aggregation complete - the minimum : " + minCount + " messages has been reached");
}
return true;
}
if (maxCount > 0 && messages.size() >= maxCount) {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Aggregation complete - the maximum : " + maxCount + " messages has been reached");
}
return true;
}
// else, has this aggregation reached its timeout?
if (expiryTimeMillis > 0 && System.currentTimeMillis() >= expiryTimeMillis) {
synLog.traceOrDebug("Aggregation complete - the aggregation has timed out");
return true;
}
} else {
synLog.traceOrDebug("Aggregation already completed - this message will not be processed in aggregation");
}
return false;
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class AggregateMediator method completeAggregate.
/**
* Invoked by the Aggregate objects that are timed out, to signal timeout/completion of
* itself
* @param aggregate the timed out Aggregate that holds collected messages and properties
*/
public boolean completeAggregate(Aggregate aggregate) {
boolean markedCompletedNow = false;
boolean wasComplete = aggregate.isCompleted();
if (wasComplete) {
return false;
}
if (log.isDebugEnabled()) {
log.debug("Aggregation completed or timed out");
}
// cancel the timer
synchronized (this) {
if (!aggregate.isCompleted()) {
aggregate.cancel();
aggregate.setCompleted(true);
MessageContext lastMessage = aggregate.getLastMessage();
if (lastMessage != null) {
Object aggregateTimeoutHolderObj = lastMessage.getProperty(id != null ? EIPConstants.EIP_SHARED_DATA_HOLDER + "." + id : EIPConstants.EIP_SHARED_DATA_HOLDER);
if (aggregateTimeoutHolderObj != null) {
SharedDataHolder sharedDataHolder = (SharedDataHolder) aggregateTimeoutHolderObj;
sharedDataHolder.markAggregationCompletion();
}
}
markedCompletedNow = true;
}
}
if (!markedCompletedNow) {
return false;
}
MessageContext newSynCtx = getAggregatedMessage(aggregate);
if (newSynCtx == null) {
log.warn("An aggregation of messages timed out with no aggregated messages", null);
return false;
} else {
isAggregationMessageCollected = true;
// in the sequence
if (newSynCtx.isContinuationEnabled()) {
try {
aggregate.getLastMessage().setEnvelope(MessageHelper.cloneSOAPEnvelope(newSynCtx.getEnvelope()));
} catch (AxisFault axisFault) {
log.warn("Error occurred while assigning aggregated message" + " back to the last received message context");
}
}
}
aggregate.clear();
activeAggregates.remove(aggregate.getCorrelation());
if ((correlateExpression != null && correlateExpression.toString().equals(aggregate.getCorrelation())) || correlateExpression == null) {
if (onCompleteSequence != null) {
ContinuationStackManager.addReliantContinuationState(newSynCtx, 0, getMediatorPosition());
boolean result = onCompleteSequence.mediate(newSynCtx);
if (result) {
ContinuationStackManager.removeReliantContinuationState(newSynCtx);
}
return result;
} else if (onCompleteSequenceRef != null && newSynCtx.getSequence(onCompleteSequenceRef) != null) {
ContinuationStackManager.updateSeqContinuationState(newSynCtx, getMediatorPosition());
return newSynCtx.getSequence(onCompleteSequenceRef).mediate(newSynCtx);
} else {
handleException("Unable to find the sequence for the mediation " + "of the aggregated message", newSynCtx);
}
}
return false;
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class CallMediator method handleNonBlockingCall.
/**
* Send request in non-blocking manner
*
* @param synInCtx message context
* @return continue the mediation flow or not
*/
private boolean handleNonBlockingCall(MessageContext synInCtx) {
SynapseLog synLog = getLog(synInCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : Call mediator - Non Blocking Call");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synInCtx.getEnvelope());
}
}
// Set the last sequence fault handler for future use
synInCtx.setProperty(SynapseConstants.LAST_SEQ_FAULT_HANDLER, getLastSequenceFaultHandler(synInCtx));
// clear the message context properties related to endpoint in last service invocation
Set keySet = synInCtx.getPropertyKeySet();
if (keySet != null) {
keySet.remove(SynapseConstants.RECEIVING_SEQUENCE);
keySet.remove(EndpointDefinition.DYNAMIC_URL_VALUE);
keySet.remove(SynapseConstants.LAST_ENDPOINT);
}
boolean outOnlyMessage = "true".equals(synInCtx.getProperty(SynapseConstants.OUT_ONLY));
// Prepare the outgoing message context
MessageContext synOutCtx = null;
if (outOnlyMessage) {
try {
synOutCtx = MessageHelper.cloneMessageContext(synInCtx);
} catch (AxisFault axisFault) {
handleException("Error occurred while cloning msg context", axisFault, synInCtx);
}
} else {
synOutCtx = synInCtx;
}
synOutCtx.setProperty(SynapseConstants.CONTINUATION_CALL, true);
ContinuationStackManager.updateSeqContinuationState(synOutCtx, getMediatorPosition());
// If no endpoints are defined, send where implicitly stated
if (endpoint == null) {
if (synLog.isTraceOrDebugEnabled()) {
StringBuffer sb = new StringBuffer();
sb.append("Calling ").append(synOutCtx.isResponse() ? "response" : "request").append(" message using implicit message properties..");
sb.append("\nCalling To: ").append(synOutCtx.getTo() != null ? synOutCtx.getTo().getAddress() : "null");
sb.append("\nSOAPAction: ").append(synOutCtx.getWSAAction() != null ? synOutCtx.getWSAAction() : "null");
synLog.traceOrDebug(sb.toString());
}
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Envelope : " + synOutCtx.getEnvelope());
}
synOutCtx.getEnvironment().send(null, synOutCtx);
} else {
endpoint.send(synOutCtx);
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("End : Call mediator - Non Blocking Call");
}
if (outOnlyMessage) {
// otherwise flow should stop from here
return true;
}
return false;
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class CallMediator method handleBlockingCall.
/**
* Send request in blocking manner
*
* @param synInCtx message context
* @return continue the mediation flow or not
*/
private boolean handleBlockingCall(MessageContext synInCtx) {
SynapseLog synLog = getLog(synInCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : Call mediator - Blocking Call");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synInCtx.getEnvelope());
}
}
MessageContext resultMsgCtx = null;
// set initClientOption with blockingMsgsender
blockingMsgSender.setInitClientOptions(initClientOptions);
// blocking sender.
if (endpoint == null) {
endpoint = new DefaultEndpoint();
EndpointDefinition endpointDefinition = new EndpointDefinition();
((DefaultEndpoint) endpoint).setDefinition(endpointDefinition);
isWrappingEndpointCreated = true;
}
try {
if ("true".equals(synInCtx.getProperty(SynapseConstants.OUT_ONLY))) {
blockingMsgSender.send(endpoint, synInCtx);
} else {
resultMsgCtx = blockingMsgSender.send(endpoint, synInCtx);
if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR))) {
handleFault(synInCtx, (Exception) resultMsgCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
}
}
} catch (Exception ex) {
handleFault(synInCtx, ex);
}
if (resultMsgCtx != null) {
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
}
try {
synInCtx.setEnvelope(resultMsgCtx.getEnvelope());
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("End : Call mediator - Blocking Call");
}
return true;
} catch (Exception e) {
handleFault(synInCtx, e);
}
} else {
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Service returned a null response");
}
}
return true;
}
use of org.apache.synapse.MessageContext 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