Search in sources :

Example 96 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class AbstractListMediator method mediate.

public boolean mediate(MessageContext synCtx, int mediatorPosition) {
    boolean returnVal = true;
    int parentsEffectiveTraceState = synCtx.getTracingState();
    // if I have been explicitly asked to enable or disable tracing, set it to the message
    // to pass it on; else, do nothing -> i.e. let the parents state flow
    setEffectiveTraceState(synCtx);
    int myEffectiveTraceState = synCtx.getTracingState();
    try {
        SynapseLog synLog = getLog(synCtx);
        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Sequence <" + getType() + "> :: mediate()");
            synLog.traceOrDebug("Mediation started from mediator position : " + mediatorPosition);
        }
        for (int i = mediatorPosition; i < mediators.size(); i++) {
            // ensure correct trace state after each invocation of a mediator
            Mediator mediator = mediators.get(i);
            if (sequenceContentAware && (mediator.isContentAware() || isStreamXpathEnabled) && (!Boolean.TRUE.equals(synCtx.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED)))) {
                buildMessage(synCtx, synLog);
            }
            if (RuntimeStatisticCollector.isStatisticsEnabled()) {
                Integer statisticReportingIndex = mediator.reportOpenStatistics(synCtx, i == mediatorPosition);
                synCtx.setTracingState(myEffectiveTraceState);
                if (!mediator.mediate(synCtx)) {
                    mediator.reportCloseStatistics(synCtx, statisticReportingIndex);
                    returnVal = false;
                    break;
                }
                mediator.reportCloseStatistics(synCtx, statisticReportingIndex);
            } else {
                synCtx.setTracingState(myEffectiveTraceState);
                if (!mediator.mediate(synCtx)) {
                    returnVal = false;
                    break;
                }
            }
        }
    } catch (SynapseException synEx) {
        // Now create matcher object.
        Matcher msgBuildFailureExMatcher = msgBuildFailureExpattern.matcher(ExceptionUtils.getStackTrace(synEx));
        if (msgBuildFailureExMatcher.find()) {
            consumeInputOnOmException(synCtx);
        }
        throw synEx;
    } catch (Exception ex) {
        String errorMsg = ex.getMessage();
        // Now create matcher object.
        Matcher msgBuildFailureExMatcher = msgBuildFailureExpattern.matcher(ExceptionUtils.getStackTrace(ex));
        if (errorMsg == null) {
            errorMsg = "Runtime error occurred while mediating the message";
        }
        if (msgBuildFailureExMatcher.find()) {
            consumeInputOnOmException(synCtx);
        }
        handleException(errorMsg, ex, synCtx);
    } finally {
        synCtx.setTracingState(parentsEffectiveTraceState);
    }
    return returnVal;
}
Also used : SynapseLog(org.apache.synapse.SynapseLog) SynapseException(org.apache.synapse.SynapseException) Matcher(java.util.regex.Matcher) Mediator(org.apache.synapse.Mediator) SynapseException(org.apache.synapse.SynapseException)

Example 97 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class AbstractTestCase method serialization.

protected boolean serialization(String inputXml, MediatorSerializer mediatorSerializer) {
    OMElement inputOM = createOMElement(inputXml);
    Mediator mediator = MediatorFactoryFinder.getInstance().getMediator(inputOM, new Properties());
    OMElement resultOM = mediatorSerializer.serializeMediator(null, mediator);
    try {
        assertXMLEqual(resultOM.toString(), inputXml);
        return true;
    } catch (SAXException e) {
        log.error(e);
    } catch (IOException e) {
        log.error(e);
    }
    return false;
}
Also used : OMElement(org.apache.axiom.om.OMElement) Mediator(org.apache.synapse.Mediator) IOException(java.io.IOException) Properties(java.util.Properties) SAXException(org.xml.sax.SAXException)

Example 98 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class ForEachMediatorTest method testForEachXpathListRelativePath.

/**
 * Testing when the relative xpath returns a list of elements
 *
 * @throws Exception
 */
public void testForEachXpathListRelativePath() throws Exception {
    // Clear envelope
    if (testCtx.getEnvelope().getBody().getFirstElement() != null) {
        testCtx.getEnvelope().getBody().getFirstElement().detach();
    }
    testCtx.getEnvelope().getBody().addChild(createOMElement("<original>" + "<itr>test-split-context-itr1-body</itr>" + "<itr>test-split-context-itr2-body</itr>" + "</original>"));
    MediatorFactory fac = new ForEachMediatorFactory();
    Mediator foreach = fac.createMediator(createOMElement("<foreach " + "expression=\"//itr\" sequence=\"seqRef\" />"), new Properties());
    helperMediator.clearMediatedContexts();
    foreach.mediate(testCtx);
    assertEquals(2, helperMediator.getMsgCount());
    assertEquals("<itr>test-split-context-itr1-body</itr>", helperMediator.getMediatedContext(0).getEnvelope().getBody().getFirstElement().toString());
    assertEquals("<itr>test-split-context-itr2-body</itr>", helperMediator.getMediatedContext(1).getEnvelope().getBody().getFirstElement().toString());
}
Also used : ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) MediatorFactory(org.apache.synapse.config.xml.MediatorFactory) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Properties(java.util.Properties)

