diff options
4 files changed, 130 insertions, 0 deletions
diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java b/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java new file mode 100644 index 00000000000..285fab88dd9 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java @@ -0,0 +1,72 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.http.filter.util; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.TextNode; +import com.yahoo.jdisc.Response; +import com.yahoo.jdisc.handler.ContentChannel; +import com.yahoo.jdisc.handler.ResponseHandler; +import com.yahoo.jdisc.http.Cookie; +import com.yahoo.jdisc.http.filter.DiscFilterRequest; + +import java.io.UncheckedIOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Optional; + +/** + * Helper methods for auth0/okta request filters. + * + * @author valerijf + */ +public class FilterUtils { + + private static final ObjectMapper mapper = new ObjectMapper(); + + public static void sendRedirectResponse(ResponseHandler handler, List<Cookie> cookies, String location) { + Response response = createResponse(Response.Status.FOUND, cookies); + response.headers().add("Location", location); + handler.handleResponse(response).close(null); + } + + public static void sendMessageResponse(ResponseHandler handler, List<Cookie> cookies, int code, String message) { + Response response = createResponse(code, cookies); + ContentChannel contentChannel = handler.handleResponse(response); + try { + ObjectNode jsonNode = mapper.createObjectNode(); + jsonNode.set("message", TextNode.valueOf(message)); + byte[] jsonBytes = mapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(jsonNode); + contentChannel.write(ByteBuffer.wrap(jsonBytes), null); + } catch (JsonProcessingException e) { + throw new UncheckedIOException(e); + } + contentChannel.close(null); + } + + private static Response createResponse(int code, List<Cookie> cookies) { + Response response = new Response(code); + List<String> setCookieHeaders = Cookie.toSetCookieHeaders(cookies); + response.headers().add("Set-Cookie", setCookieHeaders); + return response; + } + + public static URI createUriFromRequest(DiscFilterRequest request, String path) { + Optional<Integer> port = Optional.empty(); + try { + port = Optional.ofNullable(request.getHeader("Host")) + .flatMap(host -> Optional.of(host.lastIndexOf(':') + 1) + .filter(i -> i > 0) + .map(index -> Integer.parseInt(host.substring(index)))); + } catch (NumberFormatException ignored) { } + + try { + return new URI(request.getScheme(), null, request.getServerName(), port.orElse(-1), path, null, null); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } +} diff --git a/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/package-info.java b/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/package-info.java new file mode 100644 index 00000000000..b4e7fc359f8 --- /dev/null +++ b/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/package-info.java @@ -0,0 +1,5 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +@ExportPackage +package com.yahoo.jdisc.http.filter.util; + +import com.yahoo.osgi.annotation.ExportPackage; diff --git a/container-core/src/test/java/com/yahoo/jdisc/http/filter/util/FilterUtilsTest.java b/container-core/src/test/java/com/yahoo/jdisc/http/filter/util/FilterUtilsTest.java new file mode 100644 index 00000000000..2ec7c5b90c7 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/jdisc/http/filter/util/FilterUtilsTest.java @@ -0,0 +1,48 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.jdisc.http.filter.util; + +import com.yahoo.container.jdisc.RequestHandlerTestDriver; +import com.yahoo.jdisc.Response; +import com.yahoo.jdisc.http.Cookie; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static com.yahoo.jdisc.http.filter.util.FilterUtils.sendMessageResponse; +import static com.yahoo.jdisc.http.filter.util.FilterUtils.sendRedirectResponse; +import static org.junit.Assert.assertEquals; + +/** + * @author valerijf + */ +public class FilterUtilsTest { + + private static final List<Cookie> cookies = List.of(new Cookie("my-cookie", "value1"), new Cookie("second-cookie", "value2")); + + @Test + public void redirect_test() { + RequestHandlerTestDriver.MockResponseHandler responseHandler = new RequestHandlerTestDriver.MockResponseHandler(); + + String location = "http://domain.tld/path?query"; + sendRedirectResponse(responseHandler, cookies, location); + assertEquals(302, responseHandler.getStatus()); + assertHeaders(responseHandler.getResponse(), Map.entry("Location", location), Map.entry("Set-Cookie", "my-cookie=value1"), Map.entry("Set-Cookie", "second-cookie=value2")); + } + + @Test + public void message_response() { + RequestHandlerTestDriver.MockResponseHandler responseHandler = new RequestHandlerTestDriver.MockResponseHandler(); + + sendMessageResponse(responseHandler, List.of(), 404, "Not found"); + assertEquals(404, responseHandler.getStatus()); + assertHeaders(responseHandler.getResponse()); + assertEquals("{\n \"message\" : \"Not found\"\n}", responseHandler.readAll()); + } + + @SafeVarargs + @SuppressWarnings("varargs") + private static void assertHeaders(Response response, Map.Entry<String, String>... expectedHeaders) { + assertEquals(List.of(expectedHeaders), response.headers().entries()); + } +}
\ No newline at end of file diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/HeaderFields.java b/jdisc_core/src/main/java/com/yahoo/jdisc/HeaderFields.java index 532161ee272..910da0241fa 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/HeaderFields.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/HeaderFields.java @@ -301,6 +301,11 @@ public class HeaderFields implements Map<String, List<String>> { public String setValue(String value) { throw new UnsupportedOperationException(); } + + @Override + public String toString() { + return key + '=' + value; + } } } |