Search in sources :

Example 6 with Response

use of org.apache.catalina.connector.Response in project tomcat by apache.

the class ApplicationDispatcher method wrapResponse.

/**
     * Create and return a response wrapper that has been inserted in the
     * appropriate spot in the response chain.
     */
private ServletResponse wrapResponse(State state) {
    // Locate the response we should insert in front of
    ServletResponse previous = null;
    ServletResponse current = state.outerResponse;
    while (current != null) {
        if (state.hresponse == null && (current instanceof HttpServletResponse)) {
            state.hresponse = (HttpServletResponse) current;
            if (// Forward only needs hresponse
            !state.including)
                return null;
        }
        if (!(current instanceof ServletResponseWrapper))
            break;
        if (current instanceof ApplicationHttpResponse)
            break;
        if (current instanceof ApplicationResponse)
            break;
        previous = current;
        current = ((ServletResponseWrapper) current).getResponse();
    }
    // Instantiate a new wrapper at this point and insert it in the chain
    ServletResponse wrapper = null;
    if ((current instanceof ApplicationHttpResponse) || (current instanceof Response) || (current instanceof HttpServletResponse))
        wrapper = new ApplicationHttpResponse((HttpServletResponse) current, state.including);
    else
        wrapper = new ApplicationResponse(current, state.including);
    if (previous == null)
        state.outerResponse = wrapper;
    else
        ((ServletResponseWrapper) previous).setResponse(wrapper);
    state.wrapResponse = wrapper;
    return (wrapper);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.apache.catalina.connector.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponseWrapper(javax.servlet.ServletResponseWrapper)

Example 7 with Response

use of org.apache.catalina.connector.Response in project tomcat by apache.

the class ApplicationDispatcher method unwrapResponse.

/**
     * Unwrap the response if we have wrapped it.
     */
private void unwrapResponse(State state) {
    if (state.wrapResponse == null)
        return;
    if (state.outerRequest.isAsyncStarted()) {
        if (!state.outerRequest.getAsyncContext().hasOriginalRequestAndResponse()) {
            return;
        }
    }
    ServletResponse previous = null;
    ServletResponse current = state.outerResponse;
    while (current != null) {
        // If we run into the container response we are done
        if ((current instanceof Response) || (current instanceof ResponseFacade))
            break;
        // Remove the current response if it is our wrapper
        if (current == state.wrapResponse) {
            ServletResponse next = ((ServletResponseWrapper) current).getResponse();
            if (previous == null)
                state.outerResponse = next;
            else
                ((ServletResponseWrapper) previous).setResponse(next);
            break;
        }
        // Advance to the next response in the chain
        previous = current;
        current = ((ServletResponseWrapper) current).getResponse();
    }
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.apache.catalina.connector.Response) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ResponseFacade(org.apache.catalina.connector.ResponseFacade) ServletResponseWrapper(javax.servlet.ServletResponseWrapper)

Example 8 with Response

use of org.apache.catalina.connector.Response in project tomcat by apache.

the class TestAsyncContextImpl method testAsyncListenerSupplyRequestResponse.

@Test
public void testAsyncListenerSupplyRequestResponse() {
    final ServletRequest servletRequest = EasyMock.createMock(ServletRequest.class);
    final ServletResponse servletResponse = EasyMock.createMock(ServletResponse.class);
    final AsyncListener listener = new AsyncListener() {

        @Override
        public void onTimeout(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onStartAsync(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onError(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        @Override
        public void onComplete(AsyncEvent event) throws IOException {
            checkRequestResponse(event);
        }

        private void checkRequestResponse(AsyncEvent event) {
            assertEquals(servletRequest, event.getSuppliedRequest());
            assertEquals(servletResponse, event.getSuppliedResponse());
        }
    };
    final Context context = new TesterContext();
    final Response response = new Response();
    final Request request = new Request(null);
    request.setCoyoteRequest(new org.apache.coyote.Request());
    request.getMappingData().context = context;
    final AsyncContextImpl ac = new AsyncContextImpl(request);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setStarted(context, request, response, true);
    ac.addListener(listener, servletRequest, servletResponse);
    ac.setErrorState(new Exception(), true);
    ac.fireOnComplete();
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request) ServletRequest(javax.servlet.ServletRequest) AsyncEvent(javax.servlet.AsyncEvent) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.apache.catalina.connector.Response) ServletResponse(javax.servlet.ServletResponse) AsyncListener(javax.servlet.AsyncListener) TesterContext(org.apache.tomcat.unittest.TesterContext) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

Response (org.apache.catalina.connector.Response)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Context (org.apache.catalina.Context)4 IOException (java.io.IOException)3 ServletResponse (javax.servlet.ServletResponse)3 Request (org.apache.catalina.connector.Request)3 TesterContext (org.apache.tomcat.unittest.TesterContext)3 ServletException (javax.servlet.ServletException)2 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)2 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)2 TesterRequest (org.apache.tomcat.unittest.TesterRequest)2 TesterResponse (org.apache.tomcat.unittest.TesterResponse)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 Test (org.junit.Test)2 URISyntaxException (java.net.URISyntaxException)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 AuthConfigProvider (javax.security.auth.message.config.AuthConfigProvider)1 AsyncContext (javax.servlet.AsyncContext)1 AsyncEvent (javax.servlet.AsyncEvent)1