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));
}
}
}
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);
}
}
}
}
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);
}
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));
}
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);
}
Aggregations