Skip to content

Commit

Permalink
Support Tomcat uri encoding (with UTF-8 default)
Browse files Browse the repository at this point in the history
Introduce an extra `server.tomcat.uri-encoding` property used to
configure the URI encoding for the embedded tomcat container.

Defaults to `UTF-8` instead of the usual tomcat default of `ISO-8859-1`.

Fixes spring-projectsgh-540
  • Loading branch information
snicoll authored and Phillip Webb committed Mar 25, 2014
1 parent 213245c commit 7eff0fa
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
Expand Up @@ -43,6 +43,7 @@
* {@link EmbeddedServletContainerCustomizerBeanPostProcessor} is active.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = false)
public class ServerProperties implements EmbeddedServletContainerCustomizer {
Expand Down Expand Up @@ -144,6 +145,8 @@ public static class Tomcat {

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

private String uriEncoding;

public int getMaxThreads() {
return this.maxThreads;
}
Expand Down Expand Up @@ -200,6 +203,14 @@ public void setRemoteIpHeader(String remoteIpHeader) {
this.remoteIpHeader = remoteIpHeader;
}

public String getUriEncoding() {
return this.uriEncoding;
}

public void setUriEncoding(String uriEncoding) {
this.uriEncoding = uriEncoding;
}

void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
if (getBasedir() != null) {
factory.setBaseDirectory(getBasedir());
Expand Down Expand Up @@ -247,6 +258,9 @@ public void customize(Connector connector) {
valve.setSuffix(".log");
factory.addContextValves(valve);
}
if (getUriEncoding() != null) {
factory.setUriEncoding(getUriEncoding());
}
}

}
Expand Down
Expand Up @@ -36,6 +36,7 @@
* Tests for {@link ServerProperties}.
*
* @author Dave Syer
* @author Stephane Nicoll
*/
public class ServerPropertiesTests {

Expand Down Expand Up @@ -87,4 +88,13 @@ public void testCustomizeTomcatPort() throws Exception {
verify(factory).setPort(8080);
}

@Test
public void testCustomizeUriEncoding() throws Exception {
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.uriEncoding", "US-ASCII");
new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues(
map));
assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding());
}

}
Expand Up @@ -61,6 +61,7 @@ server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.basedir=/tmp # base dir (usually not needed, defaults to tmp)
server.tomcat.background-processor-delay=30; # in seconds
server.tomcat.max-threads = 0 # number of threads in protocol handler
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
# SPRING MVC ({sc-spring-boot-autoconfigure}/web/HttpMapperProperties.{sc-ext}[HttpMapperProperties])
http.mappers.json-pretty-print=false # pretty print JSON
Expand Down
Expand Up @@ -66,6 +66,7 @@
* @author Phillip Webb
* @author Dave Syer
* @author Brock Mills
* @author Stephane Nicoll
* @see #setPort(int)
* @see #setContextLifecycleListeners(Collection)
* @see TomcatEmbeddedServletContainer
Expand Down Expand Up @@ -93,6 +94,8 @@ public class TomcatEmbeddedServletContainerFactory extends

private String tldSkip;

private String uriEncoding = "UTF-8";

/**
* Create a new {@link TomcatEmbeddedServletContainerFactory} instance.
*/
Expand Down Expand Up @@ -206,6 +209,10 @@ protected void customizeConnector(Connector connector) {
.setAddress(getAddress());
}
}
if (getUriEncoding() != null) {
connector.setURIEncoding(getUriEncoding());
}

// If ApplicationContext is slow to start we want Tomcat not to bind to the socket
// prematurely...
connector.setProperty("bindOnInit", "false");
Expand Down Expand Up @@ -455,10 +462,28 @@ public List<Connector> getAdditionalTomcatConnectors() {
return this.additionalTomcatConnectors;
}

/**
* Set the character encoding to use for URL decoding. If not specified 'UTF-8' will
* be used.
* @param uriEncoding the uri encoding to set
*/
public void setUriEncoding(String uriEncoding) {
this.uriEncoding = uriEncoding;
}

/**
* Returns the character encoding to use for URL decoding.
*/
public String getUriEncoding() {
return this.uriEncoding;
}

private static class TomcatErrorPage {

private final String location;

private final String exceptionType;

private final int errorCode;

private final Object nativePage;
Expand Down
Expand Up @@ -44,6 +44,7 @@
*
* @author Phillip Webb
* @author Dave Syer
* @author Stephane Nicoll
*/
public class TomcatEmbeddedServletContainerFactoryTests extends
AbstractEmbeddedServletContainerFactoryTests {
Expand Down Expand Up @@ -190,11 +191,30 @@ public void addNullConnectorCustomizersThrows() {
factory.addConnectorCustomizers((TomcatConnectorCustomizer[]) null);
}

@Test
public void uriEncoding() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
factory.setUriEncoding("US-ASCII");
Tomcat tomcat = getTomcat(factory);
assertEquals("US-ASCII", tomcat.getConnector().getURIEncoding());
}

@Test
public void defaultUriEncoding() throws Exception {
TomcatEmbeddedServletContainerFactory factory = getFactory();
Tomcat tomcat = getTomcat(factory);
assertEquals("UTF-8", tomcat.getConnector().getURIEncoding());
}

private void assertTimeout(TomcatEmbeddedServletContainerFactory factory, int expected) {
this.container = factory.getEmbeddedServletContainer();
Tomcat tomcat = ((TomcatEmbeddedServletContainer) this.container).getTomcat();
Tomcat tomcat = getTomcat(factory);
Context context = (Context) tomcat.getHost().findChildren()[0];
assertThat(context.getSessionTimeout(), equalTo(expected));
}

private Tomcat getTomcat(TomcatEmbeddedServletContainerFactory factory) {
this.container = factory.getEmbeddedServletContainer();
return ((TomcatEmbeddedServletContainer) this.container).getTomcat();
}

}

0 comments on commit 7eff0fa

Please sign in to comment.