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