Search in sources :

Example 16 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class FileErrorPageHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    exchange.addDefaultResponseListener(new DefaultResponseListener() {

        @Override
        public boolean handleDefaultResponse(final HttpServerExchange exchange) {
            Set<Integer> codes = responseCodes;
            if (!exchange.isResponseStarted() && codes.contains(exchange.getStatusCode())) {
                serveFile(exchange);
                return true;
            }
            return false;
        }
    });
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) DefaultResponseListener(io.undertow.server.DefaultResponseListener) HashSet(java.util.HashSet) Set(java.util.Set)

Example 17 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class ProxyHandler method handleRequest.

public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final ProxyClient.ProxyTarget target = proxyClient.findTarget(exchange);
    if (target == null) {
        log.debugf("No proxy target for request to %s", exchange.getRequestURL());
        next.handleRequest(exchange);
        return;
    }
    if (exchange.isResponseStarted()) {
        // we can't proxy a request that has already started, this is basically a server configuration error
        UndertowLogger.REQUEST_LOGGER.cannotProxyStartedRequest(exchange);
        exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
        exchange.endExchange();
        return;
    }
    final long timeout = maxRequestTime > 0 ? System.currentTimeMillis() + maxRequestTime : 0;
    int maxRetries = maxConnectionRetries;
    if (target instanceof ProxyClient.MaxRetriesProxyTarget) {
        maxRetries = Math.max(maxRetries, ((ProxyClient.MaxRetriesProxyTarget) target).getMaxRetries());
    }
    final ProxyClientHandler clientHandler = new ProxyClientHandler(exchange, target, timeout, maxRetries, idempotentRequestPredicate);
    if (timeout > 0) {
        final XnioExecutor.Key key = WorkerUtils.executeAfter(exchange.getIoThread(), new Runnable() {

            @Override
            public void run() {
                clientHandler.cancel(exchange);
            }
        }, maxRequestTime, TimeUnit.MILLISECONDS);
        exchange.putAttachment(TIMEOUT_KEY, key);
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                key.remove();
                nextListener.proceed();
            }
        });
    }
    exchange.dispatch(exchange.isInIoThread() ? SameThreadExecutor.INSTANCE : exchange.getIoThread(), clientHandler);
}
Also used : XnioExecutor(org.xnio.XnioExecutor) HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 18 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class Context method handleRequest.

/**
 * Handle a proxy request for this context.
 *
 * @param target       the proxy target
 * @param exchange     the http server exchange
 * @param callback     the proxy callback
 * @param timeout      the timeout
 * @param timeUnit     the time unit
 * @param exclusive    whether this connection is exclusive
 */
void handleRequest(final ModClusterProxyTarget target, final HttpServerExchange exchange, final ProxyCallback<ProxyConnection> callback, long timeout, TimeUnit timeUnit, boolean exclusive) {
    if (addRequest()) {
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                requestDone();
                nextListener.proceed();
            }
        });
        node.getConnectionPool().connect(target, exchange, callback, timeout, timeUnit, exclusive);
    } else {
        callback.failed(exchange);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 19 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class H2CUpgradeResetTestCase method beforeClass.

/**
 * Initializes the server with the H2C handler and adds the echo handler to
 * manage the requests.
 * @throws IOException Some error
 */
@BeforeClass
public static void beforeClass() throws IOException {
    final PathHandler path = new PathHandler().addExactPath(ECHO_PATH, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            sendEchoResponse(exchange);
        }
    });
    server = Undertow.builder().addHttpListener(DefaultServer.getHostPort() + 1, DefaultServer.getHostAddress(), new Http2UpgradeHandler(path)).setSocketOption(Options.REUSE_ADDRESSES, true).build();
    server.start();
    // Create xnio worker
    final Xnio xnio = Xnio.getInstance();
    final XnioWorker xnioWorker = xnio.createWorker(null, OptionMap.builder().set(Options.WORKER_IO_THREADS, 8).set(Options.TCP_NODELAY, true).set(Options.KEEP_ALIVE, true).getMap());
    worker = xnioWorker;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Http2UpgradeHandler(io.undertow.server.protocol.http2.Http2UpgradeHandler) Xnio(org.xnio.Xnio) XnioWorker(org.xnio.XnioWorker) PathHandler(io.undertow.server.handlers.PathHandler) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 20 with HttpServerExchange

use of io.undertow.server.HttpServerExchange in project undertow by undertow-io.

the class HttpClientTestCase method beforeClass.

@BeforeClass
public static void beforeClass() throws IOException {
    // Create xnio worker
    final Xnio xnio = Xnio.getInstance();
    final XnioWorker xnioWorker = xnio.createWorker(null, DEFAULT_OPTIONS);
    worker = xnioWorker;
    DefaultServer.setRootHandler(new PathHandler().addExactPath(MESSAGE, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            sendMessage(exchange);
        }
    }).addExactPath(READTIMEOUT, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.setStatusCode(StatusCodes.OK);
            exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, 5 + "");
            StreamSinkChannel responseChannel = exchange.getResponseChannel();
            responseChannel.write(ByteBuffer.wrap(new byte[] { 'a', 'b', 'c' }));
            responseChannel.flush();
            try {
                // READ_TIMEOUT set as 600ms on the client side
                // On the server side intentionally sleep 2000ms
                // to make READ_TIMEOUT happening at client side
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            responseChannel.write(ByteBuffer.wrap(new byte[] { 'd', 'e' }));
            responseChannel.close();
        }
    }).addExactPath(POST, new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() {

                @Override
                public void handle(HttpServerExchange exchange, String message) {
                    exchange.getResponseSender().send(message);
                }
            });
        }
    }));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) Xnio(org.xnio.Xnio) XnioWorker(org.xnio.XnioWorker) PathHandler(io.undertow.server.handlers.PathHandler) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) Receiver(io.undertow.io.Receiver) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)270 HttpHandler (io.undertow.server.HttpHandler)126 Test (org.junit.Test)109 IOException (java.io.IOException)87 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)43 TestHttpClient (io.undertow.testutils.TestHttpClient)42 HttpGet (org.apache.http.client.methods.HttpGet)40 HttpResponse (org.apache.http.HttpResponse)37 HttpString (io.undertow.util.HttpString)36 Header (org.apache.http.Header)24 Undertow (io.undertow.Undertow)19 ByteBuffer (java.nio.ByteBuffer)19 Map (java.util.Map)16 SessionConfig (io.undertow.server.session.SessionConfig)15 Sender (io.undertow.io.Sender)14 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 HeaderValues (io.undertow.util.HeaderValues)12 URI (java.net.URI)12