summaryrefslogtreecommitdiff
path: root/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
diff options
context:
space:
mode:
authorEmmanuel Bourg <ebourg@apache.org>2016-08-02 11:13:32 +0200
committerEmmanuel Bourg <ebourg@apache.org>2016-08-02 11:13:32 +0200
commitf69f2a4b8ea697b3a631c0dc7a470e3c9793fee3 (patch)
treedb2f25b29aa3e59c463ab41d3f2856f6265bb1a5 /spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
parent5575b60c30c5a0c308c4ba3a2db93956d8c1746c (diff)
Imported Upstream version 4.2.6
Diffstat (limited to 'spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java')
-rw-r--r--spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java68
1 files changed, 41 insertions, 27 deletions
diff --git a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
index 919407d9..44491be0 100644
--- a/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
+++ b/spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 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.
@@ -27,7 +27,6 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Random;
import javax.mail.internet.MimeUtility;
import org.springframework.core.io.Resource;
@@ -36,8 +35,10 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
+import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
@@ -88,12 +89,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
- private static final byte[] BOUNDARY_CHARS =
- new byte[] {'-', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A',
- 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
- 'V', 'W', 'X', 'Y', 'Z'};
-
private Charset charset = DEFAULT_CHARSET;
@@ -103,8 +98,6 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
private List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
- private final Random random = new Random();
-
public FormHttpMessageConverter() {
this.supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
@@ -256,8 +249,8 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
return false;
}
- private void writeForm(MultiValueMap<String, String> form, MediaType contentType, HttpOutputMessage outputMessage)
- throws IOException {
+ private void writeForm(MultiValueMap<String, String> form, MediaType contentType,
+ HttpOutputMessage outputMessage) throws IOException {
Charset charset;
if (contentType != null) {
@@ -286,20 +279,45 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
builder.append('&');
}
}
- byte[] bytes = builder.toString().getBytes(charset.name());
+ final byte[] bytes = builder.toString().getBytes(charset.name());
outputMessage.getHeaders().setContentLength(bytes.length);
- StreamUtils.copy(bytes, outputMessage.getBody());
+
+ if (outputMessage instanceof StreamingHttpOutputMessage) {
+ StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
+ streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
+ @Override
+ public void writeTo(OutputStream outputStream) throws IOException {
+ StreamUtils.copy(bytes, outputStream);
+ }
+ });
+ }
+ else {
+ StreamUtils.copy(bytes, outputMessage.getBody());
+ }
}
- private void writeMultipart(MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
- byte[] boundary = generateMultipartBoundary();
+ private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutputMessage outputMessage) throws IOException {
+ final byte[] boundary = generateMultipartBoundary();
Map<String, String> parameters = Collections.singletonMap("boundary", new String(boundary, "US-ASCII"));
MediaType contentType = new MediaType(MediaType.MULTIPART_FORM_DATA, parameters);
- outputMessage.getHeaders().setContentType(contentType);
-
- writeParts(outputMessage.getBody(), parts, boundary);
- writeEnd(outputMessage.getBody(), boundary);
+ HttpHeaders headers = outputMessage.getHeaders();
+ headers.setContentType(contentType);
+
+ if (outputMessage instanceof StreamingHttpOutputMessage) {
+ StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
+ streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {
+ @Override
+ public void writeTo(OutputStream outputStream) throws IOException {
+ writeParts(outputStream, parts, boundary);
+ writeEnd(outputStream, boundary);
+ }
+ });
+ }
+ else {
+ writeParts(outputMessage.getBody(), parts, boundary);
+ writeEnd(outputMessage.getBody(), boundary);
+ }
}
private void writeParts(OutputStream os, MultiValueMap<String, Object> parts, byte[] boundary) throws IOException {
@@ -339,15 +357,11 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
/**
* Generate a multipart boundary.
- * <p>The default implementation returns a random boundary.
- * Can be overridden in subclasses.
+ * <p>This implementation delegates to
+ * {@link MimeTypeUtils#generateMultipartBoundary()}.
*/
protected byte[] generateMultipartBoundary() {
- byte[] boundary = new byte[this.random.nextInt(11) + 30];
- for (int i = 0; i < boundary.length; i++) {
- boundary[i] = BOUNDARY_CHARS[this.random.nextInt(BOUNDARY_CHARS.length)];
- }
- return boundary;
+ return MimeTypeUtils.generateMultipartBoundary();
}
/**