20
20
import java .time .Instant ;
21
21
import java .time .temporal .ChronoUnit ;
22
22
import java .util .Arrays ;
23
+ import java .util .Collections ;
23
24
import java .util .List ;
24
25
import java .util .Map ;
25
26
import java .util .Optional ;
26
27
import java .util .concurrent .ConcurrentHashMap ;
27
28
28
29
import reactor .core .publisher .Mono ;
29
30
31
+ import org .springframework .core .ResolvableType ;
30
32
import org .springframework .http .HttpHeaders ;
31
33
import org .springframework .http .HttpMethod ;
32
34
import org .springframework .http .HttpStatus ;
35
+ import org .springframework .http .InvalidMediaTypeException ;
36
+ import org .springframework .http .MediaType ;
37
+ import org .springframework .http .codec .FormHttpMessageReader ;
33
38
import org .springframework .http .server .reactive .ServerHttpRequest ;
34
39
import org .springframework .http .server .reactive .ServerHttpResponse ;
35
40
import org .springframework .util .Assert ;
41
+ import org .springframework .util .MultiValueMap ;
36
42
import org .springframework .util .StringUtils ;
37
43
import org .springframework .web .server .ServerWebExchange ;
38
44
import org .springframework .web .server .WebSession ;
@@ -48,6 +54,11 @@ public class DefaultServerWebExchange implements ServerWebExchange {
48
54
49
55
private static final List <HttpMethod > SAFE_METHODS = Arrays .asList (HttpMethod .GET , HttpMethod .HEAD );
50
56
57
+ private static final FormHttpMessageReader FORM_READER = new FormHttpMessageReader ();
58
+
59
+ private static final ResolvableType MULTIVALUE_TYPE =
60
+ ResolvableType .forClassWithGenerics (MultiValueMap .class , String .class , String .class );
61
+
51
62
52
63
private final ServerHttpRequest request ;
53
64
@@ -57,6 +68,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
57
68
58
69
private final Mono <WebSession > sessionMono ;
59
70
71
+ private final Mono <MultiValueMap <String , String >> formDataMono ;
72
+
60
73
private volatile boolean notModified ;
61
74
62
75
@@ -66,9 +79,25 @@ public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse re
66
79
Assert .notNull (request , "'request' is required" );
67
80
Assert .notNull (response , "'response' is required" );
68
81
Assert .notNull (response , "'sessionManager' is required" );
82
+ Assert .notNull (response , "'formReader' is required" );
69
83
this .request = request ;
70
84
this .response = response ;
71
85
this .sessionMono = sessionManager .getSession (this ).cache ();
86
+ this .formDataMono = initFormData (request );
87
+ }
88
+
89
+ private static Mono <MultiValueMap <String , String >> initFormData (ServerHttpRequest request ) {
90
+ MediaType contentType ;
91
+ try {
92
+ contentType = request .getHeaders ().getContentType ();
93
+ if (MediaType .APPLICATION_FORM_URLENCODED .isCompatibleWith (contentType )) {
94
+ return FORM_READER .readMono (MULTIVALUE_TYPE , request , Collections .emptyMap ()).cache ();
95
+ }
96
+ }
97
+ catch (InvalidMediaTypeException ex ) {
98
+ // Ignore
99
+ }
100
+ return Mono .empty ();
72
101
}
73
102
74
103
@@ -110,6 +139,11 @@ public <T extends Principal> Optional<T> getPrincipal() {
110
139
return Optional .empty ();
111
140
}
112
141
142
+ @ Override
143
+ public Mono <MultiValueMap <String , String >> getFormData () {
144
+ return this .formDataMono ;
145
+ }
146
+
113
147
@ Override
114
148
public boolean isNotModified () {
115
149
return this .notModified ;
0 commit comments