Search in sources :

Example 1 with PipeLineResult

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

the class CorePipeLineProcessor method processPipeLine.

public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, String message, IPipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    // Object is the object that is passed to and returned from Pipes
    Object object = (Object) message;
    PipeRunResult pipeRunResult;
    // the PipeLineResult
    PipeLineResult pipeLineResult = new PipeLineResult();
    if (object == null || (object instanceof String && StringUtils.isEmpty(object.toString()))) {
        if (StringUtils.isNotEmpty(pipeLine.getAdapterToRunBeforeOnEmptyInput())) {
            log.debug("running adapterBeforeOnEmptyInput");
            IAdapter adapter = pipeLine.getAdapter().getConfiguration().getIbisManager().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.getState().equals("success")) {
                    throw new PipeRunException(null, "adapterToRunBefore [" + pipeLine.getAdapterToRunBeforeOnEmptyInput() + "] ended with state [" + plr.getState() + "]");
                }
                message = plr.getResult();
                log.debug("input after running adapterBeforeOnEmptyInput [" + message + "]");
                object = (Object) message;
            }
        }
    }
    // ready indicates wether the pipeline processing is complete
    boolean ready = false;
    // get the first pipe to run
    IPipe pipeToRun = pipeLine.getPipe(pipeLine.getFirstPipe());
    boolean inputValidateError = false;
    IPipe inputValidator = pipeLine.getInputValidator();
    if (inputValidator != null) {
        log.debug("validating input");
        PipeRunResult validationResult = pipeProcessor.processPipe(pipeLine, inputValidator, messageId, message, pipeLineSession);
        if (validationResult != null) {
            if (!validationResult.getPipeForward().getName().equals("success")) {
                PipeForward validationForward = validationResult.getPipeForward();
                if (validationForward.getPath() == null) {
                    throw new PipeRunException(pipeToRun, "forward [" + validationForward.getName() + "] of inputValidator has emtpy forward path");
                }
                log.warn("setting first pipe to [" + validationForward.getPath() + "] due to validation fault");
                inputValidateError = true;
                pipeToRun = pipeLine.getPipe(validationForward.getPath());
                if (pipeToRun == null) {
                    throw new PipeRunException(pipeToRun, "forward [" + validationForward.getName() + "], path [" + validationForward.getPath() + "] does not correspond to a pipe");
                }
            }
            Object validatedMessage = validationResult.getResult();
            if (validatedMessage != null) {
                object = validatedMessage;
                message = validatedMessage.toString();
            }
        }
    }
    if (!inputValidateError) {
        IPipe inputWrapper = pipeLine.getInputWrapper();
        if (inputWrapper != null) {
            log.debug("wrapping input");
            PipeRunResult wrapResult = pipeProcessor.processPipe(pipeLine, inputWrapper, messageId, message, pipeLineSession);
            if (wrapResult != null && !wrapResult.getPipeForward().getName().equals("success")) {
                PipeForward wrapForward = wrapResult.getPipeForward();
                if (wrapForward.getPath() == null) {
                    throw new PipeRunException(pipeToRun, "forward [" + wrapForward.getName() + "] of inputWrapper has emtpy forward path");
                }
                log.warn("setting first pipe to [" + wrapForward.getPath() + "] due to wrap fault");
                pipeToRun = pipeLine.getPipe(wrapForward.getPath());
                if (pipeToRun == null) {
                    throw new PipeRunException(pipeToRun, "forward [" + wrapForward.getName() + "], path [" + wrapForward.getPath() + "] does not correspond to a pipe");
                }
            } else {
                message = wrapResult.getResult().toString();
            }
            log.debug("input after wrapping [" + message + "]");
            object = (Object) message;
        }
    }
    pipeLine.getRequestSizeStats().addValue(message.length());
    if (pipeLine.isStoreOriginalMessageWithoutNamespaces()) {
        if (XmlUtils.isWellFormed(message)) {
            String removeNamespaces_xslt = XmlUtils.makeRemoveNamespacesXslt(true, true);
            try {
                String xsltResult = null;
                Transformer transformer = XmlUtils.createTransformer(removeNamespaces_xslt);
                xsltResult = XmlUtils.transformXml(transformer, message);
                pipeLineSession.put("originalMessageWithoutNamespaces", xsltResult);
            } catch (IOException e) {
                throw new PipeRunException(pipeToRun, "cannot retrieve removeNamespaces", e);
            } catch (TransformerConfigurationException te) {
                throw new PipeRunException(pipeToRun, "got error creating transformer from removeNamespaces", te);
            } catch (TransformerException te) {
                throw new PipeRunException(pipeToRun, "got error transforming removeNamespaces", te);
            } catch (DomBuilderException te) {
                throw new PipeRunException(pipeToRun, "caught DomBuilderException", te);
            }
        } else {
            log.warn("original message is not well-formed");
            pipeLineSession.put("originalMessageWithoutNamespaces", message);
        }
    }
    boolean outputValidated = false;
    try {
        while (!ready) {
            pipeRunResult = pipeProcessor.processPipe(pipeLine, pipeToRun, messageId, object, pipeLineSession);
            object = pipeRunResult.getResult();
            if (!(pipeToRun instanceof AbstractPipe)) {
                if (object != null && object instanceof String) {
                    StatisticsKeeper sizeStat = pipeLine.getPipeSizeStatistics(pipeToRun);
                    if (sizeStat != null) {
                        sizeStat.addValue(((String) object).length());
                    }
                }
            }
            PipeForward pipeForward = pipeRunResult.getPipeForward();
            if (pipeForward == null) {
                throw new PipeRunException(pipeToRun, "Pipeline of [" + pipeLine.getOwner().getName() + "] received result from pipe [" + pipeToRun.getName() + "] without a pipeForward");
            }
            // get the next pipe to run
            String nextPath = pipeForward.getPath();
            if ((null == nextPath) || (nextPath.length() == 0)) {
                throw new PipeRunException(pipeToRun, "Pipeline of [" + pipeLine.getOwner().getName() + "] got an path that equals null or has a zero-length value from pipe [" + pipeToRun.getName() + "]. Check the configuration, probably forwards are not defined for this pipe.");
            }
            PipeLineExit plExit = pipeLine.getPipeLineExits().get(nextPath);
            if (null != plExit) {
                boolean outputWrapError = false;
                IPipe outputWrapper = pipeLine.getOutputWrapper();
                if (outputWrapper != null) {
                    log.debug("wrapping PipeLineResult");
                    PipeRunResult wrapResult = pipeProcessor.processPipe(pipeLine, outputWrapper, messageId, object, pipeLineSession);
                    if (wrapResult != null && !wrapResult.getPipeForward().getName().equals("success")) {
                        PipeForward wrapForward = wrapResult.getPipeForward();
                        if (wrapForward.getPath() == null) {
                            throw new PipeRunException(pipeToRun, "forward [" + wrapForward.getName() + "] of outputWrapper has emtpy forward path");
                        }
                        log.warn("setting next pipe to [" + wrapForward.getPath() + "] due to wrap fault");
                        outputWrapError = true;
                        pipeToRun = pipeLine.getPipe(wrapForward.getPath());
                        if (pipeToRun == null) {
                            throw new PipeRunException(pipeToRun, "forward [" + wrapForward.getName() + "], path [" + wrapForward.getPath() + "] does not correspond to a pipe");
                        }
                    } else {
                        log.debug("wrap succeeded");
                        object = wrapResult.getResult();
                    }
                    log.debug("PipeLineResult after wrapping [" + object.toString() + "]");
                }
                if (!outputWrapError) {
                    IPipe outputValidator = pipeLine.getOutputValidator();
                    if ((outputValidator != null) && !outputValidated) {
                        outputValidated = true;
                        log.debug("validating PipeLineResult");
                        PipeRunResult validationResult;
                        validationResult = pipeProcessor.processPipe(pipeLine, outputValidator, messageId, object, pipeLineSession);
                        if (validationResult != null && !validationResult.getPipeForward().getName().equals("success")) {
                            PipeForward validationForward = validationResult.getPipeForward();
                            if (validationForward.getPath() == null) {
                                throw new PipeRunException(pipeToRun, "forward [" + validationForward.getName() + "] of outputValidator has emtpy forward path");
                            }
                            log.warn("setting next pipe to [" + validationForward.getPath() + "] due to validation fault");
                            pipeToRun = pipeLine.getPipe(validationForward.getPath());
                            if (pipeToRun == null) {
                                throw new PipeRunException(pipeToRun, "forward [" + validationForward.getName() + "], path [" + validationForward.getPath() + "] does not correspond to a pipe");
                            }
                        } else {
                            log.debug("validation succeeded");
                            object = validationResult.getResult();
                            ready = true;
                        }
                    } else {
                        ready = true;
                    }
                } else {
                    ready = true;
                }
                if (ready) {
                    String state = plExit.getState();
                    pipeLineResult.setState(state);
                    pipeLineResult.setExitCode(plExit.getExitCode());
                    if (object != null && !plExit.getEmptyResult()) {
                        pipeLineResult.setResult(object.toString());
                    } else {
                        pipeLineResult.setResult(null);
                    }
                    ready = true;
                    if (log.isDebugEnabled()) {
                        // for performance reasons
                        String skString = "";
                        for (Iterator it = pipeLineSession.keySet().iterator(); it.hasNext(); ) {
                            String key = (String) 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: [" + object + "] with exit-state [" + state + "]");
                    }
                }
            } else {
                pipeToRun = pipeLine.getPipe(pipeForward.getPath());
                if (pipeToRun == null) {
                    throw new PipeRunException(null, "Pipeline of adapter [" + pipeLine.getOwner().getName() + "] got an erroneous definition. Pipe to execute [" + pipeForward.getPath() + "] is not defined.");
                }
            }
        }
    } 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 : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IPipeLineExitHandler(nl.nn.adapterframework.core.IPipeLineExitHandler) IOException(java.io.IOException) PipeForward(nl.nn.adapterframework.core.PipeForward) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) AbstractPipe(nl.nn.adapterframework.pipes.AbstractPipe) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) Iterator(java.util.Iterator) StatisticsKeeper(nl.nn.adapterframework.statistics.StatisticsKeeper) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) IAdapter(nl.nn.adapterframework.core.IAdapter) IPipe(nl.nn.adapterframework.core.IPipe) TransformerException(javax.xml.transform.TransformerException) PipeLineExit(nl.nn.adapterframework.core.PipeLineExit)

