Search in sources :

Example 61 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class Receiver method moveInProcessToErrorAndDoPostProcessing.

private void moveInProcessToErrorAndDoPostProcessing(IListener<M> origin, String messageId, String correlationId, M rawMessage, ThrowingSupplier<Message, ListenerException> messageSupplier, Map<String, Object> threadContext, ProcessResultCacheItem prci, String comments) throws ListenerException {
    Date rcvDate;
    if (prci != null) {
        comments += "; " + prci.comments;
        rcvDate = prci.receiveDate;
    } else {
        rcvDate = new Date();
    }
    if (isTransacted() || (getErrorStorage() != null && (!isCheckForDuplicates() || !getErrorStorage().containsMessageId(messageId) || !isDuplicateAndSkip(getMessageBrowser(ProcessState.ERROR), messageId, correlationId)))) {
        moveInProcessToError(messageId, correlationId, messageSupplier, rcvDate, comments, rawMessage, TXREQUIRED);
    }
    PipeLineResult plr = new PipeLineResult();
    Message result = new Message("<error>" + XmlUtils.encodeChars(comments) + "</error>");
    plr.setResult(result);
    plr.setState(ExitState.ERROR);
    if (getSender() != null) {
        String sendMsg = sendResultToSender(result);
        if (sendMsg != null) {
            log.warn("problem sending result:" + sendMsg);
        }
    }
    origin.afterMessageProcessed(plr, rawMessage, threadContext);
}
Also used : Message(nl.nn.adapterframework.stream.Message) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) Date(java.util.Date)

Example 62 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CachePipeLineProcessor method processPipeLine.

@Override
public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, Message message, PipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    ICache<String, String> cache = pipeLine.getCache();
    if (cache == null) {
        return pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    }
    String input;
    try {
        input = message.asString();
    } catch (IOException e) {
        throw new PipeRunException(pipeLine.getPipe(firstPipe), "cannot open stream", e);
    }
    String key = cache.transformKey(input, pipeLineSession);
    if (key == null) {
        if (log.isDebugEnabled())
            log.debug("cache key is null, will not use cache");
        return pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    }
    if (log.isDebugEnabled())
        log.debug("cache key [" + key + "]");
    Message result;
    String state;
    synchronized (cache) {
        result = new Message(cache.get("r" + key));
        state = cache.get("s" + key);
    }
    if (result != null && state != null) {
        if (log.isDebugEnabled())
            log.debug("retrieved result from cache using key [" + key + "]");
        PipeLineResult plr = new PipeLineResult();
        plr.setState(EnumUtils.parse(ExitState.class, state));
        plr.setResult(result);
        return plr;
    }
    if (log.isDebugEnabled())
        log.debug("no cached results found using key [" + key + "]");
    PipeLineResult plr = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    if (log.isDebugEnabled())
        log.debug("caching result using key [" + key + "]");
    String cacheValue = cache.transformValue(plr.getResult(), pipeLineSession);
    synchronized (cache) {
        cache.put("r" + key, cacheValue);
        cache.put("s" + key, plr.getState().name());
    }
    return plr;
}
Also used : Message(nl.nn.adapterframework.stream.Message) ExitState(nl.nn.adapterframework.core.PipeLine.ExitState) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException)

Example 63 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CheckMessageSizePipeProcessor method processPipe.

@Override
protected PipeRunResult processPipe(PipeLine pipeLine, IPipe pipe, Message message, PipeLineSession pipeLineSession, ThrowingFunction<Message, PipeRunResult, PipeRunException> chain) throws PipeRunException {
    checkMessageSize(message.size(), pipeLine, pipe, true);
    PipeRunResult pipeRunResult = chain.apply(message);
    Message result = pipeRunResult.getResult();
    checkMessageSize(result.size(), pipeLine, pipe, false);
    return pipeRunResult;
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) Message(nl.nn.adapterframework.stream.Message)

Example 64 with Message

use of nl.nn.adapterframework.stream.Message 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 65 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class XmlValidator method getMessageToValidate.

@Deprecated
private Message getMessageToValidate(Message message, PipeLineSession session) throws PipeRunException {
    String input;
    try {
        input = message.asString();
    } catch (IOException e) {
        throw new PipeRunException(this, getLogPrefix(session) + "cannot open stream", e);
    }
    if (XmlUtils.isWellFormed(input, "Envelope")) {
        String inputRootNs;
        try {
            inputRootNs = transformerPoolGetRootNamespace.transform(input, null);
        } catch (Exception e) {
            throw new PipeRunException(this, "cannot extract root namespace", e);
        }
        if (inputRootNs.equals(getSoapNamespace())) {
            log.debug(getLogPrefix(session) + "message to validate is a SOAP message");
            boolean extractSoapBody = true;
            if (StringUtils.isNotEmpty(getSchemaLocation())) {
                StringTokenizer st = new StringTokenizer(getSchemaLocation(), ", \t\r\n\f");
                while (st.hasMoreTokens() && extractSoapBody) {
                    if (st.nextToken().equals(getSoapNamespace())) {
                        extractSoapBody = false;
                    }
                }
            }
            if (extractSoapBody) {
                log.debug(getLogPrefix(session) + "extract SOAP body for validation");
                try {
                    input = transformerPoolExtractSoapBody.transform(input, null, true);
                } catch (Exception e) {
                    throw new PipeRunException(this, "cannot extract SOAP body", e);
                }
                try {
                    inputRootNs = transformerPoolGetRootNamespace.transform(input, null);
                } catch (Exception e) {
                    throw new PipeRunException(this, "cannot extract root namespace", e);
                }
                if (StringUtils.isNotEmpty(inputRootNs) && StringUtils.isEmpty(getSchemaLocation())) {
                    log.debug(getLogPrefix(session) + "remove namespaces from extracted SOAP body");
                    try {
                        input = transformerPoolRemoveNamespaces.transform(input, null, true);
                    } catch (Exception e) {
                        throw new PipeRunException(this, "cannot remove namespaces", e);
                    }
                }
            }
        }
    }
    return new Message(input);
}
Also used : StringTokenizer(java.util.StringTokenizer) Message(nl.nn.adapterframework.stream.Message) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19