Skip to content

Commit 57e9a43

Browse files
committed
Support for undertow-handlers.conf file
1 parent 8d84db7 commit 57e9a43

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Version 3.11.0 (2020-08-06)
1+
# Version 3.11.0 (2020-10-28)
22

33
* [brk] Config functions that find available ports now take a port name as argument so they can return the same port for each evaluation (example: `$availableTcpPort()` becomes `$availableTcpPort('web')`).
44
* [new] Support for configuration of Undertow handlers using `undertow-handlers.conf` file (at the root of the classpath by default).

web/specs/src/main/java/org/seedstack/seed/web/WebConfig.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public static class ServerConfig {
254254
private static final String DEFAULT_HOST = "0.0.0.0";
255255
private static final int DEFAULT_PORT = 8080;
256256
private static final int DEFAULT_SECURE_PORT = 8443;
257-
private static final String DEFAULT_CONTEXT_PATH = "/";
257+
private static final String ROOT_CONTEXT_PATH = "/";
258258
private static final boolean DEFAULT_HTTP_ACTIVATION = true;
259259
private static final boolean DEFAULT_HTTPS_ACTIVATION = false;
260260
private static final boolean DEFAULT_HTTP2_ACTIVATION = true;
@@ -272,7 +272,7 @@ public static class ServerConfig {
272272
@Max(65535)
273273
private int securePort = DEFAULT_SECURE_PORT;
274274
@NotBlank
275-
private String contextPath = DEFAULT_CONTEXT_PATH;
275+
private String contextPath = ROOT_CONTEXT_PATH;
276276
private boolean http = DEFAULT_HTTP_ACTIVATION;
277277
private boolean https = DEFAULT_HTTPS_ACTIVATION;
278278
private boolean http2 = DEFAULT_HTTP2_ACTIVATION;
@@ -332,6 +332,10 @@ public ServerConfig setContextPath(String contextPath) {
332332
return this;
333333
}
334334

335+
public boolean isRootContextPath() {
336+
return ROOT_CONTEXT_PATH.equals(this.contextPath);
337+
}
338+
335339
public boolean isHttp() {
336340
return http;
337341
}

web/undertow/src/main/java/org/seedstack/seed/undertow/internal/ServerFactory.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,30 @@ class ServerFactory {
4242
}
4343

4444
Undertow createServer(HttpHandler httpHandler, SSLProvider sslProvider) {
45-
InputStream handlersFile = classLoader.getResourceAsStream(undertowConfig.getHandlersFile());
45+
HttpHandler effectiveHttpHandler;
46+
47+
// Configure context path if any
48+
if (!serverConfig.isRootContextPath()) {
49+
effectiveHttpHandler = Handlers
50+
.path(Handlers.redirect(serverConfig.getContextPath()))
51+
.addPrefixPath(serverConfig.getContextPath(), httpHandler);
52+
} else {
53+
effectiveHttpHandler = httpHandler;
54+
}
55+
4656
// Configure handlers if any
57+
InputStream handlersFile = classLoader.getResourceAsStream(undertowConfig.getHandlersFile());
4758
if (handlersFile != null) {
4859
List<PredicatedHandler> handlers = PredicatedHandlersParser.parse(
4960
handlersFile,
5061
classLoader
5162
);
52-
// TODO
63+
effectiveHttpHandler = Handlers.predicates(handlers, effectiveHttpHandler);
5364
}
5465

55-
Undertow.Builder builder = Undertow.builder().setWorker(xnioWorker);
5666

5767
// Configure HTTP(s) listeners
68+
Undertow.Builder builder = Undertow.builder().setWorker(xnioWorker);
5869
if (!serverConfig.isHttp() && !serverConfig.isHttps()) {
5970
throw SeedException.createNew(UndertowErrorCode.NO_LISTENER_CONFIGURED);
6071
} else {
@@ -70,10 +81,9 @@ Undertow createServer(HttpHandler httpHandler, SSLProvider sslProvider) {
7081
}
7182
}
7283

73-
// Configure context path
74-
return builder.setHandler(Handlers
75-
.path(Handlers.redirect(serverConfig.getContextPath()))
76-
.addPrefixPath(serverConfig.getContextPath(), httpHandler))
84+
// Build the server
85+
return builder
86+
.setHandler(effectiveHttpHandler)
7787
.build();
7888
}
7989

web/undertow/src/test/java/org/seedstack/seed/undertow/AbstractUndertowIT.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,14 @@ public void sessionCookieConfigIsHonored() {
120120
.get(baseUrl + "/sessionTest");
121121
}
122122

123+
@Test
124+
public void handlersFileIsHonored() {
125+
expect()
126+
.statusCode(200)
127+
.header("handlersFileHeader", "testValue")
128+
.when()
129+
.get(baseUrl + "/index.html");
130+
}
131+
123132
abstract ResponseSpecification expect();
124133
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
header(header='handlersFileHeader', value='testValue')

0 commit comments

Comments
 (0)