|
1 | 1 | package com.retailsvc.http; |
2 | 2 |
|
| 3 | +import static java.net.HttpURLConnection.HTTP_BAD_METHOD; |
| 4 | +import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; |
3 | 5 | import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
4 | 6 | import static java.net.HttpURLConnection.HTTP_NOT_FOUND; |
| 7 | +import static java.nio.charset.StandardCharsets.UTF_8; |
5 | 8 |
|
6 | | -import com.sun.net.httpserver.HttpExchange; |
| 9 | +import com.retailsvc.http.internal.ProblemDetailRenderer; |
7 | 10 | import com.sun.net.httpserver.HttpHandler; |
8 | 11 | import java.io.IOException; |
| 12 | +import java.util.stream.Collectors; |
9 | 13 | import org.slf4j.Logger; |
10 | 14 | import org.slf4j.LoggerFactory; |
11 | 15 |
|
12 | | -public class Handlers { |
| 16 | +public final class Handlers { |
13 | 17 |
|
14 | 18 | private static final Logger LOG = LoggerFactory.getLogger(Handlers.class); |
15 | 19 |
|
16 | 20 | private Handlers() {} |
17 | 21 |
|
18 | | - public static HttpHandler notFoundHandler() { |
19 | | - return exchange -> { |
| 22 | + public static ExceptionHandler defaultExceptionHandler() { |
| 23 | + return (exchange, t) -> { |
20 | 24 | try (exchange) { |
21 | | - endRequest(exchange, HTTP_NOT_FOUND); |
| 25 | + switch (t) { |
| 26 | + case ValidationException ve -> { |
| 27 | + byte[] body = ProblemDetailRenderer.render(ve.error()).getBytes(UTF_8); |
| 28 | + exchange.getResponseHeaders().add("Content-Type", "application/problem+json"); |
| 29 | + exchange.sendResponseHeaders(HTTP_BAD_REQUEST, body.length); |
| 30 | + exchange.getResponseBody().write(body); |
| 31 | + } |
| 32 | + case NotFoundException nf -> exchange.sendResponseHeaders(HTTP_NOT_FOUND, 0); |
| 33 | + case MethodNotAllowedException mna -> { |
| 34 | + String allow = mna.allowed().stream().map(Enum::name).collect(Collectors.joining(", ")); |
| 35 | + exchange.getResponseHeaders().add("Allow", allow); |
| 36 | + exchange.sendResponseHeaders(HTTP_BAD_METHOD, 0); |
| 37 | + } |
| 38 | + default -> { |
| 39 | + LOG.error("Unhandled exception in handler", t); |
| 40 | + exchange.sendResponseHeaders(HTTP_INTERNAL_ERROR, 0); |
| 41 | + } |
| 42 | + } |
| 43 | + } catch (IOException io) { |
| 44 | + LOG.error("Failed writing error response", io); |
22 | 45 | } |
23 | 46 | }; |
24 | 47 | } |
25 | 48 |
|
26 | | - public static ExceptionHandler internalServerErrorHandler() { |
27 | | - return (exchange, throwable) -> { |
| 49 | + public static HttpHandler notFoundHandler() { |
| 50 | + return exchange -> { |
28 | 51 | try (exchange) { |
29 | | - LOG.error("Error in handling request", throwable); |
30 | | - endRequest(exchange, HTTP_INTERNAL_ERROR); |
| 52 | + exchange.sendResponseHeaders(HTTP_NOT_FOUND, 0); |
31 | 53 | } |
32 | 54 | }; |
33 | 55 | } |
34 | | - |
35 | | - public static ExceptionHandler defaultExceptionHandler() { |
36 | | - return (exchange, e) -> internalServerErrorHandler().handleException(exchange, e); |
37 | | - } |
38 | | - |
39 | | - private static void endRequest(HttpExchange exchange, int status) throws IOException { |
40 | | - exchange.sendResponseHeaders(status, 0); |
41 | | - } |
42 | 56 | } |
0 commit comments