Search in sources :

Example 41 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class WebServiceNtlmSender method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    String result = null;
    HttpPost httpPost = new HttpPost(getUrl());
    try {
        StringEntity se = new StringEntity(message.asString());
        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 (SocketTimeoutException | ConnectTimeoutException e) {
        throw new TimeoutException(e);
    } catch (Exception e) {
        throw new SenderException(e);
    } finally {
        httpPost.releaseConnection();
    }
    return new Message(result);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) Message(nl.nn.adapterframework.stream.Message) Type1Message(jcifs.ntlmssp.Type1Message) Type2Message(jcifs.ntlmssp.Type2Message) Type3Message(jcifs.ntlmssp.Type3Message) HttpResponse(org.apache.http.HttpResponse) NTLMEngineException(org.apache.http.impl.auth.NTLMEngineException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SenderException(nl.nn.adapterframework.core.SenderException) StatusLine(org.apache.http.StatusLine) StringEntity(org.apache.http.entity.StringEntity) SocketTimeoutException(java.net.SocketTimeoutException) SenderException(nl.nn.adapterframework.core.SenderException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) TimeoutException(nl.nn.adapterframework.core.TimeoutException)

Example 42 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class HttpSenderBase method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    ParameterValueList pvl = null;
    try {
        if (paramList != null) {
            pvl = paramList.getValues(message, session);
        }
    } catch (ParameterException e) {
        throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
    }
    URI targetUri;
    final HttpRequestBase httpRequestBase;
    try {
        if (urlParameter != null) {
            String url = pvl.get(getUrlParam()).asStringValue();
            targetUri = getURI(url);
        } else {
            targetUri = staticUri;
        }
        // Resolve HeaderParameters
        Map<String, String> headersParamsMap = new HashMap<>();
        if (headersParams != null && pvl != null) {
            log.debug("appending header parameters " + headersParams);
            StringTokenizer st = new StringTokenizer(getHeadersParams(), ",");
            while (st.hasMoreElements()) {
                String paramName = st.nextToken();
                ParameterValue paramValue = pvl.get(paramName);
                if (paramValue != null)
                    headersParamsMap.put(paramName, paramValue.asStringValue(null));
            }
        }
        httpRequestBase = getMethod(targetUri, message, pvl, session);
        if (httpRequestBase == null)
            throw new MethodNotSupportedException("could not find implementation for method [" + getHttpMethod() + "]");
        // Set all headers
        if (session != null && APPEND_MESSAGEID_HEADER && StringUtils.isNotEmpty(session.getMessageId())) {
            httpRequestBase.setHeader("Message-Id", session.getMessageId());
        }
        for (String param : headersParamsMap.keySet()) {
            httpRequestBase.setHeader(param, headersParamsMap.get(param));
        }
        preAuthenticate();
        log.info(getLogPrefix() + "configured httpclient for host [" + targetUri.getHost() + "]");
    } catch (Exception e) {
        throw new SenderException(e);
    }
    Message result = null;
    int statusCode = -1;
    int count = getMaxExecuteRetries();
    String msg = null;
    HttpHost targetHost = new HttpHost(targetUri.getHost(), getPort(targetUri), targetUri.getScheme());
    while (count-- >= 0 && statusCode == -1) {
        TimeoutGuard tg = new TimeoutGuard(1 + getTimeout() / 1000, getName()) {

            @Override
            protected void abort() {
                httpRequestBase.abort();
            }
        };
        try {
            log.debug(getLogPrefix() + "executing method [" + httpRequestBase.getRequestLine() + "]");
            HttpResponse httpResponse = getHttpClient().execute(targetHost, httpRequestBase, httpClientContext);
            log.debug(getLogPrefix() + "executed method");
            HttpResponseHandler responseHandler = new HttpResponseHandler(httpResponse);
            StatusLine statusline = httpResponse.getStatusLine();
            statusCode = statusline.getStatusCode();
            if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey()) && session != null) {
                session.put(getResultStatusCodeSessionKey(), Integer.toString(statusCode));
            }
            // Only give warnings for 4xx (client errors) and 5xx (server errors)
            if (statusCode >= 400 && statusCode < 600) {
                log.warn(getLogPrefix() + "status [" + statusline.toString() + "]");
            } else {
                log.debug(getLogPrefix() + "status [" + statusCode + "]");
            }
            result = extractResult(responseHandler, session);
            log.debug(getLogPrefix() + "retrieved result [" + result + "]");
        } catch (ClientProtocolException e) {
            StringBuilder msgBuilder = new StringBuilder(getLogPrefix() + "httpException with");
            if (e.getMessage() != null) {
                msg = e.getMessage();
                msgBuilder.append(" message [" + msg + "]");
            }
            Throwable throwable = e.getCause();
            if (throwable != null) {
                msgBuilder.append(" cause [" + throwable.toString() + "]");
            }
            msgBuilder.append(" executeRetries left [" + count + "]");
            log.warn(msgBuilder.toString());
        } catch (IOException e) {
            httpRequestBase.abort();
            if (e instanceof SocketTimeoutException) {
                throw new TimeoutException(e);
            }
            throw new SenderException(e);
        } finally {
            if (tg.cancel()) {
                throw new TimeoutException(getLogPrefix() + "timeout of [" + getTimeout() + "] ms exceeded");
            }
        }
    }
    if (statusCode == -1) {
        if (msg != null && 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() && !result.isEmpty()) {
        String resultString;
        try {
            resultString = result.asString();
        } catch (IOException e) {
            throw new SenderException("error reading http response as String", e);
        }
        String xhtml = XmlUtils.toXhtml(resultString);
        if (transformerPool != null && xhtml != null) {
            log.debug(getLogPrefix() + " transforming result [" + xhtml + "]");
            try {
                xhtml = transformerPool.transform(Message.asSource(xhtml));
            } catch (Exception e) {
                throw new SenderException("Exception on transforming input", e);
            }
        }
        result = Message.asMessage(xhtml);
    }
    return result;
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Message(nl.nn.adapterframework.stream.Message) HashMap(java.util.HashMap) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHost(org.apache.http.HttpHost) ParameterException(nl.nn.adapterframework.core.ParameterException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) URISyntaxException(java.net.URISyntaxException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) 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) ParameterException(nl.nn.adapterframework.core.ParameterException) StatusLine(org.apache.http.StatusLine) StringTokenizer(java.util.StringTokenizer) SocketTimeoutException(java.net.SocketTimeoutException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 43 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class WebServiceListener method processRequest.

@Override
public Message processRequest(String correlationId, Message message, Map<String, Object> requestContext) throws ListenerException {
    if (!attachmentSessionKeysList.isEmpty()) {
        XmlBuilder xmlMultipart = new XmlBuilder("parts");
        for (String attachmentSessionKey : attachmentSessionKeysList) {
            // <parts><part type=\"file\" name=\"document.pdf\" sessionKey=\"part_file\" size=\"12345\" mimeType=\"application/octet-stream\"/></parts>
            XmlBuilder part = new XmlBuilder("part");
            part.addAttribute("name", attachmentSessionKey);
            part.addAttribute("sessionKey", attachmentSessionKey);
            part.addAttribute("mimeType", "application/octet-stream");
            xmlMultipart.addSubElement(part);
        }
        requestContext.put(getMultipartXmlSessionKey(), xmlMultipart.toXML());
    }
    if (isSoap()) {
        try {
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "received SOAPMSG [" + message + "]");
            Message request = soapWrapper.getBody(message);
            Message result = super.processRequest(correlationId, request, requestContext);
            String soapNamespace = SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE;
            String soapProtocol = (String) requestContext.get("soapProtocol");
            if (SOAPConstants.SOAP_1_2_PROTOCOL.equals(soapProtocol)) {
                soapNamespace = SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE;
            }
            Message reply = soapWrapper.putInEnvelope(result, null, null, null, null, soapNamespace, null, false);
            if (log.isDebugEnabled())
                log.debug(getLogPrefix() + "replied SOAPMSG [" + reply + "]");
            return reply;
        } catch (Exception e) {
            throw new ListenerException(e);
        }
    } else
        return super.processRequest(correlationId, message, requestContext);
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) Message(nl.nn.adapterframework.stream.Message) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ListenerException(nl.nn.adapterframework.core.ListenerException)