Example 2 with PipeLineResult

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

the class ServiceClassLoader method reload.

@Override
public void reload() throws ConfigurationException {
    super.reload();
    if (adapterName == null) {
        throw new ConfigurationException("Name of adapter to provide configuration jar not specified");
    }
    IAdapter adapter = ibisManager.getRegisteredAdapter(adapterName);
    if (adapter != null) {
        IPipeLineSession pipeLineSession = new PipeLineSessionBase();
        PipeLineResult processResult = adapter.processMessage(getCorrelationId(), configurationName, pipeLineSession);
        Object object = pipeLineSession.get("configurationJar");
        if (object != null) {
            if (object instanceof byte[]) {
                readResources((byte[]) object, configurationName);
            } else {
                throw new ConfigurationException("SessionKey configurationJar not a byte array");
            }
        } else {
            throw new ConfigurationException("SessionKey configurationJar not found");
        }
    } else {
        throw new ConfigurationException("Could not find adapter: " + adapterName);
    }
}
Also used : ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) IAdapter(nl.nn.adapterframework.core.IAdapter) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Example 3 with PipeLineResult

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

the class ReceiverBase method moveInProcessToErrorAndDoPostProcessing.

private void moveInProcessToErrorAndDoPostProcessing(String messageId, String correlationId, Object rawMessage, String message, Map 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(getErrorStorage(), messageId, correlationId)))) {
        moveInProcessToError(messageId, correlationId, message, rcvDate, comments, rawMessage, TXREQUIRED);
    }
    PipeLineResult plr = new PipeLineResult();
    String result = "<error>" + XmlUtils.encodeChars(comments) + "</error>";
    plr.setResult(result);
    plr.setState("ERROR");
    if (getSender() != null) {
        // TODO correlationId should be technical correlationID!
        String sendMsg = sendResultToSender(correlationId, result);
        if (sendMsg != null) {
            log.warn("problem sending result:" + sendMsg);
        }
    }
    getListener().afterMessageProcessed(plr, rawMessage, threadContext);
}
Also used : PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) Date(java.util.Date)

