Skip to content

Commit d0d55d3

Browse files
committed
Polish "Upgrade to Jetty 9.4.21.v20190926"
See gh-18536
1 parent eae2cf1 commit d0d55d3

File tree

6 files changed

+178
-149
lines changed

6 files changed

+178
-149
lines changed

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

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,37 @@
1818

1919
import java.io.IOException;
2020

21+
import javax.servlet.ServletContext;
2122
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletRequestWrapper;
2224
import javax.servlet.http.HttpServletResponse;
2325

2426
import org.eclipse.jetty.http.HttpMethod;
2527
import org.eclipse.jetty.server.Request;
2628
import org.eclipse.jetty.server.handler.ErrorHandler;
2729
import org.eclipse.jetty.server.handler.ErrorHandler.ErrorPageMapper;
2830

31+
import org.springframework.util.ReflectionUtils;
32+
2933
/**
3034
* Variation of Jetty's {@link ErrorHandler} that supports all {@link HttpMethod
31-
* HttpMethods} rather than just {@code GET}, {@code POST} and {@code HEAD}. Jetty
32-
* <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=446039">intentionally only
35+
* HttpMethods} rather than just {@code GET}, {@code POST} and {@code HEAD}. By default
36+
* Jetty <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=446039">intentionally only
3337
* supports a limited set of HTTP methods</a> for error pages, however, Spring Boot
3438
* prefers Tomcat, Jetty and Undertow to all behave in the same way.
3539
*
3640
* @author Phillip Webb
41+
* @author Christoph Dreis
3742
*/
3843
class JettyEmbeddedErrorHandler extends ErrorHandler implements ErrorPageMapper {
3944

45+
static final boolean ERROR_PAGE_FOR_METHOD_AVAILABLE;
46+
47+
static {
48+
ERROR_PAGE_FOR_METHOD_AVAILABLE = ReflectionUtils.findMethod(ErrorHandler.class, "errorPageForMethod",
49+
String.class) != null;
50+
}
51+
4052
private final ErrorHandler delegate;
4153

4254
JettyEmbeddedErrorHandler(ErrorHandler delegate) {
@@ -46,11 +58,17 @@ class JettyEmbeddedErrorHandler extends ErrorHandler implements ErrorPageMapper
4658
@Override
4759
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
4860
throws IOException {
61+
if (!ERROR_PAGE_FOR_METHOD_AVAILABLE) {
62+
String method = request.getMethod();
63+
if (!HttpMethod.GET.is(method) && !HttpMethod.POST.is(method) && !HttpMethod.HEAD.is(method)) {
64+
request = new ErrorHttpServletRequest(request);
65+
}
66+
}
4967
this.delegate.handle(target, baseRequest, request, response);
5068
}
5169

5270
@Override
53-
public boolean errorPageForMethod(String method) {
71+
public boolean errorPageForMethod(String method) { // Available in Jetty 9.4.21+
5472
return true;
5573
}
5674

@@ -62,4 +80,25 @@ public String getErrorPage(HttpServletRequest request) {
6280
return null;
6381
}
6482

83+
private static class ErrorHttpServletRequest extends HttpServletRequestWrapper {
84+
85+
private boolean simulateGetMethod = true;
86+
87+
ErrorHttpServletRequest(HttpServletRequest request) {
88+
super(request);
89+
}
90+
91+
@Override
92+
public String getMethod() {
93+
return (this.simulateGetMethod ? HttpMethod.GET.toString() : super.getMethod());
94+
}
95+
96+
@Override
97+
public ServletContext getServletContext() {
98+
this.simulateGetMethod = false;
99+
return super.getServletContext();
100+
}
101+
102+
}
103+
65104
}

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

Lines changed: 0 additions & 81 deletions
This file was deleted.

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22-
import java.lang.reflect.Method;
2322
import java.net.InetSocketAddress;
2423
import java.net.MalformedURLException;
2524
import java.net.URL;
@@ -63,7 +62,6 @@
6362
import org.springframework.context.ResourceLoaderAware;
6463
import org.springframework.core.io.ResourceLoader;
6564
import org.springframework.util.Assert;
66-
import org.springframework.util.ReflectionUtils;
6765
import org.springframework.util.StringUtils;
6866

6967
/**
@@ -342,20 +340,10 @@ private Configuration getErrorPageConfiguration() {
342340
@Override
343341
public void configure(WebAppContext context) throws Exception {
344342
ErrorHandler errorHandler = context.getErrorHandler();
345-
context.setErrorHandler(wrapErrorHandler(errorHandler));
343+
context.setErrorHandler(new JettyEmbeddedErrorHandler(errorHandler));
346344
addJettyErrorPages(errorHandler, getErrorPages());
347345
}
348346

349-
@SuppressWarnings("deprecation")
350-
private ErrorHandler wrapErrorHandler(ErrorHandler errorHandler) {
351-
Method method = ReflectionUtils.findMethod(ErrorHandler.class, "errorPageForMethod", String.class);
352-
// Versions prior to 9.4.21.v20190926 have different error handling
353-
if (method == null) {
354-
return new JettyEmbeddedLegacyErrorHandler(errorHandler);
355-
}
356-
return new JettyEmbeddedErrorHandler(errorHandler);
357-
}
358-
359347
};
360348
}
361349

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.embedded.jetty;
18+
19+
import java.nio.charset.Charset;
20+
import java.util.Locale;
21+
import java.util.Map;
22+
23+
import org.apache.jasper.servlet.JspServlet;
24+
import org.eclipse.jetty.server.ServerConnector;
25+
import org.eclipse.jetty.servlet.ServletHolder;
26+
import org.eclipse.jetty.webapp.WebAppContext;
27+
28+
import org.springframework.boot.web.server.PortInUseException;
29+
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
30+
import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
34+
/**
35+
* Abstract base class for {@link JettyServletWebServerFactory} tests.
36+
*
37+
* @author Phillip Webb
38+
*/
39+
public abstract class AbstractJettyServletWebServerFactoryTests extends AbstractServletWebServerFactoryTests {
40+
41+
@Override
42+
protected JettyServletWebServerFactory getFactory() {
43+
return new JettyServletWebServerFactory(0);
44+
}
45+
46+
@Override
47+
protected void addConnector(int port, AbstractServletWebServerFactory factory) {
48+
((JettyServletWebServerFactory) factory).addServerCustomizers((server) -> {
49+
ServerConnector connector = new ServerConnector(server);
50+
connector.setPort(port);
51+
server.addConnector(connector);
52+
});
53+
}
54+
55+
@Override
56+
protected JspServlet getJspServlet() throws Exception {
57+
WebAppContext context = (WebAppContext) ((JettyWebServer) this.webServer).getServer().getHandler();
58+
ServletHolder holder = context.getServletHandler().getServlet("jsp");
59+
if (holder == null) {
60+
return null;
61+
}
62+
holder.start();
63+
holder.initialize();
64+
return (JspServlet) holder.getServlet();
65+
}
66+
67+
@Override
68+
protected Map<String, String> getActualMimeMappings() {
69+
WebAppContext context = (WebAppContext) ((JettyWebServer) this.webServer).getServer().getHandler();
70+
return context.getMimeTypes().getMimeMap();
71+
}
72+
73+
@Override
74+
protected Charset getCharset(Locale locale) {
75+
WebAppContext context = (WebAppContext) ((JettyWebServer) this.webServer).getServer().getHandler();
76+
String charsetName = context.getLocaleEncoding(locale);
77+
return (charsetName != null) ? Charset.forName(charsetName) : null;
78+
}
79+
80+
@Override
81+
protected void handleExceptionCausedByBlockedPort(RuntimeException ex, int blockedPort) {
82+
assertThat(ex).isInstanceOf(PortInUseException.class);
83+
assertThat(((PortInUseException) ex).getPort()).isEqualTo(blockedPort);
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.web.embedded.jetty;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
23+
import org.springframework.boot.testsupport.runner.classpath.ClassPathOverrides;
24+
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link JettyServletWebServerFactory} with Jetty 9.4.19.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
@RunWith(ModifiedClassPathRunner.class)
34+
@ClassPathExclusions({ "jetty-*.jar", "tomcat-embed*.jar" })
35+
@ClassPathOverrides({ "org.eclipse.jetty:jetty-io:9.4.19.v20190610", "org.eclipse.jetty:jetty-server:9.4.19.v20190610",
36+
"org.eclipse.jetty:jetty-servlet:9.4.19.v20190610", "org.eclipse.jetty:jetty-util:9.4.19.v20190610",
37+
"org.eclipse.jetty:jetty-webapp:9.4.19.v20190610", "org.mortbay.jasper:apache-jsp:8.5.40" })
38+
public class JettyServlet9419WebServerFactoryTests extends AbstractJettyServletWebServerFactoryTests {
39+
40+
@Test
41+
public void correctVersionOfJettyUsed() {
42+
assertThat(JettyEmbeddedErrorHandler.ERROR_PAGE_FOR_METHOD_AVAILABLE).isFalse();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)