Search in sources :

Example 76 with PipeRunException

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

the class RestListener method processRequest.

public String processRequest(String correlationId, String message, IPipeLineSession requestContext) throws ListenerException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) requestContext.get(IPipeLineSession.HTTP_REQUEST_KEY);
    String response;
    String contentType = (String) requestContext.get("contentType");
    // Check if valid path
    String requestRestPath = (String) requestContext.get("restPath");
    if (!getRestPath().equals(requestRestPath)) {
        throw new ListenerException("illegal restPath value [" + requestRestPath + "], must be '" + getRestPath() + "'");
    }
    // Check if consumes has been set or contentType is set to JSON
    if (getConsumes().equalsIgnoreCase("JSON") && "application/json".equalsIgnoreCase(httpServletRequest.getContentType())) {
        try {
            message = transformToXml(message);
        } catch (PipeRunException e) {
            throw new ListenerException("Failed to transform JSON to XML");
        }
    }
    int eTag = 0;
    // Check if contentType is not overwritten which disabled auto-converting and mediatype headers
    if (contentType == null || StringUtils.isEmpty(contentType) || contentType.equalsIgnoreCase("*/*")) {
        if (getProduces().equalsIgnoreCase("XML"))
            requestContext.put("contentType", "application/xml");
        if (getProduces().equalsIgnoreCase("JSON"))
            requestContext.put("contentType", "application/json");
        if (getProduces().equalsIgnoreCase("TEXT"))
            requestContext.put("contentType", "text/plain");
        response = super.processRequest(correlationId, message, requestContext);
        if (response != null && !response.isEmpty())
            eTag = response.hashCode();
        if (getProduces().equalsIgnoreCase("JSON")) {
            try {
                response = transformToJson(response);
            } catch (PipeRunException e) {
                throw new ListenerException("Failed to transform XML to JSON");
            }
        }
    } else {
        response = super.processRequest(correlationId, message, requestContext);
        if (response != null && !response.isEmpty())
            eTag = response.hashCode();
    }
    if (!requestContext.containsKey("etag") && getGenerateEtag() && eTag != 0) {
        // The etag can be a negative integer...
        requestContext.put("etag", RestListenerUtils.formatEtag(getRestPath(), getUriPattern(), eTag));
    }
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ListenerException(nl.nn.adapterframework.core.ListenerException) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 77 with PipeRunException

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

the class ZipWriterPipe method closeZipWriterHandle.

protected void closeZipWriterHandle(IPipeLineSession session, boolean mustFind) throws PipeRunException {
    ZipWriter sessionData = getZipWriter(session);
    if (sessionData == null) {
        if (mustFind) {
            throw new PipeRunException(this, getLogPrefix(session) + "cannot find session data");
        } else {
            log.debug(getLogPrefix(session) + "did find session data, assuming already closed");
        }
    } else {
        try {
            sessionData.close();
        } catch (CompressionException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "cannot close", e);
        }
    }
    session.remove(getZipWriterHandle());
}
Also used : PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 78 with PipeRunException

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

