FormHttpMessageConverter's charset (and its UTF-8 default) applies to part converters as well
Issue: SPR-14338
This commit is contained in:
@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
|
|||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sebastien Deleuze
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConverter<T> {
|
public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConverter<T> {
|
||||||
@@ -246,8 +247,11 @@ public abstract class AbstractHttpMessageConverter<T> implements HttpMessageConv
|
|||||||
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
|
contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse);
|
||||||
}
|
}
|
||||||
if (contentTypeToUse != null) {
|
if (contentTypeToUse != null) {
|
||||||
if (contentTypeToUse.getCharset() == null && this.defaultCharset != null) {
|
if (contentTypeToUse.getCharset() == null) {
|
||||||
contentTypeToUse = new MediaType(contentTypeToUse, this.defaultCharset);
|
Charset defaultCharset = getDefaultCharset();
|
||||||
|
if (defaultCharset != null) {
|
||||||
|
contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
headers.setContentType(contentTypeToUse);
|
headers.setContentType(contentTypeToUse);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -32,15 +32,19 @@ import org.springframework.util.StreamUtils;
|
|||||||
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
* overridden by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<byte[]> {
|
public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<byte[]> {
|
||||||
|
|
||||||
/** Creates a new instance of the {@code ByteArrayHttpMessageConverter}. */
|
/**
|
||||||
|
* Create a new instance of the {@code ByteArrayHttpMessageConverter}.
|
||||||
|
*/
|
||||||
public ByteArrayHttpMessageConverter() {
|
public ByteArrayHttpMessageConverter() {
|
||||||
super(new MediaType("application", "octet-stream"), MediaType.ALL);
|
super(new MediaType("application", "octet-stream"), MediaType.ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean supports(Class<?> clazz) {
|
public boolean supports(Class<?> clazz) {
|
||||||
return byte[].class == clazz;
|
return byte[].class == clazz;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -82,6 +82,7 @@ import org.springframework.util.StringUtils;
|
|||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see MultiValueMap
|
* @see MultiValueMap
|
||||||
*/
|
*/
|
||||||
@@ -90,14 +91,14 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||||
|
|
||||||
|
|
||||||
private Charset charset = DEFAULT_CHARSET;
|
|
||||||
|
|
||||||
private Charset multipartCharset;
|
|
||||||
|
|
||||||
private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
|
private List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
|
||||||
|
|
||||||
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
|
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
|
||||||
|
|
||||||
|
private Charset charset = DEFAULT_CHARSET;
|
||||||
|
|
||||||
|
private Charset multipartCharset;
|
||||||
|
|
||||||
|
|
||||||
public FormHttpMessageConverter() {
|
public FormHttpMessageConverter() {
|
||||||
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
|
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
@@ -108,31 +109,11 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||||||
stringHttpMessageConverter.setWriteAcceptCharset(false);
|
stringHttpMessageConverter.setWriteAcceptCharset(false);
|
||||||
this.partConverters.add(stringHttpMessageConverter);
|
this.partConverters.add(stringHttpMessageConverter);
|
||||||
this.partConverters.add(new ResourceHttpMessageConverter());
|
this.partConverters.add(new ResourceHttpMessageConverter());
|
||||||
|
|
||||||
|
applyDefaultCharset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the default character set to use for reading and writing form data when
|
|
||||||
* the request or response Content-Type header does not explicitly specify it.
|
|
||||||
* <p>By default this is set to "UTF-8".
|
|
||||||
*/
|
|
||||||
public void setCharset(Charset charset) {
|
|
||||||
this.charset = charset;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the character set to use when writing multipart data to encode file
|
|
||||||
* names. Encoding is based on the encoded-word syntax defined in RFC 2047
|
|
||||||
* and relies on {@code MimeUtility} from "javax.mail".
|
|
||||||
* <p>If not set file names will be encoded as US-ASCII.
|
|
||||||
* @param multipartCharset the charset to use
|
|
||||||
* @since 4.1.1
|
|
||||||
* @see <a href="http://en.wikipedia.org/wiki/MIME#Encoded-Word">Encoded-Word</a>
|
|
||||||
*/
|
|
||||||
public void setMultipartCharset(Charset multipartCharset) {
|
|
||||||
this.multipartCharset = multipartCharset;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the list of {@link MediaType} objects supported by this converter.
|
* Set the list of {@link MediaType} objects supported by this converter.
|
||||||
*/
|
*/
|
||||||
@@ -163,6 +144,49 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||||||
this.partConverters.add(partConverter);
|
this.partConverters.add(partConverter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the default character set to use for reading and writing form data when
|
||||||
|
* the request or response Content-Type header does not explicitly specify it.
|
||||||
|
* <p>By default this is set to "UTF-8". As of 4.3, it will also be used as
|
||||||
|
* the default charset for the conversion of text bodies in a multipart request.
|
||||||
|
* In contrast to this, {@link #setMultipartCharset} only affects the encoding of
|
||||||
|
* <i>file names</i> in a multipart request according to the encoded-word syntax.
|
||||||
|
*/
|
||||||
|
public void setCharset(Charset charset) {
|
||||||
|
if (charset != this.charset) {
|
||||||
|
this.charset = (charset != null ? charset : DEFAULT_CHARSET);
|
||||||
|
applyDefaultCharset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply the configured charset as a default to registered part converters.
|
||||||
|
*/
|
||||||
|
private void applyDefaultCharset() {
|
||||||
|
for (HttpMessageConverter<?> candidate : this.partConverters) {
|
||||||
|
if (candidate instanceof AbstractHttpMessageConverter) {
|
||||||
|
AbstractHttpMessageConverter<?> converter = (AbstractHttpMessageConverter<?>) candidate;
|
||||||
|
// Only override default charset if the converter operates with a charset to begin with...
|
||||||
|
if (converter.getDefaultCharset() != null) {
|
||||||
|
converter.setDefaultCharset(this.charset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the character set to use when writing multipart data to encode file
|
||||||
|
* names. Encoding is based on the encoded-word syntax defined in RFC 2047
|
||||||
|
* and relies on {@code MimeUtility} from "javax.mail".
|
||||||
|
* <p>If not set file names will be encoded as US-ASCII.
|
||||||
|
* @param multipartCharset the charset to use
|
||||||
|
* @since 4.1.1
|
||||||
|
* @see <a href="http://en.wikipedia.org/wiki/MIME#Encoded-Word">Encoded-Word</a>
|
||||||
|
*/
|
||||||
|
public void setMultipartCharset(Charset multipartCharset) {
|
||||||
|
this.multipartCharset = multipartCharset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRead(Class<?> clazz, MediaType mediaType) {
|
public boolean canRead(Class<?> clazz, MediaType mediaType) {
|
||||||
@@ -255,7 +279,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||||||
Charset charset;
|
Charset charset;
|
||||||
if (contentType != null) {
|
if (contentType != null) {
|
||||||
outputMessage.getHeaders().setContentType(contentType);
|
outputMessage.getHeaders().setContentType(contentType);
|
||||||
charset = contentType.getCharset() != null ? contentType.getCharset() : this.charset;
|
charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -29,8 +29,8 @@ import org.springframework.util.Assert;
|
|||||||
* An {@code HttpMessageConverter} that uses {@link StringHttpMessageConverter}
|
* An {@code HttpMessageConverter} that uses {@link StringHttpMessageConverter}
|
||||||
* for reading and writing content and a {@link ConversionService} for converting
|
* for reading and writing content and a {@link ConversionService} for converting
|
||||||
* the String content to and from the target object type.
|
* the String content to and from the target object type.
|
||||||
* <p>
|
*
|
||||||
* By default, this converter supports the media type {@code text/plain} only.
|
* <p>By default, this converter supports the media type {@code text/plain} only.
|
||||||
* This can be overridden by setting the
|
* This can be overridden by setting the
|
||||||
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
* {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||||
* Example of usage:
|
* Example of usage:
|
||||||
@@ -49,17 +49,15 @@ import org.springframework.util.Assert;
|
|||||||
*/
|
*/
|
||||||
public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
|
public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConverter<Object> {
|
||||||
|
|
||||||
private ConversionService conversionService;
|
private final ConversionService conversionService;
|
||||||
|
|
||||||
private StringHttpMessageConverter stringHttpMessageConverter;
|
private final StringHttpMessageConverter stringHttpMessageConverter;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A constructor accepting a {@code ConversionService} to use to convert the
|
* A constructor accepting a {@code ConversionService} to use to convert the
|
||||||
* (String) message body to/from the target class type. This constructor
|
* (String) message body to/from the target class type. This constructor uses
|
||||||
* uses {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default
|
* {@link StringHttpMessageConverter#DEFAULT_CHARSET} as the default charset.
|
||||||
* charset.
|
|
||||||
*
|
|
||||||
* @param conversionService the conversion service
|
* @param conversionService the conversion service
|
||||||
*/
|
*/
|
||||||
public ObjectToStringHttpMessageConverter(ConversionService conversionService) {
|
public ObjectToStringHttpMessageConverter(ConversionService conversionService) {
|
||||||
@@ -67,9 +65,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A constructor accepting a {@code ConversionService} as well as a default
|
* A constructor accepting a {@code ConversionService} as well as a default charset.
|
||||||
* charset.
|
|
||||||
*
|
|
||||||
* @param conversionService the conversion service
|
* @param conversionService the conversion service
|
||||||
* @param defaultCharset the default charset
|
* @param defaultCharset the default charset
|
||||||
*/
|
*/
|
||||||
@@ -81,6 +77,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
|
|||||||
this.stringHttpMessageConverter = new StringHttpMessageConverter(defaultCharset);
|
this.stringHttpMessageConverter = new StringHttpMessageConverter(defaultCharset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
|
* Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
|
||||||
* <p>Default is {@code true}.
|
* <p>Default is {@code true}.
|
||||||
@@ -89,6 +86,7 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
|
|||||||
this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset);
|
this.stringHttpMessageConverter.setWriteAcceptCharset(writeAcceptCharset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRead(Class<?> clazz, MediaType mediaType) {
|
public boolean canRead(Class<?> clazz, MediaType mediaType) {
|
||||||
return this.conversionService.canConvert(String.class, clazz) && canRead(mediaType);
|
return this.conversionService.canConvert(String.class, clazz) && canRead(mediaType);
|
||||||
@@ -113,8 +111,8 @@ public class ObjectToStringHttpMessageConverter extends AbstractHttpMessageConve
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
|
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException {
|
||||||
String s = this.conversionService.convert(obj, String.class);
|
String value = this.conversionService.convert(obj, String.class);
|
||||||
this.stringHttpMessageConverter.writeInternal(s, outputMessage);
|
this.stringHttpMessageConverter.writeInternal(value, outputMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2016 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -35,6 +35,7 @@ import org.springframework.util.StreamUtils;
|
|||||||
* by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
* by setting the {@link #setSupportedMediaTypes supportedMediaTypes} property.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
|
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {
|
||||||
@@ -122,7 +123,7 @@ public class StringHttpMessageConverter extends AbstractHttpMessageConverter<Str
|
|||||||
return contentType.getCharset();
|
return contentType.getCharset();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return this.getDefaultCharset();
|
return getDefaultCharset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user