Search in sources :

Example 56 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class JdbcTransactionalStorage method containsMessageId.

public boolean containsMessageId(String originalMessageId) throws ListenerException {
    Connection conn;
    try {
        conn = getConnection();
    } catch (JdbcException e) {
        throw new ListenerException(e);
    }
    try {
        PreparedStatement stmt = conn.prepareStatement(checkMessageIdQuery);
        applyStandardParameters(stmt, originalMessageId, false);
        ResultSet rs = stmt.executeQuery();
        if (!rs.next()) {
            return false;
        }
        return true;
    } catch (Exception e) {
        throw new ListenerException("cannot deserialize message", e);
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            log.error("error closing JdbcConnection", e);
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) 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 57 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class JdbcTransactionalStorage method getIterator.

public IMessageBrowsingIterator getIterator(Date startTime, Date endTime, boolean forceDescending) throws ListenerException {
    Connection conn;
    try {
        conn = getConnection();
    } catch (JdbcException e) {
        throw new ListenerException(e);
    }
    try {
        String query;
        if (startTime == null && endTime == null) {
            query = selectListQuery;
        } else {
            query = getSelectListQuery(getDbmsSupport(), startTime, endTime, forceDescending);
        }
        if (log.isDebugEnabled()) {
            log.debug("preparing selectListQuery [" + query + "]");
        }
        PreparedStatement stmt = conn.prepareStatement(query);
        if (startTime == null && endTime == null) {
            applyStandardParameters(stmt, false, false);
        } else {
            int paramPos = applyStandardParameters(stmt, true, false);
            if (startTime != null) {
                stmt.setTimestamp(paramPos++, new Timestamp(startTime.getTime()));
            }
            if (endTime != null) {
                stmt.setTimestamp(paramPos++, new Timestamp(endTime.getTime()));
            }
        }
        ResultSet rs = stmt.executeQuery();
        return new ResultSetIterator(conn, rs);
    } catch (SQLException e) {
        throw new ListenerException(e);
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 58 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class JdbcTransactionalStorage method deleteMessage.

public void deleteMessage(String messageId) throws ListenerException {
    Connection conn;
    try {
        conn = getConnection();
    } catch (JdbcException e) {
        throw new ListenerException(e);
    }
    try {
        PreparedStatement stmt = conn.prepareStatement(deleteQuery);
        applyStandardParameters(stmt, messageId, true);
        stmt.execute();
    } catch (SQLException e) {
        throw new ListenerException(e);
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            log.error("error closing JdbcConnection", e);
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 59 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class RestServiceDispatcher method dispatchRequest.

/**
 * Dispatch a request.
 * @param uri the name of the IReceiver object
 * @param method the correlationId of this request;
 * @param request the <code>String</code> with the request/input
 * @return String with the result of processing the <code>request</code> through the <code>serviceName</code>
 */
public String dispatchRequest(String restPath, String uri, HttpServletRequest httpServletRequest, String contentType, String request, IPipeLineSession context, HttpServletResponse httpServletResponse, ServletContext servletContext) throws ListenerException {
    String method = httpServletRequest.getMethod();
    if (log.isTraceEnabled())
        log.trace("searching listener for uri [" + uri + "] method [" + method + "]");
    String matchingPattern = findMatchingPattern(uri);
    if (matchingPattern == null) {
        if (uri != null && (uri.equals("/showFlowDiagram") || uri.startsWith("/showFlowDiagram/"))) {
            log.info("no REST listener configured for uri [" + uri + "], so using 'no image available'");
            noImageAvailable(httpServletResponse);
            return "";
        }
        if (uri != null && (uri.equals("/showConfigurationStatus") || uri.startsWith("/showConfigurationStatus/"))) {
            log.info("no REST listener configured for uri [" + uri + "], if REST listener does exist then trying to restart");
            if (RestListenerUtils.restartShowConfigurationStatus(servletContext)) {
                httpServletResponse.setHeader("REFRESH", "0");
                return "";
            }
        }
        throw new ListenerException("no REST listener configured for uri [" + uri + "]");
    }
    Map methodConfig = getMethodConfig(matchingPattern, method);
    if (methodConfig == null) {
        throw new ListenerException("No REST listener specified for uri [" + uri + "] method [" + method + "]");
    }
    if (context == null) {
        context = new PipeLineSessionBase();
    }
    context.put("restPath", restPath);
    context.put("uri", uri);
    context.put("method", method);
    String etag = null;
    String ifNoneMatch = httpServletRequest.getHeader("If-None-Match");
    if (ifNoneMatch != null && !ifNoneMatch.isEmpty()) {
        context.put("if-none-match", ifNoneMatch);
        etag = ifNoneMatch;
    }
    String ifMatch = httpServletRequest.getHeader("If-Match");
    if (ifMatch != null && !ifMatch.isEmpty()) {
        context.put("if-match", ifMatch);
        etag = ifMatch;
    }
    context.put("contentType", contentType);
    context.put("userAgent", httpServletRequest.getHeader("User-Agent"));
    ServiceClient listener = (ServiceClient) methodConfig.get(KEY_LISTENER);
    String etagKey = (String) methodConfig.get(KEY_ETAG_KEY);
    String contentTypeKey = (String) methodConfig.get(KEY_CONTENT_TYPE_KEY);
    Principal principal = null;
    if (httpServletRequest != null) {
        principal = httpServletRequest.getUserPrincipal();
        if (principal != null) {
            context.put("principal", principal.getName());
        }
    }
    String ctName = Thread.currentThread().getName();
    try {
        boolean writeToSecLog = false;
        if (listener instanceof RestListener) {
            RestListener restListener = (RestListener) listener;
            if (restListener.isRetrieveMultipart()) {
                if (ServletFileUpload.isMultipartContent(httpServletRequest)) {
                    try {
                        DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
                        ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
                        List<FileItem> items = servletFileUpload.parseRequest(httpServletRequest);
                        for (FileItem item : items) {
                            if (item.isFormField()) {
                                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                                String fieldName = item.getFieldName();
                                String fieldValue = item.getString();
                                log.trace("setting parameter [" + fieldName + "] to [" + fieldValue + "]");
                                context.put(fieldName, fieldValue);
                            } else {
                                // Process form file field (input type="file").
                                String fieldName = item.getFieldName();
                                String fieldNameName = fieldName + "Name";
                                String fileName = FilenameUtils.getName(item.getName());
                                if (log.isTraceEnabled())
                                    log.trace("setting parameter [" + fieldNameName + "] to [" + fileName + "]");
                                context.put(fieldNameName, fileName);
                                InputStream inputStream = item.getInputStream();
                                if (inputStream.available() > 0) {
                                    log.trace("setting parameter [" + fieldName + "] to input stream of file [" + fileName + "]");
                                    context.put(fieldName, inputStream);
                                } else {
                                    log.trace("setting parameter [" + fieldName + "] to [" + null + "]");
                                    context.put(fieldName, null);
                                }
                            }
                        }
                    } catch (FileUploadException e) {
                        throw new ListenerException(e);
                    } catch (IOException e) {
                        throw new ListenerException(e);
                    }
                }
            }
            writeToSecLog = restListener.isWriteToSecLog();
            if (writeToSecLog) {
                context.put("writeSecLogMessage", restListener.isWriteSecLogMessage());
            }
            boolean authorized = false;
            if (principal == null) {
                authorized = true;
            } else {
                String authRoles = restListener.getAuthRoles();
                if (StringUtils.isNotEmpty(authRoles)) {
                    StringTokenizer st = new StringTokenizer(authRoles, ",;");
                    while (st.hasMoreTokens()) {
                        String authRole = st.nextToken();
                        if (httpServletRequest.isUserInRole(authRole)) {
                            authorized = true;
                        }
                    }
                }
            }
            if (!authorized) {
                throw new ListenerException("Not allowed for uri [" + uri + "]");
            }
            Thread.currentThread().setName(restListener.getName() + "[" + ctName + "]");
        }
        if (etagKey != null)
            context.put(etagKey, etag);
        if (contentTypeKey != null)
            context.put(contentTypeKey, contentType);
        if (log.isTraceEnabled())
            log.trace("dispatching request, uri [" + uri + "] listener pattern [" + matchingPattern + "] method [" + method + "] etag [" + etag + "] contentType [" + contentType + "]");
        if (httpServletRequest != null)
            context.put(IPipeLineSession.HTTP_REQUEST_KEY, httpServletRequest);
        if (httpServletResponse != null)
            context.put(IPipeLineSession.HTTP_RESPONSE_KEY, httpServletResponse);
        if (servletContext != null)
            context.put(IPipeLineSession.SERVLET_CONTEXT_KEY, servletContext);
        if (writeToSecLog) {
            secLog.info(HttpUtils.getExtendedCommandIssuedBy(httpServletRequest));
        }
        // Caching: check for etags
        if (uri.startsWith("/"))
            uri = uri.substring(1);
        if (uri.indexOf("?") > -1) {
            uri = uri.split("?")[0];
        }
        String etagCacheKey = restPath + "_" + uri;
        if (cache != null && cache.containsKey(etagCacheKey)) {
            String cachedEtag = (String) cache.get(etagCacheKey);
            if (ifNoneMatch != null && ifNoneMatch.equalsIgnoreCase(cachedEtag) && method.equalsIgnoreCase("GET")) {
                // Exit with 304
                context.put("exitcode", 304);
                if (log.isDebugEnabled())
                    log.trace("aborting request with status 304, matched if-none-match [" + ifNoneMatch + "]");
                return null;
            }
            if (ifMatch != null && !ifMatch.equalsIgnoreCase(cachedEtag) && !method.equalsIgnoreCase("GET")) {
                // Exit with 412
                context.put("exitcode", 412);
                if (log.isDebugEnabled())
                    log.trace("aborting request with status 412, matched if-match [" + ifMatch + "] method [" + method + "]");
                return null;
            }
        }
        String result = listener.processRequest(null, request, context);
        // Caching: pipeline has been processed, save etag
        if (result != null && cache != null && context.containsKey("etag")) {
            // In case the eTag has manually been set and the pipeline exited in error state...
            cache.put(etagCacheKey, context.get("etag"));
        }
        if (result == null && !context.containsKey("exitcode")) {
            log.warn("result is null!");
        }
        return result;
    } finally {
        if (listener instanceof RestListener) {
            Thread.currentThread().setName(ctName);
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) ListenerException(nl.nn.adapterframework.core.ListenerException) StringTokenizer(java.util.StringTokenizer) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ServiceClient(nl.nn.adapterframework.receivers.ServiceClient) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Principal(java.security.Principal) FileUploadException(org.apache.commons.fileupload.FileUploadException) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Example 60 with ListenerException

use of nl.nn.adapterframework.core.ListenerException in project iaf by ibissource.

the class RestServiceDispatcher method noImageAvailable.

private void noImageAvailable(HttpServletResponse httpServletResponse) throws ListenerException {
    URL svgSource = ClassUtils.getResourceURL(this, SVG_FILE_NO_IMAGE_AVAILABLE);
    if (svgSource == null) {
        throw new ListenerException("cannot find resource [" + SVG_FILE_NO_IMAGE_AVAILABLE + "]");
    }
    try {
        httpServletResponse.setContentType("image/svg+xml");
        InputStream inputStream = null;
        try {
            inputStream = svgSource.openStream();
            Misc.streamToStream(inputStream, httpServletResponse.getOutputStream());
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } catch (IOException e) {
        throw new ListenerException(e);
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) InputStream(java.io.InputStream) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

ListenerException (nl.nn.adapterframework.core.ListenerException)84 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)19 HashMap (java.util.HashMap)18 Map (java.util.Map)18 JMSException (javax.jms.JMSException)16 SenderException (nl.nn.adapterframework.core.SenderException)14 SQLException (java.sql.SQLException)13 PreparedStatement (java.sql.PreparedStatement)11 Session (javax.jms.Session)10 LinkedHashMap (java.util.LinkedHashMap)9 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)9 TimeOutException (nl.nn.adapterframework.core.TimeOutException)9 IfsaException (nl.nn.adapterframework.extensions.ifsa.IfsaException)9 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 QueueSession (javax.jms.QueueSession)8 File (java.io.File)7 MessageConsumer (javax.jms.MessageConsumer)7 Message (javax.jms.Message)6