aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java
diff options
context:
space:
mode:
authorValerij Fredriksen <valerijf@verizonmedia.com>2021-07-28 09:55:00 +0200
committerValerij Fredriksen <valerijf@verizonmedia.com>2021-07-28 09:55:00 +0200
commit7a9ca4411702e0bc7492239ee6ad116527b1e4d2 (patch)
treed536c8fdc35a7223953372da23d785e63f9ea495 /container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java
parentd8acae4eeb9d66457e596fb06b0009d95b973f13 (diff)
Extract common methods for auth0/okta request filters to utils class
Diffstat (limited to 'container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java')
-rw-r--r--container-core/src/main/java/com/yahoo/jdisc/http/filter/util/FilterUtils.java72
1 files changed, 72 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);
+ }
+ }
+}