Search in sources :

Example 36 with SenderException

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

the class HttpSender method handleMultipartResponse.

public static String handleMultipartResponse(String contentType, InputStream inputStream, ParameterResolutionContext prc, HttpResponseHandler httpHandler) throws IOException, SenderException {
    String result = null;
    try {
        InputStreamDataSource dataSource = new InputStreamDataSource(httpHandler.getContentType(), inputStream);
        MimeMultipart mimeMultipart = new MimeMultipart(dataSource);
        for (int i = 0; i < mimeMultipart.getCount(); i++) {
            BodyPart bodyPart = mimeMultipart.getBodyPart(i);
            boolean lastPart = mimeMultipart.getCount() == i + 1;
            if (i == 0) {
                String charset = ContentType.parse(bodyPart.getContentType()).getCharset().name();
                InputStream bodyPartInputStream = bodyPart.getInputStream();
                result = Misc.streamToString(bodyPartInputStream, charset);
                if (lastPart) {
                    bodyPartInputStream.close();
                }
            } else {
                // When the last stream is read the
                // httpMethod.releaseConnection() can be called, hence pass
                // httpMethod to ReleaseConnectionAfterReadInputStream.
                prc.getSession().put("multipart" + i, new ReleaseConnectionAfterReadInputStream(lastPart ? httpHandler : null, bodyPart.getInputStream()));
            }
        }
    } catch (MessagingException e) {
        throw new SenderException("Could not read mime multipart response", e);
    }
    return result;
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) BodyPart(javax.mail.BodyPart) MimeMultipart(javax.mail.internet.MimeMultipart) MessagingException(javax.mail.MessagingException) InputStream(java.io.InputStream) SenderException(nl.nn.adapterframework.core.SenderException)

Example 37 with SenderException

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

the class HttpSender method getPostMethodWithParamsInBody.

