use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class JdbcTransactionalStorage method storeMessage.
public String storeMessage(String messageId, String correlationId, Date receivedDate, String comments, String label, Serializable message) throws SenderException {
TransactionStatus txStatus = null;
if (txManager != null) {
txStatus = txManager.getTransaction(TXREQUIRED);
}
try {
Connection conn;
String result;
if (messageId == null) {
throw new SenderException("messageId cannot be null");
}
if (correlationId == null) {
throw new SenderException("correlationId cannot be null");
}
try {
conn = getConnection();
} catch (JdbcException e) {
throw new SenderException(e);
}
try {
Timestamp receivedDateTime = new Timestamp(receivedDate.getTime());
if (messageId.length() > MAXIDLEN) {
messageId = messageId.substring(0, MAXIDLEN);
}
if (correlationId.length() > MAXCIDLEN) {
correlationId = correlationId.substring(0, MAXCIDLEN);
}
if (comments != null && comments.length() > MAXCOMMENTLEN) {
comments = comments.substring(0, MAXCOMMENTLEN);
}
if (label != null && label.length() > MAXLABELLEN) {
label = label.substring(0, MAXLABELLEN);
}
result = storeMessageInDatabase(conn, messageId, correlationId, receivedDateTime, comments, label, message);
if (result == null) {
result = retrieveKey(conn, messageId, correlationId, receivedDateTime);
}
return result;
} catch (Exception e) {
throw new SenderException("cannot serialize message", e);
} finally {
try {
conn.close();
} catch (SQLException e) {
log.error("error closing JdbcConnection", e);
}
}
} finally {
if (txStatus != null) {
txManager.commit(txStatus);
}
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class JdbcTransactionalStorage method retrieveKey.
/**
* Retrieves the value of the primary key for the record just inserted.
*/
protected String retrieveKey(Connection conn, String messageId, String correlationId, Timestamp receivedDateTime) throws SQLException, SenderException {
PreparedStatement stmt = null;
try {
if (log.isDebugEnabled()) {
log.debug("preparing key retrieval statement [" + selectKeyQuery + "]");
}
stmt = conn.prepareStatement(selectKeyQuery);
if (!selectKeyQueryIsDbmsSupported) {
int paramPos = applyStandardParameters(stmt, true, false);
stmt.setString(paramPos++, messageId);
stmt.setString(paramPos++, correlationId);
stmt.setTimestamp(paramPos++, receivedDateTime);
}
ResultSet rs = null;
try {
rs = stmt.executeQuery();
if (!rs.next()) {
throw new SenderException("could not retrieve key for stored message [" + messageId + "]");
}
return rs.getString(1);
} finally {
if (rs != null) {
rs.close();
}
}
} finally {
if (stmt != null) {
stmt.close();
}
}
}
use of nl.nn.adapterframework.core.SenderException 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);
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class HttpSender method extractResult.
protected String extractResult(HttpResponseHandler responseHandler, ParameterResolutionContext prc) throws SenderException, IOException {
int statusCode = responseHandler.getStatusLine().getStatusCode();
boolean ok = false;
if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey())) {
ok = true;
} else {
if (statusCode == HttpServletResponse.SC_OK) {
ok = true;
} else {
if (isIgnoreRedirects()) {
if (statusCode == HttpServletResponse.SC_MOVED_PERMANENTLY || statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY || statusCode == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
ok = true;
}
}
}
}
if (!ok) {
throw new SenderException(getLogPrefix() + "httpstatus " + statusCode + ": " + responseHandler.getStatusLine().getReasonPhrase() + " body: " + getResponseBodyAsString(responseHandler));
}
HttpServletResponse response = null;
if (isStreamResultToServlet())
response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
if (response == null) {
if (StringUtils.isEmpty(getStreamResultToFileNameSessionKey())) {
if (isBase64()) {
return getResponseBodyAsBase64(responseHandler.getResponse());
} else if (StringUtils.isNotEmpty(getStoreResultAsStreamInSessionKey())) {
prc.getSession().put(getStoreResultAsStreamInSessionKey(), responseHandler.getResponse());
return "";
} else if (StringUtils.isNotEmpty(getStoreResultAsByteArrayInSessionKey())) {
prc.getSession().put(getStoreResultAsByteArrayInSessionKey(), Misc.streamToBytes(responseHandler.getResponse()));
return "";
} else if (isMultipartResponse()) {
return handleMultipartResponse(responseHandler, prc);
} else {
return getResponseBodyAsString(responseHandler);
}
} else {
String fileName = (String) prc.getSession().get(getStreamResultToFileNameSessionKey());
File file = new File(fileName);
Misc.streamToFile(responseHandler.getResponse(), file);
return fileName;
}
} else {
streamResponseBody(responseHandler, response);
return "";
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class HttpSender method getMethod.
protected HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList parameters, Map<String, String> headersParamsMap) throws SenderException {
try {
boolean queryParametersAppended = false;
StringBuffer path = new StringBuffer(uri.getPath());
if (uri.getQueryParams().size() > 0) {
path.append("?");
for (Iterator<NameValuePair> it = uri.getQueryParams().iterator(); it.hasNext(); ) {
NameValuePair pair = it.next();
path.append(pair.getName()).append("=").append(pair.getValue());
if (it.hasNext())
path.append("&");
}
queryParametersAppended = true;
}
if (getMethodType().equals("GET")) {
if (parameters != null) {
queryParametersAppended = appendParameters(queryParametersAppended, path, parameters, headersParamsMap);
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "path after appending of parameters [" + path.toString() + "]");
}
HttpGet method = new HttpGet(path + (parameters == null ? message : ""));
for (String param : headersParamsMap.keySet()) {
method.setHeader(param, headersParamsMap.get(param));
}
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "HttpSender constructed GET-method [" + method.getURI().getQuery() + "]");
return method;
} else if (getMethodType().equals("POST")) {
HttpPost method = new HttpPost(path.toString());
if (parameters != null) {
StringBuffer msg = new StringBuffer(message);
appendParameters(true, msg, parameters, headersParamsMap);
if (StringUtils.isEmpty(message) && msg.length() > 1) {
message = msg.substring(1);
} else {
message = msg.toString();
}
}
for (String param : headersParamsMap.keySet()) {
method.setHeader(param, headersParamsMap.get(param));
}
HttpEntity entity = new ByteArrayEntity(message.getBytes(getCharSet()));
method.setEntity(entity);
return method;
}
if (getMethodType().equals("PUT")) {
HttpPut method = new HttpPut(path.toString());
if (parameters != null) {
StringBuffer msg = new StringBuffer(message);
appendParameters(true, msg, parameters, headersParamsMap);
if (StringUtils.isEmpty(message) && msg.length() > 1) {
message = msg.substring(1);
} else {
message = msg.toString();
}
}
HttpEntity entity = new ByteArrayEntity(message.getBytes(getCharSet()));
method.setEntity(entity);
return method;
}
if (getMethodType().equals("DELETE")) {
HttpDelete method = new HttpDelete(path.toString());
return method;
}
if (getMethodType().equals("HEAD")) {
HttpHead method = new HttpHead(path.toString());
return method;
}
if (getMethodType().equals("REPORT")) {
Element element = XmlUtils.buildElement(message, true);
ReportInfo reportInfo = new ReportInfo(element, 0);
HttpReport method = new HttpReport(path.toString(), reportInfo);
if (StringUtils.isNotEmpty(getContentType())) {
method.setHeader("Content-Type", getContentType());
}
return method;
}
throw new SenderException("unknown methodtype [" + getMethodType() + "], must be either GET, PUT, POST, DELETE, HEAD or REPORT");
} catch (Exception e) {
// Catch all exceptions and throw them as SenderException
throw new SenderException(e);
}
}
Aggregations