Search in sources :

Example 1 with SlingServletConfig

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

the class SlingServletResolver method getDefaultServlet.

/**
     * Returns the internal default servlet which is called in case no other
     * servlet applies for handling a request. This servlet should really only
     * be used if the default servlets have not been registered (yet).
     */
private Servlet getDefaultServlet() {
    if (defaultServlet == null) {
        try {
            Servlet servlet = new DefaultServlet();
            servlet.init(new SlingServletConfig(servletContext, null, "Apache Sling Core Default Servlet"));
            defaultServlet = servlet;
        } catch (final ServletException se) {
            LOGGER.error("Failed to initialize default servlet", se);
        }
    }
    return defaultServlet;
}
Also used : 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) DefaultServlet(org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)

Example 2 with SlingServletConfig

use of org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig 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 SlingServletConfig

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

the class SlingServletResolver method createServlet.

private boolean createServlet(final Servlet servlet, final ServiceReference<Servlet> reference) {
    // check for a name, this is required
    final String name = getName(reference);
    // check for Sling properties in the service registration
    final ServletResourceProvider provider = servletResourceProviderFactory.create(reference, servlet);
    if (provider == null) {
        // this is expected if the servlet is not destined for Sling
        return false;
    }
    // initialize now
    try {
        servlet.init(new SlingServletConfig(servletContext, reference, name));
        LOGGER.debug("bindServlet: Servlet {} initialized", name);
    } catch (ServletException ce) {
        LOGGER.error("bindServlet: Servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference) + " failed to initialize", ce);
        return false;
    } catch (Throwable t) {
        LOGGER.error("bindServlet: Unexpected problem initializing servlet " + ServletResourceProviderFactory.getServiceReferenceInfo(reference), t);
        return false;
    }
    final List<ServiceRegistration<ResourceProvider<Object>>> regs = new ArrayList<>();
    for (final String root : provider.getServletPaths()) {
        @SuppressWarnings("unchecked") final ServiceRegistration<ResourceProvider<Object>> reg = (ServiceRegistration<ResourceProvider<Object>>) context.registerService(ResourceProvider.class.getName(), provider, createServiceProperties(reference, provider, root));
        regs.add(reg);
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Registered {}", provider.toString());
    }
    synchronized (this.servletsByReference) {
        servletsByReference.put(reference, new ServletReg(servlet, regs));
    }
    return true;
}
Also used : SlingServletConfig(org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig) ArrayList(java.util.ArrayList) ServletException(javax.servlet.ServletException) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ServletResourceProvider(org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ServletException (javax.servlet.ServletException)3 SlingServletConfig (org.apache.sling.servlets.resolver.internal.helper.SlingServletConfig)3 Servlet (javax.servlet.Servlet)2 HttpServlet (javax.servlet.http.HttpServlet)2 OptingServlet (org.apache.sling.api.servlets.OptingServlet)2 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)2 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)2 ArrayList (java.util.ArrayList)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 ServletResourceProvider (org.apache.sling.servlets.resolver.internal.resource.ServletResourceProvider)1 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1