Skip to content

Commit 140f5e7

Browse files
committed
Unwraper handler to find ContextHandler when logging context path
Previously, only the top-level handlers were examined to find the ContextHandlers and log the context path. If those handlers had been wrapped, this prevented the ContextHandlers from being found and an empty string was always logged. When finding the context path, this commit unwraps the handler held by a HandlerWrapper until the ContextHandler is found. Fixes gh-19969
1 parent f506559 commit 140f5e7

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.net.BindException;
2121
import java.util.Arrays;
2222
import java.util.List;
23+
import java.util.Objects;
2324
import java.util.stream.Collectors;
2425

2526
import org.apache.commons.logging.Log;
@@ -206,8 +207,18 @@ private String getProtocols(Connector connector) {
206207
}
207208

208209
private String getContextPath() {
209-
return Arrays.stream(this.server.getHandlers()).filter(ContextHandler.class::isInstance)
210-
.map(ContextHandler.class::cast).map(ContextHandler::getContextPath).collect(Collectors.joining(" "));
210+
return Arrays.stream(this.server.getHandlers()).map(this::findContextHandler).filter(Objects::nonNull)
211+
.map(ContextHandler::getContextPath).collect(Collectors.joining(" "));
212+
}
213+
214+
private ContextHandler findContextHandler(Handler handler) {
215+
while (handler instanceof HandlerWrapper) {
216+
if (handler instanceof ContextHandler) {
217+
return (ContextHandler) handler;
218+
}
219+
handler = ((HandlerWrapper) handler).getHandler();
220+
}
221+
return null;
211222
}
212223

213224
private void handleDeferredInitialize(Handler... handlers) throws Exception {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/AbstractJettyServletWebServerFactoryTests.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,9 @@
2424
import org.eclipse.jetty.server.ServerConnector;
2525
import org.eclipse.jetty.servlet.ServletHolder;
2626
import org.eclipse.jetty.webapp.WebAppContext;
27+
import org.junit.Test;
2728

29+
import org.springframework.boot.web.server.Compression;
2830
import org.springframework.boot.web.server.PortInUseException;
2931
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
3032
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
@@ -83,4 +85,16 @@ protected void handleExceptionCausedByBlockedPort(RuntimeException ex, int block
8385
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
8486
}
8587

88+
@Test
89+
public void contextPathIsLoggedOnStartupWhenCompressionIsEnabled() {
90+
AbstractServletWebServerFactory factory = getFactory();
91+
factory.setContextPath("/custom");
92+
Compression compression = new Compression();
93+
compression.setEnabled(true);
94+
factory.setCompression(compression);
95+
this.webServer = factory.getWebServer(exampleServletRegistration());
96+
this.webServer.start();
97+
assertThat(this.output.toString()).containsOnlyOnce("with context path '/custom'");
98+
}
99+
86100
}

0 commit comments

Comments
 (0)