Search in sources :

Example 26 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class AsyncWebSocketHttpServerExchange method setResponseHeaders.

@Override
public void setResponseHeaders(final Map<String, List<String>> headers) {
    HeaderMap map = exchange.getRequestHeaders();
    map.clear();
    for (Map.Entry<String, List<String>> header : headers.entrySet()) {
        map.addAll(HttpString.tryFromString(header.getKey()), header.getValue());
    }
}
Also used : HeaderMap(io.undertow.util.HeaderMap) ArrayList(java.util.ArrayList) List(java.util.List) HttpString(io.undertow.util.HttpString) HashMap(java.util.HashMap) OptionMap(org.xnio.OptionMap) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 27 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class ChunkedRequestTrailersTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    existing = DefaultServer.getUndertowOptions();
    DefaultServer.setUndertowOptions(OptionMap.create(UndertowOptions.ALWAYS_SET_DATE, false));
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = (HttpServerConnection) exchange.getConnection();
                } else if (!DefaultServer.isProxy() && connection != exchange.getConnection()) {
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                final OutputStream outputStream = exchange.getOutputStream();
                final InputStream inputStream = exchange.getInputStream();
                String m = HttpClientUtils.readResponse(inputStream);
                Assert.assertEquals("abcdefghi", m);
                HeaderMap headers = exchange.getAttachment(HttpAttachments.REQUEST_TRAILERS);
                for (HeaderValues header : headers) {
                    for (String val : header) {
                        outputStream.write(header.getHeaderName().toString().getBytes());
                        outputStream.write(": ".getBytes());
                        outputStream.write(val.getBytes());
                        outputStream.write("\r\n".getBytes());
                    }
                }
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HttpServerConnection(io.undertow.server.protocol.http.HttpServerConnection) HeaderMap(io.undertow.util.HeaderMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HeaderValues(io.undertow.util.HeaderValues) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 28 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class ChunkedResponseTrailersTestCase method setup.

@BeforeClass
public static void setup() {
    final BlockingHandler blockingHandler = new BlockingHandler();
    DefaultServer.setRootHandler(blockingHandler);
    blockingHandler.setRootHandler(new HttpHandler() {

        @Override
        public void handleRequest(final HttpServerExchange exchange) {
            try {
                if (connection == null) {
                    connection = exchange.getConnection();
                } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) {
                    final OutputStream outputStream = exchange.getOutputStream();
                    outputStream.write("Connection not persistent".getBytes());
                    outputStream.close();
                    return;
                }
                HeaderMap trailers = new HeaderMap();
                exchange.putAttachment(HttpAttachments.RESPONSE_TRAILERS, trailers);
                trailers.put(HttpString.tryFromString("foo"), "fooVal");
                trailers.put(HttpString.tryFromString("bar"), "barVal");
                new StringWriteChannelListener(message).setup(exchange.getResponseChannel());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpHandler(io.undertow.server.HttpHandler) HeaderMap(io.undertow.util.HeaderMap) OutputStream(java.io.OutputStream) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) IOException(java.io.IOException) BeforeClass(org.junit.BeforeClass)

Example 29 with HeaderMap

use of io.undertow.util.HeaderMap in project undertow by undertow-io.

the class PushBuilderImpl method push.

@Override
public void push() {
    if (path == null) {
        throw UndertowServletMessages.MESSAGES.pathWasNotSet();
    }
    ServerConnection con = servletRequest.getExchange().getConnection();
    if (con.isPushSupported()) {
        HeaderMap newHeaders = new HeaderMap();
        for (HeaderValues entry : headers) {
            newHeaders.addAll(entry.getHeaderName(), entry);
        }
        if (conditional) {
            if (etag != null) {
                newHeaders.put(Headers.IF_NONE_MATCH, etag);
            } else if (lastModified != null) {
                newHeaders.put(Headers.IF_MODIFIED_SINCE, lastModified);
            }
        }
        if (sessionId != null) {
            //TODO: do this properly, may be a different tracking method or a different cookie name
            newHeaders.put(Headers.COOKIE, "JSESSIONID=" + sessionId);
        }
        String path = this.path;
        if (queryString != null && !queryString.isEmpty()) {
            path += "?" + queryString;
        }
        con.pushResource(path, new HttpString(method), newHeaders);
    }
    path = null;
    etag = null;
    lastModified = null;
}
Also used : HeaderMap(io.undertow.util.HeaderMap) HeaderValues(io.undertow.util.HeaderValues) ServerConnection(io.undertow.server.ServerConnection) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 30 with HeaderMap

use of io.undertow.util.HeaderMap in project indy by Commonjava.

the class BasicAuthenticationOAuthTranslator method authenticate.

@Override
public AuthenticationMechanismOutcome authenticate(final HttpServerExchange exchange, final SecurityContext securityContext) {
    if (!enabled) {
        return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
    }
    logger.debug("BASIC authenticate injector checking for " + AUTHORIZATION_HEADER + " header.");
    final HeaderMap headers = exchange.getRequestHeaders();
    final Collection<String> vals = headers.remove(AUTHORIZATION_HEADER);
    String basicAuth = null;
    String bearerAuth = null;
    final List<String> resultValues = new ArrayList<>();
    if (vals != null) {
        for (final String value : vals) {
            logger.debug("Found Authorization header: '{}'", value);
            if (value.toLowerCase().startsWith(BASIC_AUTH_PREFIX)) {
                logger.debug("detected basic auth");
                basicAuth = value;
            } else if (value.toLowerCase().startsWith(BEARER_AUTH_PREFIX)) {
                bearerAuth = value;
                resultValues.add(value);
            } else {
                resultValues.add(value);
            }
        }
    }
    if (bearerAuth == null && basicAuth != null) {
        final UserPass userPass = UserPass.parse(basicAuth);
        logger.debug("Parsed BASIC authorization: {}", userPass);
        if (userPass != null) {
            final AccessTokenResponse token = lookupToken(userPass);
            if (token != null) {
                final String encodedToken = token.getToken();
                logger.debug("Raw token: {}", encodedToken);
                final String value = BEARER_AUTH_PREFIX + " " + encodedToken;
                logger.debug("Adding {} value: {}", AUTHORIZATION_HEADER, value);
                logger.info("BASIC authentication translated into OAuth 2.0 bearer token. Handing off to Keycloak.");
                resultValues.add(value);
                KeycloakBearerTokenDebug.debugToken(encodedToken);
                exchange.getResponseHeaders().add(new HttpString(INDY_BEARER_TOKEN), encodedToken);
            }
        }
    }
    logger.debug("Re-adding {} values: {}", AUTHORIZATION_HEADER, resultValues);
    headers.addAll(new HttpString(AUTHORIZATION_HEADER), resultValues);
    // The best we can do is lookup the token for the given basic auth fields, and inject it for keycloak to use.
    return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
}
Also used : HeaderMap(io.undertow.util.HeaderMap) ArrayList(java.util.ArrayList) UserPass(org.commonjava.indy.subsys.http.util.UserPass) HttpString(io.undertow.util.HttpString) AccessTokenResponse(org.keycloak.representations.AccessTokenResponse) HttpString(io.undertow.util.HttpString)

Aggregations

HeaderMap (io.undertow.util.HeaderMap)30 HttpString (io.undertow.util.HttpString)17 HeaderValues (io.undertow.util.HeaderValues)9 ByteBuffer (java.nio.ByteBuffer)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)6 HttpServerExchange (io.undertow.server.HttpServerExchange)6 IOException (java.io.IOException)6 Http2HeadersStreamSinkChannel (io.undertow.protocols.http2.Http2HeadersStreamSinkChannel)3 HttpHandler (io.undertow.server.HttpHandler)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BeforeClass (org.junit.BeforeClass)3 ClientRequest (io.undertow.client.ClientRequest)2 DigestAlgorithm (io.undertow.security.idm.DigestAlgorithm)2 ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)2 ParameterLimitException (io.undertow.util.ParameterLimitException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2