Search in sources :

Example 1 with SharedDataHolder

use of org.apache.synapse.mediators.eip.SharedDataHolder 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;
}
Also used : AxisFault(org.apache.axis2.AxisFault) SharedDataHolder(org.apache.synapse.mediators.eip.SharedDataHolder) MessageContext(org.apache.synapse.MessageContext)

Example 2 with SharedDataHolder

use of org.apache.synapse.mediators.eip.SharedDataHolder in project wso2-synapse by wso2.

the class CloneMediator method mediate.

/**
 * This will implement the mediate method of the Mediator interface and will provide the
 * functionality of cloning message into the specified targets and mediation
 *
 * @param synCtx - MessageContext which is subjected to the cloning
 * @return boolean true if this needs to be further mediated (continueParent=true)
 */
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 : Clone mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    synCtx.setProperty(id != null ? EIPConstants.EIP_SHARED_DATA_HOLDER + "." + id : EIPConstants.EIP_SHARED_DATA_HOLDER, new SharedDataHolder());
    // get the targets list, clone the message for the number of targets and then
    // mediate the cloned messages using the targets
    Iterator<Target> iter = targets.iterator();
    int i = 0;
    while (iter.hasNext()) {
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Submitting " + (i + 1) + " of " + targets.size() + " messages for " + (isSequential() ? "sequential processing" : "parallel processing"));
        }
        MessageContext clonedMsgCtx = getClonedMessageContext(synCtx, i++, targets.size());
        ContinuationStackManager.addReliantContinuationState(clonedMsgCtx, i - 1, getMediatorPosition());
        iter.next().mediate(clonedMsgCtx);
    }
    // if the continuation of the parent message is stopped from here set the RESPONSE_WRITTEN
    // property to SKIP to skip the blank http response
    OperationContext opCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext();
    if (!continueParent && opCtx != null) {
        opCtx.setProperty(Constants.RESPONSE_WRITTEN, "SKIP");
    }
    // finalize tracing and debugging
    synLog.traceOrDebug("End : Clone mediator");
    // mediation of the message which is subjected for clonning (parent message)
    return continueParent;
}
Also used : OperationContext(org.apache.axis2.context.OperationContext) Target(org.apache.synapse.mediators.eip.Target) SynapseLog(org.apache.synapse.SynapseLog) SharedDataHolder(org.apache.synapse.mediators.eip.SharedDataHolder) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Endpoint(org.apache.synapse.endpoints.Endpoint)

Example 3 with SharedDataHolder

use of org.apache.synapse.mediators.eip.SharedDataHolder in project wso2-synapse by wso2.

the class IterateMediator method mediate.

/**
 * Splits the message by iterating over the results of the given XPath expression
 *
 * @param synCtx - MessageContext to be mediated
 * @return boolean false if need to stop processing of the parent message
 */
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 : Iterate mediator");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    try {
        // get a copy of the message for the processing, if the continueParent is set to true
        // this original message can go in further mediations and hence we should not change
        // the original message context
        SOAPEnvelope envelope = MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope());
        synCtx.setProperty(id != null ? EIPConstants.EIP_SHARED_DATA_HOLDER + "." + id : EIPConstants.EIP_SHARED_DATA_HOLDER, new SharedDataHolder());
        // get the iteration elements and iterate through the list,
        // this call will also detach all the iteration elements
        List splitElements = EIPUtils.getDetachedMatchingElements(envelope, synCtx, expression);
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Splitting with XPath : " + expression + " resulted in " + splitElements.size() + " elements");
        }
        // if not preservePayload remove all the child elements
        if (!preservePayload && envelope.getBody() != null) {
            for (Iterator itr = envelope.getBody().getChildren(); itr.hasNext(); ) {
                ((OMNode) itr.next()).detach();
            }
        }
        int msgCount = splitElements.size();
        int msgNumber = 0;
        // iterate through the list
        for (Object o : splitElements) {
            // for the moment iterator will look for an OMNode as the iteration element
            if (!(o instanceof OMNode)) {
                handleException("Error splitting message with XPath : " + expression + " - result not an OMNode", synCtx);
            }
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Submitting " + (msgNumber + 1) + " of " + msgCount + (target.isAsynchronous() ? " messages for processing in parallel" : " messages for processing in sequentially"));
            }
            MessageContext iteratedMsgCtx = getIteratedMessage(synCtx, msgNumber++, msgCount, envelope, (OMNode) o);
            ContinuationStackManager.addReliantContinuationState(iteratedMsgCtx, 0, getMediatorPosition());
            if (target.isAsynchronous()) {
                target.mediate(iteratedMsgCtx);
            } else {
                try {
                    /*
                         * if Iteration is sequential we won't be able to execute correct fault
                         * handler as data are lost with clone message ending execution. So here we
                         * copy fault stack of clone message context to original message context
                         */
                    target.mediate(iteratedMsgCtx);
                } catch (SynapseException synEx) {
                    copyFaultyIteratedMessage(synCtx, iteratedMsgCtx);
                    throw synEx;
                } catch (Exception e) {
                    copyFaultyIteratedMessage(synCtx, iteratedMsgCtx);
                    handleException("Exception occurred while executing sequential iteration " + "in the Iterator Mediator", e, synCtx);
                }
            }
        }
    } catch (JaxenException e) {
        handleException("Error evaluating split XPath expression : " + expression, e, synCtx);
    } catch (AxisFault af) {
        handleException("Error creating an iterated copy of the message", af, synCtx);
    } catch (SynapseException synEx) {
        throw synEx;
    } catch (Exception e) {
        handleException("Exception occurred while executing the Iterate Mediator", e, synCtx);
    }
    // if the continuation of the parent message is stopped from here set the RESPONSE_WRITTEN
    // property to SKIP to skip the blank http response
    OperationContext opCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getOperationContext();
    if (!continueParent && opCtx != null) {
        opCtx.setProperty(Constants.RESPONSE_WRITTEN, "SKIP");
    }
    synLog.traceOrDebug("End : Iterate mediator");
    // whether to continue mediation on the original message
    return continueParent;
}
Also used : AxisFault(org.apache.axis2.AxisFault) OperationContext(org.apache.axis2.context.OperationContext) SynapseException(org.apache.synapse.SynapseException) SharedDataHolder(org.apache.synapse.mediators.eip.SharedDataHolder) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) Endpoint(org.apache.synapse.endpoints.Endpoint) JaxenException(org.jaxen.JaxenException) SynapseException(org.apache.synapse.SynapseException) OMNode(org.apache.axiom.om.OMNode) SynapseLog(org.apache.synapse.SynapseLog) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

MessageContext (org.apache.synapse.MessageContext)3 SharedDataHolder (org.apache.synapse.mediators.eip.SharedDataHolder)3 AxisFault (org.apache.axis2.AxisFault)2 OperationContext (org.apache.axis2.context.OperationContext)2 SynapseLog (org.apache.synapse.SynapseLog)2 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)2 Endpoint (org.apache.synapse.endpoints.Endpoint)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 OMNode (org.apache.axiom.om.OMNode)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 SynapseException (org.apache.synapse.SynapseException)1 Target (org.apache.synapse.mediators.eip.Target)1 JaxenException (org.jaxen.JaxenException)1