Search in sources :

Example 6 with ParameterException

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

the class MessageSendingPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    String originalMessage = input.toString();
    String result = null;
    String correlationID = session.getMessageId();
    if (getInputWrapper() != null) {
        log.debug(getLogPrefix(session) + "wrapping input");
        PipeRunResult wrapResult = pipeProcessor.processPipe(getPipeLine(), inputWrapper, correlationID, input, session);
        if (wrapResult != null && !wrapResult.getPipeForward().getName().equals(SUCCESS_FORWARD)) {
            return wrapResult;
        } else {
            input = wrapResult.getResult();
        }
        log.debug(getLogPrefix(session) + "input after wrapping [" + input.toString() + "]");
    }
    if (getInputValidator() != null) {
        log.debug(getLogPrefix(session) + "validating input");
        PipeRunResult validationResult = pipeProcessor.processPipe(getPipeLine(), inputValidator, correlationID, input, session);
        if (validationResult != null && !validationResult.getPipeForward().getName().equals(SUCCESS_FORWARD)) {
            return validationResult;
        }
    }
    if (StringUtils.isNotEmpty(getStubFileName())) {
        ParameterList pl = getParameterList();
        result = returnString;
        if (pl != null) {
            ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
            Map params;
            try {
                params = prc.getValueMap(pl);
            } catch (ParameterException e1) {
                throw new PipeRunException(this, getLogPrefix(session) + "got exception evaluating parameters", e1);
            }
            String sfn = null;
            if (params != null && params.size() > 0) {
                sfn = (String) params.get(STUBFILENAME);
            }
            if (sfn != null) {
                try {
                    result = Misc.resourceToString(ClassUtils.getResourceURL(classLoader, sfn), SystemUtils.LINE_SEPARATOR);
                    log.info(getLogPrefix(session) + "returning result from dynamic stub [" + sfn + "]");
                } catch (Throwable e) {
                    throw new PipeRunException(this, getLogPrefix(session) + "got exception loading result from stub [" + sfn + "]", e);
                }
            } else {
                log.info(getLogPrefix(session) + "returning result from static stub [" + getStubFileName() + "]");
            }
        } else {
            log.info(getLogPrefix(session) + "returning result from static stub [" + getStubFileName() + "]");
        }
    } else {
        Map threadContext = new HashMap();
        try {
            String messageID = null;
            // sendResult has a messageID for async senders, the result for sync senders
            int retryInterval = getRetryMinInterval();
            String sendResult = null;
            boolean replyIsValid = false;
            int retriesLeft = 0;
            if (getMaxRetries() > 0) {
                retriesLeft = getMaxRetries() + 1;
            } else {
                retriesLeft = 1;
            }
            while (retriesLeft-- >= 1 && !replyIsValid) {
                try {
                    sendResult = sendMessage(input, session, correlationID, getSender(), threadContext);
                    if (retryTp != null) {
                        String retry = retryTp.transform(sendResult, null);
                        if (retry.equalsIgnoreCase("true")) {
                            if (retriesLeft >= 1) {
                                retryInterval = increaseRetryIntervalAndWait(session, retryInterval, "xpathRetry result [" + retry + "], retries left [" + retriesLeft + "]");
                            }
                        } else {
                            replyIsValid = true;
                        }
                    } else {
                        replyIsValid = true;
                    }
                } catch (TimeOutException toe) {
                    if (retriesLeft >= 1) {
                        retryInterval = increaseRetryIntervalAndWait(session, retryInterval, "timeout occured, retries left [" + retriesLeft + "]");
                    } else {
                        throw toe;
                    }
                } catch (SenderException se) {
                    if (retriesLeft >= 1) {
                        retryInterval = increaseRetryIntervalAndWait(session, retryInterval, "exception [" + (se != null ? se.getMessage() : "") + "] occured, retries left [" + retriesLeft + "]");
                    } else {
                        throw se;
                    }
                }
            }
            if (!replyIsValid) {
                throw new PipeRunException(this, getLogPrefix(session) + "invalid reply message is received");
            }
            if (getSender().isSynchronous()) {
                if (log.isInfoEnabled()) {
                    log.info(getLogPrefix(session) + "sent message to [" + getSender().getName() + "] synchronously");
                }
                result = sendResult;
            } else {
                messageID = sendResult;
                if (log.isInfoEnabled()) {
                    log.info(getLogPrefix(session) + "sent message to [" + getSender().getName() + "] messageID [" + messageID + "] correlationID [" + correlationID + "] linkMethod [" + getLinkMethod() + "]");
                }
                // as this will be used with the listener
                if (getLinkMethod().equalsIgnoreCase("MESSAGEID")) {
                    correlationID = sendResult;
                    if (log.isDebugEnabled())
                        log.debug(getLogPrefix(session) + "setting correlationId to listen for to messageId [" + correlationID + "]");
                }
            }
            ITransactionalStorage messageLog = getMessageLog();
            if (messageLog != null) {
                long messageLogStartTime = System.currentTimeMillis();
                String messageTrail = "no audit trail";
                if (auditTrailTp != null) {
                    if (isUseInputForExtract()) {
                        messageTrail = auditTrailTp.transform(originalMessage, null);
                    } else {
                        messageTrail = auditTrailTp.transform((String) input, null);
                    }
                } else {
                    if (StringUtils.isNotEmpty(getAuditTrailSessionKey())) {
                        messageTrail = (String) (session.get(getAuditTrailSessionKey()));
                    }
                }
                String storedMessageID = messageID;
                if (storedMessageID == null) {
                    storedMessageID = "-";
                }
                if (correlationIDTp != null) {
                    if (StringUtils.isNotEmpty(getCorrelationIDSessionKey())) {
                        String sourceString = (String) (session.get(getCorrelationIDSessionKey()));
                        correlationID = correlationIDTp.transform(sourceString, null);
                    } else {
                        if (isUseInputForExtract()) {
                            correlationID = correlationIDTp.transform(originalMessage, null);
                        } else {
                            correlationID = correlationIDTp.transform((String) input, null);
                        }
                    }
                    if (StringUtils.isEmpty(correlationID)) {
                        correlationID = "-";
                    }
                }
                String label = null;
                if (labelTp != null) {
                    if (isUseInputForExtract()) {
                        label = labelTp.transform(originalMessage, null);
                    } else {
                        label = labelTp.transform((String) input, null);
                    }
                }
                if (sender instanceof MailSender) {
                    String messageInMailSafeForm = (String) session.get("messageInMailSafeForm");
                    if (hideRegex != null) {
                        if (getHideMethod().equalsIgnoreCase("FIRSTHALF")) {
                            messageInMailSafeForm = Misc.hideFirstHalf(messageInMailSafeForm, hideRegex);
                        } else {
                            messageInMailSafeForm = Misc.hideAll(messageInMailSafeForm, hideRegex);
                        }
                    }
                    messageLog.storeMessage(storedMessageID, correlationID, new Date(), messageTrail, label, messageInMailSafeForm);
                } else {
                    String message = (String) input;
                    if (hideRegex != null) {
                        if (getHideMethod().equalsIgnoreCase("FIRSTHALF")) {
                            message = Misc.hideFirstHalf(message, hideRegex);
                        } else {
                            message = Misc.hideAll(message, hideRegex);
                        }
                    }
                    messageLog.storeMessage(storedMessageID, correlationID, new Date(), messageTrail, label, message);
                }
                long messageLogEndTime = System.currentTimeMillis();
                long messageLogDuration = messageLogEndTime - messageLogStartTime;
                StatisticsKeeper sk = getPipeLine().getPipeStatistics(messageLog);
                sk.addValue(messageLogDuration);
            }
            if (sender instanceof MailSender) {
                session.remove("messageInMailSafeForm");
            }
            if (getListener() != null) {
                result = listenerProcessor.getMessage(getListener(), correlationID, session);
            } else {
                result = sendResult;
            }
            if (result == null) {
                result = "";
            }
            if (timeoutPending) {
                timeoutPending = false;
                throwEvent(PIPE_CLEAR_TIMEOUT_MONITOR_EVENT);
            }
        } catch (TimeOutException toe) {
            throwEvent(PIPE_TIMEOUT_MONITOR_EVENT);
            if (!timeoutPending) {
                timeoutPending = true;
            }
            PipeForward timeoutForward = findForward(TIMEOUT_FORWARD);
            log.warn(getLogPrefix(session) + "timeout occured");
            if (timeoutForward == null) {
                if (StringUtils.isEmpty(getResultOnTimeOut())) {
                    timeoutForward = findForward(EXCEPTION_FORWARD);
                } else {
                    timeoutForward = getForward();
                }
            }
            if (timeoutForward != null) {
                String resultmsg;
                if (StringUtils.isNotEmpty(getResultOnTimeOut())) {
                    resultmsg = getResultOnTimeOut();
                } else {
                    if (input instanceof String) {
                        resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), toe, this, (String) input, session.getMessageId(), 0);
                    } else {
                        if (input == null) {
                            input = "null";
                        }
                        resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), toe, this, input.toString(), session.getMessageId(), 0);
                    }
                }
                return new PipeRunResult(timeoutForward, resultmsg);
            }
            throw new PipeRunException(this, getLogPrefix(session) + "caught timeout-exception", toe);
        } catch (Throwable t) {
            throwEvent(PIPE_EXCEPTION_MONITOR_EVENT);
            PipeForward exceptionForward = findForward(EXCEPTION_FORWARD);
            if (exceptionForward != null) {
                log.warn(getLogPrefix(session) + "exception occured, forwarding to exception-forward [" + exceptionForward.getPath() + "], exception:\n", t);
                String resultmsg;
                if (input instanceof String) {
                    resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), t, this, (String) input, session.getMessageId(), 0);
                } else {
                    if (input == null) {
                        input = "null";
                    }
                    resultmsg = new ErrorMessageFormatter().format(getLogPrefix(session), t, this, input.toString(), session.getMessageId(), 0);
                }
                return new PipeRunResult(exceptionForward, resultmsg);
            }
            throw new PipeRunException(this, getLogPrefix(session) + "caught exception", t);
        }
    }
    if (!validResult(result)) {
        PipeForward illegalResultForward = findForward(ILLEGAL_RESULT_FORWARD);
        return new PipeRunResult(illegalResultForward, result);
    }
    IPipe outputValidator = getOutputValidator();
    if (outputValidator != null) {
        log.debug(getLogPrefix(session) + "validating response");
        PipeRunResult validationResult;
        validationResult = pipeProcessor.processPipe(getPipeLine(), outputValidator, correlationID, result, session);
        if (validationResult != null && !validationResult.getPipeForward().getName().equals(SUCCESS_FORWARD)) {
            return validationResult;
        }
    }
    if (getOutputWrapper() != null) {
        log.debug(getLogPrefix(session) + "wrapping response");
        PipeRunResult wrapResult = pipeProcessor.processPipe(getPipeLine(), outputWrapper, correlationID, result, session);
        if (wrapResult != null && !wrapResult.getPipeForward().getName().equals(SUCCESS_FORWARD)) {
            return wrapResult;
        }
        result = wrapResult.getResult().toString();
        log.debug(getLogPrefix(session) + "response after wrapping [" + result + "]");
    }
    if (isStreamResultToServlet()) {
        byte[] bytes = Base64.decodeBase64(new String(result));
        try {
            String contentType = (String) session.get("contentType");
            if (StringUtils.isNotEmpty(contentType)) {
                RestListenerUtils.setResponseContentType(session, contentType);
            }
            RestListenerUtils.writeToResponseOutputStream(session, bytes);
        } catch (IOException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "caught exception", e);
        }
        return new PipeRunResult(getForward(), "");
    } else {
        return new PipeRunResult(getForward(), result);
    }
}
Also used : HashMap(java.util.HashMap) MailSender(nl.nn.adapterframework.senders.MailSender) IOException(java.io.IOException) PipeForward(nl.nn.adapterframework.core.PipeForward) Date(java.util.Date) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ErrorMessageFormatter(nl.nn.adapterframework.errormessageformatters.ErrorMessageFormatter) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterList(nl.nn.adapterframework.parameters.ParameterList) StatisticsKeeper(nl.nn.adapterframework.statistics.StatisticsKeeper) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) Map(java.util.Map) HashMap(java.util.HashMap) IPipe(nl.nn.adapterframework.core.IPipe)

