Search in sources :

Example 1 with ResourceCollector

use of org.apache.sling.servlets.resolver.internal.helper.ResourceCollector in project sling by apache.

the class SlingServletResolver method handleError.

// ---------- ErrorHandler interface --------------------------------------
/**
     * @see org.apache.sling.engine.servlets.ErrorHandler#handleError(int,
     *      String, SlingHttpServletRequest, SlingHttpServletResponse)
     */
@Override
public void handleError(final int status, final String message, final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    // do not handle, if already handling ....
    if (request.getAttribute(SlingConstants.ERROR_REQUEST_URI) != null) {
        LOGGER.error("handleError: Recursive invocation. Not further handling status " + status + "(" + message + ")");
        return;
    }
    // start tracker
    RequestProgressTracker tracker = request.getRequestProgressTracker();
    String timerName = "handleError:status=" + status;
    tracker.startTimer(timerName);
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    try {
        // find the error handler component
        Resource resource = getErrorResource(request);
        // find a servlet for the status as the method name
        ResourceCollector locationUtil = new ResourceCollector(String.valueOf(status), DEFAULT_ERROR_HANDLER_RESOURCE_TYPE, resource, this.executionPaths);
        Servlet servlet = getServletInternal(locationUtil, request, scriptResolver);
        // fall back to default servlet if none
        if (servlet == null) {
            servlet = getDefaultErrorServlet(request, resource, scriptResolver);
        }
        // set the message properties
        request.setAttribute(ERROR_STATUS, new Integer(status));
        request.setAttribute(ERROR_MESSAGE, message);
        // the servlet name for a sendError handling is still stored
        // as the request attribute
        Object servletName = request.getAttribute(SLING_CURRENT_SERVLET_NAME);
        if (servletName instanceof String) {
            request.setAttribute(ERROR_SERVLET_NAME, servletName);
        }
        // log a track entry after resolution before calling the handler
        tracker.logTimer(timerName, "Using handler {0}", RequestUtil.getServletName(servlet));
        handleError(servlet, request, response);
    } finally {
        tracker.logTimer(timerName, "Error handler finished");
    }
}
Also used : ResourceCollector(org.apache.sling.servlets.resolver.internal.helper.ResourceCollector) AbstractResourceCollector(org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector) NamedScriptResourceCollector(org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet) HttpServlet(javax.servlet.http.HttpServlet) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Example 2 with ResourceCollector

use of org.apache.sling.servlets.resolver.internal.helper.ResourceCollector in project sling by apache.

the class SlingServletResolver method getDefaultErrorServlet.

/**
     * Returns the default error handler servlet, which is called in case there
     * is no other - better matching - servlet registered to handle an error or
     * exception.
     * <p>
     * The default error handler servlet is registered for the resource type
     * "sling/servlet/errorhandler" and method "default". This may be
     * overwritten by applications globally or according to the resource type
     * hierarchy of the resource.
     * <p>
     * If no default error handler servlet can be found an adhoc error handler
     * is used as a final fallback.
     */
private Servlet getDefaultErrorServlet(final SlingHttpServletRequest request, final Resource resource, final ResourceResolver resolver) {
    // find a default error handler according to the resource type
    // tree of the given resource
    final ResourceCollector locationUtil = new ResourceCollector(ServletResolverConstants.DEFAULT_ERROR_HANDLER_METHOD, DEFAULT_ERROR_HANDLER_RESOURCE_TYPE, resource, this.executionPaths);
    final Servlet servlet = getServletInternal(locationUtil, request, resolver);
    if (servlet != null) {
        return servlet;
    }
    // the DefaultErrorHandlerServlet as an ad-hoc fallback
    if (fallbackErrorServlet == null) {
        // if the actual service is not registered (yet ?)
        try {
            final Servlet defaultServlet = new DefaultErrorHandlerServlet();
            defaultServlet.init(new SlingServletConfig(servletContext, null, "Sling (Ad Hoc) Default Error Handler Servlet"));
            fallbackErrorServlet = defaultServlet;
        } catch (ServletException se) {
            LOGGER.error("Failed to initialize error servlet", se);
        }
    }
    return fallbackErrorServlet;
}
Also used : ResourceCollector(org.apache.sling.servlets.resolver.internal.helper.ResourceCollector) AbstractResourceCollector(org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector) NamedScriptResourceCollector(org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector) ServletException(javax.servlet.ServletException) SlingServletConfig(org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet) HttpServlet(javax.servlet.http.HttpServlet) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)

Example 3 with ResourceCollector

use of org.apache.sling.servlets.resolver.internal.helper.ResourceCollector in project sling by apache.

the class SlingServletResolver method handleError.

/**
     * @see org.apache.sling.engine.servlets.ErrorHandler#handleError(java.lang.Throwable, org.apache.sling.api.SlingHttpServletRequest, org.apache.sling.api.SlingHttpServletResponse)
     */
@Override
public void handleError(final Throwable throwable, final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
    // do not handle, if already handling ....
    if (request.getAttribute(SlingConstants.ERROR_REQUEST_URI) != null) {
        LOGGER.error("handleError: Recursive invocation. Not further handling Throwable:", throwable);
        return;
    }
    // start tracker
    RequestProgressTracker tracker = request.getRequestProgressTracker();
    String timerName = "handleError:throwable=" + throwable.getClass().getName();
    tracker.startTimer(timerName);
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    try {
        // find the error handler component
        Servlet servlet = null;
        Resource resource = getErrorResource(request);
        Class<?> tClass = throwable.getClass();
        while (servlet == null && tClass != Object.class) {
            // find a servlet for the simple class name as the method name
            ResourceCollector locationUtil = new ResourceCollector(tClass.getSimpleName(), DEFAULT_ERROR_HANDLER_RESOURCE_TYPE, resource, this.executionPaths);
            servlet = getServletInternal(locationUtil, request, scriptResolver);
            // go to the base class
            tClass = tClass.getSuperclass();
        }
        if (servlet == null) {
            servlet = getDefaultErrorServlet(request, resource, scriptResolver);
        }
        // set the message properties
        request.setAttribute(SlingConstants.ERROR_EXCEPTION, throwable);
        request.setAttribute(SlingConstants.ERROR_EXCEPTION_TYPE, throwable.getClass());
        request.setAttribute(SlingConstants.ERROR_MESSAGE, throwable.getMessage());
        // log a track entry after resolution before calling the handler
        tracker.logTimer(timerName, "Using handler {0}", RequestUtil.getServletName(servlet));
        handleError(servlet, request, response);
    } finally {
        tracker.logTimer(timerName, "Error handler finished");
    }
}
Also used : ResourceCollector(org.apache.sling.servlets.resolver.internal.helper.ResourceCollector) AbstractResourceCollector(org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector) NamedScriptResourceCollector(org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) DefaultErrorHandlerServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet) HttpServlet(javax.servlet.http.HttpServlet) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet) Servlet(javax.servlet.Servlet) OptingServlet(org.apache.sling.api.servlets.OptingServlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Aggregations

Servlet (javax.servlet.Servlet)3 HttpServlet (javax.servlet.http.HttpServlet)3 OptingServlet (org.apache.sling.api.servlets.OptingServlet)3 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)3 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)3 AbstractResourceCollector (org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector)3 NamedScriptResourceCollector (org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector)3 ResourceCollector (org.apache.sling.servlets.resolver.internal.helper.ResourceCollector)3 RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)2 Resource (org.apache.sling.api.resource.Resource)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)2 ServletException (javax.servlet.ServletException)1 SlingServletConfig (org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig)1