Browse Source

Switch writeAcceptCharset to false by default

Closes gh-22506
pull/22929/head
Rossen Stoyanchev 6 years ago
parent
commit
7e6e3d7027
  1. 5
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
  2. 3
      spring-web/src/main/java/org/springframework/http/converter/ObjectToStringHttpMessageConverter.java
  3. 4
      spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java
  4. 3
      spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java
  5. 8
      spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java
  6. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java
  7. 6
      spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java
  8. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java
  9. 5
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

5
spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

@ -117,11 +117,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -117,11 +117,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
this.supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA);
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.partConverters.add(new ByteArrayHttpMessageConverter());
this.partConverters.add(stringHttpMessageConverter);
this.partConverters.add(new StringHttpMessageConverter());
this.partConverters.add(new ResourceHttpMessageConverter());
applyDefaultCharset();

3
spring-web/src/main/java/org/springframework/http/converter/ObjectToStringHttpMessageConverter.java

@ -81,8 +81,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve @@ -81,8 +81,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
/**
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
* <p>Default is {@code true}.
* Delegates to {@link StringHttpMessageConverter#setWriteAcceptCharset(boolean)}.
*/
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset);

4
spring-web/src/main/java/org/springframework/http/converter/StringHttpMessageConverter.java

@ -52,7 +52,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str @@ -52,7 +52,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
@Nullable
private volatile List<Charset> availableCharsets;
private boolean writeAcceptCharset = true;
private boolean writeAcceptCharset = false;
/**
@ -76,7 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str @@ -76,7 +76,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
* Whether the {@code Accept-Charset} header should be written to any outgoing
* request sourced from the value of {@link Charset#availableCharsets()}.
* The behavior is suppressed if the header has already been set.
* <p>Default is {@code true}.
* <p>As of 5.2, by default is set to {@code false}.
*/
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
this.writeAcceptCharset = writeAcceptCharset;

3
spring-web/src/test/java/org/springframework/http/converter/ObjectToStringHttpMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 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.
@ -107,6 +107,7 @@ public class ObjectToStringHttpMessageConverterTests { @@ -107,6 +107,7 @@ public class ObjectToStringHttpMessageConverterTests {
@Test
public void writeAcceptCharset() throws IOException {
this.converter.setWriteAcceptCharset(true);
this.converter.write(new Date(), null, this.response);
assertNotNull(this.servletResponse.getHeader("Accept-Charset"));

8
spring-web/src/test/java/org/springframework/http/converter/StringHttpMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 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.
@ -79,7 +79,7 @@ public class StringHttpMessageConverterTests { @@ -79,7 +79,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.ISO_8859_1));
assertEquals(new MediaType("text", "plain", StandardCharsets.ISO_8859_1), headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.ISO_8859_1).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
@Test
@ -91,7 +91,7 @@ public class StringHttpMessageConverterTests { @@ -91,7 +91,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
@Test // SPR-8867
@ -106,7 +106,7 @@ public class StringHttpMessageConverterTests { @@ -106,7 +106,7 @@ public class StringHttpMessageConverterTests {
assertEquals(body, this.outputMessage.getBodyAsString(StandardCharsets.UTF_8));
assertEquals(TEXT_PLAIN_UTF_8, headers.getContentType());
assertEquals(body.getBytes(StandardCharsets.UTF_8).length, headers.getContentLength());
assertFalse(headers.getAcceptCharset().isEmpty());
assertTrue(headers.getAcceptCharset().isEmpty());
}
}

5
spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.java

@ -831,11 +831,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv @@ -831,11 +831,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* @param messageConverters the list to add the default message converters to
*/
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new ResourceRegionHttpMessageConverter());
try {

6
spring-webmvc/src/main/java/org/springframework/web/servlet/function/support/RouterFunctionMapping.java

@ -149,12 +149,8 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini @@ -149,12 +149,8 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
*/
private void initMessageConverters() {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(4);
messageConverters.add(new ByteArrayHttpMessageConverter());
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new StringHttpMessageConverter());
try {
messageConverters.add(new SourceHttpMessageConverter<>());

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExceptionHandlerExceptionResolver.java

@ -105,12 +105,9 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce @@ -105,12 +105,9 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
public ExceptionHandlerExceptionResolver() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<>();
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(stringHttpMessageConverter);
this.messageConverters.add(new StringHttpMessageConverter());
try {
this.messageConverters.add(new SourceHttpMessageConverter<>());
}

5
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java

@ -193,12 +193,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter @@ -193,12 +193,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
public RequestMappingHandlerAdapter() {
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316
this.messageConverters = new ArrayList<>(4);
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(stringHttpMessageConverter);
this.messageConverters.add(new StringHttpMessageConverter());
try {
this.messageConverters.add(new SourceHttpMessageConverter<>());
}

Loading…
Cancel
Save