io.undertow.servlet.spec.HttpServletRequestImpl Java Examples

The following examples show how to use io.undertow.servlet.spec.HttpServletRequestImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AtmosphereWebSocketUndertowDestination.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void handleNormalRequest(HttpServerExchange undertowExchange) throws Exception {
    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);

    try {
        framework.doCometSupport(AtmosphereRequestImpl.wrap(request),
                                 AtmosphereResponseImpl.wrap(response));

    } catch (ServletException e) {
        throw new IOException(e);
    }
}
 
Example #2
Source File: ServletDebugPageHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public static void handleRequest(HttpServerExchange exchange, final ServletRequestContext servletRequestContext, final Throwable exception) throws IOException {
    HttpServletRequestImpl req = servletRequestContext.getOriginalRequest();
    StringBuilder sb = new StringBuilder();
    //todo: make this good
    sb.append("<html><head><title>ERROR</title>");
    sb.append(ERROR_CSS);
    sb.append("</head><body><div class=\"header\"><div class=\"error-div\"></div><div class=\"error-text-div\">Error processing request</div></div>");
    writeLabel(sb, "Context Path", req.getContextPath());
    writeLabel(sb, "Servlet Path", req.getServletPath());
    writeLabel(sb, "Path Info", req.getPathInfo());
    writeLabel(sb, "Query String", req.getQueryString());
    writeLabel(sb, "Stack Trace", "");

    sb.append("<pre>");
    StringWriter stringWriter = new StringWriter();
    exception.printStackTrace(new PrintWriter(stringWriter));
    sb.append(escapeBodyText(stringWriter.toString()));
    sb.append("</pre></body></html>");
    servletRequestContext.getOriginalResponse().setContentType("text/html");
    servletRequestContext.getOriginalResponse().setCharacterEncoding("UTF-8");
    try {
        ServletOutputStream out = servletRequestContext.getOriginalResponse().getOutputStream();
        out.write(sb.toString().getBytes(StandardCharsets.UTF_8));
        out.close();
    } catch (IllegalStateException e) {
        PrintWriter writer = servletRequestContext.getOriginalResponse().getWriter();
        writer.write(sb.toString());
        writer.close();
    }
}
 
Example #3
Source File: ServletRequestContext.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
public ServletRequestContext(final Deployment deployment, final HttpServletRequestImpl originalRequest, final HttpServletResponseImpl originalResponse, final ServletPathMatch originalServletPathMatch) {
    this.deployment = deployment;
    this.originalRequest = originalRequest;
    this.originalResponse = originalResponse;
    this.servletRequest = originalRequest;
    this.servletResponse = originalResponse;
    this.originalServletPathMatch = originalServletPathMatch;
    this.currentServletContext = deployment.getServletContext();
}
 
Example #4
Source File: ServletInitialHandler.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final String path = exchange.getRelativePath();
    if (isForbiddenPath(path)) {
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }
    final ServletPathMatch info = paths.getServletHandlerByPath(path);
    if (info.getType() == ServletPathMatch.Type.REWRITE) {
        // this can only happen if the path ends with a /
        // otherwise there would be a redirect instead
        exchange.setRelativePath(info.getRewriteLocation());
        exchange.setRequestPath(exchange.getResolvedPath() + info.getRewriteLocation());
    }
    final HttpServletResponseImpl response = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl request = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), request, response, info);
    //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);

    Executor executor = info.getServletChain().getExecutor();
    if (executor == null) {
        executor = servletContext.getDeployment().getExecutor();
    }

    if (exchange.isInIoThread() || executor != null) {
        //either the exchange has not been dispatched yet, or we need to use a special executor
        exchange.dispatch(executor, dispatchHandler);
    } else {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    }
}
 
Example #5
Source File: AbstractResponseWrapperTestCase.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoWrapper() throws IOException, ServletException {

    TestHttpClient client = new TestHttpClient();
    try {
        HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/servletContext/");
        HttpResponse result = client.execute(get);
        Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
        final String response = HttpClientUtils.readResponse(result);
        Assert.assertEquals(HttpServletRequestImpl.class.getName() + "\n" + HttpServletResponseImpl.class.getName(), response);

    } finally {
        client.getConnectionManager().shutdown();
    }
}
 
Example #6
Source File: ServletDebugPageHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static void handleRequest(HttpServerExchange exchange, final ServletRequestContext servletRequestContext, final Throwable exception) throws IOException {
    HttpServletRequestImpl req = servletRequestContext.getOriginalRequest();
    StringBuilder sb = new StringBuilder();
    //todo: make this good
    sb.append("<html><head><title>ERROR</title>");
    sb.append(ERROR_CSS);
    sb.append("</head><body><div class=\"header\"><div class=\"error-div\"></div><div class=\"error-text-div\">Error processing request</div></div>");
    writeLabel(sb, "Context Path", req.getContextPath());
    writeLabel(sb, "Servlet Path", req.getServletPath());
    writeLabel(sb, "Path Info", req.getPathInfo());
    writeLabel(sb, "Query String", req.getQueryString());
    writeLabel(sb, "Stack Trace", "");

    sb.append("<pre>");
    StringWriter stringWriter = new StringWriter();
    exception.printStackTrace(new PrintWriter(stringWriter));
    sb.append(escapeBodyText(stringWriter.toString()));
    sb.append("</pre></body></html>");
    servletRequestContext.getOriginalResponse().setContentType("text/html");
    servletRequestContext.getOriginalResponse().setCharacterEncoding("UTF-8");
    try {
        ServletOutputStream out = servletRequestContext.getOriginalResponse().getOutputStream();
        out.write(sb.toString().getBytes(StandardCharsets.UTF_8));
        out.close();
    } catch (IllegalStateException e) {
        PrintWriter writer = servletRequestContext.getOriginalResponse().getWriter();
        writer.write(sb.toString());
        writer.close();
    }
}
 
