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;
}
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;
}
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());
}
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);
}
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");
}
}
Aggregations