diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java index e27fffd04dd..d174f2fc56a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/JettyWebServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.BindException; import java.util.Arrays; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import org.apache.commons.logging.Log; @@ -206,8 +207,18 @@ public class JettyWebServer implements WebServer { } private String getContextPath() { - return Arrays.stream(this.server.getHandlers()).filter(ContextHandler.class::isInstance) - .map(ContextHandler.class::cast).map(ContextHandler::getContextPath).collect(Collectors.joining(" ")); + return Arrays.stream(this.server.getHandlers()).map(this::findContextHandler).filter(Objects::nonNull) + .map(ContextHandler::getContextPath).collect(Collectors.joining(" ")); + } + + private ContextHandler findContextHandler(Handler handler) { + while (handler instanceof HandlerWrapper) { + if (handler instanceof ContextHandler) { + return (ContextHandler) handler; + } + handler = ((HandlerWrapper) handler).getHandler(); + } + return null; } private void handleDeferredInitialize(Handler... handlers) throws Exception { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/AbstractJettyServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/AbstractJettyServletWebServerFactoryTests.java index 4ed7c62367c..7032c59b3fe 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/AbstractJettyServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/jetty/AbstractJettyServletWebServerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,7 +24,10 @@ import org.apache.jasper.servlet.JspServlet; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.WebAppContext; +import org.junit.jupiter.api.Test; +import org.springframework.boot.testsupport.system.CapturedOutput; +import org.springframework.boot.web.server.Compression; import org.springframework.boot.web.server.PortInUseException; import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory; import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests; @@ -88,4 +91,16 @@ abstract class AbstractJettyServletWebServerFactoryTests extends AbstractServlet this.handleExceptionCausedByBlockedPortOnPrimaryConnector(ex, blockedPort); } + @Test + void contextPathIsLoggedOnStartupWhenCompressionIsEnabled(CapturedOutput output) { + AbstractServletWebServerFactory factory = getFactory(); + factory.setContextPath("/custom"); + Compression compression = new Compression(); + compression.setEnabled(true); + factory.setCompression(compression); + this.webServer = factory.getWebServer(exampleServletRegistration()); + this.webServer.start(); + assertThat(output).containsOnlyOnce("with context path '/custom'"); + } + }