Search in sources :

Example 51 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class SlingRequestProcessorImpl method handleError.

// just rethrow the exception as explained in the class comment
private void handleError(final Throwable throwable, final SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    // wrap the response ensuring getWriter will fall back to wrapping
    // the response output stream if reset does not reset this
    response = new ErrorResponseWrapper(response);
    FilterHandle[] filters = filterManager.getFilters(FilterChainType.ERROR);
    if (filters != null && filters.length > 0) {
        FilterChain processor = new AbstractSlingFilterChain(filters) {

            @Override
            protected void render(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
                errorHandler.handleError(throwable, request, response);
            }
        };
        request.getRequestProgressTracker().log("Applying " + FilterChainType.ERROR + " filters");
        try {
            processor.doFilter(request, response);
        } catch (ServletException se) {
            throw new SlingServletException(se);
        }
    } else {
        errorHandler.handleError(throwable, request, response);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ServletException(javax.servlet.ServletException) SlingServletException(org.apache.sling.api.SlingServletException) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) SlingServletException(org.apache.sling.api.SlingServletException) 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) AbstractSlingFilterChain(org.apache.sling.engine.impl.filter.AbstractSlingFilterChain) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 52 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class SlingRequestProcessorImpl method dispatchRequest.

// ---------- Generic Content Request processor ----------------------------
/**
     * Dispatches the request on behalf of the
     * {@link org.apache.sling.engine.impl.request.SlingRequestDispatcher}.
     */
public void dispatchRequest(ServletRequest request, ServletResponse response, Resource resource, RequestPathInfo resolvedURL, boolean include) throws IOException, ServletException {
    // we need a SlingHttpServletRequest/SlingHttpServletResponse tupel
    // to continue
    SlingHttpServletRequest cRequest = RequestData.toSlingHttpServletRequest(request);
    SlingHttpServletResponse cResponse = RequestData.toSlingHttpServletResponse(response);
    // get the request data (and btw check the correct type)
    final RequestData requestData = RequestData.getRequestData(cRequest);
    final ContentData oldContentData = requestData.getContentData();
    final ContentData contentData = requestData.setContent(resource, resolvedURL);
    try {
        // resolve the servlet
        Servlet servlet = servletResolver.resolveServlet(cRequest);
        contentData.setServlet(servlet);
        FilterChainType type = include ? FilterChainType.INCLUDE : FilterChainType.FORWARD;
        processComponent(cRequest, cResponse, type);
    } finally {
        requestData.resetContent(oldContentData);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) ContentData(org.apache.sling.engine.impl.request.ContentData) RequestData(org.apache.sling.engine.impl.request.RequestData) Servlet(javax.servlet.Servlet) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) FilterChainType(org.apache.sling.engine.impl.filter.ServletFilterManager.FilterChainType)

Example 53 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class AbstractSlingFilterChain method doFilter.

public void doFilter(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    final int filterIdx = ++this.current;
    final long start = System.currentTimeMillis();
    // the previous filter may have wrapped non-Sling request and response
    // wrappers (e.g. WebCastellum does this), so we have to make
    // sure the request and response are Sling types again
    SlingHttpServletRequest slingRequest = toSlingRequest(request);
    SlingHttpServletResponse slingResponse = toSlingResponse(response);
    try {
        if (this.current < this.filters.length) {
            // continue filtering with the next filter
            FilterHandle filter = this.filters[this.current];
            if (filter.select(slingRequest)) {
                trackFilter(slingRequest, filter);
                filter.getFilter().doFilter(slingRequest, slingResponse, this);
            } else {
                if (this.current == this.filters.length - 1) {
                    this.render(slingRequest, slingResponse);
                } else {
                    doFilter(slingRequest, slingResponse);
                }
            }
        } else {
            this.render(slingRequest, slingResponse);
        }
    } finally {
        times[filterIdx] = System.currentTimeMillis() - start;
        if (filterIdx == 0) {
            consolidateFilterTimings(slingRequest);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 54 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project sling by apache.

the class RequestData method toSlingHttpServletResponse.

/**
     * @param response
     * @throws IllegalArgumentException if <code>response</code> is not a
     *             <code>HttpServletResponse</code> of if
     *             <code>response</code> is not backed by
     *             <code>SlingHttpServletResponseImpl</code>.
     */
public static SlingHttpServletResponse toSlingHttpServletResponse(ServletResponse response) {
    // unwrap to SlingHttpServletResponse
    SlingHttpServletResponse cResponse = unwrap(response);
    // check type of response, don't care actually for the response itself
    RequestData.unwrap(cResponse);
    // are done
    if (cResponse == response) {
        return cResponse;
    }
    // ensure the response is a HTTP response
    if (!(response instanceof HttpServletResponse)) {
        throw new IllegalArgumentException("Response is not an HTTP response");
    }
    // and unwrapped component response
    return new SlingServletResponseAdapter(cResponse, (HttpServletResponse) response);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) SlingServletResponseAdapter(org.apache.sling.engine.impl.adapter.SlingServletResponseAdapter)

Example 55 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse 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)55 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)49 Resource (org.apache.sling.api.resource.Resource)39 Test (org.junit.Test)35 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)31 Expectations (org.jmock.Expectations)31 ValueMap (org.apache.sling.api.resource.ValueMap)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ServletException (javax.servlet.ServletException)5 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)5 FilterChain (javax.servlet.FilterChain)4 Servlet (javax.servlet.Servlet)4 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)4 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)4 SlingBindings (org.apache.sling.api.scripting.SlingBindings)4 ServletResolver (org.apache.sling.api.servlets.ServletResolver)4 IOException (java.io.IOException)3 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)3 AbstractSlingFilterChain (org.apache.sling.engine.impl.filter.AbstractSlingFilterChain)3 FilterHandle (org.apache.sling.engine.impl.filter.FilterHandle)3