Example 4 with PipeLineResult

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

the class LockerPipeLineProcessor method processPipeLine.

public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, String message, IPipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    PipeLineResult pipeLineResult;
    Locker locker = pipeLine.getLocker();
    String objectId = null;
    if (locker != null) {
        try {
            objectId = locker.lock();
        } catch (Exception e) {
            boolean isUniqueConstraintViolation = false;
            if (e instanceof SQLException) {
                SQLException sqle = (SQLException) e;
                isUniqueConstraintViolation = locker.getDbmsSupport().isUniqueConstraintViolation(sqle);
            }
            if (isUniqueConstraintViolation) {
                String msg = "error while setting lock: " + e.getMessage();
                log.info(msg);
            } else {
                throw new PipeRunException(null, "error while setting lock", e);
            }
        }
        if (objectId != null) {
            try {
                pipeLineResult = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
            } finally {
                try {
                    locker.unlock(objectId);
                } catch (Exception e) {
                    // throw new PipeRunException(null, "error while removing lock", e);
                    String msg = "error while removing lock: " + e.getMessage();
                    log.warn(msg);
                }
            }
        } else {
            pipeLineResult = new PipeLineResult();
            pipeLineResult.setState("success");
        }
    } else {
        pipeLineResult = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    }
    return pipeLineResult;
}
Also used : Locker(nl.nn.adapterframework.util.Locker) SQLException(java.sql.SQLException) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) SQLException(java.sql.SQLException)

