Search in sources :

Example 86 with SenderException

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);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) SenderException(nl.nn.adapterframework.core.SenderException) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) ListenerException(nl.nn.adapterframework.core.ListenerException) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 87 with SenderException

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();
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SenderException(nl.nn.adapterframework.core.SenderException)

Example 88 with SenderException

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);
}
Also used : StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) ParameterException(nl.nn.adapterframework.core.ParameterException) SenderException(nl.nn.adapterframework.core.SenderException) StrBuilder(org.apache.commons.lang.text.StrBuilder) Date(java.util.Date)

Example 89 with SenderException

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 "";
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) SenderException(nl.nn.adapterframework.core.SenderException) File(java.io.File)

Example 90 with SenderException

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);
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) Element(org.w3c.dom.Element) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) URISyntaxException(java.net.URISyntaxException) MessagingException(javax.mail.MessagingException) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) SenderException(nl.nn.adapterframework.core.SenderException)

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