Search in sources :

Example 6 with ExchangeCompletionListener

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

the class MetricsHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final long start = System.currentTimeMillis();
    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
            long time = System.currentTimeMillis() - start;
            totalResult.update((int) time);
            nextListener.proceed();
        }
    });
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 7 with ExchangeCompletionListener

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

the class HttpServerConnection method sendOutOfBandResponse.

@Override
public HttpServerExchange sendOutOfBandResponse(HttpServerExchange exchange) {
    if (exchange == null || !HttpContinue.requiresContinueResponse(exchange)) {
        throw UndertowMessages.MESSAGES.outOfBandResponseOnlyAllowedFor100Continue();
    }
    final ConduitState state = resetChannel();
    HttpServerExchange newExchange = new HttpServerExchange(this);
    for (HttpString header : exchange.getRequestHeaders().getHeaderNames()) {
        newExchange.getRequestHeaders().putAll(header, exchange.getRequestHeaders().get(header));
    }
    newExchange.setProtocol(exchange.getProtocol());
    newExchange.setRequestMethod(exchange.getRequestMethod());
    exchange.setRequestURI(exchange.getRequestURI(), exchange.isHostIncludedInRequestURI());
    exchange.setRequestPath(exchange.getRequestPath());
    exchange.setRelativePath(exchange.getRelativePath());
    newExchange.getRequestHeaders().put(Headers.CONNECTION, Headers.KEEP_ALIVE.toString());
    newExchange.getRequestHeaders().put(Headers.CONTENT_LENGTH, 0);
    newExchange.setPersistent(true);
    Connectors.terminateRequest(newExchange);
    newExchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

        @Override
        public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
            ServerFixedLengthStreamSinkConduit fixed = new ServerFixedLengthStreamSinkConduit(new HttpResponseConduit(getSinkChannel().getConduit(), getByteBufferPool(), HttpServerConnection.this, exchange), false, false);
            fixed.reset(0, exchange);
            return fixed;
        }
    });
    //we restore the read channel immediately, as this out of band response has no read side
    channel.getSourceChannel().setConduit(source(state));
    newExchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
            restoreChannel(state);
        }
    });
    return newExchange;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) HttpString(io.undertow.util.HttpString)

Example 8 with ExchangeCompletionListener

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

the class MultiPartParserDefinition method create.

@Override
public FormDataParser create(final HttpServerExchange exchange) {
    String mimeType = exchange.getRequestHeaders().getFirst(Headers.CONTENT_TYPE);
    if (mimeType != null && mimeType.startsWith(MULTIPART_FORM_DATA)) {
        String boundary = Headers.extractQuotedValueFromHeader(mimeType, "boundary");
        if (boundary == null) {
            UndertowLogger.REQUEST_LOGGER.debugf("Could not find boundary in multipart request with ContentType: %s, multipart data will not be available", mimeType);
            return null;
        }
        final MultiPartUploadHandler parser = new MultiPartUploadHandler(exchange, boundary, maxIndividualFileSize, defaultEncoding);
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) {
                IoUtils.safeClose(parser);
                nextListener.proceed();
            }
        });
        Long sizeLimit = exchange.getConnection().getUndertowOptions().get(UndertowOptions.MULTIPART_MAX_ENTITY_SIZE);
        if (sizeLimit != null) {
            exchange.setMaxEntitySize(sizeLimit);
        }
        UndertowLogger.REQUEST_LOGGER.tracef("Created multipart parser for %s", exchange);
        return parser;
    }
    return null;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 9 with ExchangeCompletionListener

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

the class Http2ServerConnection method terminateRequestChannel.

@Override
public void terminateRequestChannel(HttpServerExchange exchange) {
    if (HttpContinue.requiresContinueResponse(exchange.getRequestHeaders()) && !continueSent) {
        requestChannel.setIgnoreForceClose(true);
        requestChannel.close();
        //if this request requires a 100-continue and it was not sent we have to reset the stream
        //we do it in a completion listener though, to make sure the response is sent first
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                try {
                    channel.sendRstStream(responseChannel.getStreamId(), Http2Channel.ERROR_CANCEL);
                } finally {
                    nextListener.proceed();
                }
            }
        });
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 10 with ExchangeCompletionListener

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

the class CompletionLatchHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
            latch.countDown();
            nextListener.proceed();
        }
    });
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Aggregations

ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)12 HttpServerExchange (io.undertow.server.HttpServerExchange)12 HeaderValues (io.undertow.util.HeaderValues)2 SecurityContext (io.undertow.security.api.SecurityContext)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Deque (java.util.Deque)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)1 XnioExecutor (org.xnio.XnioExecutor)1 StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)1