Search in sources :

Example 1 with MediatorFaultHandler

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

the class SequenceMediator method mediate.

public boolean mediate(MessageContext synCtx, ContinuationState continuationState) {
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Mediating using the SeqContinuationState type : " + ((SeqContinuationState) continuationState).getSeqType() + " name : " + ((SeqContinuationState) continuationState).getSeqName());
    }
    Mediator errorHandlerMediator = null;
    // push the errorHandler sequence into the current message as the fault handler
    if (errorHandler != null) {
        errorHandlerMediator = synCtx.getSequence(errorHandler);
        if (errorHandlerMediator != null) {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Setting the onError handler : " + errorHandler + " for the sequence : " + name);
            }
            synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
        } else {
            synLog.auditWarn("onError handler : " + errorHandler + " for sequence : " + name + " cannot be found");
        }
    }
    boolean result;
    if (!continuationState.hasChild()) {
        result = super.mediate(synCtx, continuationState.getPosition() + 1);
    } else {
        // if children exists first mediate from them starting from grandchild.
        do {
            FlowContinuableMediator mediator = (FlowContinuableMediator) getChild(continuationState.getPosition());
            result = mediator.mediate(synCtx, continuationState.getChildContState());
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                ((Mediator) mediator).reportCloseStatistics(synCtx, null);
            }
            if (result) {
                // if flow completed remove leaf child
                continuationState.removeLeafChild();
            }
        } while (result && continuationState.hasChild());
        if (result) {
            // after mediating from children, mediate from current SeqContinuationState
            result = super.mediate(synCtx, continuationState.getPosition() + 1);
        }
    }
    if (result) {
        // if flow completed, remove top ContinuationState from stack
        ContinuationStackManager.popContinuationStateStack(synCtx);
    }
    // before we exit normally without an exception
    if (errorHandlerMediator != null) {
        Stack faultStack = synCtx.getFaultStack();
        if (faultStack != null && !faultStack.isEmpty()) {
            Object o = faultStack.peek();
            if (o instanceof MediatorFaultHandler && errorHandlerMediator.equals(((MediatorFaultHandler) o).getFaultMediator())) {
                faultStack.pop();
            }
        }
    }
    return result;
}
Also used : SeqContinuationState(org.apache.synapse.continuation.SeqContinuationState) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Example 2 with MediatorFaultHandler

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

the class SequenceMediator method mediate.

/**
 * If this mediator refers to another named Sequence, execute that. Else
 * execute the list of mediators (children) contained within this. If a referenced
 * named sequence mediator instance cannot be found at runtime, an exception is
 * thrown. This may occur due to invalid configuration of an erroneous runtime
 * change of the synapse configuration. It is the responsibility of the
 * SynapseConfiguration builder to ensure that dead references are not present.
 *
 * @param synCtx the synapse message
 * @return as per standard mediator result
 */
