Skip to content

Commit 46599e7

Browse files
sdeleuzerstoyanchev
authored andcommitted
Add FormHttpMessageReader/Writer
Issue: SPR-14540
1 parent c3f22b7 commit 46599e7

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLDecoder;
21+
import java.nio.CharBuffer;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import reactor.core.publisher.Flux;
29+
import reactor.core.publisher.Mono;
30+
31+
import org.springframework.core.ResolvableType;
32+
import org.springframework.core.io.buffer.DataBuffer;
33+
import org.springframework.core.io.buffer.DataBufferUtils;
34+
import org.springframework.http.MediaType;
35+
import org.springframework.http.ReactiveHttpInputMessage;
36+
import org.springframework.util.Assert;
37+
import org.springframework.util.LinkedMultiValueMap;
38+
import org.springframework.util.MultiValueMap;
39+
import org.springframework.util.StringUtils;
40+
41+
/**
42+
* Implementation of {@link HttpMessageReader} to read 'normal' HTML
43+
* forms with {@code "application/x-www-form-urlencoded"} media type.
44+
*
45+
* @author Sebastien Deleuze
46+
*/
47+
public class FormHttpMessageReader implements HttpMessageReader<MultiValueMap<String, String>> {
48+
49+
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
50+
51+
private static final ResolvableType formType = ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class);
52+
53+
private Charset charset = DEFAULT_CHARSET;
54+
55+
56+
@Override
57+
public boolean canRead(ResolvableType elementType, MediaType mediaType) {
58+
return (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) &&
59+
formType.isAssignableFrom(elementType);
60+
}
61+
62+
@Override
63+
public Flux<MultiValueMap<String, String>> read(ResolvableType elementType,
64+
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
65+
return Flux.from(readMono(elementType, inputMessage, hints));
66+
}
67+
68+
@Override
69+
public Mono<MultiValueMap<String, String>> readMono(ResolvableType elementType,
70+
ReactiveHttpInputMessage inputMessage, Map<String, Object> hints) {
71+
72+
MediaType contentType = inputMessage.getHeaders().getContentType();
73+
Charset charset = (contentType.getCharset() != null ? contentType.getCharset() : this.charset);
74+
75+
return inputMessage.getBody()
76+
.reduce(DataBuffer::write)
77+
.map(buffer -> {
78+
CharBuffer charBuffer = charset.decode(buffer.asByteBuffer());
79+
DataBufferUtils.release(buffer);
80+
String body = charBuffer.toString();
81+
String[] pairs = StringUtils.tokenizeToStringArray(body, "&");
82+
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(pairs.length);
83+
try {
84+
for (String pair : pairs) {
85+
int idx = pair.indexOf('=');
86+
if (idx == -1) {
87+
result.add(URLDecoder.decode(pair, charset.name()), null);
88+
}
89+
else {
90+
String name = URLDecoder.decode(pair.substring(0, idx), charset.name());
91+
String value = URLDecoder.decode(pair.substring(idx + 1), charset.name());
92+
result.add(name, value);
93+
}
94+
}
95+
}
96+
catch (UnsupportedEncodingException ex) {
97+
throw new IllegalStateException(ex);
98+
}
99+
100+
return result;
101+
});
102+
}
103+
104+
@Override
105+
public List<MediaType> getReadableMediaTypes() {
106+
return Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED);
107+
}
108+
109+
/**
110+
* Set the default character set to use for reading form data when the request
111+
* Content-Type header does not explicitly specify it.
112+
* <p>By default this is set to "UTF-8".
113+
*/
114+
public void setCharset(Charset charset) {
115+
Assert.notNull(charset, "'charset' must not be null");
116+
this.charset = charset;
117+
}
118+
119+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLEncoder;
21+
import java.nio.ByteBuffer;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.StandardCharsets;
24+
import java.util.Collections;
25+
import java.util.Iterator;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
import org.reactivestreams.Publisher;
30+
import reactor.core.publisher.Flux;
31+
import reactor.core.publisher.Mono;
32+
33+
import org.springframework.core.ResolvableType;
34+
import org.springframework.core.io.buffer.DataBuffer;
35+
import org.springframework.http.MediaType;
36+
import org.springframework.http.ReactiveHttpOutputMessage;
37+
import org.springframework.util.Assert;
38+
import org.springframework.util.MultiValueMap;
39+
40+
/**
41+
* Implementation of {@link HttpMessageWriter} to write 'normal' HTML
42+
* forms with {@code "application/x-www-form-urlencoded"} media type.
43+
*
44+
* @author Sebastien Deleuze
45+
* @since 5.0
46+
* @see MultiValueMap
47+
*/
48+
public class FormHttpMessageWriter implements HttpMessageWriter<MultiValueMap<String, String>> {
49+
50+
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
51+
52+
private static final ResolvableType formType = ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class);
53+
54+
private Charset charset = DEFAULT_CHARSET;
55+
56+
57+
@Override
58+
public boolean canWrite(ResolvableType elementType, MediaType mediaType) {
59+
return (mediaType == null || MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)) &&
60+
formType.isAssignableFrom(elementType);
61+
}
62+
63+
@Override
64+
public Mono<Void> write(Publisher<? extends MultiValueMap<String, String>> inputStream,
65+
ResolvableType elementType, MediaType mediaType, ReactiveHttpOutputMessage outputMessage,
66+
Map<String, Object> hints) {
67+
68+
MediaType contentType = outputMessage.getHeaders().getContentType();
69+
Charset charset;
70+
if (contentType != null) {
71+
outputMessage.getHeaders().setContentType(contentType);
72+
charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : this.charset);
73+
}
74+
else {
75+
outputMessage.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
76+
charset = this.charset;
77+
}
78+
return Flux
79+
.from(inputStream)
80+
.single()
81+
.map(form -> generateForm(form))
82+
.then(value -> {
83+
ByteBuffer byteBuffer = charset.encode(value);
84+
DataBuffer buffer = outputMessage.bufferFactory().wrap(byteBuffer);
85+
outputMessage.getHeaders().setContentLength(byteBuffer.remaining());
86+
return outputMessage.writeWith(Mono.just(buffer));
87+
});
88+
89+
}
90+
91+
private String generateForm(MultiValueMap<String, String> form) {
92+
StringBuilder builder = new StringBuilder();
93+
try {
94+
for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext();) {
95+
String name = nameIterator.next();
96+
for (Iterator<String> valueIterator = form.get(name).iterator(); valueIterator.hasNext();) {
97+
String value = valueIterator.next();
98+
builder.append(URLEncoder.encode(name, charset.name()));
99+
if (value != null) {
100+
builder.append('=');
101+
builder.append(URLEncoder.encode(value, charset.name()));
102+
if (valueIterator.hasNext()) {
103+
builder.append('&');
104+
}
105+
}
106+
}
107+
if (nameIterator.hasNext()) {
108+
builder.append('&');
109+
}
110+
}
111+
}
112+
catch (UnsupportedEncodingException ex) {
113+
throw new IllegalStateException(ex);
114+
}
115+
return builder.toString();
116+
}
117+
118+
@Override
119+
public List<MediaType> getWritableMediaTypes() {
120+
return Collections.singletonList(MediaType.APPLICATION_FORM_URLENCODED);
121+
}
122+
123+
/**
124+
* Set the default character set to use for writing form data when the response
125+
* Content-Type header does not explicitly specify it.
126+
* <p>By default this is set to "UTF-8".
127+
*/
128+
public void setCharset(Charset charset) {
129+
Assert.notNull(charset, "'charset' must not be null");
130+
this.charset = charset;
131+
}
132+
133+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.codec;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import static org.junit.Assert.*;
23+
import org.junit.Test;
24+
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.http.MediaType;
27+
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
28+
import org.springframework.util.MultiValueMap;
29+
30+
/**
31+
* @author Sebastien Deleuze
32+
*/
33+
public class FormHttpMessageReaderTests {
34+
35+
private final FormHttpMessageReader reader = new FormHttpMessageReader();
36+
37+
@Test
38+
public void canRead() {
39+
assertTrue(this.reader.canRead(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
40+
MediaType.APPLICATION_FORM_URLENCODED));
41+
assertFalse(this.reader.canRead(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Object.class),
42+
MediaType.APPLICATION_FORM_URLENCODED));
43+
assertFalse(this.reader.canRead(ResolvableType.forClassWithGenerics(MultiValueMap.class, Object.class, String.class),
44+
MediaType.APPLICATION_FORM_URLENCODED));
45+
assertFalse(this.reader.canRead(ResolvableType.forClassWithGenerics(Map.class, String.class, String.class),
46+
MediaType.APPLICATION_FORM_URLENCODED));
47+
assertFalse(this.reader.canRead(ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, String.class),
48+
MediaType.MULTIPART_FORM_DATA));
49+
}
50+
51+
@Test
52+
public void readFormAsMono() {
53+
String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
54+
MockServerHttpRequest request = new MockServerHttpRequest();
55+
request.setBody(body);
56+
request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
57+
MultiValueMap<String, String> result = this.reader.readMono(null, request, null).block();
58+
59+
assertEquals("Invalid result", 3, result.size());
60+
assertEquals("Invalid result", "value 1", result.getFirst("name 1"));
61+
List<String> values = result.get("name 2");
62+
assertEquals("Invalid result", 2, values.size());
63+
assertEquals("Invalid result", "value 2+1", values.get(0));
64+
assertEquals("Invalid result", "value 2+2", values.get(1));
65+
assertNull("Invalid result", result.getFirst("name 3"));
66+
}
67+
68+
@Test
69+
public void readFormAsFlux() {
70+
String body = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3";
71+
MockServerHttpRequest request = new MockServerHttpRequest();
72+
request.setBody(body);
73+
request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED);
74+
MultiValueMap<String, String> result = this.reader.read(null, request, null).single().block();
75+
76+
assertEquals("Invalid result", 3, result.size());
77+
assertEquals("Invalid result", "value 1", result.getFirst("name 1"));
78+
List<String> values = result.get("name 2");
79+
assertEquals("Invalid result", 2, values.size());
80+
assertEquals("Invalid result", "value 2+1", values.get(0));
81+
assertEquals("Invalid result", "value 2+2", values.get(1));
82+
assertNull("Invalid result", result.getFirst("name 3"));
83+
}
84+
85+
}

0 commit comments

Comments
 (0)