Search in sources :

Example 91 with SlingHttpServletRequest

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

the class SlingRequestProcessorImpl method handleError.

// ---------- Error Handling with Filters
void handleError(final int status, final String message, 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(status, message, request, response);
            }
        };
        request.getRequestProgressTracker().log("Applying " + FilterChainType.ERROR + " filters");
        try {
            processor.doFilter(request, response);
        } catch (ServletException se) {
            throw new SlingServletException(se);
        }
    } else {
        errorHandler.handleError(status, message, 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 92 with SlingHttpServletRequest

use of org.apache.sling.api.SlingHttpServletRequest 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 93 with SlingHttpServletRequest

use of org.apache.sling.api.SlingHttpServletRequest 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 94 with SlingHttpServletRequest

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

the class RequestData method toSlingHttpServletRequest.

/**
     * @param request
     * @throws IllegalArgumentException if <code>request</code> is not a
     *             <code>HttpServletRequest</code> of if <code>request</code>
     *             is not backed by <code>SlingHttpServletRequestImpl</code>.
     */
public static SlingHttpServletRequest toSlingHttpServletRequest(ServletRequest request) {
    // unwrap to SlingHttpServletRequest, may throw if no
    // SlingHttpServletRequest is wrapped in request
    SlingHttpServletRequest cRequest = unwrap(request);
    // ensure the SlingHttpServletRequest is backed by
    // SlingHttpServletRequestImpl
    RequestData.unwrap(cRequest);
    // if the request is not wrapper at all, we are done
    if (cRequest == request) {
        return cRequest;
    }
    // ensure the request is a HTTP request
    if (!(request instanceof HttpServletRequest)) {
        throw new IllegalArgumentException("Request is not an HTTP request");
    }
    // and unwrapped component response
    return new SlingServletRequestAdapter(cRequest, (HttpServletRequest) request);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SlingServletRequestAdapter(org.apache.sling.engine.impl.adapter.SlingServletRequestAdapter) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Example 95 with SlingHttpServletRequest

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

the class RequestData method getSlingHttpServletRequestFactory.

// SlingHttpServletRequest instance factory
private static SlingHttpServletRequestFactory getSlingHttpServletRequestFactory() {
    SlingHttpServletRequestFactory factory = RequestData.REQUEST_FACTORY;
    if (factory == null) {
        factory = new SlingHttpServletRequestFactory() {

            @Override
            public SlingHttpServletRequest createRequest(RequestData requestData, HttpServletRequest request) {
                return new SlingHttpServletRequestImpl(requestData, request);
            }
        };
        RequestData.REQUEST_FACTORY = factory;
    }
    return factory;
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SlingHttpServletRequestImpl(org.apache.sling.engine.impl.SlingHttpServletRequestImpl) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest)

Aggregations

SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)98 Resource (org.apache.sling.api.resource.Resource)52 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)49 Test (org.junit.Test)48 Expectations (org.jmock.Expectations)32 RewriterResponse (org.apache.sling.security.impl.ContentDispositionFilter.RewriterResponse)31 ValueMap (org.apache.sling.api.resource.ValueMap)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IOException (java.io.IOException)10 Bindings (javax.script.Bindings)10 SlingBindings (org.apache.sling.api.scripting.SlingBindings)9 ServletException (javax.servlet.ServletException)8 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)8 AbstractPipeTest (org.apache.sling.pipes.AbstractPipeTest)8 ContainerPipeTest (org.apache.sling.pipes.ContainerPipeTest)8 PrintWriter (java.io.PrintWriter)7 Map (java.util.Map)7 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)7 MockSlingHttpServlet3Request (org.apache.sling.servlets.post.impl.helper.MockSlingHttpServlet3Request)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5