Browse Source

Protected methods for serializing form content

Issue: SPR-16855
pull/1857/merge
Rossen Stoyanchev 7 years ago
parent
commit
0b61c748f6
  1. 2
      spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java
  2. 42
      spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

2
spring-web/src/main/java/org/springframework/http/codec/FormHttpMessageWriter.java

@ -147,7 +147,7 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St @@ -147,7 +147,7 @@ public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<St
}
}
private String serializeForm(MultiValueMap<String, String> formData, Charset charset) {
protected String serializeForm(MultiValueMap<String, String> formData, Charset charset) {
StringBuilder builder = new StringBuilder();
formData.forEach((name, values) ->
values.forEach(value -> {

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

@ -298,25 +298,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -298,25 +298,7 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
Charset charset = contentType.getCharset();
Assert.notNull(charset, "No charset"); // should never occur
StringBuilder builder = new StringBuilder();
formData.forEach((name, values) ->
values.forEach(value -> {
try {
if (builder.length() != 0) {
builder.append('&');
}
builder.append(URLEncoder.encode(name, charset.name()));
if (value != null) {
builder.append('=');
builder.append(URLEncoder.encode(value, charset.name()));
}
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}));
final byte[] bytes = builder.toString().getBytes(charset);
final byte[] bytes = serializeForm(formData, charset).getBytes(charset);
outputMessage.getHeaders().setContentLength(bytes.length);
if (outputMessage instanceof StreamingHttpOutputMessage) {
@ -340,6 +322,28 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue @@ -340,6 +322,28 @@ public class FormHttpMessageConverter implements HttpMessageConverter<MultiValue
}
}
protected String serializeForm(MultiValueMap<String, String> formData, Charset charset) {
StringBuilder builder = new StringBuilder();
formData.forEach((name, values) ->
values.forEach(value -> {
try {
if (builder.length() != 0) {
builder.append('&');
}
builder.append(URLEncoder.encode(name, charset.name()));
if (value != null) {
builder.append('=');
builder.append(URLEncoder.encode(value, charset.name()));
}
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException(ex);
}
}));
return builder.toString();
}
private void writeMultipart(final MultiValueMap<String, Object> parts,
HttpOutputMessage outputMessage) throws IOException {

Loading…
Cancel
Save