Search in sources :

Example 1 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class CoreFormHandlingServletTest method testDoPost.

@Test
public void testDoPost() throws Exception {
    SlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    SlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doPost(request, response);
    verify(formsHandlingServletHelper).doPost(request, response);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.servlethelpers.MockSlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.servlethelpers.MockSlingHttpServletRequest) MockSlingHttpServletResponse(org.apache.sling.servlethelpers.MockSlingHttpServletResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.servlethelpers.MockSlingHttpServletRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class CoreFormHandlingServletTest method testDoFilter.

@Test
public void testDoFilter() throws Exception {
    SlingHttpServletRequest request = new MockSlingHttpServletRequest(context.resourceResolver());
    SlingHttpServletResponse response = new MockSlingHttpServletResponse();
    FilterChain filterChain = mock(FilterChain.class);
    servlet.doFilter(request, response, filterChain);
    verify(formsHandlingServletHelper).handleFilter(request, response, filterChain, EXTENSION, SELECTOR);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) MockSlingHttpServletResponse(org.apache.sling.servlethelpers.MockSlingHttpServletResponse) MockSlingHttpServletRequest(org.apache.sling.servlethelpers.MockSlingHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockSlingHttpServletResponse(org.apache.sling.servlethelpers.MockSlingHttpServletResponse) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.servlethelpers.MockSlingHttpServletRequest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with SlingHttpServletResponse

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

the class SlingRequestProcessorImpl method doProcessRequest.

/**
     * This method is directly called by the Sling main servlet.
     */
public void doProcessRequest(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final ResourceResolver resourceResolver) throws IOException {
    // setting the Sling request and response
    final RequestData requestData = new RequestData(this, servletRequest, servletResponse);
    final SlingHttpServletRequest request = requestData.getSlingRequest();
    final SlingHttpServletResponse response = requestData.getSlingResponse();
    // record the request for the web console display
    RequestHistoryConsolePlugin.recordRequest(request);
    try {
        final ServletResolver sr = this.servletResolver;
        // check that we have all required services
        if (resourceResolver == null) {
            throw new UnavailableException("ResourceResolver");
        } else if (sr == null) {
            throw new UnavailableException("ServletResolver");
        }
        // initialize the request data - resolve resource and servlet
        Resource resource = requestData.initResource(resourceResolver);
        requestData.initServlet(resource, sr);
        FilterHandle[] filters = filterManager.getFilters(FilterChainType.REQUEST);
        if (filters != null) {
            FilterChain processor = new RequestSlingFilterChain(this, filters);
            request.getRequestProgressTracker().log("Applying " + FilterChainType.REQUEST + "filters");
            processor.doFilter(request, response);
        } else {
            // no filters, directly call resource level filters and servlet
            processComponent(request, response, FilterChainType.COMPONENT);
        }
    } catch (final SlingHttpServletResponseImpl.WriterAlreadyClosedException wace) {
        log.error("Writer has already been closed.", wace);
    } catch (ResourceNotFoundException rnfe) {
        // send this exception as a 404 status
        log.info("service: Resource {} not found", rnfe.getResource());
        handleError(HttpServletResponse.SC_NOT_FOUND, rnfe.getMessage(), request, response);
    } catch (final SlingException se) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        // send this exception as is (albeit unwrapping and wrapped
        // exception.
        Throwable t = se;
        while (t instanceof SlingException && t.getCause() != null) {
            t = t.getCause();
        }
        log.error("service: Uncaught SlingException", t);
        handleError(t, request, response);
    } catch (AccessControlException ace) {
        // SLING-319 if anything goes wrong, send 403/FORBIDDEN
        log.info("service: Authenticated user {} does not have enough rights to executed requested action", request.getRemoteUser());
        handleError(HttpServletResponse.SC_FORBIDDEN, null, request, response);
    } catch (UnavailableException ue) {
        // exception is thrown before the SlingHttpServletRequest/Response
        // is properly set up due to missing dependencies. In this case
        // we must not use the Sling error handling infrastructure but
        // just return a 503 status response handled by the servlet
        // container environment
        final int status = HttpServletResponse.SC_SERVICE_UNAVAILABLE;
        final String errorMessage = ue.getMessage() + " service missing, cannot service requests";
        log.error("{} , sending status {}", errorMessage, status);
        servletResponse.sendError(status, errorMessage);
    } catch (IOException ioe) {
        // forward IOException up the call chain to properly handle it
        throw ioe;
    } catch (Throwable t) {
        // we assume, that this is the name of the causing servlet
        if (requestData.getActiveServletName() != null) {
            request.setAttribute(ERROR_SERVLET_NAME, requestData.getActiveServletName());
        }
        log.error("service: Uncaught Throwable", t);
        handleError(t, request, response);
    } finally {
        if (mbean != null) {
            mbean.addRequestData(requestData);
        }
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) FilterHandle(org.apache.sling.engine.impl.filter.FilterHandle) 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) UnavailableException(javax.servlet.UnavailableException) Resource(org.apache.sling.api.resource.Resource) AccessControlException(java.security.AccessControlException) IOException(java.io.IOException) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) RequestSlingFilterChain(org.apache.sling.engine.impl.filter.RequestSlingFilterChain) ServletResolver(org.apache.sling.api.servlets.ServletResolver) RequestData(org.apache.sling.engine.impl.request.RequestData) SlingException(org.apache.sling.api.SlingException) ResourceNotFoundException(org.apache.sling.api.resource.ResourceNotFoundException)

Example 4 with SlingHttpServletResponse

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

the class ResourceResolverInjectorTest method testFromSomethingElse.

@Test
public void testFromSomethingElse() {
    SlingHttpServletResponse response = mock(SlingHttpServletResponse.class);
    Object result = injector.getValue(response, "resourceResolver", ResourceResolver.class, element, registry);
    assertNull(result);
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) Test(org.junit.Test)

Example 5 with SlingHttpServletResponse

use of org.apache.sling.api.SlingHttpServletResponse 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)

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