Search in sources :

Example 1 with IForwardTarget

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;
}
Also used : IbisManager(nl.nn.adapterframework.configuration.IbisManager) Message(nl.nn.adapterframework.stream.Message) TransformerPool(nl.nn.adapterframework.util.TransformerPool) SAXException(org.xml.sax.SAXException) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ExitState(nl.nn.adapterframework.core.PipeLine.ExitState) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) StatisticsKeeper(nl.nn.adapterframework.statistics.StatisticsKeeper) IAdapter(nl.nn.adapterframework.core.IAdapter) TransformerException(javax.xml.transform.TransformerException) IPipeLineExitHandler(nl.nn.adapterframework.core.IPipeLineExitHandler) IOException(java.io.IOException) PipeForward(nl.nn.adapterframework.core.PipeForward) AbstractPipe(nl.nn.adapterframework.pipes.AbstractPipe) IValidator(nl.nn.adapterframework.core.IValidator) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) IForwardTarget(nl.nn.adapterframework.core.IForwardTarget) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IPipe(nl.nn.adapterframework.core.IPipe) PipeLineExit(nl.nn.adapterframework.core.PipeLineExit)

Example 2 with IForwardTarget

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());
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) StringWriter(java.io.StringWriter) INamedObject(nl.nn.adapterframework.core.INamedObject) IForwardTarget(nl.nn.adapterframework.core.IForwardTarget) INamedObject(nl.nn.adapterframework.core.INamedObject) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 3 with IForwardTarget

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());
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) StringWriter(java.io.StringWriter) INamedObject(nl.nn.adapterframework.core.INamedObject) IForwardTarget(nl.nn.adapterframework.core.IForwardTarget) OutputStream(java.io.OutputStream) INamedObject(nl.nn.adapterframework.core.INamedObject) Test(org.junit.Test)

Example 4 with IForwardTarget

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);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message) IForwardTarget(nl.nn.adapterframework.core.IForwardTarget) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeForward(nl.nn.adapterframework.core.PipeForward) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) StreamingException(nl.nn.adapterframework.stream.StreamingException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 5 with IForwardTarget

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());
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) INamedObject(nl.nn.adapterframework.core.INamedObject) IForwardTarget(nl.nn.adapterframework.core.IForwardTarget) INamedObject(nl.nn.adapterframework.core.INamedObject) ContentHandler(org.xml.sax.ContentHandler) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Aggregations

IForwardTarget (nl.nn.adapterframework.core.IForwardTarget)6 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)6 StringWriter (java.io.StringWriter)4 INamedObject (nl.nn.adapterframework.core.INamedObject)4 Test (org.junit.Test)4 Writer (java.io.Writer)3 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 PipeForward (nl.nn.adapterframework.core.PipeForward)2 PipeRunException (nl.nn.adapterframework.core.PipeRunException)2 Message (nl.nn.adapterframework.stream.Message)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 TransformerException (javax.xml.transform.TransformerException)1 IbisManager (nl.nn.adapterframework.configuration.IbisManager)1 IAdapter (nl.nn.adapterframework.core.IAdapter)1 IPipe (nl.nn.adapterframework.core.IPipe)1 IPipeLineExitHandler (nl.nn.adapterframework.core.IPipeLineExitHandler)1 IValidator (nl.nn.adapterframework.core.IValidator)1 ExitState (nl.nn.adapterframework.core.PipeLine.ExitState)1 PipeLineExit (nl.nn.adapterframework.core.PipeLineExit)1