Example 44 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class WebServiceSender method getMethod.

@Override
protected HttpRequestBase getMethod(URI uri, Message message, ParameterValueList parameters, PipeLineSession session) throws SenderException {
    String serviceNamespaceURI;
    if (serviceNamespaceURIParameter != null) {
        serviceNamespaceURI = parameters.get(getServiceNamespaceParam()).asStringValue(getServiceNamespace());
    } else {
        serviceNamespaceURI = getServiceNamespace();
    }
    String soapActionURI;
    if (soapActionParameter != null) {
        soapActionURI = parameters.get(getSoapActionParam()).asStringValue(getSoapAction());
    } else {
        soapActionURI = getSoapAction();
    }
    Message soapmsg;
    try {
        if (isSoap()) {
            soapmsg = soapWrapper.putInEnvelope(message, getEncodingStyle(), serviceNamespaceURI, null, getNamespaceDefs());
        } else {
            soapmsg = message;
        }
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "error reading message", e);
    }
    if (wsscf != null) {
        soapmsg = soapWrapper.signMessage(soapmsg, wsscf.getUsername(), wsscf.getPassword(), isWssPasswordDigest());
    }
    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "SOAPMSG [" + soapmsg + "]");
    HttpRequestBase method = super.getMethod(uri, soapmsg, parameters, session);
    log.debug(getLogPrefix() + "setting SOAPAction header [" + soapActionURI + "]");
    method.setHeader("SOAPAction", soapActionURI);
    return method;
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) Message(nl.nn.adapterframework.stream.Message) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 45 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class JdbcListener method extractRawMessage.