Example 99 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class ForEachMediatorTest method testSequenceValidity.

/**
 * Testing validity of reference sequence. Other cases : inline sequence
 * is covered in ForEachMediatorFactory
 *
 * @throws Exception
 */
public void testSequenceValidity() throws Exception {
    // Clear envelope
    if (testCtx.getEnvelope().getBody().getFirstElement() != null) {
        testCtx.getEnvelope().getBody().getFirstElement().detach();
    }
    testCtx.getEnvelope().getBody().addChild(createOMElement("<original>" + "<itr>test-split-context-itr1-body</itr>" + "<itr>test-split-context-itr2-body</itr>" + "</original>"));
    MediatorFactory fac = new ForEachMediatorFactory();
    Mediator foreachInvalid = fac.createMediator(createOMElement("<foreach expression=\"//original/itr\" sequence=\"seqRefInvalid\" />"), new Properties());
    String result = null;
    try {
        foreachInvalid.mediate(testCtx);
        result = testCtx.getEnvelope().getBody().getFirstElement().toString();
    } catch (SynapseException ex) {
        result = ex.getMessage();
    }
    assertTrue(result.contains("Referred sequence cannot contain Call"));
    // Clear envelope
    if (testCtx.getEnvelope().getBody().getFirstElement() != null) {
        testCtx.getEnvelope().getBody().getFirstElement().detach();
    }
    testCtx.getEnvelope().getBody().addChild(createOMElement("<original>" + "<itr>test-split-context-itr1-body</itr>" + "<itr>test-split-context-itr2-body</itr>" + "</original>"));
    Mediator foreachValid = fac.createMediator(createOMElement("<foreach " + "expression=\"//original/itr\" sequence=\"seqRef\" />"), new Properties());
    boolean successValid = foreachValid.mediate(testCtx);
    assertEquals(true, successValid);
}
Also used : SynapseException(org.apache.synapse.SynapseException) ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) ForEachMediatorFactory(org.apache.synapse.config.xml.ForEachMediatorFactory) MediatorFactory(org.apache.synapse.config.xml.MediatorFactory) Mediator(org.apache.synapse.Mediator) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) Properties(java.util.Properties)

Example 100 with Mediator

use of org.apache.synapse.Mediator in project wso2-synapse by wso2.

the class CloneMediatorTest method testClonningScenarioOne.

public void testClonningScenarioOne() throws Exception {
    Mediator clone = fac.createMediator(createOMElement("<clone " + "xmlns=\"http://ws.apache.org/ns/synapse\"><target soapAction=\"urn:clone\" " + "sequence=\"seqRef\"/><target to=\"http://test\"><sequence><sequence " + "key=\"seqRef\"/></sequence></target></clone>"), new Properties());
    clone.mediate(testCtx);
    while (helperMediator.getMediatedContext(1) == null) {
        Thread.sleep(100);
    }
    MessageContext mediatedCtx = helperMediator.getMediatedContext(0);
    String formerSAction = mediatedCtx.getSoapAction();
    mediatedCtx = helperMediator.getMediatedContext(1);
    if ("urn:clone".equals(formerSAction)) {
        assertEquals(mediatedCtx.getSoapAction(), "urn:test");
        assertEquals(mediatedCtx.getTo().getAddress(), "http://test");
    } else {
        assertEquals(mediatedCtx.getSoapAction(), "urn:clone");
    }
}
Also used : Mediator(org.apache.synapse.Mediator) PropertyMediator(org.apache.synapse.mediators.builtin.PropertyMediator) MessageContext(org.apache.synapse.MessageContext) Properties(java.util.Properties)

Aggregations

Mediator (org.apache.synapse.Mediator)108 Properties (java.util.Properties)30 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)24 OMElement (org.apache.axiom.om.OMElement)22 AbstractMediator (org.apache.synapse.mediators.AbstractMediator)22 MessageContext (org.apache.synapse.MessageContext)16 SynapseLog (org.apache.synapse.SynapseLog)16 FlowContinuableMediator (org.apache.synapse.mediators.FlowContinuableMediator)13 SynapseException (org.apache.synapse.SynapseException)12 TestMessageContext (org.apache.synapse.TestMessageContext)12 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)12 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)12 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)12 Test (org.junit.Test)10 AbstractListMediator (org.apache.synapse.mediators.AbstractListMediator)9 SynapseSequenceType (org.apache.synapse.debug.constructs.SynapseSequenceType)8 SequenceMediationFlowPoint (org.apache.synapse.debug.constructs.SequenceMediationFlowPoint)6 ForEachMediatorFactory (org.apache.synapse.config.xml.ForEachMediatorFactory)5 MediatorFactory (org.apache.synapse.config.xml.MediatorFactory)5 MediatorFaultHandler (org.apache.synapse.mediators.MediatorFaultHandler)5