|
1 | 1 | package com.retailsvc.http; |
2 | 2 |
|
3 | | -import static com.retailsvc.http.Handlers.notFoundHandler; |
4 | 3 | import static java.lang.Thread.ofVirtual; |
5 | | -import static java.util.Objects.isNull; |
6 | 4 | import static java.util.Objects.requireNonNull; |
7 | 5 | import static java.util.concurrent.Executors.newThreadPerTaskExecutor; |
8 | 6 |
|
9 | | -import com.retailsvc.http.openapi.OpenApiValidationFilter; |
10 | | -import com.retailsvc.http.openapi.RequestDispatchingHandler; |
11 | | -import com.retailsvc.http.openapi.model.JsonMapper; |
12 | | -import com.retailsvc.http.openapi.model.OpenApi; |
13 | | -import com.sun.net.httpserver.Filter; |
| 7 | +import com.retailsvc.http.internal.DispatchHandler; |
| 8 | +import com.retailsvc.http.internal.ExceptionFilter; |
| 9 | +import com.retailsvc.http.internal.RequestPreparationFilter; |
| 10 | +import com.retailsvc.http.internal.Router; |
| 11 | +import com.retailsvc.http.spec.Spec; |
| 12 | +import com.retailsvc.http.validate.DefaultValidator; |
14 | 13 | import com.sun.net.httpserver.HttpContext; |
15 | 14 | import com.sun.net.httpserver.HttpHandler; |
16 | 15 | import com.sun.net.httpserver.HttpServer; |
17 | 16 | import java.io.IOException; |
18 | 17 | import java.net.InetSocketAddress; |
19 | | -import java.util.List; |
20 | 18 | import java.util.Map; |
21 | 19 | import java.util.Optional; |
22 | 20 | import org.slf4j.Logger; |
|
30 | 28 | public class OpenApiServer implements AutoCloseable { |
31 | 29 |
|
32 | 30 | private static final Logger LOG = LoggerFactory.getLogger(OpenApiServer.class); |
33 | | - private static final int PORT = 8080; |
| 31 | + private static final int DEFAULT_PORT = 8080; |
34 | 32 |
|
35 | 33 | private final HttpServer httpServer; |
36 | 34 |
|
37 | 35 | /** |
38 | | - * @param specification The {@link OpenApi} specification |
39 | | - * @param requestHandlers The mappings between operationId and {@link HttpHandler} |
40 | | - * @param exceptionHandler Error handler receiving exception being thrown from a handler |
41 | | - * @throws IOException If an error occur during server start |
| 36 | + * @param spec The parsed {@link Spec} |
| 37 | + * @param jsonMapper Body deserializer |
| 38 | + * @param handlers Mappings between operationId and {@link HttpHandler} |
| 39 | + * @param exceptionHandler Error handler receiving exceptions thrown from a handler |
| 40 | + * @throws IOException If an error occurs during server start |
42 | 41 | */ |
43 | 42 | public OpenApiServer( |
44 | | - OpenApi specification, |
| 43 | + Spec spec, |
45 | 44 | JsonMapper jsonMapper, |
46 | | - Map<String, HttpHandler> requestHandlers, |
| 45 | + Map<String, HttpHandler> handlers, |
47 | 46 | ExceptionHandler exceptionHandler) |
48 | 47 | throws IOException { |
49 | | - this(specification, jsonMapper, requestHandlers, exceptionHandler, PORT); |
| 48 | + this(spec, jsonMapper, handlers, exceptionHandler, DEFAULT_PORT); |
50 | 49 | } |
51 | 50 |
|
52 | 51 | /** |
53 | | - * @param specification The {@link OpenApi} specification |
54 | | - * @param requestHandlers The mappings between operationId and {@link HttpHandler} |
55 | | - * @param exceptionHandler Error handler receiving exception being thrown from a handler |
56 | | - * @param httpPort The server port to use |
57 | | - * @throws IOException If an error occur during server start |
| 52 | + * @param spec The parsed {@link Spec} |
| 53 | + * @param jsonMapper Body deserializer |
| 54 | + * @param handlers Mappings between operationId and {@link HttpHandler} |
| 55 | + * @param exceptionHandler Error handler receiving exceptions thrown from a handler |
| 56 | + * @param port The server port to use |
| 57 | + * @throws IOException If an error occurs during server start |
58 | 58 | */ |
59 | 59 | public OpenApiServer( |
60 | | - OpenApi specification, |
| 60 | + Spec spec, |
61 | 61 | JsonMapper jsonMapper, |
62 | | - Map<String, HttpHandler> requestHandlers, |
| 62 | + Map<String, HttpHandler> handlers, |
63 | 63 | ExceptionHandler exceptionHandler, |
64 | | - int httpPort) |
| 64 | + int port) |
65 | 65 | throws IOException { |
66 | 66 |
|
67 | | - long t0 = System.currentTimeMillis(); |
68 | | - LOG.debug("Starting server..."); |
69 | | - |
70 | | - requireNonNull(specification, "OpenAPI specification must not be null"); |
71 | | - requireNonNull(jsonMapper, "Request body mapper must not be null"); |
72 | | - requireNonNull(requestHandlers, "Request handlers must not be null"); |
73 | | - |
74 | | - if (isNull(exceptionHandler)) { |
75 | | - LOG.warn("No exception handler set, using default."); |
| 67 | + requireNonNull(spec, "Spec must not be null"); |
| 68 | + requireNonNull(jsonMapper, "JsonMapper must not be null"); |
| 69 | + requireNonNull(handlers, "handlers must not be null"); |
| 70 | + if (exceptionHandler == null) { |
| 71 | + LOG.warn("No ExceptionHandler set, using default"); |
76 | 72 | exceptionHandler = Handlers.defaultExceptionHandler(); |
77 | 73 | } |
78 | 74 |
|
79 | | - httpServer = |
80 | | - initializeServer( |
81 | | - httpPort, specification, jsonMapper, requestHandlers, exceptionHandler, t0); |
82 | | - } |
83 | | - |
84 | | - public int listenPort() { |
85 | | - return httpServer.getAddress().getPort(); |
86 | | - } |
87 | | - |
88 | | - private HttpServer initializeServer( |
89 | | - int port, |
90 | | - OpenApi specification, |
91 | | - JsonMapper jsonMapper, |
92 | | - Map<String, HttpHandler> requestHandlers, |
93 | | - ExceptionHandler errorHandler, |
94 | | - long t0) |
95 | | - throws IOException { |
96 | | - |
97 | | - HttpServer server = createHttpServer(port); |
98 | | - HttpContext context = server.createContext(specification.basePath()); |
| 75 | + long t0 = System.currentTimeMillis(); |
| 76 | + Router router = new Router(spec.operations()); |
| 77 | + DefaultValidator validator = new DefaultValidator(spec::resolveSchema); |
99 | 78 |
|
100 | | - List<Filter> filters = context.getFilters(); |
101 | | - filters.add(new ExceptionHandlingFilter(errorHandler)); |
102 | | - filters.add(new BodyHandler()); |
103 | | - filters.add(new OpenApiValidationFilter(specification, jsonMapper)); |
| 79 | + this.httpServer = HttpServer.create(new InetSocketAddress(port), 0); |
| 80 | + httpServer.setExecutor(newThreadPerTaskExecutor(ofVirtual().name("http-", 0).factory())); |
104 | 81 |
|
105 | | - context.setHandler(new RequestDispatchingHandler(requestHandlers)); |
| 82 | + HttpContext ctx = httpServer.createContext(Optional.ofNullable(spec.basePath()).orElse("/")); |
| 83 | + ctx.getFilters().add(new ExceptionFilter(exceptionHandler)); |
| 84 | + ctx.getFilters().add(new RequestPreparationFilter(spec, router, validator, jsonMapper)); |
| 85 | + ctx.setHandler(new DispatchHandler(handlers)); |
106 | 86 |
|
107 | | - server.createContext("/", notFoundHandler()); |
108 | | - server.start(); |
| 87 | + httpServer.createContext("/", Handlers.notFoundHandler()); |
| 88 | + httpServer.start(); |
109 | 89 |
|
110 | | - LOG.info("Server started (port {}) in {}ms", PORT, System.currentTimeMillis() - t0); |
| 90 | + LOG.info("Server started (port {}) in {}ms", port, System.currentTimeMillis() - t0); |
| 91 | + } |
111 | 92 |
|
112 | | - return server; |
| 93 | + public int listenPort() { |
| 94 | + return httpServer.getAddress().getPort(); |
113 | 95 | } |
114 | 96 |
|
115 | 97 | @Override |
116 | 98 | public void close() { |
117 | | - Optional.ofNullable(httpServer).ifPresent(server -> server.stop(0)); |
118 | | - } |
119 | | - |
120 | | - private HttpServer createHttpServer(int port) throws IOException { |
121 | | - HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); |
122 | | - server.setExecutor(newThreadPerTaskExecutor(ofVirtual().name("http-", 0).factory())); |
123 | | - return server; |
| 99 | + if (httpServer != null) { |
| 100 | + httpServer.stop(0); |
| 101 | + } |
124 | 102 | } |
125 | 103 | } |
0 commit comments