Search in sources :

Example 6 with HttpString

use of io.undertow.util.HttpString in project camel by apache.

the class DefaultUndertowHttpBinding method populateCamelHeaders.

@Override
public void populateCamelHeaders(HttpServerExchange httpExchange, Map<String, Object> headersMap, Exchange exchange) throws Exception {
    LOG.trace("populateCamelHeaders: {}");
    // NOTE: these headers is applied using the same logic as camel-http/camel-jetty to be consistent
    headersMap.put(Exchange.HTTP_METHOD, httpExchange.getRequestMethod().toString());
    // strip query parameters from the uri
    headersMap.put(Exchange.HTTP_URL, httpExchange.getRequestURL());
    // uri is without the host and port
    headersMap.put(Exchange.HTTP_URI, httpExchange.getRequestURI());
    headersMap.put(Exchange.HTTP_QUERY, httpExchange.getQueryString());
    headersMap.put(Exchange.HTTP_RAW_QUERY, httpExchange.getQueryString());
    String path = httpExchange.getRequestPath();
    UndertowEndpoint endpoint = (UndertowEndpoint) exchange.getFromEndpoint();
    if (endpoint.getHttpURI() != null) {
        // need to match by lower case as we want to ignore case on context-path
        String endpointPath = endpoint.getHttpURI().getPath();
        String matchPath = path.toLowerCase(Locale.US);
        String match = endpointPath.toLowerCase(Locale.US);
        if (match != null && matchPath.startsWith(match)) {
            path = path.substring(endpointPath.length());
        }
    }
    headersMap.put(Exchange.HTTP_PATH, path);
    if (LOG.isTraceEnabled()) {
        LOG.trace("HTTP-Method {}", httpExchange.getRequestMethod());
        LOG.trace("HTTP-Uri {}", httpExchange.getRequestURI());
    }
    for (HttpString name : httpExchange.getRequestHeaders().getHeaderNames()) {
        //String name = httpName.toString();
        if (name.toString().toLowerCase(Locale.US).equals("content-type")) {
            name = ExchangeHeaders.CONTENT_TYPE;
        }
        if (name.toString().toLowerCase(Locale.US).equals("authorization")) {
            String value = httpExchange.getRequestHeaders().get(name).toString();
            // store a special header that this request was authenticated using HTTP Basic
            if (value != null && value.trim().startsWith("Basic")) {
                if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(Exchange.AUTHENTICATION, "Basic", exchange)) {
                    UndertowHelper.appendHeader(headersMap, Exchange.AUTHENTICATION, "Basic");
                }
            }
        }
        // add the headers one by one, and use the header filter strategy
        Iterator<?> it = httpExchange.getRequestHeaders().get(name).iterator();
        while (it.hasNext()) {
            Object value = it.next();
            LOG.trace("HTTP-header: {}", value);
            if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name.toString(), value, exchange)) {
                UndertowHelper.appendHeader(headersMap, name.toString(), value);
            }
        }
    }
    //process uri parameters as headers
    Map<String, Deque<String>> pathParameters = httpExchange.getQueryParameters();
    //continue if the map is not empty, otherwise there are no params
    if (!pathParameters.isEmpty()) {
        for (Map.Entry<String, Deque<String>> entry : pathParameters.entrySet()) {
            String name = entry.getKey();
            Object values = entry.getValue();
            Iterator<?> it = ObjectHelper.createIterator(values);
            while (it.hasNext()) {
                Object value = it.next();
                LOG.trace("URI-Parameter: {}", value);
                if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, exchange)) {
                    UndertowHelper.appendHeader(headersMap, name, value);
                }
            }
        }
    }
    // Create headers for REST path placeholder variables
    Map<String, Object> predicateContextParams = httpExchange.getAttachment(Predicate.PREDICATE_CONTEXT);
    if (predicateContextParams != null) {
        // Remove this as it's an unwanted artifact of our Undertow predicate chain
        predicateContextParams.remove("remaining");
        for (String paramName : predicateContextParams.keySet()) {
            LOG.trace("REST Template Variable {}: {})", paramName, predicateContextParams.get(paramName));
            headersMap.put(paramName, predicateContextParams.get(paramName));
        }
    }
}
Also used : HttpString(io.undertow.util.HttpString) Deque(java.util.Deque) HashMap(java.util.HashMap) HeaderMap(io.undertow.util.HeaderMap) Map(java.util.Map) HttpString(io.undertow.util.HttpString)

Example 7 with HttpString

use of io.undertow.util.HttpString in project camel by apache.

the class DefaultUndertowHttpBinding method populateCamelHeaders.

