SPR-7789 - FormHttpMessageConverter does not honor the charset in the content type when writing a form and uses a wrong default charset
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3826 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
parent
bc235c5b03
commit
a3583fd5c4
|
|
@ -45,35 +45,26 @@ import org.springframework.util.MultiValueMap;
|
|||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Implementation of {@link HttpMessageConverter} that can handle form data, including multipart form data
|
||||
* (i.e. file uploads).
|
||||
* Implementation of {@link HttpMessageConverter} that can handle form data, including multipart form data (i.e. file
|
||||
* uploads).
|
||||
*
|
||||
* <p>This converter can write the {@code application/x-www-form-urlencoded} and {@code multipart/form-data} media
|
||||
* types, and read the {@code application/x-www-form-urlencoded}) media type (but not {@code multipart/form-data}).
|
||||
*
|
||||
* <p>In other words, this converter can read and write 'normal' HTML forms (as
|
||||
* {@link MultiValueMap MultiValueMap<String, String>}), and it can write multipart form (as
|
||||
* {@link MultiValueMap MultiValueMap<String, Object>}. When writing multipart, this converter uses other
|
||||
* {@link HttpMessageConverter HttpMessageConverters} to write the respective MIME parts. By default, basic converters
|
||||
* are registered (supporting {@code Strings} and {@code Resources}, for instance); these can be overridden by setting
|
||||
* the {@link #setPartConverters(java.util.List) partConverters} property.
|
||||
* <p>In other words, this converter can read and write 'normal' HTML forms (as {@link MultiValueMap
|
||||
* MultiValueMap<String, String>}), and it can write multipart form (as {@link MultiValueMap
|
||||
* MultiValueMap<String, Object>}. When writing multipart, this converter uses other {@link HttpMessageConverter
|
||||
* HttpMessageConverters} to write the respective MIME parts. By default, basic converters are registered (supporting
|
||||
* {@code Strings} and {@code Resources}, for instance); these can be overridden by setting the {@link
|
||||
* #setPartConverters(java.util.List) partConverters} property.
|
||||
*
|
||||
* <p>For example, the following snippet shows how to submit an HTML form:
|
||||
* <pre class="code">
|
||||
* RestTemplate template = new RestTemplate(); // FormHttpMessageConverter is configured by default
|
||||
* MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
|
||||
* form.add("field 1", "value 1");
|
||||
* form.add("field 2", "value 2");
|
||||
* form.add("field 2", "value 3");
|
||||
* template.postForLocation("http://example.com/myForm", form);
|
||||
* </pre>
|
||||
* <p>The following snippet shows how to do a file upload:
|
||||
* <pre class="code">
|
||||
* MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
|
||||
* parts.add("field 1", "value 1");
|
||||
* parts.add("file", new ClassPathResource("myFile.jpg"));
|
||||
* template.postForLocation("http://example.com/myFileUpload", parts);
|
||||
* </pre>
|
||||
* <p>For example, the following snippet shows how to submit an HTML form: <pre class="code"> RestTemplate template =
|
||||
* new RestTemplate(); // FormHttpMessageConverter is configured by default MultiValueMap<String, String> form =
|
||||
* new LinkedMultiValueMap<String, String>(); form.add("field 1", "value 1"); form.add("field 2", "value 2");
|
||||
* form.add("field 2", "value 3"); template.postForLocation("http://example.com/myForm", form); </pre> <p>The following
|
||||
* snippet shows how to do a file upload: <pre class="code"> MultiValueMap<String, Object> parts = new
|
||||
* LinkedMultiValueMap<String, Object>(); parts.add("field 1", "value 1"); parts.add("file", new
|
||||
* ClassPathResource("myFile.jpg")); template.postForLocation("http://example.com/myFileUpload", parts); </pre>
|
||||
*
|
||||
* <p>Some methods in this class were inspired by {@link org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}.
|
||||
*
|
||||
|
|
@ -95,7 +86,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
|
||||
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
|
||||
|
||||
|
||||
public FormHttpMessageConverter() {
|
||||
this.partConverters.add(new ByteArrayHttpMessageConverter());
|
||||
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
|
||||
|
|
@ -104,31 +94,23 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
this.partConverters.add(new ResourceHttpMessageConverter());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a message body converter. Such a converters is used to convert objects to MIME parts.
|
||||
*/
|
||||
/** Add a message body converter. Such a converters is used to convert objects to MIME parts. */
|
||||
public final void addPartConverter(HttpMessageConverter<?> partConverter) {
|
||||
Assert.notNull(partConverter, "'partConverter' must not be NULL");
|
||||
this.partConverters.add(partConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the message body converters to use. These converters are used to convert objects to MIME parts.
|
||||
*/
|
||||
/** Set the message body converters to use. These converters are used to convert objects to MIME parts. */
|
||||
public final void setPartConverters(List<HttpMessageConverter<?>> partConverters) {
|
||||
Assert.notEmpty(partConverters, "'partConverters' must not be empty");
|
||||
this.partConverters = partConverters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the character set used for writing form data.
|
||||
*/
|
||||
/** Sets the character set used for writing form data. */
|
||||
public void setCharset(Charset charset) {
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
|
||||
public boolean canRead(Class<?> clazz, MediaType mediaType) {
|
||||
if (!MultiValueMap.class.isAssignableFrom(clazz)) {
|
||||
return false;
|
||||
|
|
@ -187,7 +169,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage)
|
||||
throws IOException, HttpMessageNotWritableException {
|
||||
if (!isMultipart(map, contentType)) {
|
||||
writeForm((MultiValueMap<String, String>) map, outputMessage);
|
||||
writeForm((MultiValueMap<String, String>) map, contentType, outputMessage);
|
||||
}
|
||||
else {
|
||||
writeMultipart((MultiValueMap<String, Object>) map, outputMessage);
|
||||
|
|
@ -208,8 +190,16 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
return false;
|
||||
}
|
||||
|
||||
private void writeForm(MultiValueMap<String, String> form, HttpOutputMessage outputMessage) throws IOException {
|
||||
private void writeForm(MultiValueMap<String, String> form, MediaType contentType, HttpOutputMessage outputMessage)
|
||||
throws IOException {
|
||||
Charset charset;
|
||||
if (contentType != null) {
|
||||
outputMessage.getHeaders().setContentType(contentType);
|
||||
charset = contentType.getCharSet() != null ? contentType.getCharSet() : this.charset;
|
||||
} else {
|
||||
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
charset = this.charset;
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
|
||||
String name = nameIterator.next();
|
||||
|
|
@ -231,7 +221,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
FileCopyUtils.copy(builder.toString(), new OutputStreamWriter(outputMessage.getBody(), charset));
|
||||
}
|
||||
|
||||
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
|
||||
private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage)
|
||||
throws IOException {
|
||||
byte[] boundary = generateMultipartBoundary();
|
||||
|
||||
Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
|
||||
|
|
@ -308,8 +299,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
}
|
||||
|
||||
/**
|
||||
* Generate a multipart boundary.
|
||||
* <p>Default implementation returns a random boundary. Can be overridden in subclasses.
|
||||
* Generate a multipart boundary. <p>Default implementation returns a random boundary. Can be overridden in
|
||||
* subclasses.
|
||||
*/
|
||||
protected byte[] generateMultipartBoundary() {
|
||||
byte[] boundary = new byte[rnd.nextInt(11) + 30];
|
||||
|
|
@ -321,8 +312,9 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
|
||||
/**
|
||||
* Return the filename of the given multipart part. This value will be used for the {@code Content-Disposition} header.
|
||||
* <p>Default implementation returns {@link Resource#getFilename()} if the part is a {@code Resource}, and
|
||||
* {@code null} in other cases. Can be overridden in subclasses.
|
||||
* <p>Default implementation returns {@link Resource#getFilename()} if the part is a {@code Resource}, and {@code null}
|
||||
* in other cases. Can be overridden in subclasses.
|
||||
*
|
||||
* @param part the part to determine the file name for
|
||||
* @return the filename, or {@code null} if not known
|
||||
*/
|
||||
|
|
@ -336,10 +328,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of {@link org.springframework.http.HttpOutputMessage} used for writing multipart data.
|
||||
*/
|
||||
/** Implementation of {@link org.springframework.http.HttpOutputMessage} used for writing multipart data. */
|
||||
private class MultipartHttpOutputMessage implements HttpOutputMessage {
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
|
|
|||
Loading…
Reference in New Issue