public boolean mediate(MessageContext synCtx) {
    if (synCtx.getEnvironment().isDebuggerEnabled()) {
        if (super.divertMediationRoute(synCtx)) {
            return true;
        }
    }
    SynapseLog synLog = getLog(synCtx);
    if (sequenceType == SequenceType.NAMED) {
        CustomLogSetter.getInstance().setLogAppender(artifactContainerName);
    }
    if (synLog.isTraceOrDebugEnabled()) {
        synLog.traceOrDebug("Start : Sequence " + (name == null ? (key == null ? "<anonymous" : "key=<" + key) : "<" + name) + ">");
        if (synLog.isTraceTraceEnabled()) {
            synLog.traceTrace("Message : " + synCtx.getEnvelope());
        }
    }
    if (key == null) {
        // The onError sequence for handling errors which may occur during the
        // mediation through this sequence
        Mediator errorHandlerMediator = null;
        Integer statisticReportingIndex = null;
        if (RuntimeStatisticCollector.isStatisticsEnabled()) {
            statisticReportingIndex = reportOpenStatistics(synCtx, false);
        }
        try {
            // push the errorHandler sequence into the current message as the fault handler
            if (errorHandler != null) {
                errorHandlerMediator = synCtx.getSequence(errorHandler);
                if (errorHandlerMediator != null) {
                    if (synLog.isTraceOrDebugEnabled()) {
                        synLog.traceOrDebug("Setting the onError handler : " + errorHandler + " for the sequence : " + name);
                    }
                    synCtx.pushFaultHandler(new MediatorFaultHandler(errorHandlerMediator));
                } else {
                    synLog.auditWarn("onError handler : " + errorHandler + " for sequence : " + name + " cannot be found");
                }
            }
            // Add a new SeqContinuationState as we branched to new Sequence.
            boolean skipAddition = ContinuationStackManager.isSkipSeqContinuationStateAddition(synCtx);
            if (!skipAddition) {
                if (dynamic && registryKey != null) {
                    ContinuationStackManager.addSeqContinuationState(synCtx, registryKey, sequenceType);
                } else {
                    ContinuationStackManager.addSeqContinuationState(synCtx, name, sequenceType);
                }
            }
            boolean result = super.mediate(synCtx);
            if (result && !skipAddition) {
                // if flow completed remove the previously added SeqContinuationState
                ContinuationStackManager.removeSeqContinuationState(synCtx, sequenceType);
            }
            // before we exit normally without an exception
            if (errorHandlerMediator != null) {
                Stack faultStack = synCtx.getFaultStack();
                if (faultStack != null && !faultStack.isEmpty()) {
                    Object o = faultStack.peek();
                    if (o instanceof MediatorFaultHandler && errorHandlerMediator.equals(((MediatorFaultHandler) o).getFaultMediator())) {
                        faultStack.pop();
                    }
                }
            }
            if (synLog.isTraceOrDebugEnabled()) {
                if (synLog.isTraceTraceEnabled()) {
                    synLog.traceTrace("Message : " + synCtx.getEnvelope());
                }
                synLog.traceOrDebug("End : Sequence <" + (name == null ? "anonymous" : name) + ">");
            }
            return result;
        } finally {
            // End Statistics
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                reportCloseStatistics(synCtx, statisticReportingIndex);
            }
        }
    } else {
        String sequenceKey = key.evaluateValue(synCtx);
        // Mediator m = synCtx.getSequence(key);
        Mediator m = synCtx.getSequence(sequenceKey);
        if (m == null) {
            handleException("Sequence named " + key + " cannot be found", synCtx);
        } else {
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("Executing with key " + key);
            }
            // Update the SeqContinuationState position with the mediator position of
            // sequence mediator in the sequence
            ContinuationStackManager.updateSeqContinuationState(synCtx, getMediatorPosition());
            boolean result = m.mediate(synCtx);
            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("End : Sequence key=<" + key + ">");
            }
            return result;
        }
    }
    return false;
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) SynapseLog(org.apache.synapse.SynapseLog) FlowContinuableMediator(org.apache.synapse.mediators.FlowContinuableMediator) Mediator(org.apache.synapse.Mediator) AbstractListMediator(org.apache.synapse.mediators.AbstractListMediator) Stack(java.util.Stack)

Example 3 with MediatorFaultHandler

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

the class TemplateMessageExecutor method execute.

public void execute() {
    // Inject the message into SynapseEnvironment
    MessageContext mc = synapseEnvironment.createMessageContext();
    mc.pushFaultHandler(new MediatorFaultHandler(mc.getFaultSequence()));
    synapseEnvironment.injectAsync(mc, seqMed);
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) MessageContext(org.apache.synapse.MessageContext)

Example 4 with MediatorFaultHandler

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

the class ProxyServiceTest method testRegisterFaultHandler.

/**
 * Tests if the correct fault handler is set when a fault handler is not explicitly set, and when set by using
 * targetFaultSequence and targetInlineFaultSequence properties of the proxy.
 */
