Search in sources :

Example 11 with RequestProgressTracker

use of org.apache.sling.api.request.RequestProgressTracker 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

RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)11 Servlet (javax.servlet.Servlet)4 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)4 Resource (org.apache.sling.api.resource.Resource)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 HttpServlet (javax.servlet.http.HttpServlet)3 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)3 OptingServlet (org.apache.sling.api.servlets.OptingServlet)3 DefaultErrorHandlerServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultErrorHandlerServlet)3 DefaultServlet (org.apache.sling.servlets.resolver.internal.defaults.DefaultServlet)3 PrintWriter (java.io.PrintWriter)2 RequestData (org.apache.sling.engine.impl.request.RequestData)2 AbstractResourceCollector (org.apache.sling.servlets.resolver.internal.helper.AbstractResourceCollector)2 NamedScriptResourceCollector (org.apache.sling.servlets.resolver.internal.helper.NamedScriptResourceCollector)2 ResourceCollector (org.apache.sling.servlets.resolver.internal.helper.ResourceCollector)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 JspException (javax.servlet.jsp.JspException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 SlingBindings (org.apache.sling.api.scripting.SlingBindings)1