Search in sources :

Example 56 with HttpServerExchange

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

the class StoredResponseHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.addResponseWrapper(new ConduitWrapper<StreamSinkConduit>() {

        @Override
        public StreamSinkConduit wrap(ConduitFactory<StreamSinkConduit> factory, HttpServerExchange exchange) {
            return new StoredResponseStreamSinkConduit(factory.create(), exchange);
        }
    });
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) StoredResponseStreamSinkConduit(io.undertow.conduits.StoredResponseStreamSinkConduit) StoredResponseStreamSinkConduit(io.undertow.conduits.StoredResponseStreamSinkConduit) StreamSinkConduit(org.xnio.conduits.StreamSinkConduit)

Example 57 with HttpServerExchange

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

the class ExtendedAccessLogParser method getXParameterElement.

protected ExchangeAttribute getXParameterElement(PatternTokenizer tokenizer) throws IOException {
    if (!tokenizer.hasSubToken()) {
        UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
        return null;
    }
    final String token = tokenizer.getToken();
    if (!tokenizer.hasParameter()) {
        UndertowLogger.ROOT_LOGGER.extendedAccessLogBadXParam();
        return null;
    }
    String parameter = tokenizer.getParameter();
    if (parameter == null) {
        UndertowLogger.ROOT_LOGGER.extendedAccessLogMissingClosing();
        return null;
    }
    if ("A".equals(token)) {
        return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{sc," + parameter + "}"), "-");
    } else if ("C".equals(token)) {
        return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(new CookieAttribute(parameter), "-");
    } else if ("R".equals(token)) {
        return parser.parse("%{r," + parameter + "}");
    } else if ("S".equals(token)) {
        return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{s," + parameter + "}"), "-");
    } else if ("H".equals(token)) {
        return getServletRequestElement(parameter);
    } else if ("P".equals(token)) {
        return new SubstituteEmptyWrapper.SubstituteEmptyAttribute(parser.parse("%{rp," + parameter + "}"), "-");
    } else if ("O".equals(token)) {
        return new QuotingExchangeAttribute(new ExchangeAttribute() {

            @Override
            public String readAttribute(HttpServerExchange exchange) {
                HeaderValues values = exchange.getResponseHeaders().get(parameter);
                if (values != null && values.size() > 0) {
                    StringBuilder buffer = new StringBuilder();
                    for (int i = 0; i < values.size(); i++) {
                        String string = values.get(i);
                        buffer.append(string);
                        if (i + 1 < values.size())
                            buffer.append(",");
                    }
                    return buffer.toString();
                }
                return null;
            }

            @Override
            public void writeAttribute(HttpServerExchange exchange, String newValue) throws ReadOnlyAttributeException {
                throw new ReadOnlyAttributeException();
            }
        });
    }
    UndertowLogger.ROOT_LOGGER.extendedAccessLogCannotDecodeXParamValue(token);
    return null;
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) SecureExchangeAttribute(io.undertow.attribute.SecureExchangeAttribute) AuthenticationTypeExchangeAttribute(io.undertow.attribute.AuthenticationTypeExchangeAttribute) ConstantExchangeAttribute(io.undertow.attribute.ConstantExchangeAttribute) ExchangeAttribute(io.undertow.attribute.ExchangeAttribute) CompositeExchangeAttribute(io.undertow.attribute.CompositeExchangeAttribute) QuotingExchangeAttribute(io.undertow.attribute.QuotingExchangeAttribute) HeaderValues(io.undertow.util.HeaderValues) ReadOnlyAttributeException(io.undertow.attribute.ReadOnlyAttributeException) HttpString(io.undertow.util.HttpString) QuotingExchangeAttribute(io.undertow.attribute.QuotingExchangeAttribute) SubstituteEmptyWrapper(io.undertow.attribute.SubstituteEmptyWrapper) CookieAttribute(io.undertow.attribute.CookieAttribute)

Example 58 with HttpServerExchange

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

the class MetricsHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    if (!exchange.isComplete()) {
        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, exchange.getStatusCode());
                nextListener.proceed();
            }
        });
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 59 with HttpServerExchange

use of io.undertow.server.HttpServerExchange 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");
    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");
        }
    }
    for (Cookie cookie : exchange.requestCookies()) {
        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("          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("            scheme=" + exchange.getRequestScheme() + "\n");
    sb.append("              host=" + exchange.getRequestHeaders().getFirst(Headers.HOST) + "\n");
    sb.append("        serverPort=" + exchange.getDestinationAddress().getPort() + "\n");
    sb.append("          isSecure=" + exchange.isSecure() + "\n");
    exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

        @Override
        public void exchangeEvent(final HttpServerExchange exchange, final NextListener nextListener) {
            dumpRequestBody(exchange, sb);
            // 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");
            for (Cookie cookie : exchange.responseCookies()) {
                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("\n==============================================================");
            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 60 with HttpServerExchange

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

the class ChannelUpgradeHandler method addProtocol.

private synchronized void addProtocol(String productString, HttpUpgradeListener openListener, final ChannelListener<? super StreamConnection> channelListener, final HttpUpgradeHandshake handshake) {
    if (productString == null) {
        throw new IllegalArgumentException("productString is null");
    }
    if (openListener == null && channelListener == null) {
        throw new IllegalArgumentException("openListener is null");
    }
    if (openListener == null) {
        openListener = new HttpUpgradeListener() {

            @Override
            public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                ChannelListeners.invokeChannelListener(streamConnection, channelListener);
            }
        };
    }
    List<Holder> list = handlers.get(productString);
    if (list == null) {
        handlers.put(productString, list = new CopyOnWriteArrayList<>());
    }
    list.add(new Holder(openListener, handshake, channelListener));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

HttpServerExchange (io.undertow.server.HttpServerExchange)277 HttpHandler (io.undertow.server.HttpHandler)127 Test (org.junit.Test)109 IOException (java.io.IOException)90 UnitTest (io.undertow.testutils.category.UnitTest)45 BeforeClass (org.junit.BeforeClass)44 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 SessionConfig (io.undertow.server.session.SessionConfig)16 Map (java.util.Map)16 Sender (io.undertow.io.Sender)15 ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)14 HeaderMap (io.undertow.util.HeaderMap)13 URI (java.net.URI)13 HeaderValues (io.undertow.util.HeaderValues)12