Search in sources :

Example 1 with RequestDispatcherImpl

use of io.undertow.servlet.spec.RequestDispatcherImpl in project undertow by undertow-io.

the class ServletInitialHandler method handleFirstRequest.

private void handleFirstRequest(final HttpServerExchange exchange, ServletRequestContext servletRequestContext) throws Exception {
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    //set request attributes from the connector
    //generally this is only applicable if apache is sending AJP_ prefixed environment variables
    Map<String, String> attrs = exchange.getAttachment(HttpServerExchange.REQUEST_ATTRIBUTES);
    if (attrs != null) {
        for (Map.Entry<String, String> entry : attrs.entrySet()) {
            request.setAttribute(entry.getKey(), entry.getValue());
        }
    }
    servletRequestContext.setRunningInsideHandler(true);
    try {
        listeners.requestInitialized(request);
        next.handleRequest(exchange);
        //
        if (servletRequestContext.getErrorCode() > 0) {
            servletRequestContext.getOriginalResponse().doErrorDispatch(servletRequestContext.getErrorCode(), servletRequestContext.getErrorMessage());
        }
    } catch (Throwable t) {
        //by default this will just log the exception
        boolean handled = exceptionHandler.handleThrowable(exchange, request, response, t);
        if (handled) {
            exchange.endExchange();
        } else if (request.isAsyncStarted() || request.getDispatcherType() == DispatcherType.ASYNC) {
            exchange.unDispatch();
            servletRequestContext.getOriginalRequest().getAsyncContextInternal().handleError(t);
        } else {
            if (!exchange.isResponseStarted()) {
                //reset the response
                response.reset();
                exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                exchange.getResponseHeaders().clear();
                String location = servletContext.getDeployment().getErrorPages().getErrorLocation(t);
                if (location == null) {
                    location = servletContext.getDeployment().getErrorPages().getErrorLocation(StatusCodes.INTERNAL_SERVER_ERROR);
                }
                if (location != null) {
                    RequestDispatcherImpl dispatcher = new RequestDispatcherImpl(location, servletContext);
                    try {
                        dispatcher.error(servletRequestContext, request, response, servletRequestContext.getOriginalServletPathMatch().getServletChain().getManagedServlet().getServletInfo().getName(), t);
                    } catch (Exception e) {
                        UndertowLogger.REQUEST_LOGGER.exceptionGeneratingErrorPage(e, location);
                    }
                } else {
                    if (servletRequestContext.displayStackTraces()) {
                        ServletDebugPageHandler.handleRequest(exchange, servletRequestContext, t);
                    } else {
                        servletRequestContext.getOriginalResponse().doErrorDispatch(StatusCodes.INTERNAL_SERVER_ERROR, StatusCodes.INTERNAL_SERVER_ERROR_STRING);
                    }
                }
            }
        }
    } finally {
        servletRequestContext.setRunningInsideHandler(false);
        listeners.requestDestroyed(request);
    }
    //if it is not dispatched and is not a mock request
    if (!exchange.isDispatched() && !(exchange.getConnection() instanceof MockServerConnection)) {
        servletRequestContext.getOriginalResponse().responseDone();
        servletRequestContext.getOriginalRequest().clearAttributes();
    }
    if (!exchange.isDispatched()) {
        AsyncContextImpl ctx = servletRequestContext.getOriginalRequest().getAsyncContextInternal();
        if (ctx != null) {
            ctx.complete();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContextImpl(io.undertow.servlet.spec.AsyncContextImpl) HttpString(io.undertow.util.HttpString) OptionMap(org.xnio.OptionMap) Map(java.util.Map) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RequestDispatcherImpl(io.undertow.servlet.spec.RequestDispatcherImpl)

Example 2 with RequestDispatcherImpl

use of io.undertow.servlet.spec.RequestDispatcherImpl in project undertow by undertow-io.

the class MockRequestTestCase method testMockDispatch.

@Test
public void testMockDispatch() throws Exception {
    MockHttpResponse response = new MockHttpResponse();
    MockHttpRequest request = new MockHttpRequest();
    RequestDispatcher rd = deployment.getServletContext().getRequestDispatcher("/aa");
    Assert.assertNotNull(rd);
    Assert.assertTrue(rd instanceof RequestDispatcherImpl);
    RequestDispatcherImpl rdi = (RequestDispatcherImpl) rd;
    rdi.mock(request, response);
    Assert.assertEquals(HELLO_WORLD, new String(response.out.toByteArray()));
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher) RequestDispatcherImpl(io.undertow.servlet.spec.RequestDispatcherImpl) Test(org.junit.Test)

Aggregations

RequestDispatcherImpl (io.undertow.servlet.spec.RequestDispatcherImpl)2 AsyncContextImpl (io.undertow.servlet.spec.AsyncContextImpl)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Map (java.util.Map)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.Test)1 OptionMap (org.xnio.OptionMap)1