@Override
public void populateCamelHeaders(ClientResponse response, Map<String, Object> headersMap, Exchange exchange) throws Exception {
    LOG.trace("populateCamelHeaders: {}");
    headersMap.put(Exchange.HTTP_RESPONSE_CODE, response.getResponseCode());
    for (HttpString name : response.getResponseHeaders().getHeaderNames()) {
        //String name = httpName.toString();
        if (name.toString().toLowerCase(Locale.US).equals("content-type")) {
            name = ExchangeHeaders.CONTENT_TYPE;
        }
        if (name.toString().toLowerCase(Locale.US).equals("authorization")) {
            String value = response.getResponseHeaders().get(name).toString();
            // store a special header that this request was authenticated using HTTP Basic
            if (value != null && value.trim().startsWith("Basic")) {
                if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(Exchange.AUTHENTICATION, "Basic", exchange)) {
                    UndertowHelper.appendHeader(headersMap, Exchange.AUTHENTICATION, "Basic");
                }
            }
        }
        // add the headers one by one, and use the header filter strategy
        Iterator<?> it = response.getResponseHeaders().get(name).iterator();
        while (it.hasNext()) {
            Object value = it.next();
            LOG.trace("HTTP-header: {}", value);
            if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(name.toString(), value, exchange)) {
                UndertowHelper.appendHeader(headersMap, name.toString(), value);
            }
        }
    }
}
Also used : HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 8 with HttpString

use of io.undertow.util.HttpString in project wildfly by wildfly.

the class SetHeaderHandler method handleRequest.

public void handleRequest(HttpServerExchange exchange) throws Exception {
    exchange.getResponseHeaders().put(new HttpString(name), value);
    next.handleRequest(exchange);
}
Also used : HttpString(io.undertow.util.HttpString)

Example 9 with HttpString

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

the class PredicateParsingTestCase method testPredicateContextVariable.

@Test
public void testPredicateContextVariable() {
    Predicate predicate = PredicateParser.parse("regex[pattern=\"/publicdb/(.*?)/.*\", value=\"%R\", full-match=false] and equals[%{i,username}, ${1}]", PredicateParsingTestCase.class.getClassLoader());
    HttpServerExchange e = new HttpServerExchange(null);
    e.setRelativePath("/publicdb/foo/bar");
    Assert.assertFalse(predicate.resolve(e));
    e.getRequestHeaders().add(new HttpString("username"), "foo");
    Assert.assertTrue(predicate.resolve(e));
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString) UnitTest(io.undertow.testutils.category.UnitTest) Test(org.junit.Test)

Example 10 with HttpString

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

the class SimpleParserTestCase method runTest.

private void runTest(final byte[] in, String lastHeader) throws HttpRequestParser.BadRequestException {
    parseState.reset();
    HttpServerExchange result = new HttpServerExchange(null);
    HttpRequestParser.instance(OptionMap.EMPTY).handle(ByteBuffer.wrap(in), parseState, result);
    Assert.assertSame(Methods.GET, result.getRequestMethod());
    Assert.assertEquals("/somepath", result.getRequestURI());
    Assert.assertSame(Protocols.HTTP_1_1, result.getProtocol());
    Assert.assertEquals(2, result.getRequestHeaders().getHeaderNames().size());
    Assert.assertEquals("www.somehost.net", result.getRequestHeaders().getFirst(new HttpString("Host")));
    Assert.assertEquals(lastHeader, result.getRequestHeaders().getFirst(new HttpString("OtherHeader")));
    Assert.assertEquals(ParseState.PARSE_COMPLETE, parseState.state);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpString(io.undertow.util.HttpString)

Aggregations

HttpString (io.undertow.util.HttpString)59 IOException (java.io.IOException)18 HttpServerExchange (io.undertow.server.HttpServerExchange)16 HeaderMap (io.undertow.util.HeaderMap)15 HttpHandler (io.undertow.server.HttpHandler)9 ByteBuffer (java.nio.ByteBuffer)9 Map (java.util.Map)8 HashMap (java.util.HashMap)7 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)6 HeaderValues (io.undertow.util.HeaderValues)6 ClientRequest (io.undertow.client.ClientRequest)5 Session (io.undertow.server.session.Session)5 URISyntaxException (java.net.URISyntaxException)5 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 InMemorySessionManager (io.undertow.server.session.InMemorySessionManager)4 SessionAttachmentHandler (io.undertow.server.session.SessionAttachmentHandler)4 SessionManager (io.undertow.server.session.SessionManager)4 List (java.util.List)4 TypeConverter (org.apache.camel.TypeConverter)4