16
16
17
17
package org .springframework .http .server .reactive ;
18
18
19
+ import java .net .InetSocketAddress ;
19
20
import java .net .URI ;
20
21
import java .net .URISyntaxException ;
22
+ import java .util .LinkedList ;
23
+ import java .util .List ;
24
+ import java .util .Map ;
25
+ import java .util .function .Consumer ;
21
26
27
+ import reactor .core .publisher .Flux ;
28
+
29
+ import org .springframework .core .io .buffer .DataBuffer ;
30
+ import org .springframework .http .HttpCookie ;
22
31
import org .springframework .http .HttpHeaders ;
23
32
import org .springframework .http .HttpMethod ;
24
- import org .springframework .http .server .RequestPath ;
25
33
import org .springframework .lang .Nullable ;
26
34
import org .springframework .util .Assert ;
35
+ import org .springframework .util .LinkedMultiValueMap ;
36
+ import org .springframework .util .MultiValueMap ;
27
37
28
38
/**
29
39
* Package-private default implementation of {@link ServerHttpRequest.Builder}.
34
44
*/
35
45
class DefaultServerHttpRequestBuilder implements ServerHttpRequest .Builder {
36
46
37
- private final ServerHttpRequest delegate ;
47
+ private URI uri ;
48
+
49
+ private HttpHeaders httpHeaders ;
50
+
51
+ private String httpMethodValue ;
52
+
53
+ private final MultiValueMap <String , HttpCookie > cookies ;
38
54
39
55
@ Nullable
40
- private HttpMethod httpMethod ;
56
+ private final InetSocketAddress remoteAddress ;
41
57
42
58
@ Nullable
43
- private String path ;
59
+ private String uriPath ;
44
60
45
61
@ Nullable
46
62
private String contextPath ;
47
63
48
- @ Nullable
49
- private HttpHeaders httpHeaders ;
64
+ private Flux <DataBuffer > body ;
65
+
66
+ public DefaultServerHttpRequestBuilder (ServerHttpRequest original ) {
67
+ Assert .notNull (original , "ServerHttpRequest is required" );
68
+
69
+ this .uri = original .getURI ();
70
+ this .httpMethodValue = original .getMethodValue ();
71
+ this .remoteAddress = original .getRemoteAddress ();
72
+ this .body = original .getBody ();
50
73
74
+ this .httpHeaders = new HttpHeaders ();
75
+ copyMultiValueMap (original .getHeaders (), this .httpHeaders );
51
76
52
- public DefaultServerHttpRequestBuilder (ServerHttpRequest delegate ) {
53
- Assert .notNull (delegate , "ServerHttpRequest delegate is required" );
54
- this .delegate = delegate ;
77
+ this .cookies = new LinkedMultiValueMap <>(original .getCookies ().size ());
78
+ copyMultiValueMap (original .getCookies (), this .cookies );
79
+ }
80
+
81
+ private static <K , V > void copyMultiValueMap (MultiValueMap <K ,V > source ,
82
+ MultiValueMap <K ,V > destination ) {
83
+
84
+ for (Map .Entry <K , List <V >> entry : source .entrySet ()) {
85
+ K key = entry .getKey ();
86
+ List <V > values = new LinkedList <>(entry .getValue ());
87
+ destination .put (key , values );
88
+ }
55
89
}
56
90
57
91
58
92
@ Override
59
93
public ServerHttpRequest .Builder method (HttpMethod httpMethod ) {
60
- this .httpMethod = httpMethod ;
94
+ this .httpMethodValue = httpMethod .name ();
95
+ return this ;
96
+ }
97
+
98
+ @ Override
99
+ public ServerHttpRequest .Builder uri (URI uri ) {
100
+ this .uri = uri ;
61
101
return this ;
62
102
}
63
103
64
104
@ Override
65
105
public ServerHttpRequest .Builder path (String path ) {
66
- this .path = path ;
106
+ this .uriPath = path ;
67
107
return this ;
68
108
}
69
109
@@ -75,111 +115,79 @@ public ServerHttpRequest.Builder contextPath(String contextPath) {
75
115
76
116
@ Override
77
117
public ServerHttpRequest .Builder header (String key , String value ) {
78
- if (this .httpHeaders == null ) {
79
- this .httpHeaders = new HttpHeaders ();
80
- }
81
118
this .httpHeaders .add (key , value );
82
119
return this ;
83
120
}
84
121
122
+ @ Override
123
+ public ServerHttpRequest .Builder headers (Consumer <HttpHeaders > headersConsumer ) {
124
+ Assert .notNull (headersConsumer , "'headersConsumer' must not be null" );
125
+ headersConsumer .accept (this .httpHeaders );
126
+ return this ;
127
+ }
128
+
85
129
@ Override
86
130
public ServerHttpRequest build () {
87
131
URI uriToUse = getUriToUse ();
88
- RequestPath path = getRequestPathToUse (uriToUse );
89
- HttpHeaders headers = getHeadersToUse ( );
90
- return new MutativeDecorator ( this . delegate , this . httpMethod , uriToUse , path , headers );
132
+ return new DefaultServerHttpRequest (uriToUse , this . contextPath , this . httpHeaders ,
133
+ this . httpMethodValue , this . cookies , this . remoteAddress , this . body );
134
+
91
135
}
92
136
93
- @ Nullable
94
137
private URI getUriToUse () {
95
- if (this .path == null ) {
96
- return null ;
138
+ if (this .uriPath == null ) {
139
+ return this . uri ;
97
140
}
98
- URI uri = this .delegate .getURI ();
99
141
try {
100
- return new URI (uri .getScheme (), uri .getUserInfo (), uri .getHost (), uri .getPort (),
101
- this . path , uri .getQuery (), uri .getFragment ());
142
+ return new URI (this . uri .getScheme (), this . uri .getUserInfo (), uri .getHost (), uri .getPort (),
143
+ uriPath , uri .getQuery (), uri .getFragment ());
102
144
}
103
145
catch (URISyntaxException ex ) {
104
- throw new IllegalStateException ("Invalid URI path: \" " + this .path + "\" " );
105
- }
106
- }
107
-
108
- @ Nullable
109
- private RequestPath getRequestPathToUse (@ Nullable URI uriToUse ) {
110
- if (uriToUse == null && this .contextPath == null ) {
111
- return null ;
112
- }
113
- else if (uriToUse == null ) {
114
- return this .delegate .getPath ().modifyContextPath (this .contextPath );
115
- }
116
- else {
117
- return RequestPath .parse (uriToUse , this .contextPath );
118
- }
119
- }
120
-
121
- @ Nullable
122
- private HttpHeaders getHeadersToUse () {
123
- if (this .httpHeaders != null ) {
124
- HttpHeaders headers = new HttpHeaders ();
125
- headers .putAll (this .delegate .getHeaders ());
126
- headers .putAll (this .httpHeaders );
127
- return headers ;
128
- }
129
- else {
130
- return null ;
146
+ throw new IllegalStateException ("Invalid URI path: \" " + this .uriPath + "\" " );
131
147
}
132
148
}
133
149
150
+ private static class DefaultServerHttpRequest extends AbstractServerHttpRequest {
134
151
135
- /**
136
- * An immutable wrapper of a request returning property overrides -- given
137
- * to the constructor -- or original values otherwise.
138
- */
139
- private static class MutativeDecorator extends ServerHttpRequestDecorator {
140
-
141
- @ Nullable
142
- private final HttpMethod httpMethod ;
143
-
144
- @ Nullable
145
- private final URI uri ;
152
+ private final String methodValue ;
146
153
147
- @ Nullable
148
- private final RequestPath requestPath ;
154
+ private final MultiValueMap <String , HttpCookie > cookies ;
149
155
150
156
@ Nullable
151
- private final HttpHeaders httpHeaders ;
152
-
153
-
154
- public MutativeDecorator (ServerHttpRequest delegate , @ Nullable HttpMethod method ,
155
- @ Nullable URI uri , @ Nullable RequestPath requestPath , @ Nullable HttpHeaders httpHeaders ) {
156
-
157
- super (delegate );
158
- this .httpMethod = method ;
159
- this .uri = uri ;
160
- this .requestPath = requestPath ;
161
- this .httpHeaders = httpHeaders ;
157
+ private final InetSocketAddress remoteAddress ;
158
+
159
+ private final Flux <DataBuffer > body ;
160
+
161
+ public DefaultServerHttpRequest (URI uri , @ Nullable String contextPath ,
162
+ HttpHeaders headers , String methodValue ,
163
+ MultiValueMap <String , HttpCookie > cookies , @ Nullable InetSocketAddress remoteAddress ,
164
+ Flux <DataBuffer > body ) {
165
+ super (uri , contextPath , headers );
166
+ this .methodValue = methodValue ;
167
+ this .cookies = cookies ;
168
+ this .remoteAddress = remoteAddress ;
169
+ this .body = body ;
162
170
}
163
171
164
172
@ Override
165
- @ Nullable
166
- public HttpMethod getMethod () {
167
- return (this .httpMethod != null ? this .httpMethod : super .getMethod ());
173
+ public String getMethodValue () {
174
+ return this .methodValue ;
168
175
}
169
176
170
177
@ Override
171
- public URI getURI () {
172
- return ( this .uri != null ? this . uri : super . getURI ()) ;
178
+ protected MultiValueMap < String , HttpCookie > initCookies () {
179
+ return this .cookies ;
173
180
}
174
181
182
+ @ Nullable
175
183
@ Override
176
- public RequestPath getPath () {
177
- return ( this .requestPath != null ? this . requestPath : super . getPath ()) ;
184
+ public InetSocketAddress getRemoteAddress () {
185
+ return this .remoteAddress ;
178
186
}
179
187
180
188
@ Override
181
- public HttpHeaders getHeaders () {
182
- return ( this .httpHeaders != null ? this . httpHeaders : super . getHeaders ()) ;
189
+ public Flux < DataBuffer > getBody () {
190
+ return this .body ;
183
191
}
184
192
}
185
193
0 commit comments