Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JettyServerFactory.java
Go to the documentation of this file.
1package dev.rafex.ether.http.jetty12;
2
3/*-
4 * #%L
5 * ether-http-jetty12
6 * %%
7 * Copyright (C) 2025 - 2026 Raúl Eduardo González Argote
8 * %%
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 * #L%
27 */
28
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Objects;
32
33import org.eclipse.jetty.http.pathmap.PathSpec;
34import org.eclipse.jetty.server.Handler;
35import org.eclipse.jetty.server.Server;
36import org.eclipse.jetty.server.ServerConnector;
37import org.eclipse.jetty.server.handler.PathMappingsHandler;
38import org.eclipse.jetty.util.thread.QueuedThreadPool;
39
40import dev.rafex.ether.json.JsonCodec;
41import dev.rafex.ether.http.core.AuthPolicy;
42
43public final class JettyServerFactory {
44
45 private JettyServerFactory() {
46 }
47
48 public static JettyServerRunner create(final JettyServerConfig config, final JettyRouteRegistry routeRegistry,
49 final JsonCodec jsonCodec) {
50 return create(config, routeRegistry, jsonCodec, null, List.of(), List.of());
51 }
52
53 public static JettyServerRunner create(final JettyServerConfig config, final JettyRouteRegistry routeRegistry,
54 final JsonCodec jsonCodec, final TokenVerifier tokenVerifier, final List<AuthPolicy> authPolicies,
55 final List<JettyMiddleware> middlewares) {
56
57 Objects.requireNonNull(config, "config");
58 Objects.requireNonNull(routeRegistry, "routeRegistry");
59 Objects.requireNonNull(jsonCodec, "jsonCodec");
60
61 final var pool = new QueuedThreadPool();
62 pool.setMaxThreads(config.maxThreads());
63 pool.setMinThreads(config.minThreads());
64 pool.setIdleTimeout(config.idleTimeoutMs());
65 pool.setName(config.threadPoolName());
66
67 final var server = new Server(pool);
68 final var connector = new ServerConnector(server);
69 connector.setPort(config.port());
70 server.addConnector(connector);
71
72 final var routesHandler = buildRoutes(routeRegistry.routes());
73 final var withAuth = applyAuthPolicies(routesHandler, tokenVerifier, jsonCodec, authPolicies);
74 final var appHandler = applyMiddlewares(withAuth, middlewares);
75 server.setHandler(appHandler);
76
77 return new JettyServerRunner(server);
78 }
79
80 public static JettyServerRunner create(final JettyServerConfig config, final JsonCodec jsonCodec,
81 final TokenVerifier tokenVerifier, final List<JettyModule> modules) {
82 final var routeRegistry = new JettyRouteRegistry();
83 final var authPolicyRegistry = new JettyAuthPolicyRegistry();
84 final var middlewareRegistry = new JettyMiddlewareRegistry();
85
86 final var context = new JettyModuleContext(config, jsonCodec, tokenVerifier);
87 for (final var module : modules == null ? List.<JettyModule>of() : modules) {
88 module.registerRoutes(routeRegistry, context);
89 module.registerAuthPolicies(authPolicyRegistry, context);
90 module.registerMiddlewares(middlewareRegistry, context);
91 }
92
93 return create(config, routeRegistry, jsonCodec, tokenVerifier, authPolicyRegistry.policies(),
94 middlewareRegistry.middlewares());
95 }
96
97 private static PathMappingsHandler buildRoutes(final List<JettyRouteRegistration> routes) {
98 final var handler = new PathMappingsHandler();
99 for (final var route : routes) {
100 handler.addMapping(PathSpec.from(route.pathSpec()), route.handler());
101 }
102 return handler;
103 }
104
105 private static Handler applyAuthPolicies(final Handler delegate, final TokenVerifier tokenVerifier,
106 final JsonCodec jsonCodec, final List<AuthPolicy> policies) {
107 if (tokenVerifier == null || policies == null || policies.isEmpty()) {
108 return delegate;
109 }
110 return new JettyAuthHandler(delegate, tokenVerifier, jsonCodec).authPolicies(policies);
111 }
112
113 private static Handler applyMiddlewares(final Handler delegate, final List<JettyMiddleware> middlewares) {
114 var current = delegate;
115 final var stack = middlewares == null ? List.<JettyMiddleware>of() : new ArrayList<>(middlewares);
116 for (int i = stack.size() - 1; i >= 0; i--) {
117 current = stack.get(i).wrap(current);
118 }
119 return current;
120 }
121}
static JettyServerRunner create(final JettyServerConfig config, final JettyRouteRegistry routeRegistry, final JsonCodec jsonCodec)
static JettyServerRunner create(final JettyServerConfig config, final JettyRouteRegistry routeRegistry, final JsonCodec jsonCodec, final TokenVerifier tokenVerifier, final List< AuthPolicy > authPolicies, final List< JettyMiddleware > middlewares)
static JettyServerRunner create(final JettyServerConfig config, final JsonCodec jsonCodec, final TokenVerifier tokenVerifier, final List< JettyModule > modules)
record JettyModuleContext(JettyServerConfig config, JsonCodec jsonCodec, TokenVerifier tokenVerifier)
record JettyServerConfig(int port, int maxThreads, int minThreads, int idleTimeoutMs, String threadPoolName, String environment)