Example 7 with ParameterException

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

the class CompareStringPipe method doPipe.

@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception extracting parameters", e);
        }
    }
    String operand1 = getParameterValue(pvl, OPERAND1);
    if (operand1 == null) {
        if (StringUtils.isNotEmpty(getSessionKey1())) {
            operand1 = (String) session.get(getSessionKey1());
        }
        if (operand1 == null) {
            operand1 = (String) input;
        }
    }
    String operand2 = getParameterValue(pvl, OPERAND2);
    if (operand2 == null) {
        if (StringUtils.isNotEmpty(getSessionKey2())) {
            operand2 = (String) session.get(getSessionKey2());
        }
        if (operand2 == null) {
            operand2 = (String) input;
        }
    }
    if (isXml()) {
        try {
            operand1 = XmlUtils.canonicalize(operand1);
            operand2 = XmlUtils.canonicalize(operand2);
        } catch (Exception e) {
            throw new PipeRunException(this, getLogPrefix(session) + " Exception on pretty printing input", e);
        }
    }
    String ip = getParameterValue(pvl, IGNOREPATTERNS);
    if (ip != null) {
        try {
            Node n = XmlUtils.buildNode(ip);
            if (n.getNodeName().equals("ignores")) {
                NodeList nList = n.getChildNodes();
                for (int i = 0; i <= nList.getLength() - 1; i++) {
                    Node cn = nList.item(i);
                    if (cn.getNodeName().equals("ignore")) {
                        NodeList cnList = cn.getChildNodes();
                        String after = null;
                        String before = null;
                        for (int j = 0; j <= cnList.getLength() - 1; j++) {
                            Node ccn = cnList.item(j);
                            if (ccn.getNodeName().equals("after")) {
                                after = ccn.getFirstChild().getNodeValue();
                            } else {
                                if (ccn.getNodeName().equals("before")) {
                                    before = ccn.getFirstChild().getNodeValue();
                                }
                            }
                        }
                        operand1 = ignoreBetween(operand1, after, before);
                        operand2 = ignoreBetween(operand2, after, before);
                    }
                }
            }
        } catch (Exception e) {
            throw new PipeRunException(this, getLogPrefix(session) + " Exception on ignoring parts of input", e);
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("operand1 [" + operand1 + "]");
        log.debug("operand2 [" + operand2 + "]");
    }
    int comparison = operand1.compareTo(operand2);
    if (comparison == 0)
        return new PipeRunResult(findForward(EQUALSFORWARD), input);
    else if (comparison < 0)
        return new PipeRunResult(findForward(LESSTHANFORWARD), input);
    else
        return new PipeRunResult(findForward(GREATERTHANFORWARD), input);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException)

