Search in sources :

Example 1 with CaptureResponseWrapper

use of org.apache.sling.scripting.core.servlet.CaptureResponseWrapper in project sling by apache.

the class IncludeTagHandler method dispatch.

protected void dispatch(RequestDispatcher dispatcher, ServletRequest request, ServletResponse response) throws IOException, ServletException {
    // optionally flush
    if (flush && !(pageContext.getOut() instanceof BodyContent)) {
        // might throw an IOException of course
        pageContext.getOut().flush();
    }
    if (var == null) {
        dispatcher.include(request, response);
    } else {
        final CaptureResponseWrapper wrapper = new CaptureResponseWrapper((HttpServletResponse) response);
        dispatcher.include(request, wrapper);
        if (!wrapper.isBinaryResponse()) {
            pageContext.setAttribute(var, wrapper.getCapturedCharacterResponse(), scope);
        }
    }
}
Also used : BodyContent(javax.servlet.jsp.tagext.BodyContent) CaptureResponseWrapper(org.apache.sling.scripting.core.servlet.CaptureResponseWrapper)

Example 2 with CaptureResponseWrapper

use of org.apache.sling.scripting.core.servlet.CaptureResponseWrapper in project sling by apache.

the class SlingIncludeAttributeTagProcessor method dispatch.

/**
     * @param resource the resource to include
     * @param path the path to include
     * @param slingHttpServletRequest the current request
     * @param slingHttpServletResponse the current response
     * @param requestDispatcherOptions the options for the request dispatcher
     * @return the character response from the include call to request dispatcher
     * @see "org.apache.sling.scripting.jsp.taglib.IncludeTagHandler"
     */
protected String dispatch(Resource resource, String path, final SlingHttpServletRequest slingHttpServletRequest, final SlingHttpServletResponse slingHttpServletResponse, final RequestDispatcherOptions requestDispatcherOptions) {
    // ensure the path (if set) is absolute and normalized
    if (path != null) {
        if (!path.startsWith("/")) {
            path = slingHttpServletRequest.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 = slingHttpServletRequest.getResource();
        } else {
            // check whether the path (would) resolve, else SyntheticRes.
            final String resourceType = requestDispatcherOptions.getForceResourceType();
            final Resource tmp = slingHttpServletRequest.getResourceResolver().resolve(path);
            if (tmp == null && resourceType != null) {
                // TODO DispatcherSyntheticResource?
                resource = new SyntheticResource(slingHttpServletRequest.getResourceResolver(), path, resourceType);
                // remove resource type overwrite as synthetic resource is correctly typed as requested
                requestDispatcherOptions.remove(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
            }
        }
    }
    try {
        // create a dispatcher for the resource or path
        final RequestDispatcher dispatcher;
        if (resource != null) {
            dispatcher = slingHttpServletRequest.getRequestDispatcher(resource, requestDispatcherOptions);
        } else {
            dispatcher = slingHttpServletRequest.getRequestDispatcher(path, requestDispatcherOptions);
        }
        if (dispatcher != null) {
            try {
                final CaptureResponseWrapper wrapper = new CaptureResponseWrapper(slingHttpServletResponse);
                dispatcher.include(slingHttpServletRequest, wrapper);
                if (!wrapper.isBinaryResponse()) {
                    return wrapper.getCapturedCharacterResponse();
                }
            } catch (ServletException e) {
                logger.error(e.getMessage(), e);
            }
        } else {
            logger.error("no request dispatcher: unable to include {}/'{}'", resource, path);
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    return null;
}
Also used : ServletException(javax.servlet.ServletException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) IOException(java.io.IOException) CaptureResponseWrapper(org.apache.sling.scripting.core.servlet.CaptureResponseWrapper) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

CaptureResponseWrapper (org.apache.sling.scripting.core.servlet.CaptureResponseWrapper)2 IOException (java.io.IOException)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 BodyContent (javax.servlet.jsp.tagext.BodyContent)1 Resource (org.apache.sling.api.resource.Resource)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1