Search in sources :

Example 71 with ServletRequest

use of javax.servlet.ServletRequest 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 72 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class SSLInformationAssociationHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
    SSLSessionInfo ssl = exchange.getConnection().getSslSessionInfo();
    if (ssl != null) {
        request.setAttribute("javax.servlet.request.cipher_suite", ssl.getCipherSuite());
        request.setAttribute("javax.servlet.request.key_size", getKeyLength(ssl.getCipherSuite()));
        request.setAttribute("javax.servlet.request.ssl_session_id", ssl.getSessionId());
        X509Certificate[] certs = getCerts(ssl);
        if (certs != null) {
            request.setAttribute("javax.servlet.request.X509Certificate", certs);
        }
    }
    next.handleRequest(exchange);
}
Also used : ServletRequest(javax.servlet.ServletRequest) SSLSessionInfo(io.undertow.server.SSLSessionInfo) X509Certificate(java.security.cert.X509Certificate)

Example 73 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class FilterHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest request = servletRequestContext.getServletRequest();
    ServletResponse response = servletRequestContext.getServletResponse();
    DispatcherType dispatcher = servletRequestContext.getDispatcherType();
    Boolean supported = asyncSupported.get(dispatcher);
    if (supported != null && !supported) {
        servletRequestContext.setAsyncSupported(false);
    }
    final List<ManagedFilter> filters = this.filters.get(dispatcher);
    if (filters == null) {
        next.handleRequest(exchange);
    } else {
        final FilterChainImpl filterChain = new FilterChainImpl(exchange, filters, next, allowNonStandardWrappers);
        filterChain.doFilter(request, response);
    }
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) ManagedFilter(io.undertow.servlet.core.ManagedFilter) DispatcherType(javax.servlet.DispatcherType)

Example 74 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class ServletFormAuthenticationMechanism method servePage.

@Override
protected Integer servePage(final HttpServerExchange exchange, final String location) {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest req = servletRequestContext.getServletRequest();
    ServletResponse resp = servletRequestContext.getServletResponse();
    RequestDispatcher disp = req.getRequestDispatcher(location);
    //make sure the login page is never cached
    exchange.getResponseHeaders().add(Headers.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
    exchange.getResponseHeaders().add(Headers.PRAGMA, "no-cache");
    exchange.getResponseHeaders().add(Headers.EXPIRES, "0");
    final FormResponseWrapper respWrapper = exchange.getStatusCode() != OK && resp instanceof HttpServletResponse ? new FormResponseWrapper((HttpServletResponse) resp) : null;
    try {
        disp.forward(req, respWrapper != null ? respWrapper : resp);
    } catch (ServletException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return respWrapper != null ? respWrapper.getStatus() : null;
}
Also used : ServletException(javax.servlet.ServletException) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 75 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class ServletSecurityRoleHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest request = servletRequestContext.getServletRequest();
    if (request.getDispatcherType() == DispatcherType.REQUEST) {
        List<SingleConstraintMatch> constraints = servletRequestContext.getRequiredConstrains();
        SecurityContext sc = exchange.getSecurityContext();
        if (!authorizationManager.canAccessResource(constraints, sc.getAuthenticatedAccount(), servletRequestContext.getCurrentServlet().getManagedServlet().getServletInfo(), servletRequestContext.getOriginalRequest(), servletRequestContext.getDeployment())) {
            HttpServletResponse response = (HttpServletResponse) servletRequestContext.getServletResponse();
            response.sendError(StatusCodes.FORBIDDEN);
            return;
        }
    }
    next.handleRequest(exchange);
}
Also used : ServletRequest(javax.servlet.ServletRequest) SingleConstraintMatch(io.undertow.servlet.api.SingleConstraintMatch) SecurityContext(io.undertow.security.api.SecurityContext) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

ServletRequest (javax.servlet.ServletRequest)185 ServletResponse (javax.servlet.ServletResponse)129 HttpServletRequest (javax.servlet.http.HttpServletRequest)117 HttpServletResponse (javax.servlet.http.HttpServletResponse)95 FilterChain (javax.servlet.FilterChain)79 Test (org.junit.Test)75 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 Injector (com.google.inject.Injector)25 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)21 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14