Example 8 with ParameterException

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

the class ParameterResolutionContext method getValues.

/**
 * @param parameters
 * @return arraylist of <link>ParameterValue<link> objects
 */
public ParameterValueList getValues(ParameterList parameters) throws ParameterException {
    if (parameters == null)
        return null;
    ParameterValueList result = new ParameterValueList();
    for (Iterator<Parameter> parmIterator = parameters.iterator(); parmIterator.hasNext(); ) {
        Parameter parm = parmIterator.next();
        String parmSessionKey = parm.getSessionKey();
        if ("*".equals(parmSessionKey)) {
            String parmName = parm.getName();
            for (Iterator<String> keyIterator = session.keySet().iterator(); keyIterator.hasNext(); ) {
                String key = keyIterator.next();
                if (!PipeLineSessionBase.tsReceivedKey.equals(key)) {
                    if ((key.startsWith(parmName) || "*".equals(parmName))) {
                        Parameter newParm = new Parameter();
                        newParm.setName(key);
                        newParm.setSessionKey(key);
                        try {
                            newParm.configure();
                        } catch (ConfigurationException e) {
                            throw new ParameterException(e);
                        }
                        result.add(getValue(result, newParm));
                    }
                }
            }
        } else {
            result.add(getValue(result, parm));
        }
    }
    return result;
}
Also used : ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException)

