blob: 665254ad2cae2551cf98e945019f9d10e27be82c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.filter.util;
import ai.vespa.json.Jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
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 java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.util.List;
/**
* Helper methods for auth0/okta request filters.
*
* @author valerijf
*/
public class FilterUtils {
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 {
var mapper = Jackson.mapper();
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;
}
}
|