protected HttpPost getPostMethodWithParamsInBody(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
    try {
        HttpPost hmethod = new HttpPost(uri.build());
        if (!isMultipart() && StringUtils.isEmpty(getMultipartXmlSessionKey())) {
            List<NameValuePair> Parameters = new ArrayList<NameValuePair>();
            if (StringUtils.isNotEmpty(getInputMessageParam())) {
                Parameters.add(new BasicNameValuePair(getInputMessageParam(), message));
                log.debug(getLogPrefix() + "appended parameter [" + getInputMessageParam() + "] with value [" + message + "]");
            }
            if (parameters != null) {
                for (int i = 0; i < parameters.size(); i++) {
                    ParameterValue pv = parameters.getParameterValue(i);
                    String name = pv.getDefinition().getName();
                    String value = pv.asStringValue("");
                    if (headersParamsMap.keySet().contains(name)) {
                        hmethod.addHeader(name, value);
                        if (log.isDebugEnabled())
                            log.debug(getLogPrefix() + "appended header [" + name + "] with value [" + value + "]");
                    } else {
                        Parameters.add(new BasicNameValuePair(name, value));
                        if (log.isDebugEnabled())
                            log.debug(getLogPrefix() + "appended parameter [" + name + "] with value [" + value + "]");
                    }
                }
            }
            try {
                hmethod.setEntity(new UrlEncodedFormEntity(Parameters));
            } catch (UnsupportedEncodingException e) {
                throw new SenderException(getLogPrefix() + "unsupported encoding for one or more post parameters");
            }
        } else {
            HttpEntity requestEntity = createMultiPartEntity(message, parameters, session);
            hmethod.setEntity(requestEntity);
        }
        return hmethod;
    } catch (URISyntaxException e) {
        throw new SenderException(getLogPrefix() + "cannot find path from url [" + getUrl() + "]", e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) URISyntaxException(java.net.URISyntaxException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) SenderException(nl.nn.adapterframework.core.SenderException)

Example 38 with SenderException

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

the class WebServiceNtlmSender method sendMessage.

public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    String result = null;
    HttpPost httpPost = new HttpPost(getUrl());
    try {
        StringEntity se = new StringEntity(message);
        httpPost.setEntity(se);
        if (StringUtils.isNotEmpty(getContentType())) {
            log.debug(getLogPrefix() + "setting Content-Type header [" + getContentType() + "]");
            httpPost.addHeader("Content-Type", getContentType());
        }
        if (StringUtils.isNotEmpty(getSoapAction())) {
            log.debug(getLogPrefix() + "setting SOAPAction header [" + getSoapAction() + "]");
            httpPost.addHeader("SOAPAction", getSoapAction());
        }
        log.debug(getLogPrefix() + "executing method");
        HttpResponse httpresponse = httpClient.execute(httpPost);
        log.debug(getLogPrefix() + "executed method");
        StatusLine statusLine = httpresponse.getStatusLine();
        if (statusLine == null) {
            throw new SenderException(getLogPrefix() + "no statusline found");
        } else {
            int statusCode = statusLine.getStatusCode();
            String statusMessage = statusLine.getReasonPhrase();
            if (statusCode == HttpServletResponse.SC_OK) {
                log.debug(getLogPrefix() + "status code [" + statusCode + "] message [" + statusMessage + "]");
            } else {
                throw new SenderException(getLogPrefix() + "status code [" + statusCode + "] message [" + statusMessage + "]");
            }
        }
        HttpEntity httpEntity = httpresponse.getEntity();
        if (httpEntity == null) {
            log.warn(getLogPrefix() + "no response found");
        } else {
            log.debug(getLogPrefix() + "response content length [" + httpEntity.getContentLength() + "]");
            result = EntityUtils.toString(httpEntity);
            log.debug(getLogPrefix() + "retrieved result [" + result + "]");
        }
    } catch (Exception e) {
        if (e instanceof SocketTimeoutException) {
            throw new TimeOutException(e);
        }
        if (e instanceof ConnectTimeoutException) {
            throw new TimeOutException(e);
        }
        throw new SenderException(e);
    } finally {
        httpPost.releaseConnection();
    }
    return result;
}
Also used : StatusLine(org.apache.http.StatusLine) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SocketTimeoutException(java.net.SocketTimeoutException) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) SenderException(nl.nn.adapterframework.core.SenderException) NTLMEngineException(org.apache.http.impl.auth.NTLMEngineException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 39 with SenderException

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

the class BisJmsSender method sendMessage.

public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    String messageHeader;
    try {
        messageHeader = bisUtils.prepareMessageHeader(null, isMessageHeaderInSoapBody(), (String) prc.getSession().get(getConversationIdSessionKey()), (String) prc.getSession().get(getExternalRefToMessageIdSessionKey()));
    } catch (Exception e) {
        throw new SenderException(e);
    }
    String payload;
    try {
        payload = bisUtils.prepareReply(message, isMessageHeaderInSoapBody() ? messageHeader : null, null, false);
        if (StringUtils.isNotEmpty(getRequestNamespace())) {
            payload = XmlUtils.addRootNamespace(payload, getRequestNamespace());
        }
    } catch (Exception e) {
        throw new SenderException(e);
    }
    String replyMessage = super.sendMessage(correlationID, payload, prc, isMessageHeaderInSoapBody() ? null : messageHeader);
    if (isSynchronous()) {
        String bisError;
        String bisErrorList;
        try {
            bisError = bisErrorTp.transform(replyMessage, null, true);
            bisErrorList = bisErrorListTp.transform(replyMessage, null, true);
        } catch (Exception e) {
            throw new SenderException(e);
        }
        if (Boolean.valueOf(bisError).booleanValue()) {
            log.debug("put in session [" + getErrorListSessionKey() + "] [" + bisErrorList + "]");
            prc.getSession().put(getErrorListSessionKey(), bisErrorList);
            throw new SenderException("bisErrorXPath [" + (isResultInPayload() ? bisUtils.getBisErrorXPath() : bisUtils.getOldBisErrorXPath()) + "] returns true");
        }
        try {
            replyMessage = responseTp.transform(replyMessage, null, true);
            if (isRemoveResponseNamespaces()) {
                replyMessage = XmlUtils.removeNamespaces(replyMessage);
            }
            if (isResultInPayload()) {
                Element soapBodyElement = XmlUtils.buildElement(replyMessage, true);
                Element resultElement = XmlUtils.getFirstChildTag(soapBodyElement, "Result");
                if (resultElement != null) {
                    soapBodyElement.removeChild(resultElement);
                }
                replyMessage = XmlUtils.nodeToString(soapBodyElement);
            }
            return replyMessage;
        } catch (Exception e) {
            throw new SenderException(e);
        }
    } else {
        return replyMessage;
    }
}
Also used : Element(org.w3c.dom.Element) SenderException(nl.nn.adapterframework.core.SenderException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 40 with SenderException

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

the class HttpSenderBase method open.

public void open() throws SenderException {
    connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(getMaxConnections());
    log.debug(getLogPrefix() + "set up connectionManager, inactivity checking [" + connectionManager.getValidateAfterInactivity() + "]");
    boolean staleChecking = (connectionManager.getValidateAfterInactivity() >= 0);
    if (staleChecking != isStaleChecking()) {
        log.info(getLogPrefix() + "set up connectionManager, setting stale checking [" + isStaleChecking() + "]");
        connectionManager.setValidateAfterInactivity(getStaleTimeout());
    }
    httpClientBuilder.useSystemProperties();
    httpClientBuilder.disableAuthCaching();
    httpClientBuilder.setConnectionManager(connectionManager);
    if (transformerPool != null) {
        try {
            transformerPool.open();
        } catch (Exception e) {
            throw new SenderException(getLogPrefix() + "cannot start TransformerPool", e);
        }
    }
}
Also used : SenderException(nl.nn.adapterframework.core.SenderException) 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) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

SenderException (nl.nn.adapterframework.core.SenderException)130 TimeOutException (nl.nn.adapterframework.core.TimeOutException)41 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)36 SQLException (java.sql.SQLException)25 HashMap (java.util.HashMap)21 ParameterException (nl.nn.adapterframework.core.ParameterException)21 PreparedStatement (java.sql.PreparedStatement)20 Map (java.util.Map)20 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)18 Iterator (java.util.Iterator)17 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)16 InputStream (java.io.InputStream)15 ResultSet (java.sql.ResultSet)13 Element (org.w3c.dom.Element)13 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)12 Date (java.util.Date)10 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)10 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9