Search in sources :

Example 1 with ExchangeCompletionListener

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

the class RunningRequestsHttpHandler method handleRequest.

/**
     * Increments the counter and registers a listener to decrement the counter upon exchange complete event.
     *
     * @param exchange
     * @throws Exception
     */
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    runningCount.increment();
    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
            runningCount.decrement();
            // Proceed to next listener must be called!
            nextListener.proceed();
        }
    });
    wrappedHandler.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 2 with ExchangeCompletionListener

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

the class DebugExtensionsHeaderHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    final StringBuilder sb = new StringBuilder();
    requestExtensions = exchange.getRequestHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING);
    if (requestExtensions != null) {
        for (String value : requestExtensions) {
            sb.append("\n").append("--- REQUEST ---").append("\n").append(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING).append(": ").append(value).append("\n");
        }
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                responseExtensions = exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING);
                if (responseExtensions != null) {
                    for (String value : responseExtensions) {
                        sb.append("\n").append("--- RESPONSE ---").append("\n").append(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING).append(": ").append(value).append("\n");
                    }
                }
                nextListener.proceed();
                UndertowLogger.REQUEST_DUMPER_LOGGER.info(sb.toString());
            }
        });
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 3 with ExchangeCompletionListener

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

the class RequestDumpingHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    final StringBuilder sb = new StringBuilder();
    // Log pre-service information
    final SecurityContext sc = exchange.getSecurityContext();
    sb.append("\n----------------------------REQUEST---------------------------\n");
    sb.append("               URI=" + exchange.getRequestURI() + "\n");
    sb.append(" characterEncoding=" + exchange.getRequestHeaders().get(Headers.CONTENT_ENCODING) + "\n");
    sb.append("     contentLength=" + exchange.getRequestContentLength() + "\n");
    sb.append("       contentType=" + exchange.getRequestHeaders().get(Headers.CONTENT_TYPE) + "\n");
    //sb.append("       contextPath=" + exchange.getContextPath());
    if (sc != null) {
        if (sc.isAuthenticated()) {
            sb.append("          authType=" + sc.getMechanismName() + "\n");
            sb.append("         principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
        } else {
            sb.append("          authType=none" + "\n");
        }
    }
    Map<String, Cookie> cookies = exchange.getRequestCookies();
    if (cookies != null) {
        for (Map.Entry<String, Cookie> entry : cookies.entrySet()) {
            Cookie cookie = entry.getValue();
            sb.append("            cookie=" + cookie.getName() + "=" + cookie.getValue() + "\n");
        }
    }
    for (HeaderValues header : exchange.getRequestHeaders()) {
        for (String value : header) {
            sb.append("            header=" + header.getHeaderName() + "=" + value + "\n");
        }
    }
    sb.append("            locale=" + LocaleUtils.getLocalesFromHeader(exchange.getRequestHeaders().get(Headers.ACCEPT_LANGUAGE)) + "\n");
    sb.append("            method=" + exchange.getRequestMethod() + "\n");
    Map<String, Deque<String>> pnames = exchange.getQueryParameters();
    for (Map.Entry<String, Deque<String>> entry : pnames.entrySet()) {
        String pname = entry.getKey();
        Iterator<String> pvalues = entry.getValue().iterator();
        sb.append("         parameter=");
        sb.append(pname);
        sb.append('=');
        while (pvalues.hasNext()) {
            sb.append(pvalues.next());
            if (pvalues.hasNext()) {
                sb.append(", ");
            }
        }
        sb.append("\n");
    }
    //sb.append("          pathInfo=" + exchange.getPathInfo());
    sb.append("          protocol=" + exchange.getProtocol() + "\n");
    sb.append("       queryString=" + exchange.getQueryString() + "\n");
    sb.append("        remoteAddr=" + exchange.getSourceAddress() + "\n");
    sb.append("        remoteHost=" + exchange.getSourceAddress().getHostName() + "\n");
    //sb.append("requestedSessionId=" + exchange.getRequestedSessionId());
    sb.append("            scheme=" + exchange.getRequestScheme() + "\n");
    sb.append("              host=" + exchange.getRequestHeaders().getFirst(Headers.HOST) + "\n");
    sb.append("        serverPort=" + exchange.getDestinationAddress().getPort() + "\n");
    //sb.append("       servletPath=" + exchange.getServletPath());
    //sb.append("          isSecure=" + exchange.isSecure());
    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) {
            // Log post-service information
            sb.append("--------------------------RESPONSE--------------------------\n");
            if (sc != null) {
                if (sc.isAuthenticated()) {
                    sb.append("          authType=" + sc.getMechanismName() + "\n");
                    sb.append("         principle=" + sc.getAuthenticatedAccount().getPrincipal() + "\n");
                } else {
                    sb.append("          authType=none" + "\n");
                }
            }
            sb.append("     contentLength=" + exchange.getResponseContentLength() + "\n");
            sb.append("       contentType=" + exchange.getResponseHeaders().getFirst(Headers.CONTENT_TYPE) + "\n");
            Map<String, Cookie> cookies = exchange.getResponseCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies.values()) {
                    sb.append("            cookie=" + cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain() + "; path=" + cookie.getPath() + "\n");
                }
            }
            for (HeaderValues header : exchange.getResponseHeaders()) {
                for (String value : header) {
                    sb.append("            header=" + header.getHeaderName() + "=" + value + "\n");
                }
            }
            sb.append("            status=" + exchange.getStatusCode() + "\n");
            String storedResponse = StoredResponse.INSTANCE.readAttribute(exchange);
            if (storedResponse != null) {
                sb.append("body=\n");
                sb.append(storedResponse);
            }
            sb.append("==============================================================");
            nextListener.proceed();
            UndertowLogger.REQUEST_DUMPER_LOGGER.info(sb.toString());
        }
    });
    // Perform the exchange
    next.handleRequest(exchange);
}
Also used : HeaderValues(io.undertow.util.HeaderValues) Deque(java.util.Deque) HttpServerExchange(io.undertow.server.HttpServerExchange) SecurityContext(io.undertow.security.api.SecurityContext) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) Map(java.util.Map)

Example 4 with ExchangeCompletionListener

use of io.undertow.server.ExchangeCompletionListener 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 5 with ExchangeCompletionListener

use of io.undertow.server.ExchangeCompletionListener 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)

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