Search in sources :

Example 6 with PipeLineResult

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

the class ReceiverBase method processMessageInAdapter.

/*
	 * Assumes message is read, and when transacted, transaction is still open.
	 */
private String processMessageInAdapter(IListener origin, Object rawMessage, String message, String messageId, String technicalCorrelationId, Map threadContext, long waitingDuration, boolean manualRetry) throws ListenerException {
    String result = null;
    PipeLineResult pipeLineResult = null;
    long startProcessingTimestamp = System.currentTimeMillis();
    // if (message==null) {
    // requestSizeStatistics.addValue(0);
    // } else {
    // requestSizeStatistics.addValue(message.length());
    // }
    lastMessageDate = startProcessingTimestamp;
    log.debug(getLogPrefix() + "received message with messageId [" + messageId + "] (technical) correlationId [" + technicalCorrelationId + "]");
    if (StringUtils.isEmpty(messageId)) {
        messageId = Misc.createSimpleUUID();
        if (log.isDebugEnabled())
            log.debug(getLogPrefix() + "generated messageId [" + messageId + "]");
    }
    if (getChompCharSize() != null || getElementToMove() != null || getElementToMoveChain() != null) {
        log.debug(getLogPrefix() + "compact received message");
        try {
            InputStream xmlInput = IOUtils.toInputStream(message, "UTF-8");
            CompactSaxHandler handler = new CompactSaxHandler();
            handler.setChompCharSize(getChompCharSize());
            handler.setElementToMove(getElementToMove());
            handler.setElementToMoveChain(getElementToMoveChain());
            handler.setElementToMoveSessionKey(getElementToMoveSessionKey());
            handler.setRemoveCompactMsgNamespaces(isRemoveCompactMsgNamespaces());
            if (threadContext != null) {
                handler.setContext(threadContext);
            }
            SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
            parserFactory.setNamespaceAware(true);
            SAXParser saxParser = parserFactory.newSAXParser();
            try {
                saxParser.parse(xmlInput, handler);
                message = handler.getXmlString();
            } catch (Exception e) {
                warn("received message could not be compacted: " + e.getMessage());
            }
            handler = null;
        } catch (Exception e) {
            throw new ListenerException("error during compacting received message to more compact format: " + e.getMessage());
        }
    }
    String businessCorrelationId = null;
    if (correlationIDTp != null) {
        try {
            businessCorrelationId = correlationIDTp.transform(message, null);
        } catch (Exception e) {
            // throw new ListenerException(getLogPrefix()+"could not extract businessCorrelationId",e);
            log.warn(getLogPrefix() + "could not extract businessCorrelationId");
        }
        if (StringUtils.isEmpty(businessCorrelationId)) {
            String cidText;
            if (StringUtils.isNotEmpty(getCorrelationIDXPath())) {
                cidText = "xpathExpression [" + getCorrelationIDXPath() + "]";
            } else {
                cidText = "styleSheet [" + getCorrelationIDStyleSheet() + "]";
            }
            if (StringUtils.isNotEmpty(technicalCorrelationId)) {
                log.info(getLogPrefix() + "did not find correlationId using " + cidText + ", reverting to correlationId of transfer [" + technicalCorrelationId + "]");
                businessCorrelationId = technicalCorrelationId;
            }
        }
    } else {
        businessCorrelationId = technicalCorrelationId;
    }
    if (StringUtils.isEmpty(businessCorrelationId)) {
        if (StringUtils.isNotEmpty(messageId)) {
            log.info(getLogPrefix() + "did not find (technical) correlationId, reverting to messageId [" + messageId + "]");
            businessCorrelationId = messageId;
        }
    }
    log.info(getLogPrefix() + "messageId [" + messageId + "] technicalCorrelationId [" + technicalCorrelationId + "] businessCorrelationId [" + businessCorrelationId + "]");
    threadContext.put(IPipeLineSession.businessCorrelationIdKey, businessCorrelationId);
    String label = null;
    if (labelTp != null) {
        try {
            label = labelTp.transform(message, null);
        } catch (Exception e) {
            // throw new ListenerException(getLogPrefix()+"could not extract label",e);
            log.warn(getLogPrefix() + "could not extract label: (" + ClassUtils.nameOf(e) + ") " + e.getMessage());
        }
    }
    if (hasProblematicHistory(messageId, manualRetry, rawMessage, message, threadContext, businessCorrelationId)) {
        if (!isTransacted()) {
            log.warn(getLogPrefix() + "received message with messageId [" + messageId + "] which has a problematic history; aborting processing");
        }
        numRejected.increase();
        return result;
    }
    if (isDuplicateAndSkip(getMessageLog(), messageId, businessCorrelationId)) {
        numRejected.increase();
        return result;
    }
    if (getCachedProcessResult(messageId) != null) {
        numRetried.increase();
    }
    int txOption = this.getTransactionAttributeNum();
    TransactionDefinition txDef = SpringTxManagerProxy.getTransactionDefinition(txOption, getTransactionTimeout());
    // TransactionStatus txStatus = txManager.getTransaction(txDef);
    IbisTransaction itx = new IbisTransaction(txManager, txDef, "receiver [" + getName() + "]");
    TransactionStatus txStatus = itx.getStatus();
    // update processing statistics
    // count in processing statistics includes messages that are rolled back to input
    startProcessingMessage(waitingDuration);
    IPipeLineSession pipelineSession = null;
    String errorMessage = "";
    boolean messageInError = false;
    try {
        String pipelineMessage;
        if (origin instanceof IBulkDataListener) {
            try {
                IBulkDataListener bdl = (IBulkDataListener) origin;
                pipelineMessage = bdl.retrieveBulkData(rawMessage, message, threadContext);
            } catch (Throwable t) {
                errorMessage = t.getMessage();
                messageInError = true;
                ListenerException l = wrapExceptionAsListenerException(t);
                throw l;
            }
        } else {
            pipelineMessage = message;
        }
        numReceived.increase();
        // Note: errorMessage is used to pass value from catch-clause to finally-clause!
        pipelineSession = createProcessingContext(businessCorrelationId, threadContext, messageId);
        // threadContext=pipelineSession; // this is to enable Listeners to use session variables, for instance in afterProcessMessage()
        try {
            if (getMessageLog() != null) {
                getMessageLog().storeMessage(messageId, businessCorrelationId, new Date(), RCV_MESSAGE_LOG_COMMENTS, label, pipelineMessage);
            }
            log.debug(getLogPrefix() + "preparing TimeoutGuard");
            TimeoutGuard tg = new TimeoutGuard("Receiver " + getName());
            try {
                if (log.isDebugEnabled())
                    log.debug(getLogPrefix() + "activating TimeoutGuard with transactionTimeout [" + transactionTimeout + "]s");
                tg.activateGuard(getTransactionTimeout());
                pipeLineResult = adapter.processMessageWithExceptions(businessCorrelationId, pipelineMessage, pipelineSession);
                pipelineSession.put("exitcode", "" + pipeLineResult.getExitCode());
                result = pipeLineResult.getResult();
                errorMessage = "exitState [" + pipeLineResult.getState() + "], result [" + result + "]";
                if (pipelineSession.containsKey("exitcode")) {
                    int status = Integer.parseInt("" + pipelineSession.get("exitcode"));
                    if (status > 0)
                        errorMessage += ", exitcode [" + status + "]";
                }
                if (log.isDebugEnabled()) {
                    log.debug(getLogPrefix() + "received result: " + errorMessage);
                }
                messageInError = txStatus.isRollbackOnly();
            } finally {
                log.debug(getLogPrefix() + "canceling TimeoutGuard, isInterrupted [" + Thread.currentThread().isInterrupted() + "]");
                if (tg.cancel()) {
                    errorMessage = "timeout exceeded";
                    if (StringUtils.isEmpty(result)) {
                        result = "<timeout/>";
                    }
                    messageInError = true;
                }
            }
            if (!messageInError && !isTransacted()) {
                String commitOnState = ((Adapter) adapter).getPipeLine().getCommitOnState();
                if (StringUtils.isNotEmpty(commitOnState) && !commitOnState.equalsIgnoreCase(pipeLineResult.getState())) {
                    messageInError = true;
                }
            }
        } catch (Throwable t) {
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                log.debug("<*>" + getLogPrefix() + "TX Update: Received failure, transaction " + (txStatus.isRollbackOnly() ? "already" : "not yet") + " marked for rollback-only");
            }
            errorMessage = t.getMessage();
            messageInError = true;
            if (pipeLineResult == null) {
                pipeLineResult = new PipeLineResult();
            }
            if (StringUtils.isEmpty(pipeLineResult.getResult())) {
                String formattedErrorMessage = adapter.formatErrorMessage("exception caught", t, message, messageId, this, startProcessingTimestamp);
                pipeLineResult.setResult(formattedErrorMessage);
            }
            ListenerException l = wrapExceptionAsListenerException(t);
            throw l;
        } finally {
            putSessionKeysIntoThreadContext(threadContext, pipelineSession);
        }
        // }
        if (getSender() != null) {
            String sendMsg = sendResultToSender(technicalCorrelationId, result);
            if (sendMsg != null) {
                errorMessage = sendMsg;
            }
        }
    } finally {
        cacheProcessResult(messageId, businessCorrelationId, errorMessage, new Date(startProcessingTimestamp));
        if (!isTransacted() && messageInError) {
            if (!manualRetry) {
                moveInProcessToError(messageId, businessCorrelationId, message, new Date(startProcessingTimestamp), errorMessage, rawMessage, TXNEW_CTRL);
            }
        }
        try {
            Map afterMessageProcessedMap;
            if (threadContext != null) {
                afterMessageProcessedMap = threadContext;
                if (pipelineSession != null) {
                    threadContext.putAll(pipelineSession);
                }
            } else {
                afterMessageProcessedMap = pipelineSession;
            }
            origin.afterMessageProcessed(pipeLineResult, rawMessage, afterMessageProcessedMap);
        } finally {
            long finishProcessingTimestamp = System.currentTimeMillis();
            finishProcessingMessage(finishProcessingTimestamp - startProcessingTimestamp);
            if (!txStatus.isCompleted()) {
                // NB: Spring will take care of executing a commit or a rollback;
                // Spring will also ONLY commit the transaction if it was newly created
                // by the above call to txManager.getTransaction().
                // txManager.commit(txStatus);
                itx.commit();
            } else {
                throw new ListenerException(getLogPrefix() + "Transaction already completed; we didn't expect this");
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "messageId [" + messageId + "] correlationId [" + businessCorrelationId + "] returning result [" + result + "]");
    return result;
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionDefinition(org.springframework.transaction.TransactionDefinition) InputStream(java.io.InputStream) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Date(java.util.Date) ListenerException(nl.nn.adapterframework.core.ListenerException) IBulkDataListener(nl.nn.adapterframework.core.IBulkDataListener) CompactSaxHandler(nl.nn.adapterframework.util.CompactSaxHandler) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) SAXParser(javax.xml.parsers.SAXParser) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with PipeLineResult

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

the class CachePipeLineProcessor method processPipeLine.

public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, String message, IPipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    ICacheAdapter cache = pipeLine.getCache();
    if (cache == null) {
        return pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    }
    String key = cache.transformKey(message, 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 + "]");
    String result;
    String state;
    synchronized (cache) {
        result = cache.getString("r" + key);
        state = cache.getString("s" + key);
    }
    if (result != null && state != null) {
        if (log.isDebugEnabled())
            log.debug("retrieved result from cache using key [" + key + "]");
        PipeLineResult prr = new PipeLineResult();
        prr.setState(state);
        prr.setResult(result);
        return prr;
    }
    if (log.isDebugEnabled())
        log.debug("no cached results found using key [" + key + "]");
    PipeLineResult prr = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
    if (log.isDebugEnabled())
        log.debug("caching result using key [" + key + "]");
    String cacheValue = cache.transformValue(prr.getResult(), pipeLineSession);
    synchronized (cache) {
        cache.putString("r" + key, cacheValue);
        cache.putString("s" + key, prr.getState());
    }
    return prr;
}
Also used : ICacheAdapter(nl.nn.adapterframework.cache.ICacheAdapter) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult)

