Skip to content

Commit 25aabcc

Browse files
author
Phillip Webb
committed
Merge pull request spring-projects#542 from snicoll/spring-projectsgh-540
* spring-projectsgh-540: Support Tomcat uri encoding (with UTF-8 default)
2 parents 213245c + 7eff0fa commit 25aabcc

File tree

5 files changed

+72
-2
lines changed

5 files changed

+72
-2
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* {@link EmbeddedServletContainerCustomizerBeanPostProcessor} is active.
4444
*
4545
* @author Dave Syer
46+
* @author Stephane Nicoll
4647
*/
4748
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = false)
4849
public class ServerProperties implements EmbeddedServletContainerCustomizer {
@@ -144,6 +145,8 @@ public static class Tomcat {
144145

145146
private int maxThreads = 0; // Number of threads in protocol handler
146147

148+
private String uriEncoding;
149+
147150
public int getMaxThreads() {
148151
return this.maxThreads;
149152
}
@@ -200,6 +203,14 @@ public void setRemoteIpHeader(String remoteIpHeader) {
200203
this.remoteIpHeader = remoteIpHeader;
201204
}
202205

206+
public String getUriEncoding() {
207+
return this.uriEncoding;
208+
}
209+
210+
public void setUriEncoding(String uriEncoding) {
211+
this.uriEncoding = uriEncoding;
212+
}
213+
203214
void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
204215
if (getBasedir() != null) {
205216
factory.setBaseDirectory(getBasedir());
@@ -247,6 +258,9 @@ public void customize(Connector connector) {
247258
valve.setSuffix(".log");
248259
factory.addContextValves(valve);
249260
}
261+
if (getUriEncoding() != null) {
262+
factory.setUriEncoding(getUriEncoding());
263+
}
250264
}
251265

252266
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Tests for {@link ServerProperties}.
3737
*
3838
* @author Dave Syer
39+
* @author Stephane Nicoll
3940
*/
4041
public class ServerPropertiesTests {
4142

@@ -87,4 +88,13 @@ public void testCustomizeTomcatPort() throws Exception {
8788
verify(factory).setPort(8080);
8889
}
8990

91+
@Test
92+
public void testCustomizeUriEncoding() throws Exception {
93+
Map<String, String> map = new HashMap<String, String>();
94+
map.put("server.tomcat.uriEncoding", "US-ASCII");
95+
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
96+
map));
97+
assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding());
98+
}
99+
90100
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ server.tomcat.remote-ip-header=x-forwarded-for
6161
server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
6262
server.tomcat.background-processor-delay=30; # in seconds
6363
server.tomcat.max-threads = 0 # number of threads in protocol handler
64+
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
6465
6566
# SPRING MVC ({sc-spring-boot-autoconfigure}/web/HttpMapperProperties.{sc-ext}[HttpMapperProperties])
6667
http.mappers.json-pretty-print=false # pretty print JSON

spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @author Phillip Webb
6767
* @author Dave Syer
6868
* @author Brock Mills
69+
* @author Stephane Nicoll
6970
* @see #setPort(int)
7071
* @see #setContextLifecycleListeners(Collection)
7172
* @see TomcatEmbeddedServletContainer
@@ -93,6 +94,8 @@ public class TomcatEmbeddedServletContainerFactory extends
9394

9495
private String tldSkip;
9596

97+
private String uriEncoding = "UTF-8";
98+
9699
/**
97100
* Create a new {@link TomcatEmbeddedServletContainerFactory} instance.
98101
*/
@@ -206,6 +209,10 @@ protected void customizeConnector(Connector connector) {
206209
.setAddress(getAddress());
207210
}
208211
}
212+
if (getUriEncoding() != null) {
213+
connector.setURIEncoding(getUriEncoding());
214+
}
215+
209216
// If ApplicationContext is slow to start we want Tomcat not to bind to the socket
210217
// prematurely...
211218
connector.setProperty("bindOnInit", "false");
@@ -455,10 +462,28 @@ public List<Connector> getAdditionalTomcatConnectors() {
455462
return this.additionalTomcatConnectors;
456463
}
457464

465+
/**
466+
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will
467+
* be used.
468+
* @param uriEncoding the uri encoding to set
469+
*/
470+
public void setUriEncoding(String uriEncoding) {
471+
this.uriEncoding = uriEncoding;
472+
}
473+
474+
/**
475+
* Returns the character encoding to use for URL decoding.
476+
*/
477+
public String getUriEncoding() {
478+
return this.uriEncoding;
479+
}
480+
458481
private static class TomcatErrorPage {
459482

460483
private final String location;
484+
461485
private final String exceptionType;
486+
462487
private final int errorCode;
463488

464489
private final Object nativePage;

spring-boot/src/test/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactoryTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*
4545
* @author Phillip Webb
4646
* @author Dave Syer
47+
* @author Stephane Nicoll
4748
*/
4849
public class TomcatEmbeddedServletContainerFactoryTests extends
4950
AbstractEmbeddedServletContainerFactoryTests {
@@ -190,11 +191,30 @@ public void addNullConnectorCustomizersThrows() {
190191
factory.addConnectorCustomizers((TomcatConnectorCustomizer[]) null);
191192
}
192193

194+
@Test
195+
public void uriEncoding() throws Exception {
196+
TomcatEmbeddedServletContainerFactory factory = getFactory();
197+
factory.setUriEncoding("US-ASCII");
198+
Tomcat tomcat = getTomcat(factory);
199+
assertEquals("US-ASCII", tomcat.getConnector().getURIEncoding());
200+
}
201+
202+
@Test
203+
public void defaultUriEncoding() throws Exception {
204+
TomcatEmbeddedServletContainerFactory factory = getFactory();
205+
Tomcat tomcat = getTomcat(factory);
206+
assertEquals("UTF-8", tomcat.getConnector().getURIEncoding());
207+
}
208+
193209
private void assertTimeout(TomcatEmbeddedServletContainerFactory factory, int expected) {
194-
this.container = factory.getEmbeddedServletContainer();
195-
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
210+
Tomcat tomcat = getTomcat(factory);
196211
Context context = (Context) tomcat.getHost().findChildren()[0];
197212
assertThat(context.getSessionTimeout(), equalTo(expected));
198213
}
199214

215+
private Tomcat getTomcat(TomcatEmbeddedServletContainerFactory factory) {
216+
this.container = factory.getEmbeddedServletContainer();
217+
return ((TomcatEmbeddedServletContainer) this.container).getTomcat();
218+
}
219+
200220
}

0 commit comments

Comments
 (0)