public void testRegisterFaultHandler() {
    ProxyService proxyService = new ProxyService("TestRegisterFaultHandlerProxy");
    MessageContext messageContext = new TestMessageContext();
    SynapseConfiguration synCfg = new SynapseConfiguration();
    messageContext.setConfiguration(synCfg);
    SequenceMediator faultMediator = new SequenceMediator();
    synCfg.addSequence("fault", faultMediator);
    // test if the default fault proxy set in the message context is returned when a fault sequence is not
    // explicitly set
    faultMediator.setName("defaultFault");
    proxyService.registerFaultHandler(messageContext);
    String faultMediatorName = ((SequenceMediator) ((MediatorFaultHandler) messageContext.getFaultStack().pop()).getFaultMediator()).getName();
    Assert.assertEquals("Incorrect fault handler set: " + faultMediatorName, "defaultFault", faultMediatorName);
    // tests the functionality when the fault sequence is set in line in the target
    faultMediator.setName("targetInLineFaultSequenceMediator");
    proxyService.setTargetInLineFaultSequence(faultMediator);
    proxyService.registerFaultHandler(messageContext);
    faultMediatorName = ((SequenceMediator) ((MediatorFaultHandler) messageContext.getFaultStack().pop()).getFaultMediator()).getName();
    Assert.assertEquals("Incorrect fault handler set: " + faultMediatorName, "targetInLineFaultSequenceMediator", faultMediatorName);
    // tests the functionality when the fault sequence is set separately in the target
    AxisConfiguration axisCfg = new AxisConfiguration();
    SynapseEnvironment synEnv = new Axis2SynapseEnvironment(new ConfigurationContext(axisCfg), synCfg);
    messageContext.setEnvironment(synEnv);
    proxyService.setTargetFaultSequence("targetFaultSequence");
    // when the message context does not have the correct fault sequence specified as the target fault sequence
    faultMediator.setName("defaultFaultMediator");
    proxyService.registerFaultHandler(messageContext);
    faultMediatorName = ((SequenceMediator) ((MediatorFaultHandler) messageContext.getFaultStack().pop()).getFaultMediator()).getName();
    Assert.assertEquals("Incorrect fault handler set: " + faultMediatorName, "defaultFaultMediator", faultMediatorName);
    // when the message context has the correct fault sequence specified as the target fault sequence
    faultMediator.setName("targetFaultSequenceMediator");
    synCfg.addSequence("targetFaultSequence", faultMediator);
    proxyService.registerFaultHandler(messageContext);
    faultMediatorName = ((SequenceMediator) ((MediatorFaultHandler) messageContext.getFaultStack().pop()).getFaultMediator()).getName();
    Assert.assertEquals("Incorrect fault handler set: " + faultMediatorName, "targetFaultSequenceMediator", faultMediatorName);
}
Also used : AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) TestMessageContext(org.apache.synapse.TestMessageContext) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) MessageContext(org.apache.synapse.MessageContext) TestMessageContext(org.apache.synapse.TestMessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration)

Example 5 with MediatorFaultHandler

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

the class Axis2SynapseEnvironment method injectMessage.

