Search in sources :

Example 1 with OptingServlet

use of org.apache.sling.api.servlets.OptingServlet 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)

Aggregations

Servlet (javax.servlet.Servlet)1 HttpServlet (javax.servlet.http.HttpServlet)1 Resource (org.apache.sling.api.resource.Resource)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)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