Support Tomcat uri encoding (with UTF-8 default)
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 gh-540
This commit is contained in:
committed by
Phillip Webb
parent
75548c1205
commit
87321edf36
@@ -66,6 +66,7 @@ import org.springframework.util.StreamUtils;
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
* @author Brock Mills
|
||||
* @author Stephane Nicoll
|
||||
* @see #setPort(int)
|
||||
* @see #setContextLifecycleListeners(Collection)
|
||||
* @see TomcatEmbeddedServletContainer
|
||||
@@ -93,6 +94,8 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
|
||||
private String tldSkip;
|
||||
|
||||
private String uriEncoding = "UTF-8";
|
||||
|
||||
/**
|
||||
* Create a new {@link TomcatEmbeddedServletContainerFactory} instance.
|
||||
*/
|
||||
@@ -206,6 +209,10 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
.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");
|
||||
@@ -455,10 +462,28 @@ public class TomcatEmbeddedServletContainerFactory extends
|
||||
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;
|
||||
|
||||
@@ -44,6 +44,7 @@ import static org.mockito.Mockito.verify;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Dave Syer
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class TomcatEmbeddedServletContainerFactoryTests extends
|
||||
AbstractEmbeddedServletContainerFactoryTests {
|
||||
@@ -190,11 +191,30 @@ public class TomcatEmbeddedServletContainerFactoryTests extends
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user