protected M extractRawMessage(ResultSet rs) throws JdbcException {
    try {
        M result;
        String key = rs.getString(getKeyField());
        if (StringUtils.isNotEmpty(getMessageField())) {
            Message message;
            switch(getMessageFieldType()) {
                case CLOB:
                    message = new Message(JdbcUtil.getClobAsString(getDbmsSupport(), rs, getMessageField(), false));
                    break;
                case BLOB:
                    if (isBlobSmartGet() || StringUtils.isNotEmpty(getBlobCharset())) {
                        message = new Message(JdbcUtil.getBlobAsString(getDbmsSupport(), rs, getMessageField(), getBlobCharset(), isBlobsCompressed(), isBlobSmartGet(), false));
                    } else {
                        try (InputStream blobStream = JdbcUtil.getBlobInputStream(getDbmsSupport(), rs, getMessageField(), isBlobsCompressed())) {
                            message = new Message(blobStream);
                            message.preserve();
                        }
                    }
                    break;
                case STRING:
                    message = new Message(rs.getString(getMessageField()));
                    break;
                default:
                    throw new IllegalArgumentException("Illegal messageFieldType [" + getMessageFieldType() + "]");
            }
            // log.debug("building wrapper for key ["+key+"], message ["+message+"]");
            MessageWrapper<?> mw = new MessageWrapper<Object>();
            mw.setId(key);
            mw.setMessage(message);
            result = (M) mw;
        } else {
            result = (M) key;
        }
        return result;
    } catch (SQLException | IOException e) {
        throw new JdbcException(e);
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) MessageWrapper(nl.nn.adapterframework.receivers.MessageWrapper) IMessageWrapper(nl.nn.adapterframework.core.IMessageWrapper) IOException(java.io.IOException)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19