public boolean injectMessage(final MessageContext synCtx) {
    try {
        if (log.isDebugEnabled()) {
            log.debug("Injecting MessageContext");
        }
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.acquireMediationFlowLock();
            debugManager.advertiseMediationFlowStartPoint(synCtx);
        }
        // setting transport-in name as a message context property
        TransportInDescription trpInDesc = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getTransportIn();
        if (trpInDesc != null) {
            synCtx.setProperty(SynapseConstants.TRANSPORT_IN_NAME, trpInDesc.getName());
        }
        synCtx.setEnvironment(this);
        if (!invokeHandlers(synCtx)) {
            return false;
        }
        Mediator mandatorySeq = synCtx.getConfiguration().getMandatorySequence();
        // the mandatory sequence is optional and hence check for the existence before mediation
        if (mandatorySeq != null) {
            if (log.isDebugEnabled()) {
                log.debug("Start mediating the message in the " + "pre-mediate state using the mandatory sequence");
            }
            if (!mandatorySeq.mediate(synCtx)) {
                if (log.isDebugEnabled()) {
                    log.debug((synCtx.isResponse() ? "Response" : "Request") + " message for the " + (synCtx.getProperty(SynapseConstants.PROXY_SERVICE) != null ? "proxy service " + synCtx.getProperty(SynapseConstants.PROXY_SERVICE) : "message mediation") + " dropped in the " + "pre-mediation state by the mandatory sequence : \n" + synCtx);
                }
                return false;
            }
        }
        String receivingSequence = (String) synCtx.getProperty(SynapseConstants.RECEIVING_SEQUENCE);
        Boolean isContinuationCall = (Boolean) synCtx.getProperty(SynapseConstants.CONTINUATION_CALL);
        // clear the message context properties related to endpoint in last service invocation
        Set keySet = synCtx.getPropertyKeySet();
        if (keySet != null) {
            keySet.remove(SynapseConstants.RECEIVING_SEQUENCE);
            keySet.remove(SynapseConstants.CONTINUATION_CALL);
        }
        if (isContinuationCall != null && isContinuationCall && !synCtx.getContinuationStateStack().isEmpty()) {
            if (log.isDebugEnabled()) {
                log.debug("Response received for the Continuation Call service invocation");
            }
            return mediateFromContinuationStateStack(synCtx);
        }
        // if this is not a response to a proxy service
        String proxyName = (String) synCtx.getProperty(SynapseConstants.PROXY_SERVICE);
        if (proxyName == null || "".equals(proxyName)) {
            // set default fault handler
            synCtx.pushFaultHandler(new MediatorFaultHandler(synCtx.getFaultSequence()));
            if (receivingSequence != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Using Sequence with name: " + receivingSequence + " for injected message");
                }
                Mediator seqMediator = synCtx.getSequence(receivingSequence);
                if (seqMediator != null) {
                    return seqMediator.mediate(synCtx);
                } else {
                    log.warn("Cannot find a Sequence with name: " + receivingSequence + " for injecting the response message");
                    return false;
                }
            } else {
                boolean processed = restHandler.process(synCtx);
                if (processed) {
                    return true;
                }
                if (log.isDebugEnabled()) {
                    log.debug("Using Main Sequence for injected message");
                }
                return synCtx.getMainSequence().mediate(synCtx);
            }
        }
        ProxyService proxyService = synCtx.getConfiguration().getProxyService(proxyName);
        if (proxyService != null) {
            proxyService.registerFaultHandler(synCtx);
            Mediator outSequence = getProxyOutSequence(synCtx, proxyService);
            if (receivingSequence != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Using Sequence with name: " + receivingSequence + " for injected message");
                }
                Mediator seqMediator = synCtx.getSequence(receivingSequence);
                if (seqMediator != null) {
                    seqMediator.mediate(synCtx);
                } else {
                    log.warn("Cannot find a Sequence with name: " + receivingSequence + " for injecting the message");
                    return false;
                }
            } else if (outSequence != null) {
                outSequence.mediate(synCtx);
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(proxyService + " does not specifies an out-sequence - sending the response back");
                }
                Axis2Sender.sendBack(synCtx);
            }
        }
        return true;
    } finally {
        if (synCtx.getEnvironment().isDebuggerEnabled()) {
            SynapseDebugManager debugManager = synCtx.getEnvironment().getSynapseDebugManager();
            debugManager.advertiseMediationFlowTerminatePoint(synCtx);
            debugManager.releaseMediationFlowLock();
        }
    }
}
Also used : MediatorFaultHandler(org.apache.synapse.mediators.MediatorFaultHandler) Set(java.util.Set) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) TransportInDescription(org.apache.axis2.description.TransportInDescription) SynapseDebugManager(org.apache.synapse.debug.SynapseDebugManager)

Aggregations

MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)10 Mediator (org.apache.synapse.Mediator)5 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)4 MessageContext (org.apache.synapse.MessageContext)3 Stack (java.util.Stack)2 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)2 SynapseLog (org.apache.synapse.SynapseLog)2 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)2 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 OMElement (org.apache.axiom.om.OMElement)1 OMText (org.apache.axiom.om.OMText)1 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)1 AxisFault (org.apache.axis2.AxisFault)1 EndpointReference (org.apache.axis2.addressing.EndpointReference)1 AxisService (org.apache.axis2.description.AxisService)1 TransportInDescription (org.apache.axis2.description.TransportInDescription)1