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);
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations