Search in sources :

Example 21 with ParameterException

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

the class XslErrorMessageFormatter method format.

@Override
public String format(String message, Throwable t, INamedObject location, String originalMessage, String messageId, long receivedTime) {
    String result = super.format(message, t, location, originalMessage, messageId, receivedTime);
    if (StringUtils.isNotEmpty(getStyleSheet()) || StringUtils.isNotEmpty(getXpathExpression())) {
        try {
            Transformer errorTransformer;
            if (StringUtils.isNotEmpty(getStyleSheet())) {
                errorTransformer = XmlUtils.createTransformer(ClassUtils.getResourceURL(this, styleSheet));
            } else {
                String xpath = getXpathExpression();
                // if (StringUtils.isEmpty(xpath)) {
                // xpath="/errorMessage/@message";
                // }
                errorTransformer = XmlUtils.createTransformer(XmlUtils.createXPathEvaluatorSource(xpath));
            }
            ParameterList params = getParameterList();
            if (params != null) {
                try {
                    params.configure();
                } catch (ConfigurationException e) {
                    log.error("exception while configuring parameters", e);
                }
                ParameterResolutionContext prc = new ParameterResolutionContext(message, new PipeLineSessionBase());
                Map<String, Object> parametervalues = null;
                try {
                    parametervalues = prc.getValueMap(params);
                } catch (ParameterException e) {
                    log.error("got exception extracting parameters", e);
                }
                XmlUtils.setTransformerParameters(errorTransformer, parametervalues);
            }
            result = XmlUtils.transformXml(errorTransformer, result);
        } catch (IOException e) {
            log.error(" cannot retrieve [" + styleSheet + "]", e);
        } catch (javax.xml.transform.TransformerConfigurationException te) {
            log.error("got error creating transformer from file [" + styleSheet + "]", te);
        } catch (Exception tfe) {
            log.error("could not transform [" + result + "] using stylesheet [" + styleSheet + "]", tfe);
        }
    } else
        log.warn("no stylesheet defined for XslErrorMessageFormatter");
    return result;
}
Also used : Transformer(javax.xml.transform.Transformer) IOException(java.io.IOException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterException(nl.nn.adapterframework.core.ParameterException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ParameterList(nl.nn.adapterframework.parameters.ParameterList) INamedObject(nl.nn.adapterframework.core.INamedObject) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Example 22 with ParameterException

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

the class HttpSenderBase method sendMessageWithTimeoutGuarded.

@Override
public String sendMessageWithTimeoutGuarded(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    ParameterValueList pvl = null;
    try {
        if (prc != null && paramList != null) {
            pvl = prc.getValues(paramList);
        }
    } catch (ParameterException e) {
        throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
    }
    URIBuilder uri;
    HttpRequestBase httpRequestBase;
    try {
        if (urlParameter != null) {
            String url = (String) pvl.getParameterValue(getUrlParam()).getValue();
            uri = getURI(url);
        } else {
            uri = staticUri;
        }
        httpTarget = new HttpHost(uri.getHost(), getPort(uri), uri.getScheme());
        Map<String, String> headersParamsMap = new HashMap<String, String>();
        if (headersParams != null) {
            StringTokenizer st = new StringTokenizer(headersParams, ",");
            while (st.hasMoreElements()) {
                headersParamsMap.put(st.nextToken(), null);
            }
        }
        if (isEncodeMessages()) {
            message = URLEncoder.encode(message, getCharSet());
        }
        httpRequestBase = getMethod(uri, message, pvl, headersParamsMap, prc.getSession());
        if (httpRequestBase == null)
            throw new MethodNotSupportedException("could not find implementation for method [" + getMethodType() + "]");
        if (!"POST".equals(getMethodType()) && !"PUT".equals(getMethodType()) && !"REPORT".equals(getMethodType())) {
            httpClientBuilder.setRedirectStrategy(new DefaultRedirectStrategy() {

                @Override
                protected boolean isRedirectable(String method) {
                    return true;
                }
            });
        }
        if (StringUtils.isNotEmpty(getContentType())) {
            httpRequestBase.setHeader("Content-Type", getContentType());
        }
        if (credentials != null && !StringUtils.isEmpty(credentials.getUsername())) {
            AuthCache authCache = httpClientContext.getAuthCache();
            if (authCache == null)
                authCache = new BasicAuthCache();
            authCache.put(httpTarget, new BasicScheme());
            httpClientContext.setAuthCache(authCache);
        }
        log.info(getLogPrefix() + "configured httpclient for host [" + uri.getHost() + "]");
    } catch (Exception e) {
        throw new SenderException(e);
    }
    CloseableHttpClient httpClient = httpClientBuilder.build();
    String result = null;
    int statusCode = -1;
    int count = getMaxExecuteRetries();
    String msg = null;
    while (count-- >= 0 && statusCode == -1) {
        try {
            log.debug(getLogPrefix() + "executing method [" + httpRequestBase.getRequestLine() + "]");
            HttpResponse httpResponse = httpClient.execute(httpTarget, httpRequestBase, httpClientContext);
            log.debug(getLogPrefix() + "executed method");
            HttpResponseHandler responseHandler = new HttpResponseHandler(httpResponse);
            StatusLine statusline = httpResponse.getStatusLine();
            statusCode = statusline.getStatusCode();
            if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey()) && prc != null) {
                prc.getSession().put(getResultStatusCodeSessionKey(), Integer.toString(statusCode));
            }
            if (statusCode != HttpServletResponse.SC_OK) {
                log.warn(getLogPrefix() + "status [" + statusline.toString() + "]");
            } else {
                log.debug(getLogPrefix() + "status [" + statusCode + "]");
            }
            result = extractResult(responseHandler, prc);
            log.debug(getLogPrefix() + "retrieved result [" + result + "]");
        } catch (ClientProtocolException e) {
            httpRequestBase.abort();
            Throwable throwable = e.getCause();
            String cause = null;
            if (throwable != null) {
                cause = throwable.toString();
            }
            msg = e.getMessage();
            log.warn(getLogPrefix() + "httpException with message [" + msg + "] and cause [" + cause + "], executeRetries left [" + count + "]");
        } catch (IOException e) {
            httpRequestBase.abort();
            if (e instanceof SocketTimeoutException) {
                throw new TimeOutException(e);
            }
            throw new SenderException(e);
        } finally {
        // By forcing the use of the HttpResponseHandler the resultStream
        // will automatically be closed when it has been read.
        // See HttpResponseHandler and ReleaseConnectionAfterReadInputStream.
        // We cannot close the connection as the response might be kept
        // in a sessionKey for later use in the pipeline.
        // 
        // IMPORTANT: It is possible that poorly written implementations
        // wont read or close the response.
        // This will cause the connection to become stale..
        }
    }
    if (statusCode == -1) {
        if (StringUtils.contains(msg.toUpperCase(), "TIMEOUTEXCEPTION")) {
            // java.net.SocketTimeoutException: Read timed out
            throw new TimeOutException("Failed to recover from timeout exception");
        }
        throw new SenderException("Failed to recover from exception");
    }
    if (isXhtml() && StringUtils.isNotEmpty(result)) {
        result = XmlUtils.skipDocTypeDeclaration(result.trim());
        if (result.startsWith("<html>") || result.startsWith("<html ")) {
            CleanerProperties props = new CleanerProperties();
            HtmlCleaner cleaner = new HtmlCleaner(props);
            TagNode tagNode = cleaner.clean(result);
            result = new SimpleXmlSerializer(props).getXmlAsString(tagNode);
            if (transformerPool != null) {
                log.debug(getLogPrefix() + " transforming result [" + result + "]");
                ParameterResolutionContext prc_xslt = new ParameterResolutionContext(result, null, true, true);
                try {
                    result = transformerPool.transform(prc_xslt.getInputSource(), null);
                } catch (Exception e) {
                    throw new SenderException("Exception on transforming input", e);
                }
            }
        }
    }
    return result;
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HashMap(java.util.HashMap) SimpleXmlSerializer(org.htmlcleaner.SimpleXmlSerializer) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHost(org.apache.http.HttpHost) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) ParameterException(nl.nn.adapterframework.core.ParameterException) CleanerProperties(org.htmlcleaner.CleanerProperties) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) BasicScheme(org.apache.http.impl.auth.BasicScheme) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) SenderException(nl.nn.adapterframework.core.SenderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) SocketTimeoutException(java.net.SocketTimeoutException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ParameterException(nl.nn.adapterframework.core.ParameterException) HtmlCleaner(org.htmlcleaner.HtmlCleaner) URIBuilder(org.apache.http.client.utils.URIBuilder) StatusLine(org.apache.http.StatusLine) StringTokenizer(java.util.StringTokenizer) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SocketTimeoutException(java.net.SocketTimeoutException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) SenderException(nl.nn.adapterframework.core.SenderException) TagNode(org.htmlcleaner.TagNode)

Example 23 with ParameterException

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

the class MessageStoreSender method sendMessage.

@Override
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    if (sessionKeys != null) {
        List<String> list = new ArrayList<String>();
        list.add(StringEscapeUtils.escapeCsv(message));
        StringTokenizer tokenizer = new StringTokenizer(sessionKeys, ",");
        while (tokenizer.hasMoreElements()) {
            String sessionKey = (String) tokenizer.nextElement();
            list.add(StringEscapeUtils.escapeCsv((String) prc.getSession().get(sessionKey)));
        }
        StrBuilder sb = new StrBuilder();
        sb.appendWithSeparators(list, ",");
        message = sb.toString();
    }
    String messageId = prc.getSession().getMessageId();
    if (prc != null && paramList != null && paramList.findParameter("messageId") != null) {
        try {
            messageId = (String) prc.getValueMap(paramList).get("messageId");
        } catch (ParameterException e) {
            throw new SenderException("Could not resolve parameter messageId", e);
        }
    }
    return storeMessage(messageId, correlationID, new Date(), null, null, message);
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException) StrBuilder(org.apache.commons.lang.text.StrBuilder) Date(java.util.Date)

