Search in sources :

Example 1 with ResponseState

use of org.apache.synapse.core.axis2.ResponseState in project wso2-synapse by wso2.

the class MessageHelper method cloneMessageContext.

/**
 * This method will simulate cloning the message context and creating an exact copy of the
 * passed message. One should use this method with care; that is because, inside the new MC,
 * most of the attributes of the MC like opCtx and so on are still kept as references inside
 * the axis2 MessageContext for performance improvements. (Note: U dont have to worrie
 * about the SOAPEnvelope, it is a cloned copy and not a reference from any other MC)
 * @param synCtx - this will be cloned
 * @param cloneSoapEnvelope whether to clone the soap envelope
 * @return cloned Synapse MessageContext
 * @throws AxisFault if there is a failure in creating the new Synapse MC or in a failure in
 *          clonning the underlying axis2 MessageContext
 *
 * @see MessageHelper#cloneAxis2MessageContext
 */
public static MessageContext cloneMessageContext(MessageContext synCtx, boolean cloneSoapEnvelope) throws AxisFault {
    // creates the new MessageContext and clone the internal axis2 MessageContext
    // inside the synapse message context and place that in the new one
    MessageContext newCtx = synCtx.getEnvironment().createMessageContext();
    Axis2MessageContext axis2MC = (Axis2MessageContext) newCtx;
    axis2MC.setAxis2MessageContext(cloneAxis2MessageContext(((Axis2MessageContext) synCtx).getAxis2MessageContext(), cloneSoapEnvelope));
    newCtx.setConfiguration(synCtx.getConfiguration());
    newCtx.setEnvironment(synCtx.getEnvironment());
    newCtx.setContextEntries(synCtx.getContextEntries());
    // set the parent correlation details to the cloned MC -
    // for the use of aggregation like tasks
    newCtx.setProperty(EIPConstants.AGGREGATE_CORRELATION, synCtx.getMessageID());
    // copying the core parameters of the synapse MC
    newCtx.setTo(synCtx.getTo());
    newCtx.setReplyTo(synCtx.getReplyTo());
    newCtx.setSoapAction(synCtx.getSoapAction());
    newCtx.setWSAAction(synCtx.getWSAAction());
    newCtx.setResponse(synCtx.isResponse());
    // copy all the synapse level properties to the newCtx
    for (Object o : synCtx.getPropertyKeySet()) {
        // If there are non String keyed properties neglect them rather than trow exception
        if (o instanceof String) {
            String strkey = (String) o;
            Object obj = synCtx.getProperty(strkey);
            if (obj instanceof String) {
            // No need to do anything since Strings are immutable
            } else if (obj instanceof ArrayList) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Started for  ArrayList property: " + strkey + ".");
                }
                // Call this method to deep clone ArrayList
                obj = cloneArrayList((ArrayList) obj);
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone Ended for  ArrayList property: " + strkey + ".");
                }
            } else if (obj instanceof Stack && strkey.equals(SynapseConstants.SYNAPSE__FUNCTION__STACK)) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for Template function stack");
                }
                obj = getClonedTemplateStack((Stack<TemplateContext>) obj);
            } else if (obj instanceof OMElement) {
                if (log.isDebugEnabled()) {
                    log.debug("Deep clone for OMElement");
                }
                obj = (OMElement) ((OMElement) obj).cloneOMElement();
            } else if (obj instanceof ResponseState) {
            // do nothing and let the same reference to go to the cloned context
            } else {
                /**
                 * Need to add conditions according to type if found in
                 * future
                 */
                if (log.isDebugEnabled()) {
                    log.warn("Deep clone not happened for property : " + strkey + ". Class type : " + obj.getClass().getName());
                }
            }
            newCtx.setProperty(strkey, obj);
        }
    }
    // Make deep copy of fault stack so that parent will not be lost it's fault stack
    Stack<FaultHandler> faultStack = synCtx.getFaultStack();
    if (!faultStack.isEmpty()) {
        List<FaultHandler> newFaultStack = new ArrayList<FaultHandler>();
        newFaultStack.addAll(faultStack);
        for (FaultHandler faultHandler : newFaultStack) {
            if (faultHandler != null) {
                newCtx.pushFaultHandler(faultHandler);
            }
        }
    }
    Stack<TemplateContext> functionStack = (Stack) synCtx.getProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK);
    if (functionStack != null) {
        newCtx.setProperty(SynapseConstants.SYNAPSE__FUNCTION__STACK, functionStack.clone());
    }
    if (log.isDebugEnabled()) {
        log.info("Parent's Fault Stack : " + faultStack + " : Child's Fault Stack :" + newCtx.getFaultStack());
    }
    // Copy ContinuationStateStack from original MC to the new MC
    if (synCtx.isContinuationEnabled()) {
        Stack<ContinuationState> continuationStates = synCtx.getContinuationStateStack();
        newCtx.setContinuationEnabled(true);
        for (ContinuationState continuationState : continuationStates) {
            if (continuationState != null) {
                newCtx.pushContinuationState(ContinuationStackManager.getClonedSeqContinuationState((SeqContinuationState) continuationState));
            }
        }
    }
    newCtx.setMessageFlowTracingState(synCtx.getMessageFlowTracingState());
    return newCtx;
}
Also used : ArrayList(java.util.ArrayList) OMElement(org.apache.axiom.om.OMElement) TemplateContext(org.apache.synapse.mediators.template.TemplateContext) Stack(java.util.Stack) ContinuationState(org.apache.synapse.ContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) ResponseState(org.apache.synapse.core.axis2.ResponseState) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) FaultHandler(org.apache.synapse.FaultHandler) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Aggregations

ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 OMElement (org.apache.axiom.om.OMElement)1 ContinuationState (org.apache.synapse.ContinuationState)1 FaultHandler (org.apache.synapse.FaultHandler)1 MessageContext (org.apache.synapse.MessageContext)1 SeqContinuationState (org.apache.synapse.continuation.SeqContinuationState)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1 ResponseState (org.apache.synapse.core.axis2.ResponseState)1 TemplateContext (org.apache.synapse.mediators.template.TemplateContext)1