Search in sources :

Example 1 with JspSlingHttpServletResponseWrapper

use of org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper in project sling by apache.

the class AbstractDispatcherTagHandler 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("AbstractDispatcherTagHandler.doEndTag");
    final SlingHttpServletRequest request = TagUtil.getRequest(pageContext);
    // set request dispatcher options according to tag attributes. This
    // depends on the implementation, that using a "null" argument
    // has no effect
    final RequestDispatcherOptions opts = new RequestDispatcherOptions();
    opts.setForceResourceType(resourceType);
    opts.setReplaceSelectors(replaceSelectors);
    opts.setAddSelectors(addSelectors);
    opts.setReplaceSuffix(replaceSuffix);
    // ensure the path (if set) is absolute and normalized
    if (path != null) {
        if (!path.startsWith("/")) {
            path = request.getResource().getPath() + "/" + path;
        }
        path = ResourceUtil.normalize(path);
    }
    // check the resource
    if (resource == null) {
        if (path == null) {
            // neither resource nor path is defined, use current resource
            resource = request.getResource();
        } else {
            // check whether the path (would) resolve, else SyntheticRes.
            Resource tmp = request.getResourceResolver().resolve(path);
            if (tmp == null && resourceType != null) {
                resource = new DispatcherSyntheticResource(request.getResourceResolver(), path, resourceType);
                // remove resource type overwrite as synthetic resource
                // is correctly typed as requested
                opts.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
            }
        }
    }
    try {
        // create a dispatcher for the resource or path
        RequestDispatcher dispatcher;
        if (resource != null) {
            dispatcher = request.getRequestDispatcher(resource, opts);
        } else {
            dispatcher = request.getRequestDispatcher(path, opts);
        }
        if (dispatcher != null) {
            SlingHttpServletResponse response = new JspSlingHttpServletResponseWrapper(pageContext);
            dispatch(dispatcher, request, response);
        } else {
            TagUtil.log(log, pageContext, "No content to include...", null);
        }
    } catch (final JspTagException jte) {
        throw jte;
    } catch (final IOException ioe) {
        throw new JspTagException(ioe);
    } catch (final ServletException ce) {
        throw new JspTagException(TagUtil.getRootCause(ce));
    }
    return EVAL_PAGE;
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) JspSlingHttpServletResponseWrapper(org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) JspTagException(javax.servlet.jsp.JspTagException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 2 with JspSlingHttpServletResponseWrapper

use of org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper 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 3 with JspSlingHttpServletResponseWrapper

use of org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper 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)3 Resource (org.apache.sling.api.resource.Resource)3 JspSlingHttpServletResponseWrapper (org.apache.sling.scripting.jsp.util.JspSlingHttpServletResponseWrapper)3 Servlet (javax.servlet.Servlet)2 JspException (javax.servlet.jsp.JspException)2 BodyContent (javax.servlet.jsp.tagext.BodyContent)2 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)2 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)2 SlingBindings (org.apache.sling.api.scripting.SlingBindings)2 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)2 ServletResolver (org.apache.sling.api.servlets.ServletResolver)2 IOException (java.io.IOException)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 JspTagException (javax.servlet.jsp.JspTagException)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)1 RequestProgressTracker (org.apache.sling.api.request.RequestProgressTracker)1