Search in sources :

Example 1 with ExitState

use of nl.nn.adapterframework.core.PipeLine.ExitState 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 ExitState

use of nl.nn.adapterframework.core.PipeLine.ExitState in project iaf by ibissource.

the class TestPipeline method processZipFile.

private void processZipFile(Map<String, Object> returnResult, InputStream inputStream, String fileEncoding, IAdapter adapter, Map<String, String> sessionKeyMap, boolean writeSecLogMessage) throws IOException {
    StringBuilder result = new StringBuilder();
    ExitState lastState = null;
    try (ZipInputStream archive = new ZipInputStream(inputStream)) {
        for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) {
            String name = entry.getName();
            byte[] contentBytes = StreamUtil.streamToByteArray(StreamUtil.dontClose(archive), true);
            String message = XmlUtils.readXml(contentBytes, fileEncoding, false);
            if (result.length() > 0) {
                result.append("\n");
            }
            lastState = processMessage(adapter, message, sessionKeyMap, writeSecLogMessage).getState();
            result.append(name + ":" + lastState);
            archive.closeEntry();
        }
    }
    returnResult.put(PIPELINE_RESULT_STATE, lastState);
    returnResult.put(PIPELINE_RESULT, result);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ExitState(nl.nn.adapterframework.core.PipeLine.ExitState) ZipEntry(java.util.zip.ZipEntry)

Example 3 with ExitState

use of nl.nn.adapterframework.core.PipeLine.ExitState in project iaf by ibissource.

the class IbisLocalSender method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    String correlationID = session == null ? null : session.getMessageId();
    Message result = null;
    HashMap<String, Object> context = null;
    if (paramList != null) {
        try {
            context = (HashMap<String, Object>) paramList.getValues(message, session).getValueMap();
        } catch (ParameterException e) {
            throw new SenderException(getLogPrefix() + "exception evaluating parameters", e);
        }
    }
    if (context == null) {
        context = new HashMap<>();
    }
    String serviceIndication;
    if (StringUtils.isNotEmpty(getServiceName())) {
        serviceIndication = "service [" + getServiceName() + "]";
        try {
            if (isIsolated()) {
                if (isSynchronous()) {
                    log.debug(getLogPrefix() + "calling " + serviceIndication + " in separate Thread");
                    result = isolatedServiceCaller.callServiceIsolated(getServiceName(), correlationID, message, context, false);
                } else {
                    log.debug(getLogPrefix() + "calling " + serviceIndication + " in asynchronously");
                    isolatedServiceCaller.callServiceAsynchronous(getServiceName(), correlationID, message, context, false);
                    result = message;
                }
            } else {
                log.debug(getLogPrefix() + "calling " + serviceIndication + " in same Thread");
                result = new Message(ServiceDispatcher.getInstance().dispatchRequest(getServiceName(), correlationID, message.asString(), context));
            }
        } catch (ListenerException | IOException e) {
            if (ExceptionUtils.getRootCause(e) instanceof TimeoutException) {
                throw new TimeoutException(getLogPrefix() + "timeout calling " + serviceIndication + "", e);
            }
            throw new SenderException(getLogPrefix() + "exception calling " + serviceIndication + "", e);
        } finally {
            if (log.isDebugEnabled() && StringUtils.isNotEmpty(getReturnedSessionKeys())) {
                log.debug("returning values of session keys [" + getReturnedSessionKeys() + "]");
            }
            if (session != null) {
                Misc.copyContext(getReturnedSessionKeys(), context, session, this);
            }
        }
    } else {
        String javaListener;
        if (StringUtils.isNotEmpty(getJavaListenerSessionKey())) {
            try {
                javaListener = session.getMessage(getJavaListenerSessionKey()).asString();
            } catch (IOException e) {
                throw new SenderException("unable to resolve session key [" + getJavaListenerSessionKey() + "]", e);
            }
        } else {
            javaListener = getJavaListener();
        }
        serviceIndication = "JavaListener [" + javaListener + "]";
        try {
            JavaListener listener = JavaListener.getListener(javaListener);
            if (listener == null) {
                String msg = "could not find JavaListener [" + javaListener + "]";
                if (isThrowJavaListenerNotFoundException()) {
                    throw new SenderException(msg);
                }
                log.info(getLogPrefix() + msg);
                return new Message("<error>" + msg + "</error>");
            }
            if (isIsolated()) {
                if (isSynchronous()) {
                    log.debug(getLogPrefix() + "calling " + serviceIndication + " in separate Thread");
                    result = isolatedServiceCaller.callServiceIsolated(javaListener, correlationID, message, context, true);
                } else {
                    log.debug(getLogPrefix() + "calling " + serviceIndication + " in asynchronously");
                    isolatedServiceCaller.callServiceAsynchronous(javaListener, correlationID, message, context, true);
                    result = message;
                }
            } else {
                log.debug(getLogPrefix() + "calling " + serviceIndication + " in same Thread");
                result = new Message(listener.processRequest(correlationID, message.asString(), context));
            }
        } catch (ListenerException | IOException e) {
            if (ExceptionUtils.getRootCause(e) instanceof TimeoutException) {
                throw new TimeoutException(getLogPrefix() + "timeout calling " + serviceIndication, e);
            }
            throw new SenderException(getLogPrefix() + "exception calling " + serviceIndication, e);
        } finally {
            if (log.isDebugEnabled() && StringUtils.isNotEmpty(getReturnedSessionKeys())) {
                log.debug("returning values of session keys [" + getReturnedSessionKeys() + "]");
            }
            if (session != null) {
                Misc.copyContext(getReturnedSessionKeys(), context, session, this);
            }
        }
    }
    ExitState exitState = (ExitState) context.remove(PipeLineSession.EXIT_STATE_CONTEXT_KEY);
    Object exitCode = context.remove(PipeLineSession.EXIT_CODE_CONTEXT_KEY);
    if (exitState != null && exitState != ExitState.SUCCESS) {
        context.put("originalResult", result);
        throw new SenderException(getLogPrefix() + "call to " + serviceIndication + " resulted in exitState [" + exitState + "] exitCode [" + exitCode + "]");
    }
    return result;
}
Also used : Message(nl.nn.adapterframework.stream.Message) IOException(java.io.IOException) ListenerException(nl.nn.adapterframework.core.ListenerException) ExitState(nl.nn.adapterframework.core.PipeLine.ExitState) JavaListener(nl.nn.adapterframework.receivers.JavaListener) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

Aggregations

ExitState (nl.nn.adapterframework.core.PipeLine.ExitState)3 IOException (java.io.IOException)2 Message (nl.nn.adapterframework.stream.Message)2 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 TransformerException (javax.xml.transform.TransformerException)1 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)1 IbisManager (nl.nn.adapterframework.configuration.IbisManager)1 IAdapter (nl.nn.adapterframework.core.IAdapter)1 IForwardTarget (nl.nn.adapterframework.core.IForwardTarget)1 IPipe (nl.nn.adapterframework.core.IPipe)1 IPipeLineExitHandler (nl.nn.adapterframework.core.IPipeLineExitHandler)1 IValidator (nl.nn.adapterframework.core.IValidator)1 ListenerException (nl.nn.adapterframework.core.ListenerException)1 ParameterException (nl.nn.adapterframework.core.ParameterException)1 PipeForward (nl.nn.adapterframework.core.PipeForward)1 PipeLineExit (nl.nn.adapterframework.core.PipeLineExit)1 PipeLineResult (nl.nn.adapterframework.core.PipeLineResult)1 PipeRunException (nl.nn.adapterframework.core.PipeRunException)1 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)1