use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class XfbSender method sendMessage.
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
File file = new File(message);
if (getCopy()) {
File fromFile = file;
String name = fromFile.getName();
if (name.startsWith(getCopyPrefix())) {
name = name.substring(getCopyPrefix().length());
} else {
name = getCopyPrefix() + name;
}
File toFile = new File(fromFile.getParentFile(), name);
file = toFile;
if (toFile.exists()) {
throw new SenderException("File " + toFile.getAbsolutePath() + " already exist");
}
if (!FileUtils.copyFile(fromFile, toFile, false)) {
throw new SenderException("Could not copy file");
}
}
String command = getScript() + " ft=" + getFt() + " flow=" + getFlow() + " appli=" + getAppli();
if (StringUtils.isNotEmpty(getNoname())) {
command = command + " noname=" + getNoname();
}
command = command + " filename=" + file.getAbsolutePath();
String output = ProcessUtil.executeCommand(command);
return output;
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class FtpSender method sendMessage.
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
try {
IPipeLineSession session = null;
if (prc != null) {
session = prc.getSession();
}
ftpSession.put(paramList, session, message, remoteDirectory, remoteFilenamePattern, true);
} catch (SenderException e) {
throw e;
} catch (Exception e) {
throw new SenderException("Error during ftp-ing " + message, e);
}
return message;
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class MqttSender method sendMessage.
public String sendMessage(String correlationID, String message, ParameterResolutionContext prc, String soapHeader) throws SenderException, TimeOutException {
try {
if (!client.isConnected()) {
super.open();
}
log.debug(message);
MqttMessage MqttMessage = new MqttMessage();
MqttMessage.setPayload(message.getBytes());
MqttMessage.setQos(getQos());
client.publish(getTopic(), MqttMessage);
} catch (Exception e) {
throw new SenderException(e);
}
return message;
}
use of nl.nn.adapterframework.core.SenderException 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;
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class HttpSenderBase method appendParameters.
protected boolean appendParameters(boolean parametersAppended, StringBuffer path, ParameterValueList parameters, Map<String, String> headersParamsMap) throws SenderException {
if (parameters != null) {
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "appending [" + parameters.size() + "] parameters");
}
for (int i = 0; i < parameters.size(); i++) {
if (parametersToSkip.contains(paramList.get(i))) {
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "skipping [" + paramList.get(i) + "]");
continue;
}
ParameterValue pv = parameters.getParameterValue(i);
if (headersParamsMap.keySet().contains(pv.getDefinition().getName())) {
headersParamsMap.put(pv.getDefinition().getName(), pv.asStringValue(""));
} else {
try {
if (parametersAppended) {
path.append("&");
} else {
path.append("?");
parametersAppended = true;
}
String parameterToAppend = pv.getDefinition().getName() + "=" + URLEncoder.encode(pv.asStringValue(""), getCharSet());
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "appending parameter [" + parameterToAppend + "]");
path.append(parameterToAppend);
} catch (UnsupportedEncodingException e) {
throw new SenderException(getLogPrefix() + "[" + getCharSet() + "] encoding error. Failed to add parameter [" + pv.getDefinition().getName() + "]");
}
}
}
return parametersAppended;
}
Aggregations