1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import java .util .concurrent .ConcurrentHashMap ;
26
26
import java .util .function .Function ;
27
27
28
+ import reactor .core .publisher .Flux ;
28
29
import reactor .core .publisher .Mono ;
29
30
30
31
import org .springframework .context .ApplicationContext ;
@@ -65,8 +66,7 @@ public class DefaultServerWebExchange implements ServerWebExchange {
65
66
private static final ResolvableType FORM_DATA_TYPE =
66
67
ResolvableType .forClassWithGenerics (MultiValueMap .class , String .class , String .class );
67
68
68
- private static final ResolvableType MULTIPART_DATA_TYPE = ResolvableType .forClassWithGenerics (
69
- MultiValueMap .class , String .class , Part .class );
69
+ private static final ResolvableType PARTS_DATA_TYPE = ResolvableType .forClass (Part .class );
70
70
71
71
private static final Mono <MultiValueMap <String , String >> EMPTY_FORM_DATA =
72
72
Mono .just (CollectionUtils .unmodifiableMultiValueMap (new LinkedMultiValueMap <String , String >(0 )))
@@ -91,6 +91,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
91
91
92
92
private final Mono <MultiValueMap <String , Part >> multipartDataMono ;
93
93
94
+ private final Flux <Part > partFlux ;
95
+
94
96
@ Nullable
95
97
private final ApplicationContext applicationContext ;
96
98
@@ -129,7 +131,8 @@ public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse re
129
131
this .sessionMono = sessionManager .getSession (this ).cache ();
130
132
this .localeContextResolver = localeContextResolver ;
131
133
this .formDataMono = initFormData (request , codecConfigurer , getLogPrefix ());
132
- this .multipartDataMono = initMultipartData (request , codecConfigurer , getLogPrefix ());
134
+ this .partFlux = initParts (request , codecConfigurer , getLogPrefix ());
135
+ this .multipartDataMono = initMultipartData (this .partFlux );
133
136
this .applicationContext = applicationContext ;
134
137
}
135
138
@@ -156,28 +159,34 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
156
159
}
157
160
158
161
@ SuppressWarnings ("unchecked" )
159
- private static Mono <MultiValueMap <String , Part >> initMultipartData (ServerHttpRequest request ,
160
- ServerCodecConfigurer configurer , String logPrefix ) {
161
-
162
+ private static Flux <Part > initParts (ServerHttpRequest request , ServerCodecConfigurer configurer , String logPrefix ) {
162
163
try {
163
164
MediaType contentType = request .getHeaders ().getContentType ();
164
165
if (MediaType .MULTIPART_FORM_DATA .isCompatibleWith (contentType )) {
165
- return ((HttpMessageReader <MultiValueMap < String , Part >>) configurer .getReaders ().stream ()
166
- .filter (reader -> reader .canRead (MULTIPART_DATA_TYPE , MediaType .MULTIPART_FORM_DATA ))
166
+ return ((HttpMessageReader <Part >) configurer .getReaders ().stream ()
167
+ .filter (reader -> reader .canRead (PARTS_DATA_TYPE , MediaType .MULTIPART_FORM_DATA ))
167
168
.findFirst ()
168
169
.orElseThrow (() -> new IllegalStateException ("No multipart HttpMessageReader." )))
169
- .readMono (MULTIPART_DATA_TYPE , request , Hints .from (Hints .LOG_PREFIX_HINT , logPrefix ))
170
- .switchIfEmpty (EMPTY_MULTIPART_DATA )
170
+ .read (PARTS_DATA_TYPE , request , Hints .from (Hints .LOG_PREFIX_HINT , logPrefix ))
171
171
.cache ();
172
172
}
173
173
}
174
174
catch (InvalidMediaTypeException ex ) {
175
175
// Ignore
176
176
}
177
- return EMPTY_MULTIPART_DATA ;
177
+ return Flux .empty ();
178
+ }
179
+
180
+ private static Mono <MultiValueMap <String , Part >> initMultipartData (Flux <Part > parts ) {
181
+ return parts .collect (
182
+ () -> (MultiValueMap <String , Part >) new LinkedMultiValueMap <String , Part >(),
183
+ (map , part ) -> map .add (part .name (), part ))
184
+ .switchIfEmpty (EMPTY_MULTIPART_DATA )
185
+ .cache ();
178
186
}
179
187
180
188
189
+
181
190
@ Override
182
191
public ServerHttpRequest getRequest () {
183
192
return this .request ;
@@ -221,6 +230,11 @@ public Mono<MultiValueMap<String, Part>> getMultipartData() {
221
230
return this .multipartDataMono ;
222
231
}
223
232
233
+ @ Override
234
+ public Flux <Part > getParts () {
235
+ return this .partFlux ;
236
+ }
237
+
224
238
@ Override
225
239
public LocaleContext getLocaleContext () {
226
240
return this .localeContextResolver .resolveLocaleContext (this );
0 commit comments