Example 8 with PipeLineResult

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

the class TestPipeline method postTestPipeLine.

@POST
@RolesAllowed({ "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/test-pipeline")
@Relation("pipeline")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postTestPipeLine(MultipartFormDataInput input) throws ApiException, PipeRunException {
    Map<String, Object> result = new HashMap<String, Object>();
    IbisManager ibisManager = getIbisManager();
    if (ibisManager == null) {
        throw new ApiException("Config not found!");
    }
    String message = null, fileEncoding = null, fileName = null;
    InputStream file = null;
    IAdapter adapter = null;
    Map<String, List<InputPart>> inputDataMap = input.getFormDataMap();
    try {
        if (inputDataMap.get("message") != null)
            message = inputDataMap.get("message").get(0).getBodyAsString();
        if (inputDataMap.get("encoding") != null)
            fileEncoding = inputDataMap.get("encoding").get(0).getBodyAsString();
        if (inputDataMap.get("adapter") != null) {
            String adapterName = inputDataMap.get("adapter").get(0).getBodyAsString();
            adapter = ibisManager.getRegisteredAdapter(adapterName);
        }
        if (inputDataMap.get("file") != null) {
            file = inputDataMap.get("file").get(0).getBody(InputStream.class, null);
            MultivaluedMap<String, String> headers = inputDataMap.get("file").get(0).getHeaders();
            String[] contentDispositionHeader = headers.getFirst("Content-Disposition").split(";");
            for (String name : contentDispositionHeader) {
                if ((name.trim().startsWith("filename"))) {
                    String[] tmp = name.split("=");
                    fileName = tmp[1].trim().replaceAll("\"", "");
                }
            }
            if (fileEncoding == null || fileEncoding.isEmpty())
                fileEncoding = Misc.DEFAULT_INPUT_STREAM_ENCODING;
            if (StringUtils.endsWithIgnoreCase(fileName, ".zip")) {
                try {
                    processZipFile(result, file, fileEncoding, adapter, secLogMessage);
                } catch (Exception e) {
                    throw new PipeRunException(this, getLogPrefix(null) + "exception on processing zip file", e);
                }
            } else {
                message = Misc.streamToString(file, "\n", fileEncoding, false);
            }
        }
    } catch (IOException e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    if (fileEncoding == null || StringUtils.isEmpty(fileEncoding))
        fileEncoding = Misc.DEFAULT_INPUT_STREAM_ENCODING;
    if (adapter == null && (message == null && file == null)) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    if (StringUtils.isNotEmpty(message)) {
        try {
            PipeLineResult plr = processMessage(adapter, message, secLogMessage);
            result.put("state", plr.getState());
            result.put("result", plr.getResult());
        } catch (Exception e) {
            throw new PipeRunException(this, getLogPrefix(null) + "exception on sending message", e);
        }
    }
    return Response.status(Response.Status.CREATED).entity(result).build();
}
Also used : IbisManager(nl.nn.adapterframework.configuration.IbisManager) HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) List(java.util.List) IAdapter(nl.nn.adapterframework.core.IAdapter) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 9 with PipeLineResult

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

the class TestPipeLine method doPost.

private String doPost(IPipeLineSession session) throws PipeRunException {
    Object form_file = session.get("file");
    String form_message = null;
    form_message = (String) session.get("message");
    if (form_file == null && (StringUtils.isEmpty(form_message))) {
        throw new PipeRunException(this, getLogPrefix(session) + "Nothing to send or test");
    }
    String form_adapterName = (String) session.get("adapterName");
    if (StringUtils.isEmpty(form_adapterName)) {
        throw new PipeRunException(this, getLogPrefix(session) + "No adapter selected");
    }
    IAdapter adapter = RestListenerUtils.retrieveIbisManager(session).getRegisteredAdapter(form_adapterName);
    if (adapter == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "Adapter with specified name [" + form_adapterName + "] could not be retrieved");
    }
    boolean writeSecLogMessage = false;
    if (secLogMessage) {
        writeSecLogMessage = (Boolean) session.get("writeSecLogMessage");
    }
    if (form_file != null) {
        if (form_file instanceof InputStream) {
            InputStream inputStream = (InputStream) form_file;
            String form_fileName = (String) session.get("fileName");
            String form_fileEncoding = (String) session.get("fileEncoding");
            try {
                if (inputStream.available() > 0) {
                    String fileEncoding;
                    if (StringUtils.isNotEmpty(form_fileEncoding)) {
                        fileEncoding = form_fileEncoding;
                    } else {
                        fileEncoding = Misc.DEFAULT_INPUT_STREAM_ENCODING;
                    }
                    if (StringUtils.endsWithIgnoreCase(form_fileName, ".zip")) {
                        try {
                            form_message = processZipFile(session, inputStream, fileEncoding, adapter, writeSecLogMessage);
                        } catch (Exception e) {
                            throw new PipeRunException(this, getLogPrefix(session) + "exception on processing zip file", e);
                        }
                    } else {
                        form_message = Misc.streamToString(inputStream, "\n", fileEncoding, false);
                    }
                }
            } catch (IOException e) {
                throw new PipeRunException(this, getLogPrefix(session) + "exception on converting stream to string", e);
            }
        } else {
            form_message = form_file.toString();
        }
        session.put("message", form_message);
    }
    if (StringUtils.isNotEmpty(form_message)) {
        try {
            PipeLineResult plr = processMessage(adapter, form_message, writeSecLogMessage);
            session.put("state", plr.getState());
            session.put("result", plr.getResult());
        } catch (Exception e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on sending message", e);
        }
    }
    return "<dummy/>";
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) IAdapter(nl.nn.adapterframework.core.IAdapter) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException)

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