Example #7
Source File: ServletRequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ServletRequestContext(final Deployment deployment, final HttpServletRequestImpl originalRequest, final HttpServletResponseImpl originalResponse, final ServletPathMatch originalServletPathMatch) {
    this.deployment = deployment;
    this.originalRequest = originalRequest;
    this.originalResponse = originalResponse;
    this.servletRequest = originalRequest;
    this.servletResponse = originalResponse;
    this.originalServletPathMatch = originalServletPathMatch;
    this.currentServletContext = deployment.getServletContext();
}
 
Example #8
Source File: UndertowServer.java    From digdag with Apache License 2.0 5 votes vote down vote up
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
{
    if (request instanceof HttpServletRequestImpl) {
        HttpServerExchange exchange = ((HttpServletRequestImpl) request).getExchange();
        String name = exchange.getAttachment(LISTEN_ADDRESS_NAME_ATTACHMENT);
        if (name != null) {
            request.setAttribute(GuiceRsServerRuntimeInfo.LISTEN_ADDRESS_NAME_ATTRIBUTE, name);
        }
        chain.doFilter(request, response);
    }
}
 
Example #9
Source File: UndertowWebSocketDestination.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void handleNormalRequest(HttpServerExchange undertowExchange) throws Exception {
    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);
    doService(request, response);
}
 
Example #10
Source File: UndertowHTTPTestHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange undertowExchange) throws Exception {
    try {

        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);

        // just return the response for testing
        response.getOutputStream().write(responseStr.getBytes());
        response.flushBuffer();
    } 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,
                                                      Integer.toString(errorPage.length()));
            undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
            Sender sender = undertowExchange.getResponseSender();
            sender.send(errorPage);
        }
    }
}
 
Example #11
Source File: ServletRequestContext.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
public HttpServletRequestImpl getOriginalRequest() {
    return originalRequest;
}
 
Example #12
Source File: EarlyCloseServlet.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    req.getInputStream().close();
    HttpServletRequestImpl request = ServletRequestContext.requireCurrent().getOriginalRequest();
}
 
Example #13
Source File: ServletRequestContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public HttpServletRequestImpl getOriginalRequest() {
    return originalRequest;
}
 
Example #14
Source File: ServletInitialHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final String path = exchange.getRelativePath();
    if(isForbiddenPath(path)) {
        exchange.setStatusCode(StatusCodes.NOT_FOUND);
        return;
    }
    final ServletPathMatch info = paths.getServletHandlerByPath(path);
    //https://issues.jboss.org/browse/WFLY-3439
    //if the request is an upgrade request then we don't want to redirect
    //as there is a good chance the web socket client won't understand the redirect
    //we make an exception for HTTP2 upgrade requests, as this would have already be handled at
    //the connector level if it was going to be handled.
    String upgradeString = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
    boolean isUpgradeRequest = upgradeString != null && !upgradeString.startsWith(HTTP2_UPGRADE_PREFIX);
    if (info.getType() == ServletPathMatch.Type.REDIRECT && !isUpgradeRequest) {
        //UNDERTOW-89
        //we redirect on GET requests to the root context to add an / to the end
        if(exchange.getRequestMethod().equals(Methods.GET) || exchange.getRequestMethod().equals(Methods.HEAD)) {
            exchange.setStatusCode(StatusCodes.FOUND);
        } else {
            exchange.setStatusCode(StatusCodes.TEMPORARY_REDIRECT);
        }
        exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true));
        return;
    } else if (info.getType() == ServletPathMatch.Type.REWRITE) {
        //this can only happen if the path ends with a /
        //otherwise there would be a redirect instead
        exchange.setRelativePath(info.getRewriteLocation());
        exchange.setRequestPath(exchange.getResolvedPath() + info.getRewriteLocation());
    }

    final HttpServletResponseImpl response = new HttpServletResponseImpl(exchange, servletContext);
    final HttpServletRequestImpl request = new HttpServletRequestImpl(exchange, servletContext);
    final ServletRequestContext servletRequestContext = new ServletRequestContext(servletContext.getDeployment(), request, response, info);
    //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);

    Executor executor = info.getServletChain().getExecutor();
    if (executor == null) {
        executor = servletContext.getDeployment().getExecutor();
    }

    if (exchange.isInIoThread() || executor != null) {
        //either the exchange has not been dispatched yet, or we need to use a special executor
        exchange.dispatch(executor, dispatchHandler);
    } else {
        dispatchRequest(exchange, servletRequestContext, info.getServletChain(), DispatcherType.REQUEST);
    }
}
 
Example #15
Source File: ServletInitialHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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);
    }
}
 
Example #16
Source File: UndertowHTTPHandler.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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);
        if (request.getMethod().equals(METHOD_TRACE)) {
            response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            return;
        }
        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,
                                                      Integer.toString(errorPage.length()));
            undertowExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
            Sender sender = undertowExchange.getResponseSender();
            sender.send(errorPage);
        }
    }
}