Search in sources :

Example 1 with DefaultErrorHandlerServlet

use of org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet 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)

Aggregations

Servlet (javax.servlet.Servlet)1 ServletException (javax.servlet.ServletException)1 HttpServlet (javax.servlet.http.HttpServlet)1 OptingServlet (org.apache.sling.api.servlets.OptingServlet)1 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)1 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)1 AbstractResourceCollector (org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector)1 NamedScriptResourceCollector (org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector)1 ResourceCollector (org.apache.sling.servlets.resolver.internal.helper.ResourceCollector)1 SlingServletConfig (org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig)1