use of nl.nn.adapterframework.core.IForwardTarget in project iaf by ibissource.
the class CorePipeLineProcessor method processPipeLine.
@Override
public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, Message message, PipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
if (message.isEmpty()) {
if (StringUtils.isNotEmpty(pipeLine.getAdapterToRunBeforeOnEmptyInput())) {
log.debug("running adapterBeforeOnEmptyInput");
IbisManager ibisManager = applicationContext.getBean(IbisManager.class);
IAdapter adapter = ibisManager.getRegisteredAdapter(pipeLine.getAdapterToRunBeforeOnEmptyInput());
if (adapter == null) {
log.warn("adapterToRunBefore with specified name [" + pipeLine.getAdapterToRunBeforeOnEmptyInput() + "] could not be retrieved");
} else {
PipeLineResult plr = adapter.processMessage(messageId, message, pipeLineSession);
if (plr == null || !plr.isSuccessful()) {
throw new PipeRunException(null, "adapterToRunBefore [" + pipeLine.getAdapterToRunBeforeOnEmptyInput() + "] ended with state [" + (plr == null ? "null" : plr.getState()) + "]");
}
message = plr.getResult();
log.debug("input after running adapterBeforeOnEmptyInput [" + message + "]");
}
}
}
// ready indicates whether the pipeline processing is complete
boolean ready = false;
// get the first pipe to run
IForwardTarget forwardTarget = pipeLine.getPipe(pipeLine.getFirstPipe());
boolean inputValidateError = false;
IValidator inputValidator = pipeLine.getInputValidator();
if (inputValidator != null) {
log.debug("validating input");
PipeRunResult validationResult = pipeProcessor.processPipe(pipeLine, inputValidator, message, pipeLineSession);
if (validationResult != null) {
if (!validationResult.isSuccessful()) {
forwardTarget = pipeLine.resolveForward(inputValidator, validationResult.getPipeForward());
log.warn("forwarding execution flow to [" + forwardTarget.getName() + "] due to validation fault");
inputValidateError = true;
}
Message validatedMessage = validationResult.getResult();
if (!validatedMessage.isEmpty()) {
message = validatedMessage;
}
}
}
if (!inputValidateError) {
IPipe inputWrapper = pipeLine.getInputWrapper();
if (inputWrapper != null) {
log.debug("wrapping input");
PipeRunResult wrapResult = pipeProcessor.processPipe(pipeLine, inputWrapper, message, pipeLineSession);
if (wrapResult != null && !wrapResult.isSuccessful()) {
forwardTarget = pipeLine.resolveForward(inputWrapper, wrapResult.getPipeForward());
log.warn("forwarding execution flow to [" + forwardTarget.getName() + "] due to wrap fault");
} else {
message = wrapResult.getResult();
}
log.debug("input after wrapping [" + message + "]");
}
}
long size = message.size();
if (size > 0) {
pipeLine.getRequestSizeStats().addValue(size);
}
if (pipeLine.isStoreOriginalMessageWithoutNamespaces()) {
String input;
try {
input = message.asString();
} catch (IOException e) {
throw new PipeRunException(null, "cannot open stream", e);
}
if (XmlUtils.isWellFormed(input)) {
IPipe pipe = forwardTarget instanceof IPipe ? (IPipe) forwardTarget : null;
try {
TransformerPool tpRemoveNamespaces = XmlUtils.getRemoveNamespacesTransformerPool(true, true);
String xsltResult = tpRemoveNamespaces.transform(message, null);
pipeLineSession.put("originalMessageWithoutNamespaces", xsltResult);
} catch (IOException e) {
throw new PipeRunException(pipe, "cannot retrieve removeNamespaces", e);
} catch (ConfigurationException ce) {
throw new PipeRunException(pipe, "got error creating transformer for removeNamespaces", ce);
} catch (TransformerException te) {
throw new PipeRunException(pipe, "got error transforming removeNamespaces", te);
} catch (SAXException se) {
throw new PipeRunException(pipe, "caught SAXException", se);
}
} else {
log.warn("original message is not well-formed");
pipeLineSession.put("originalMessageWithoutNamespaces", message);
}
}
PipeLineResult pipeLineResult = new PipeLineResult();
boolean outputValidationFailed = false;
try {
while (!ready) {
if (forwardTarget instanceof PipeLineExit) {
PipeLineExit plExit = (PipeLineExit) forwardTarget;
if (!plExit.isEmptyResult()) {
boolean outputWrapError = false;
IPipe outputWrapper = pipeLine.getOutputWrapper();
if (outputWrapper != null) {
log.debug("wrapping PipeLineResult");
PipeRunResult wrapResult = pipeProcessor.processPipe(pipeLine, outputWrapper, message, pipeLineSession);
if (wrapResult != null && !wrapResult.isSuccessful()) {
forwardTarget = pipeLine.resolveForward(outputWrapper, wrapResult.getPipeForward());
log.warn("forwarding execution flow to [" + forwardTarget.getName() + "] due to wrap fault");
outputWrapError = true;
} else {
log.debug("wrap succeeded");
message = wrapResult.getResult();
}
if (log.isDebugEnabled())
log.debug("PipeLineResult after wrapping: " + (message == null ? "<null>" : "(" + message.getClass().getSimpleName() + ") [" + message + "]"));
}
if (!outputWrapError) {
IValidator outputValidator = pipeLine.getOutputValidator();
if (outputValidator != null) {
if (outputValidationFailed) {
log.debug("validating error message after PipeLineResult validation failed");
} else {
log.debug("validating PipeLineResult");
}
String exitSpecificResponseRoot = plExit.getResponseRoot();
PipeRunResult validationResult = pipeProcessor.validate(pipeLine, outputValidator, message, pipeLineSession, exitSpecificResponseRoot);
if (!validationResult.isSuccessful()) {
if (!outputValidationFailed) {
outputValidationFailed = true;
forwardTarget = pipeLine.resolveForward(outputValidator, validationResult.getPipeForward());
log.warn("forwarding execution flow to [" + forwardTarget.getName() + "] due to validation fault");
} else {
// to avoid endless looping
log.warn("validation of error message by validator [" + outputValidator.getName() + "] failed, returning result anyhow");
message = validationResult.getResult();
ready = true;
}
} else {
log.debug("validation succeeded");
message = validationResult.getResult();
ready = true;
}
} else {
ready = true;
}
} else {
ready = true;
}
} else {
ready = true;
}
if (ready) {
ExitState state = plExit.getState();
pipeLineResult.setState(state);
pipeLineResult.setExitCode(plExit.getExitCode());
if (message.asObject() != null && !plExit.isEmptyResult()) {
// TODO Replace with Message.isEmpty() once Larva can handle NULL responses...
pipeLineResult.setResult(message);
} else {
pipeLineResult.setResult(Message.nullMessage());
}
ready = true;
if (log.isDebugEnabled()) {
// for performance reasons
String skString = "";
for (Iterator<String> it = pipeLineSession.keySet().iterator(); it.hasNext(); ) {
String key = it.next();
Object value = pipeLineSession.get(key);
skString = skString + "\n " + key + "=[" + value + "]";
}
log.debug("Available session keys at finishing pipeline of adapter [" + pipeLine.getOwner().getName() + "]:" + skString);
log.debug("Pipeline of adapter [" + pipeLine.getOwner().getName() + "] finished processing messageId [" + messageId + "] result: " + (message == null ? "<null>" : "(" + message.getClass().getSimpleName() + ") [" + message + "]") + " with exit-state [" + state + "]");
}
}
} else {
IPipe pipeToRun = (IPipe) forwardTarget;
PipeRunResult pipeRunResult = pipeProcessor.processPipe(pipeLine, pipeToRun, message, pipeLineSession);
message = pipeRunResult.getResult();
// TODO: this should be moved to a StatisticsPipeProcessor
if (!(pipeToRun instanceof AbstractPipe) && !message.isEmpty()) {
StatisticsKeeper sizeStat = pipeLine.getPipeSizeStatistics(pipeToRun);
if (sizeStat != null) {
sizeStat.addValue(message.size());
}
}
PipeForward pipeForward = pipeRunResult.getPipeForward();
// get the next pipe to run
forwardTarget = pipeLine.resolveForward(pipeToRun, pipeForward);
}
}
} finally {
for (int i = 0; i < pipeLine.getExitHandlers().size(); i++) {
IPipeLineExitHandler exitHandler = pipeLine.getExitHandlers().get(i);
try {
if (log.isDebugEnabled())
log.debug("processing ExitHandler [" + exitHandler.getName() + "]");
exitHandler.atEndOfPipeLine(messageId, pipeLineResult, pipeLineSession);
} catch (Throwable t) {
log.warn("Caught Exception processing ExitHandler [" + exitHandler.getName() + "]", t);
}
}
}
return pipeLineResult;
}
use of nl.nn.adapterframework.core.IForwardTarget in project iaf by ibissource.
the class MessageStreamCapTest method testNativeCap.
@Test
public /*
* if used as 'native', then the underlying buffer must be a Writer, probably a StringWriter
*/
void testNativeCap() throws Exception {
INamedObject owner = new Owner();
IForwardTarget forward = null;
String responseMessage = "fakeResponseMessage";
StringWriter captureWriter = null;
try (MessageOutputStreamCap cap = new MessageOutputStreamCap(owner, forward)) {
captureWriter = cap.captureCharacterStream();
Object capNative = cap.asNative();
assertTrue(capNative instanceof Writer);
try (Writer capWriter = (Writer) capNative) {
capWriter.write(responseMessage);
}
PipeRunResult result = cap.getPipeRunResult();
assertEquals(responseMessage, result.getResult().asString());
}
assertEquals(responseMessage, captureWriter.toString());
}
use of nl.nn.adapterframework.core.IForwardTarget in project iaf by ibissource.
the class MessageStreamCapTest method testBytesCap.
@Test
public void testBytesCap() throws Exception {
INamedObject owner = new Owner();
IForwardTarget forward = null;
byte[] responseMessage = "fakeResponseMessage".getBytes();
StringWriter captureWriter = null;
try (MessageOutputStreamCap cap = new MessageOutputStreamCap(owner, forward)) {
captureWriter = cap.captureCharacterStream();
try (OutputStream capStream = cap.asStream()) {
Object capNative = cap.asNative();
assertEquals(capStream.getClass(), capNative.getClass());
assertEquals(capStream, capNative);
capStream.write(responseMessage);
}
PipeRunResult result = cap.getPipeRunResult();
assertEquals(responseMessage.getClass(), result.getResult().asObject().getClass());
assertEquals(new String(responseMessage), new String((byte[]) result.getResult().asObject()));
}
assertEquals(new String(responseMessage), captureWriter.toString());
}
use of nl.nn.adapterframework.core.IForwardTarget in project iaf by ibissource.
the class XsltPipe method doPipe.
@Override
public PipeRunResult doPipe(Message input, PipeLineSession session) throws PipeRunException {
if (Message.isEmpty(input)) {
throw new PipeRunException(this, getLogPrefix(session) + "got null input");
}
try {
IForwardTarget nextPipe;
if (canStreamToNextPipe()) {
nextPipe = getNextPipe();
} else {
nextPipe = null;
if (StringUtils.isNotEmpty(getSessionKey())) {
input.preserve();
}
}
PipeRunResult prr = sender.sendMessage(input, session, nextPipe);
Message result = prr.getResult();
PipeForward forward = prr.getPipeForward();
if (nextPipe == null || forward.getPath() == null) {
forward = getSuccessForward();
}
if (StringUtils.isNotEmpty(getSessionKey())) {
session.put(getSessionKey(), result.asString());
return new PipeRunResult(forward, input);
}
return new PipeRunResult(forward, result);
} catch (Exception e) {
throw new PipeRunException(this, getLogPrefix(session) + " Exception on transforming input", e);
}
}
use of nl.nn.adapterframework.core.IForwardTarget in project iaf by ibissource.
the class MessageStreamCapTest method testContentHandlerCap.
@Test
public /*
* if used as a ContentHandler, then the underlying buffer must be a Writer, probably a StringWriter
*/
void testContentHandlerCap() throws Exception {
INamedObject owner = new Owner();
IForwardTarget forward = null;
String expectedResponseMessage = "<root/>";
StringWriter captureWriter = null;
try (MessageOutputStreamCap cap = new MessageOutputStreamCap(owner, forward)) {
captureWriter = cap.captureCharacterStream();
ContentHandler capContentHandler = cap.asContentHandler();
Object capNative = cap.asNative();
assertTrue(capNative instanceof Writer);
capContentHandler.startDocument();
capContentHandler.startElement("", "root", "root", new AttributesImpl());
capContentHandler.endElement("", "root", "root");
capContentHandler.endDocument();
PipeRunResult result = cap.getPipeRunResult();
assertEquals(expectedResponseMessage, result.getResult().asString());
}
assertEquals(expectedResponseMessage, captureWriter.toString());
}
Aggregations