Example 5 with PipeLineResult

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

the class TransactionAttributePipeLineProcessor method processPipeLine.

public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, String message, IPipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    try {
        // TransactionStatus txStatus = txManager.getTransaction(txDef);
        IbisTransaction itx = new IbisTransaction(txManager, pipeLine.getTxDef(), "pipeline of adapter [" + pipeLine.getOwner().getName() + "]");
        TransactionStatus txStatus = itx.getStatus();
        try {
            TimeoutGuard tg = new TimeoutGuard("pipeline of adapter [" + pipeLine.getOwner().getName() + "]");
            Throwable tCaught = null;
            try {
                tg.activateGuard(pipeLine.getTransactionTimeout());
                PipeLineResult pipeLineResult = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
                boolean mustRollback = false;
                if (pipeLineResult == null) {
                    mustRollback = true;
                    log.warn("Pipeline received null result for messageId [" + messageId + "], transaction (when present and active) will be rolled back");
                } else {
                    if (StringUtils.isNotEmpty(pipeLine.getCommitOnState()) && !pipeLine.getCommitOnState().equalsIgnoreCase(pipeLineResult.getState())) {
                        mustRollback = true;
                        log.warn("Pipeline result state [" + pipeLineResult.getState() + "] for messageId [" + messageId + "] is not equal to commitOnState [" + pipeLine.getCommitOnState() + "], transaction (when present and active) will be rolled back");
                    }
                }
                if (mustRollback) {
                    try {
                        txStatus.setRollbackOnly();
                    } catch (Exception e) {
                        throw new PipeRunException(null, "Could not set RollBackOnly", e);
                    }
                }
                return pipeLineResult;
            } catch (Throwable t) {
                tCaught = t;
                throw tCaught;
            } finally {
                if (tg.cancel()) {
                    if (tCaught == null) {
                        throw new InterruptedException(tg.getDescription() + " was interrupted");
                    } else {
                        log.warn("Thread interrupted, but propagating other caught exception of type [" + ClassUtils.nameOf(tCaught) + "]");
                    }
                }
            }
        } catch (Throwable t) {
            log.debug("setting RollBackOnly for pipeline after catching exception");
            txStatus.setRollbackOnly();
            if (t instanceof Error) {
                throw (Error) t;
            } else if (t instanceof RuntimeException) {
                throw (RuntimeException) t;
            } else if (t instanceof PipeRunException) {
                throw (PipeRunException) t;
            } else {
                throw new PipeRunException(null, "Caught unknown checked exception", t);
            }
        } finally {
            // txManager.commit(txStatus);
            itx.commit();
        }
    } catch (RuntimeException e) {
        throw new PipeRunException(null, "RuntimeException calling PipeLine with tx attribute [" + pipeLine.getTransactionAttribute() + "]", e);
    }
}
Also used : IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Aggregations

PipeLineResult (nl.nn.adapterframework.core.PipeLineResult)9 PipeRunException (nl.nn.adapterframework.core.PipeRunException)5 IAdapter (nl.nn.adapterframework.core.IAdapter)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 ZipInputStream (java.util.zip.ZipInputStream)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)2 IbisTransaction (nl.nn.adapterframework.core.IbisTransaction)2 TimeoutGuard (nl.nn.adapterframework.task.TimeoutGuard)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1