Search in sources :

Example 1 with FilterHandle

use of org.apache.sling.engine.impl.filter.FilterHandle in project sling by apache.

the class SlingRequestProcessorImpl method doProcessRequest.

/**
     * This method is directly called by the Sling main servlet.
     */
public void doProcessRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final ResourceResolver resourceResolver) throws IOException {
    // setting the Sling request and response
    final RequestData requestData = new RequestData(this, servletRequest, servletResponse);
    final SlingHttpServletRequest request = requestData.getSlingRequest();
    final SlingHttpServletResponse response = requestData.getSlingResponse();
    // record the request for the web console display
    RequestHistoryConsolePlugin.recordRequest(request);
    try {
        final ServletResolver sr = this.servletResolver;
        // check that we have all required services
        if (resourceResolver == null) {
            throw new UnavailableException("ResourceResolver");
        } else if (sr == null) {
            throw new UnavailableException("ServletResolver");
        }
        // initialize the request data - resolve resource and servlet
        Resource resource = requestData.initResource(resourceResolver);
        requestData.initServlet(resource, sr);
        FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
        if (filters != null) {
            FilterChain processor = new RequestSlingFilterChain(this, filters);
            request.getRequestProgressTracker().log("Applying " + FilterChainType.REQUEST + "filters");
            processor.doFilter(request, response);
        } else {
            // no filters, directly call resource level filters and servlet
            processComponent(request, response, FilterChainType.COMPONENT);
        }
    } catch (final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace) {
        log.error("Writer has already been closed.", wace);
    } catch (ResourceNotFoundException rnfe) {
        // send this exception as a 404 status
        log.info("service: Resource {} not found", rnfe.getResource());
        handleError(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage(), request, response);
    } catch (final SlingException se) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        // send this exception as is (albeit unwrapping and wrapped
        // exception.
        Throwable t = se;
        while (t instanceof SlingException && t.getCause() != null) {
            t = t.getCause();
        }
        log.error("service: Uncaught SlingException", t);
        handleError(t, request, response);
    } catch (AccessControlException ace) {
        // SLING-319 if anything goes wrong, send 403/FORBIDDEN
        log.info("service: Authenticated user {} does not have enough rights to executed requested action", request.getRemoteUser());
        handleError(HttpServletResponse.SC_FORBIDDEN, null, request, response);
    } catch (UnavailableException ue) {
        // exception is thrown before the SlingHttpServletRequest/Response
        // is properly set up due to missing dependencies. In this case
        // we must not use the Sling error handling infrastructure but
        // just return a 503 status response handled by the servlet
        // container environment
        final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
        final String errorMessage = ue.getMessage() + " service missing, cannot service requests";
        log.error("{} , sending status {}", errorMessage, status);
        servletResponse.sendError(status, errorMessage);
    } catch (IOException ioe) {
        // forward IOException up the call chain to properly handle it
        throw ioe;
    } catch (Throwable t) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        log.error("service: Uncaught Throwable", t);
        handleError(t, request, response);
    } finally {
        if (mbean != null) {
            mbean.addRequestData(requestData);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) UnavailableException(javax.servlet.UnavailableException) Resource(org.apache.sling.api.resource.Resource) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) ServletResolver(org.apache.sling.api.servlets.ServletResolver) RequestData(org.apache.sling.engine.impl.request.RequestData) SlingException(org.apache.sling.api.SlingException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 2 with FilterHandle

use of org.apache.sling.engine.impl.filter.FilterHandle in project sling by apache.

the class SlingRequestProcessorImpl method processComponent.

/**
     * Renders the component defined by the RequestData's current ComponentData
     * instance after calling all filters of the given
     * {@link org.apache.sling.engine.impl.filter.ServletFilterManager.FilterChainType
     * filterChainType}.
     *
     * @param request
     * @param response
     * @param filterChainType
     * @throws IOException
     * @throws ServletException
     */
public void processComponent(SlingHttpServletRequest request, SlingHttpServletResponse response, final FilterChainType filterChainType) throws IOException, ServletException {
    FilterHandle[] filters = filterManager.getFilters(filterChainType);
    if (filters != null) {
        FilterChain processor = new SlingComponentFilterChain(filters);
        request.getRequestProgressTracker().log("Applying " + filterChainType + "filters");
        processor.doFilter(request, response);
    } else {
        log.debug("service: No Resource level filters, calling servlet");
        RequestData.service(request, response);
    }
}
Also used : SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain)

Example 3 with FilterHandle

use of org.apache.sling.engine.impl.filter.FilterHandle in project sling by apache.

the class SlingRequestProcessorImpl method handleError.

// ---------- Error Handling with Filters
void handleError(final int status, final String message, final SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // wrap the response ensuring getWriter will fall back to wrapping
    // the response output stream if reset does not reset this
    response = new ErrorResponseWrapper(response);
    FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
    if (filters != null && filters.length > 0) {
        FilterChain processor = new AbstractSlingFilterChain(filters) {

            @Override
            protected void render(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
                errorHandler.handleError(status, message, request, response);
            }
        };
        request.getRequestProgressTracker().log("Applying " + FilterChainType.ERROR + " filters");
        try {
            processor.doFilter(request, response);
        } catch (ServletException se) {
            throw new SlingServletException(se);
        }
    } else {
        errorHandler.handleError(status, message, request, response);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) SlingServletException(org.apache.sling.api.SlingServletException) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 4 with FilterHandle

use of org.apache.sling.engine.impl.filter.FilterHandle in project sling by apache.

the class SlingRequestProcessorImpl method handleError.

// just rethrow the exception as explained in the class comment
private void handleError(final Throwable throwable, final SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // wrap the response ensuring getWriter will fall back to wrapping
    // the response output stream if reset does not reset this
    response = new ErrorResponseWrapper(response);
    FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
    if (filters != null && filters.length > 0) {
        FilterChain processor = new AbstractSlingFilterChain(filters) {

            @Override
            protected void render(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
                errorHandler.handleError(throwable, request, response);
            }
        };
        request.getRequestProgressTracker().log("Applying " + FilterChainType.ERROR + " filters");
        try {
            processor.doFilter(request, response);
        } catch (ServletException se) {
            throw new SlingServletException(se);
        }
    } else {
        errorHandler.handleError(throwable, request, response);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) SlingServletException(org.apache.sling.api.SlingServletException) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

FilterChain (javax.servlet.FilterChain)4 AbstractSlingFilterChain (org.apache.sling.engine.impl.filter.AbstractSlingFilterChain)4 FilterHandle (org.apache.sling.engine.impl.filter.FilterHandle)4 RequestSlingFilterChain (org.apache.sling.engine.impl.filter.RequestSlingFilterChain)4 SlingComponentFilterChain (org.apache.sling.engine.impl.filter.SlingComponentFilterChain)4 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)3 ServletException (javax.servlet.ServletException)2 SlingServletException (org.apache.sling.api.SlingServletException)2 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1 UnavailableException (javax.servlet.UnavailableException)1 SlingException (org.apache.sling.api.SlingException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 ServletResolver (org.apache.sling.api.servlets.ServletResolver)1 RequestData (org.apache.sling.engine.impl.request.RequestData)1