Example 24 with ParameterException

use of nl.nn.adapterframework.core.ParameterException 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 25 with ParameterException

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

the class ZipWriterSender method sendMessage.

public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    ParameterValueList pvl;
    try {
        pvl = prc.getValues(paramList);
    } catch (ParameterException e) {
        throw new SenderException("cannot determine filename and/or contents of zip entry", e);
    }
    IPipeLineSession session = prc.getSession();
    ZipWriter sessionData = ZipWriter.getZipWriter(session, getZipWriterHandle());
    if (sessionData == null) {
        throw new SenderException("zipWriterHandle in session key [" + getZipWriterHandle() + "] is not open");
    }
    String filename = filenameParameter == null ? message : (String) pvl.getParameterValue(PARAMETER_FILENAME).getValue();
    try {
        if (contentsParameter == null) {
            if (message != null) {
                sessionData.writeEntry(filename, message, isCloseInputstreamOnExit(), getCharset());
            }
        } else {
            Object paramValue = pvl.getParameterValue(PARAMETER_CONTENTS).getValue();
            sessionData.writeEntry(filename, paramValue, isCloseInputstreamOnExit(), getCharset());
        }
        return message;
    } catch (UnsupportedEncodingException e) {
        throw new SenderException(getLogPrefix() + "cannot encode zip entry", e);
    } catch (CompressionException e) {
        throw new SenderException(getLogPrefix() + "cannot store zip entry", e);
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "cannot store zip entry", e);
    }
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession)

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