Search in sources :

Example 36 with Servlet

use of javax.servlet.Servlet 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 37 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class SlingServletResolver method resolveServlet.

/**
     * @see org.apache.sling.api.servlets.ServletResolver#resolveServlet(org.apache.sling.api.resource.Resource, java.lang.String)
     */
@Override
public Servlet resolveServlet(final Resource resource, final String scriptName) {
    if (resource == null) {
        throw new IllegalArgumentException("Resource must not be null");
    }
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("resolveServlet called for resource {} with script name {}", resource, scriptName);
    }
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    final Servlet servlet = resolveServletInternal(null, resource, scriptName, scriptResolver);
    // log the servlet found
    if (LOGGER.isDebugEnabled()) {
        if (servlet != null) {
            LOGGER.debug("Servlet {} found for resource {} and script name {}", new Object[] { RequestUtil.getServletName(servlet), resource, scriptName });
        } else {
            LOGGER.debug("No servlet found for resource {} and script name {}", resource, scriptName);
        }
    }
    return servlet;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) 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)

Example 38 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class SlingServletResolver method getServletInternal.

/**
     * Returns a servlet suitable for handling a request. The
     * <code>locationUtil</code> is used find any servlets or scripts usable for
     * the request. Each servlet returned is in turn asked whether it is
     * actually willing to handle the request in case the servlet is an
     * <code>OptingServlet</code>. The first servlet willing to handle the
     * request is used.
     *
     * @param locationUtil The helper used to find appropriate servlets ordered
     *            by matching priority.
     * @param request The request used to give to any <code>OptingServlet</code>
     *            for them to decide on whether they are willing to handle the
     *            request
     * @param resolver The <code>ResourceResolver</code> used for resolving the servlets.
     * @return a servlet for handling the request or <code>null</code> if no
     *         such servlet willing to handle the request could be found.
     */
private Servlet getServletInternal(final AbstractResourceCollector locationUtil, final SlingHttpServletRequest request, final ResourceResolver resolver) {
    final Servlet scriptServlet = (this.cache != null ? this.cache.get(locationUtil) : null);
    if (scriptServlet != null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Using cached servlet {}", RequestUtil.getServletName(scriptServlet));
        }
        return scriptServlet;
    }
    final Collection<Resource> candidates = locationUtil.getServlets(resolver);
    if (LOGGER.isDebugEnabled()) {
        if (candidates.isEmpty()) {
            LOGGER.debug("No servlet candidates found");
        } else {
            LOGGER.debug("Ordered list of servlet candidates follows");
            for (Resource candidateResource : candidates) {
                LOGGER.debug("Servlet candidate: {}", candidateResource.getPath());
            }
        }
    }
    boolean hasOptingServlet = false;
    for (final Resource candidateResource : candidates) {
        LOGGER.debug("Checking if candidate resource {} adapts to servlet and accepts request", candidateResource.getPath());
        Servlet candidate = this.getServlet(candidateResource);
        if (candidate != null) {
            final boolean isOptingServlet = candidate instanceof OptingServlet;
            boolean servletAcceptsRequest = !isOptingServlet || (request != null && ((OptingServlet) candidate).accepts(request));
            if (servletAcceptsRequest) {
                if (!hasOptingServlet && !isOptingServlet && this.cache != null) {
                    if (this.cache.size() < this.cacheSize) {
                        this.cache.put(locationUtil, candidate);
                    } else if (this.logCacheSizeWarning) {
                        this.logCacheSizeWarning = false;
                        LOGGER.warn("Script cache has reached its limit of {}. You might want to increase the cache size for the servlet resolver.", this.cacheSize);
                    }
                }
                LOGGER.debug("Using servlet provided by candidate resource {}", candidateResource.getPath());
                return candidate;
            }
            if (isOptingServlet) {
                hasOptingServlet = true;
            }
            LOGGER.debug("Candidate {} does not accept request, ignored", candidateResource.getPath());
        } else {
            LOGGER.debug("Candidate {} does not adapt to a servlet, ignored", candidateResource.getPath());
        }
    }
    // exhausted all candidates, we don't have a servlet
    return null;
}
Also used : OptingServlet(org.apache.sling.api.servlets.OptingServlet) 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)

Example 39 with Servlet

use of javax.servlet.Servlet in project sling by apache.

the class SlingServletResolver method resolveServlet.

// ---------- ServletResolver interface -----------------------------------
/**
     * @see org.apache.sling.api.servlets.ServletResolver#resolveServlet(org.apache.sling.api.SlingHttpServletRequest)
     */
@Override
public Servlet resolveServlet(final SlingHttpServletRequest request) {
    final Resource resource = request.getResource();
    // start tracking servlet resolution
    final RequestProgressTracker tracker = request.getRequestProgressTracker();
    final String timerName = "resolveServlet(" + resource.getPath() + ")";
    tracker.startTimer(timerName);
    final String type = resource.getResourceType();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("resolveServlet called for resource {}", resource);
    }
    final ResourceResolver scriptResolver = this.getScriptResourceResolver();
    Servlet servlet = null;
    if (type != null && type.length() > 0) {
        servlet = resolveServletInternal(request, null, type, scriptResolver);
    }
    // last resort, use the core bundle default servlet
    if (servlet == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("No specific servlet found, trying default");
        }
        servlet = getDefaultServlet();
    }
    // track servlet resolution termination
    if (servlet == null) {
        tracker.logTimer(timerName, "Servlet resolution failed. See log for details");
    } else {
        tracker.logTimer(timerName, "Using servlet {0}", RequestUtil.getServletName(servlet));
    }
    // log the servlet found
    if (LOGGER.isDebugEnabled()) {
        if (servlet != null) {
            LOGGER.debug("Servlet {} found for resource={}", RequestUtil.getServletName(servlet), resource);
        } else {
            LOGGER.debug("No servlet found for resource={}", resource);
        }
    }
    return servlet;
}
Also used : Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) 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 40 with Servlet

use of javax.servlet.Servlet 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)

Aggregations

Servlet (javax.servlet.Servlet)86 Test (org.junit.Test)18 HttpServlet (javax.servlet.http.HttpServlet)16 ServletException (javax.servlet.ServletException)15 IOException (java.io.IOException)11 OptingServlet (org.apache.sling.api.servlets.OptingServlet)11 GenericServlet (javax.servlet.GenericServlet)10 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)9 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)9 ServletContext (javax.servlet.ServletContext)8 UnavailableException (javax.servlet.UnavailableException)8 Resource (org.apache.sling.api.resource.Resource)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)7 MockServletWebServerFactory (org.springframework.boot.web.servlet.server.MockServletWebServerFactory)7 ServletInfo (io.undertow.servlet.api.ServletInfo)6 ArrayList (java.util.ArrayList)5 Filter (javax.servlet.Filter)5 Context (org.apache.catalina.Context)5 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)5