the class ZipWriterPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    if (ACTION_CLOSE.equals(getAction())) {
        closeZipWriterHandle(session, true);
        return new PipeRunResult(getForward(), input);
    }
    String msg = null;
    if (input instanceof String) {
        msg = (String) input;
    }
    ParameterResolutionContext prc = new ParameterResolutionContext(msg, session);
    ParameterValueList pvl;
    try {
        pvl = prc.getValues(getParameterList());
    } catch (ParameterException e1) {
        throw new PipeRunException(this, getLogPrefix(session) + "cannot determine filename", e1);
    }
    ZipWriter sessionData = getZipWriter(session);
    if (ACTION_OPEN.equals(getAction())) {
        if (sessionData != null) {
            throw new PipeRunException(this, getLogPrefix(session) + "zipWriterHandle in session key [" + getZipWriterHandle() + "] is already open");
        }
        sessionData = createZipWriter(session, pvl, input);
        return new PipeRunResult(getForward(), input);
    }
    // from here on action must be 'write' or 'stream'
    if (sessionData == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "zipWriterHandle in session key [" + getZipWriterHandle() + "] is not open");
    }
    String filename = (String) pvl.getParameterValue(PARAMETER_FILENAME).getValue();
    if (StringUtils.isEmpty(filename)) {
        throw new PipeRunException(this, getLogPrefix(session) + "filename cannot be empty");
    }
    try {
        if (ACTION_STREAM.equals(getAction())) {
            sessionData.openEntry(filename);
            PipeRunResult prr = new PipeRunResult(getForward(), sessionData.getZipoutput());
            return prr;
        }
        if (ACTION_WRITE.equals(getAction())) {
            try {
                if (isCompleteFileHeader()) {
                    sessionData.writeEntryWithCompletedHeader(filename, input, isCloseInputstreamOnExit(), getCharset());
                } else {
                    sessionData.writeEntry(filename, input, isCloseInputstreamOnExit(), getCharset());
                }
            } catch (IOException e) {
                throw new PipeRunException(this, getLogPrefix(session) + "cannot add data to zipentry for [" + filename + "]", e);
            }
            return new PipeRunResult(getForward(), input);
        }
        throw new PipeRunException(this, getLogPrefix(session) + "illegal action [" + getAction() + "]");
    } catch (CompressionException e) {
        throw new PipeRunException(this, getLogPrefix(session) + "cannot add zipentry for [" + filename + "]", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 79 with PipeRunException

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

the class BatchFileTransformerPipe method doPipe.

/**
 * Open a reader for the file named according the input messsage and
 * transform it.
 * Move the input file to a done directory when transformation is finished
 * and return the names of the generated files.
 *
 * @see nl.nn.adapterframework.core.IPipe#doPipe(java.lang.Object, nl.nn.adapterframework.core.IPipeLineSession)
 */
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    if (input == null) {
        throw new PipeRunException(this, "got null input instead of String containing filename");
    }
    if (!(input instanceof String)) {
        throw new PipeRunException(this, "expected String containing filename as input, got [" + ClassUtils.nameOf(input) + "], value [" + input + "]");
    }
    String filename = input.toString();
    File file = new File(filename);
    try {
        PipeRunResult result = super.doPipe(file, session);
        try {
            FileUtils.moveFileAfterProcessing(file, getMove2dirAfterTransform(), isDelete(), isOverwrite(), getNumberOfBackups());
        } catch (Exception e) {
            log.error(getLogPrefix(session), e);
        }
        return result;
    } catch (PipeRunException e) {
        try {
            FileUtils.moveFileAfterProcessing(file, getMove2dirAfterError(), isDelete(), isOverwrite(), getNumberOfBackups());
        } catch (Exception e2) {
            log.error(getLogPrefix(session) + "Could not move file after exception [" + e2 + "]");
        }
        throw e;
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) File(java.io.File) PipeRunException(nl.nn.adapterframework.core.PipeRunException) FileNotFoundException(java.io.FileNotFoundException)

Example 80 with PipeRunException

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

PipeRunException (nl.nn.adapterframework.core.PipeRunException)101 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)65 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)40 IOException (java.io.IOException)34 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)32 PipeForward (nl.nn.adapterframework.core.PipeForward)20 ParameterException (nl.nn.adapterframework.core.ParameterException)16 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)13 Parameter (nl.nn.adapterframework.parameters.Parameter)12 InputStream (java.io.InputStream)10 File (java.io.File)9 Map (java.util.Map)9 ParameterList (nl.nn.adapterframework.parameters.ParameterList)8 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)7 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)7 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)6 PipeStartException (nl.nn.adapterframework.core.PipeStartException)6 ArrayList (java.util.ArrayList)5 ZipInputStream (java.util.zip.ZipInputStream)5 IAdapter (nl.nn.adapterframework.core.IAdapter)5