Example 9 with ParameterException

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

the class ParameterValue method asCollection.

public Collection<Node> asCollection() throws ParameterException {
    if (value == null) {
        return null;
    }
    try {
        log.debug("rendering Parameter [" + getDefinition().getName() + "] value [" + value + "] as Collection");
        Element holder = XmlUtils.buildElement("<root>" + value + "</root>");
        return XmlUtils.getChildTags(holder, "*");
    } catch (DomBuilderException e) {
        throw new ParameterException("Parameter [" + getDefinition().getName() + "] cannot create Collection from [" + value + "]", e);
    }
}
Also used : Element(org.w3c.dom.Element) ParameterException(nl.nn.adapterframework.core.ParameterException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException)

Example 10 with ParameterException

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

the class TimeoutGuardPipe method doPipe.

public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException {
    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
        }
    }
    int timeout_work;
    String timeout_work_str = getParameterValue(pvl, "timeout");
    if (timeout_work_str == null) {
        timeout_work = getTimeout();
    } else {
        timeout_work = Integer.valueOf(timeout_work_str);
    }
    DoPipe doPipe = new DoPipe(input, session, Thread.currentThread().getName(), NDC.peek());
    ExecutorService service = Executors.newSingleThreadExecutor();
    Future future = service.submit(doPipe);
    String result = null;
    try {
        log.debug(getLogPrefix(session) + "setting timeout of [" + timeout_work + "] s");
        result = (String) future.get(timeout_work, TimeUnit.SECONDS);
    } catch (Exception e) {
        String msg;
        if (e instanceof TimeoutException) {
            String errorMsg = getLogPrefix(session) + "exceeds timeout of [" + timeout_work + "] s, interupting";
            future.cancel(true);
            msg = e.getClass().getName() + ": " + errorMsg;
        } else {
            msg = e.getClass().getName();
        }
        if (isThrowException()) {
            throw new PipeRunException(this, msg, e);
        } else {
            String msgString = msg + ": " + e.getMessage();
            log.error(msgString, e);
            String msgCdataString = "<![CDATA[" + msgString + "]]>";
            result = "<error>" + msgCdataString + "</error>";
        }
    } finally {
        service.shutdown();
    }
    return new PipeRunResult(getForward(), result);
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) PipeRunException(nl.nn.adapterframework.core.PipeRunException) TimeoutException(java.util.concurrent.TimeoutException) ParameterException(nl.nn.adapterframework.core.ParameterException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ParameterException (nl.nn.adapterframework.core.ParameterException)35 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)23 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)18 PipeRunException (nl.nn.adapterframework.core.PipeRunException)15 SenderException (nl.nn.adapterframework.core.SenderException)14 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)13 IOException (java.io.IOException)11 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)10 TimeOutException (nl.nn.adapterframework.core.TimeOutException)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ParameterList (nl.nn.adapterframework.parameters.ParameterList)6 JMSException (javax.jms.JMSException)5 PipeForward (nl.nn.adapterframework.core.PipeForward)5 Parameter (nl.nn.adapterframework.parameters.Parameter)5 ParameterValue (nl.nn.adapterframework.parameters.ParameterValue)5 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)5 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 INamedObject (nl.nn.adapterframework.core.INamedObject)4