-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlers.java
More file actions
145 lines (131 loc) · 5.67 KB
/
Copy pathHandlers.java
File metadata and controls
145 lines (131 loc) · 5.67 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.retailsvc.http;
import static java.net.HttpURLConnection.HTTP_BAD_METHOD;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.net.HttpURLConnection.HTTP_UNAVAILABLE;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.retailsvc.http.internal.ClasspathResourceHandler;
import com.retailsvc.http.internal.MethodLimitedHandler;
import com.retailsvc.http.internal.ProblemDetailRenderer;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class Handlers {
private static final Logger LOG = LoggerFactory.getLogger(Handlers.class);
private Handlers() {}
public static ExceptionHandler defaultExceptionHandler() {
return (exchange, t) -> {
try (exchange) {
switch (t) {
case ValidationException ve -> {
byte[] body = ProblemDetailRenderer.render(ve.error()).getBytes(UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/problem+json");
exchange.sendResponseHeaders(HTTP_BAD_REQUEST, body.length);
exchange.getResponseBody().write(body);
}
case NotFoundException _ -> exchange.sendResponseHeaders(HTTP_NOT_FOUND, -1);
case MethodNotAllowedException mna -> {
String allow = mna.allowed().stream().map(Enum::name).collect(Collectors.joining(", "));
exchange.getResponseHeaders().add("Allow", allow);
exchange.sendResponseHeaders(HTTP_BAD_METHOD, -1);
}
default -> {
LOG.error("Unhandled exception in handler", t);
exchange.sendResponseHeaders(HTTP_INTERNAL_ERROR, -1);
}
}
} catch (IOException io) {
LOG.error("Failed writing error response", io);
}
};
}
public static HttpHandler notFoundHandler() {
return exchange -> {
try (exchange) {
exchange.sendResponseHeaders(HTTP_NOT_FOUND, -1);
}
};
}
/** Returns 204 No Content on GET/HEAD; 405 with {@code Allow: GET, HEAD} otherwise. */
public static HttpHandler aliveHandler() {
return new MethodLimitedHandler(
exchange -> {
try (exchange) {
exchange.sendResponseHeaders(HTTP_NO_CONTENT, -1);
}
});
}
/**
* Health endpoint handler. Accepts GET and HEAD; returns 200 with {@code application/json} body
* when the supplied probe reports {@code up == true}, and 503 with the same body shape otherwise.
* A probe that throws a {@link RuntimeException} or returns {@code null} is mapped to a {@code
* Down} outcome with an empty dependency list (and 503); the failure is never propagated to the
* default exception handler.
*
* <p>The wire shape is
*
* <pre>{@code
* {"outcome":"Up","dependencies":[{"id":"jdbc","status":"Up"}]}
* }</pre>
*
* <p>Serialisation is delegated to the supplied {@code jsonMapper} — typically the same {@link
* TypeMapper} the caller registered for {@code application/json} on the server. The handler hands
* the mapper a record-shaped DTO with the components in the order shown above; any standard JSON
* library (Gson, Jackson, …) serialises it identically.
*
* @param jsonMapper used to encode the wire-shape DTO to bytes
* @param probe supplier of the current {@link HealthOutcome}
*/
public static HttpHandler healthHandler(TypeMapper jsonMapper, Supplier<HealthOutcome> probe) {
Objects.requireNonNull(jsonMapper, "jsonMapper");
Objects.requireNonNull(probe, "probe");
return new MethodLimitedHandler(
exchange -> {
try (exchange) {
HealthOutcome outcome;
try {
outcome = Objects.requireNonNull(probe.get(), "Health probe returned null");
} catch (RuntimeException e) {
LOG.warn("Health probe failed", e);
outcome = new HealthOutcome(false, List.of());
}
byte[] body = jsonMapper.writeTo(toWireShape(outcome));
int status = outcome.up() ? HTTP_OK : HTTP_UNAVAILABLE;
exchange.getResponseHeaders().add("Content-Type", "application/json");
exchange.sendResponseHeaders(status, body.length);
exchange.getResponseBody().write(body);
}
});
}
private static HealthBody toWireShape(HealthOutcome outcome) {
return new HealthBody(
label(outcome.up()),
outcome.dependencies().stream()
.map(d -> new DependencyBody(d.id(), label(d.up())))
.toList());
}
private static String label(boolean up) {
return up ? "Up" : "Down";
}
/** Wire-shape DTO for the health endpoint. Component order defines JSON field order. */
private record HealthBody(String outcome, List<DependencyBody> dependencies) {}
/** Wire-shape DTO for a single dependency entry. */
private record DependencyBody(String id, String status) {}
/**
* Serves a classpath resource. Content-Type is inferred from the file extension. The resource is
* loaded eagerly; a missing resource fails immediately with {@link IllegalArgumentException}.
*
* @param classpathResource absolute classpath path, e.g. {@code /schemas/v1/openapi.yaml}
*/
public static HttpHandler specHandler(String classpathResource) {
return new MethodLimitedHandler(new ClasspathResourceHandler(classpathResource));
}
}