Search in sources :

Example 1 with ServletResolver

use of org.apache.sling.api.servlets.ServletResolver in project sling by apache.

the class EvalTagHandler method doEndTag.

/**
     * Called after the body has been processed.
     *
     * @return whether additional evaluations of the body are desired
     */
public int doEndTag() throws JspException {
    log.debug("EvalTagHandler doEndTag");
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final Servlet servlet;
    if (!this.ignoreResourceTypeHierarchy) {
        // detecte resource
        final Resource resource;
        if (this.resource != null) {
            resource = this.resource;
        } else if (this.resourceType != null) {
            resource = new SyntheticResource(bindings.getRequest().getResourceResolver(), bindings.getResource().getPath(), this.resourceType);
        } else {
            resource = bindings.getResource();
        }
        servlet = servletResolver.resolveServlet(resource, this.script);
    } else {
        final ResourceResolver rr = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : rr.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + '/' + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(rr, scriptPath);
    }
    if (servlet == null) {
        throw new JspException("Could not find script '" + script + "' referenced in jsp " + scriptHelper.getScript().getScriptResource().getPath());
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        servlet.service(pageContext.getRequest(), response);
        return EVAL_PAGE;
    } catch (Exception e) {
        log.error("Error while executing script " + script, e);
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet)

Example 2 with ServletResolver

use of org.apache.sling.api.servlets.ServletResolver in project sling by apache.

the class IncludeRuntimeExtension method includeScript.

private void includeScript(final Bindings bindings, String script, PrintWriter out) {
    if (StringUtils.isEmpty(script)) {
        throw new SightlyException("Path for data-sly-include is empty");
    } else {
        LOG.debug("Attempting to include script {}.", script);
        SlingScriptHelper slingScriptHelper = BindingsUtils.getHelper(bindings);
        ServletResolver servletResolver = slingScriptHelper.getService(ServletResolver.class);
        if (servletResolver != null) {
            SlingHttpServletRequest request = BindingsUtils.getRequest(bindings);
            Servlet servlet = servletResolver.resolveServlet(request.getResource(), script);
            if (servlet != null) {
                try {
                    SlingHttpServletResponse response = BindingsUtils.getResponse(bindings);
                    PrintWriterResponseWrapper resWrapper = new PrintWriterResponseWrapper(out, response);
                    servlet.service(request, resWrapper);
                } catch (Exception e) {
                    throw new SightlyException("Failed to include script " + script, e);
                }
            } else {
                throw new SightlyException("Failed to locate script " + script);
            }
        } else {
            throw new SightlyException("Sling ServletResolver service is unavailable, failed to include " + script);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletResolver(org.apache.sling.api.servlets.ServletResolver) SightlyException(org.apache.sling.scripting.sightly.SightlyException) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) SightlyException(org.apache.sling.scripting.sightly.SightlyException)

Example 3 with ServletResolver

use of org.apache.sling.api.servlets.ServletResolver in project sling by apache.

the class SlingRequestProcessorImpl method doProcessRequest.

/**
     * This method is directly called by the Sling main servlet.
     */
public void doProcessRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final ResourceResolver resourceResolver) throws IOException {
    // setting the Sling request and response
    final RequestData requestData = new RequestData(this, servletRequest, servletResponse);
    final SlingHttpServletRequest request = requestData.getSlingRequest();
    final SlingHttpServletResponse response = requestData.getSlingResponse();
    // record the request for the web console display
    RequestHistoryConsolePlugin.recordRequest(request);
    try {
        final ServletResolver sr = this.servletResolver;
        // check that we have all required services
        if (resourceResolver == null) {
            throw new UnavailableException("ResourceResolver");
        } else if (sr == null) {
            throw new UnavailableException("ServletResolver");
        }
        // initialize the request data - resolve resource and servlet
        Resource resource = requestData.initResource(resourceResolver);
        requestData.initServlet(resource, sr);
        FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
        if (filters != null) {
            FilterChain processor = new RequestSlingFilterChain(this, filters);
            request.getRequestProgressTracker().log("Applying " + FilterChainType.REQUEST + "filters");
            processor.doFilter(request, response);
        } else {
            // no filters, directly call resource level filters and servlet
            processComponent(request, response, FilterChainType.COMPONENT);
        }
    } catch (final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace) {
        log.error("Writer has already been closed.", wace);
    } catch (ResourceNotFoundException rnfe) {
        // send this exception as a 404 status
        log.info("service: Resource {} not found", rnfe.getResource());
        handleError(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage(), request, response);
    } catch (final SlingException se) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        // send this exception as is (albeit unwrapping and wrapped
        // exception.
        Throwable t = se;
        while (t instanceof SlingException && t.getCause() != null) {
            t = t.getCause();
        }
        log.error("service: Uncaught SlingException", t);
        handleError(t, request, response);
    } catch (AccessControlException ace) {
        // SLING-319 if anything goes wrong, send 403/FORBIDDEN
        log.info("service: Authenticated user {} does not have enough rights to executed requested action", request.getRemoteUser());
        handleError(HttpServletResponse.SC_FORBIDDEN, null, request, response);
    } catch (UnavailableException ue) {
        // exception is thrown before the SlingHttpServletRequest/Response
        // is properly set up due to missing dependencies. In this case
        // we must not use the Sling error handling infrastructure but
        // just return a 503 status response handled by the servlet
        // container environment
        final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
        final String errorMessage = ue.getMessage() + " service missing, cannot service requests";
        log.error("{} , sending status {}", errorMessage, status);
        servletResponse.sendError(status, errorMessage);
    } catch (IOException ioe) {
        // forward IOException up the call chain to properly handle it
        throw ioe;
    } catch (Throwable t) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        log.error("service: Uncaught Throwable", t);
        handleError(t, request, response);
    } finally {
        if (mbean != null) {
            mbean.addRequestData(requestData);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) SlingComponentFilterChain(org.apache.sling.engine.impl.filter.SlingComponentFilterChain) FilterChain(javax.servlet.FilterChain) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) UnavailableException(javax.servlet.UnavailableException) Resource(org.apache.sling.api.resource.Resource) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) ServletResolver(org.apache.sling.api.servlets.ServletResolver) RequestData(org.apache.sling.engine.impl.request.RequestData) SlingException(org.apache.sling.api.SlingException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 4 with ServletResolver

use of org.apache.sling.api.servlets.ServletResolver in project sling by apache.

the class CallTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    final SlingBindings bindings = (SlingBindings) pageContext.getRequest().getAttribute(SlingBindings.class.getName());
    final SlingScriptHelper scriptHelper = bindings.getSling();
    final ServletResolver servletResolver = scriptHelper.getService(ServletResolver.class);
    final RequestProgressTracker tracker = TagUtil.getRequest(pageContext).getRequestProgressTracker();
    String servletName = null;
    final Servlet servlet;
    if (!ignoreComponentHierarchy) {
        final Resource resource = bindings.getResource();
        servlet = servletResolver.resolveServlet(resource, this.script);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} for path={1}, type={2}: {3}", script, resource.getPath(), resource.getResourceType(), servletName);
        }
    } else {
        final ResourceResolver resolver = bindings.getRequest().getResourceResolver();
        final String scriptPath;
        if (!script.startsWith("/")) {
            // resolve relative script
            String parentPath = ResourceUtil.getParent(scriptHelper.getScript().getScriptResource().getPath());
            // check if parent resides on search path
            for (String sp : resolver.getSearchPath()) {
                if (parentPath.startsWith(sp)) {
                    parentPath = parentPath.substring(sp.length());
                    break;
                }
            }
            scriptPath = parentPath + "/" + script;
        } else {
            scriptPath = this.script;
        }
        servlet = servletResolver.resolveServlet(resolver, scriptPath);
        if (servlet != null) {
            servletName = RequestUtil.getServletName(servlet);
            tracker.log("Including script {0} (ignoring component hierarchy): {1}", script, servletName);
        }
    }
    if (servlet == null) {
        throw new JspException("Could not find script " + script);
    }
    try {
        if (flush && !(pageContext.getOut() instanceof BodyContent)) {
            // might throw an IOException of course
            pageContext.getOut().flush();
        }
        // wrap the response to get the correct output order
        SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
        tracker.startTimer(servletName);
        servlet.service(pageContext.getRequest(), response);
        tracker.logTimer(servletName);
        return EVAL_PAGE;
    } catch (Exception e) {
        throw new JspException("Error while executing script " + script, e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) SlingBindings(org.apache.sling.api.scripting.SlingBindings) SlingScriptHelper(org.apache.sling.api.scripting.SlingScriptHelper) Resource(org.apache.sling.api.resource.Resource) JspException(javax.servlet.jsp.JspException) BodyContent(javax.servlet.jsp.tagext.BodyContent) JspException(javax.servlet.jsp.JspException) ServletResolver(org.apache.sling.api.servlets.ServletResolver) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Servlet(javax.servlet.Servlet) RequestProgressTracker(org.apache.sling.api.request.RequestProgressTracker)

Aggregations

SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)4 ServletResolver (org.apache.sling.api.servlets.ServletResolver)4 Servlet (javax.servlet.Servlet)3 Resource (org.apache.sling.api.resource.Resource)3 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)3 JspException (javax.servlet.jsp.JspException)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 JspSlingHttpServletResponseWrapper (org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper)2 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1 FilterChain (javax.servlet.FilterChain)1 UnavailableException (javax.servlet.UnavailableException)1 SlingException (org.apache.sling.api.SlingException)1 RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)1 ResourceNotFoundException (org.apache.sling.api.resource.ResourceNotFoundException)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 AbstractSlingFilterChain (org.apache.sling.engine.impl.filter.AbstractSlingFilterChain)1