Search in sources :

Example 6 with HttpServletRequestImpl

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

the class ServletBlockingHttpExchange method close.

@Override
public void close() throws IOException {
    ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    if (!exchange.isComplete()) {
        try {
            HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
            request.closeAndDrainRequest();
        } finally {
            HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
            response.closeStreamAndWriter();
        }
    } else {
        try {
            HttpServletRequestImpl request = servletRequestContext.getOriginalRequest();
            request.freeResources();
        } finally {
            HttpServletResponseImpl response = servletRequestContext.getOriginalResponse();
            response.freeResources();
        }
    }
}
Also used : HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl)

Example 7 with HttpServletRequestImpl

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

the class ServletInitialHandler method dispatchMockRequest.

@Override
public void dispatchMockRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    final DefaultByteBufferPool bufferPool = new DefaultByteBufferPool(false, 1024, 0, 0);
    MockServerConnection connection = new MockServerConnection(bufferPool);
    HttpServerExchange exchange = new HttpServerExchange(connection);
    exchange.setRequestScheme(request.getScheme());
    exchange.setRequestMethod(new HttpString(request.getMethod()));
    exchange.setProtocol(Protocols.HTTP_1_0);
    exchange.setResolvedPath(request.getContextPath());
    String relative;
    if (request.getPathInfo() == null) {
        relative = request.getServletPath();
    } else {
        relative = request.getServletPath() + request.getPathInfo();
    }
    exchange.setRelativePath(relative);
    final ServletPathMatch info = paths.getServletHandlerByPath(request.getServletPath());
    final HttpServletResponseImpl oResponse = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl oRequest = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), oRequest, oResponse, info);
    servletRequestContext.setServletRequest(request);
    servletRequestContext.setServletResponse(response);
    //set the max request size if applicable
    if (info.getServletChain().getManagedServlet().getMaxRequestSize() > 0) {
        exchange.setMaxEntitySize(info.getServletChain().getManagedServlet().getMaxRequestSize());
    }
    exchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
    exchange.startBlocking(new ServletBlockingHttpExchange(exchange));
    servletRequestContext.setServletPathMatch(info);
    try {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        }
        throw new ServletException(e);
    }
}
Also used : DefaultByteBufferPool(io.undertow.server.DefaultByteBufferPool) HttpString(io.undertow.util.HttpString) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl) ServletBlockingHttpExchange(io.undertow.servlet.core.ServletBlockingHttpExchange) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpServerExchange(io.undertow.server.HttpServerExchange) ServletException(javax.servlet.ServletException) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) HttpString(io.undertow.util.HttpString)

Example 8 with HttpServletRequestImpl

use of io.undertow.servlet.spec.HttpServletRequestImpl in project cxf by apache.

the class UndertowHTTPHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange undertowExchange) throws Exception {
    try {
        // perform blocking operation on exchange
        if (undertowExchange.isInIoThread()) {
            undertowExchange.dispatch(this);
            return;
        }
        HttpServletResponseImpl response = new HttpServletResponseImpl(undertowExchange, (ServletContextImpl) servletContext);
        HttpServletRequestImpl request = new HttpServletRequestImpl(undertowExchange, (ServletContextImpl) servletContext);
        ServletRequestContext servletRequestContext = new ServletRequestContext(((ServletContextImpl) servletContext).getDeployment(), request, response, null);
        undertowExchange.putAttachment(ServletRequestContext.ATTACHMENT_KEY, servletRequestContext);
        request.setAttribute("HTTP_HANDLER", this);
        request.setAttribute("UNDERTOW_DESTINATION", undertowHTTPDestination);
        SSLSessionInfo ssl = undertowExchange.getConnection().getSslSessionInfo();
        if (ssl != null) {
            request.setAttribute(SSL_CIPHER_SUITE_ATTRIBUTE, ssl.getCipherSuite());
            try {
                request.setAttribute(SSL_PEER_CERT_CHAIN_ATTRIBUTE, ssl.getPeerCertificates());
            } catch (Exception e) {
            // for some case won't have the peer certification
            // do nothing
            }
        }
        undertowHTTPDestination.doService(servletContext, request, response);
    } catch (Throwable t) {
        t.printStackTrace();
        if (undertowExchange.isResponseChannelAvailable()) {
            undertowExchange.setStatusCode(500);
            final String errorPage = "<html><head><title>Error</title>" + "</head><body>Internal Error 500" + t.getMessage() + "</body></html>";
            undertowExchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + errorPage.length());
            undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
            Sender sender = undertowExchange.getResponseSender();
            sender.send(errorPage);
        }
    }
}
Also used : Sender(io.undertow.io.Sender) HttpServletRequestImpl(io.undertow.servlet.spec.HttpServletRequestImpl) SSLSessionInfo(io.undertow.server.SSLSessionInfo) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) HttpServletResponseImpl(io.undertow.servlet.spec.HttpServletResponseImpl)

Aggregations

HttpServletRequestImpl (io.undertow.servlet.spec.HttpServletRequestImpl)8 HttpServletResponseImpl (io.undertow.servlet.spec.HttpServletResponseImpl)6 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)4 Sender (io.undertow.io.Sender)2 ServletBlockingHttpExchange (io.undertow.servlet.core.ServletBlockingHttpExchange)2 HttpString (io.undertow.util.HttpString)2 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 SSLSessionInfo (io.undertow.server.SSLSessionInfo)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Executor (java.util.concurrent.Executor)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Cookie (javax.servlet.http.Cookie)1