@@ -100,6 +100,9 @@ public int hashCode() {
100
100
}
101
101
};
102
102
103
+ private static final MultiValueMap <String , String > EMPTY_QUERY_PARAMS =
104
+ CollectionUtils .unmodifiableMultiValueMap (new LinkedMultiValueMap <>(0 ));
105
+
103
106
104
107
@ Nullable
105
108
private final String userInfo ;
@@ -127,22 +130,21 @@ public int hashCode() {
127
130
* @param host the host
128
131
* @param port the port
129
132
* @param path the path
130
- * @param queryParams the query parameters
133
+ * @param query the query parameters
131
134
* @param fragment the fragment
132
135
* @param encoded whether the components are already encoded
133
136
*/
134
137
HierarchicalUriComponents (@ Nullable String scheme , @ Nullable String fragment , @ Nullable String userInfo ,
135
138
@ Nullable String host , @ Nullable String port , @ Nullable PathComponent path ,
136
- @ Nullable MultiValueMap <String , String > queryParams , boolean encoded ) {
139
+ @ Nullable MultiValueMap <String , String > query , boolean encoded ) {
137
140
138
141
super (scheme , fragment );
139
142
140
143
this .userInfo = userInfo ;
141
144
this .host = host ;
142
145
this .port = port ;
143
- this .path = (path != null ? path : NULL_PATH_COMPONENT );
144
- this .queryParams = CollectionUtils .unmodifiableMultiValueMap (
145
- queryParams != null ? queryParams : new LinkedMultiValueMap <>(0 ));
146
+ this .path = path != null ? path : NULL_PATH_COMPONENT ;
147
+ this .queryParams = query != null ? CollectionUtils .unmodifiableMultiValueMap (query ) : EMPTY_QUERY_PARAMS ;
146
148
this .encodeState = encoded ? EncodeState .FULLY_ENCODED : EncodeState .RAW ;
147
149
148
150
// Check for illegal characters..
@@ -151,19 +153,18 @@ public int hashCode() {
151
153
}
152
154
}
153
155
154
- private HierarchicalUriComponents (@ Nullable String scheme , @ Nullable String fragment , @ Nullable String userInfo ,
155
- @ Nullable String host , @ Nullable String port , @ Nullable PathComponent path ,
156
- @ Nullable MultiValueMap <String , String > queryParams , EncodeState encodeState ,
157
- @ Nullable UnaryOperator <String > variableEncoder ) {
156
+ private HierarchicalUriComponents (@ Nullable String scheme , @ Nullable String fragment ,
157
+ @ Nullable String userInfo , @ Nullable String host , @ Nullable String port ,
158
+ PathComponent path , MultiValueMap <String , String > queryParams ,
159
+ EncodeState encodeState , @ Nullable UnaryOperator <String > variableEncoder ) {
158
160
159
161
super (scheme , fragment );
160
162
161
163
this .userInfo = userInfo ;
162
164
this .host = host ;
163
165
this .port = port ;
164
- this .path = (path != null ? path : NULL_PATH_COMPONENT );
165
- this .queryParams = CollectionUtils .unmodifiableMultiValueMap (
166
- queryParams != null ? queryParams : new LinkedMultiValueMap <>(0 ));
166
+ this .path = path ;
167
+ this .queryParams = queryParams ;
167
168
this .encodeState = encodeState ;
168
169
this .variableEncoder = variableEncoder ;
169
170
}
@@ -254,6 +255,11 @@ public MultiValueMap<String, String> getQueryParams() {
254
255
255
256
// Encoding
256
257
258
+ /**
259
+ * Identical to {@link #encode()} but skipping over URI variable placeholders.
260
+ * Also {@link #variableEncoder} is initialized with the given charset for
261
+ * use later when URI variables are expanded.
262
+ */
257
263
HierarchicalUriComponents encodeTemplate (Charset charset ) {
258
264
if (this .encodeState .isEncoded ()) {
259
265
return this ;
@@ -268,10 +274,10 @@ HierarchicalUriComponents encodeTemplate(Charset charset) {
268
274
String userInfoTo = (getUserInfo () != null ? encoder .apply (getUserInfo (), Type .USER_INFO ) : null );
269
275
String hostTo = (getHost () != null ? encoder .apply (getHost (), getHostType ()) : null );
270
276
PathComponent pathTo = this .path .encode (encoder );
271
- MultiValueMap <String , String > paramsTo = encodeQueryParams (encoder );
277
+ MultiValueMap <String , String > queryParamsTo = encodeQueryParams (encoder );
272
278
273
279
return new HierarchicalUriComponents (schemeTo , fragmentTo , userInfoTo ,
274
- hostTo , this .port , pathTo , paramsTo , EncodeState .TEMPLATE_ENCODED , this .variableEncoder );
280
+ hostTo , this .port , pathTo , queryParamsTo , EncodeState .TEMPLATE_ENCODED , this .variableEncoder );
275
281
}
276
282
277
283
@ Override
@@ -287,10 +293,10 @@ public HierarchicalUriComponents encode(Charset charset) {
287
293
String hostTo = (this .host != null ? encodeUriComponent (this .host , charset , getHostType ()) : null );
288
294
BiFunction <String , Type , String > encoder = (s , type ) -> encodeUriComponent (s , charset , type );
289
295
PathComponent pathTo = this .path .encode (encoder );
290
- MultiValueMap <String , String > paramsTo = encodeQueryParams (encoder );
296
+ MultiValueMap <String , String > queryParamsTo = encodeQueryParams (encoder );
291
297
292
298
return new HierarchicalUriComponents (schemeTo , fragmentTo , userInfoTo ,
293
- hostTo , this .port , pathTo , paramsTo , EncodeState .FULLY_ENCODED , null );
299
+ hostTo , this .port , pathTo , queryParamsTo , EncodeState .FULLY_ENCODED , null );
294
300
}
295
301
296
302
private MultiValueMap <String , String > encodeQueryParams (BiFunction <String , Type , String > encoder ) {
@@ -300,11 +306,11 @@ private MultiValueMap<String, String> encodeQueryParams(BiFunction<String, Type,
300
306
String name = encoder .apply (key , Type .QUERY_PARAM );
301
307
List <String > encodedValues = new ArrayList <>(values .size ());
302
308
for (String value : values ) {
303
- encodedValues .add (encoder .apply (value , Type .QUERY_PARAM ));
309
+ encodedValues .add (value != null ? encoder .apply (value , Type .QUERY_PARAM ) : null );
304
310
}
305
311
result .put (name , encodedValues );
306
312
});
307
- return result ;
313
+ return CollectionUtils . unmodifiableMultiValueMap ( result ) ;
308
314
}
309
315
310
316
/**
@@ -428,10 +434,10 @@ protected HierarchicalUriComponents expandInternal(UriTemplateVariables uriVaria
428
434
String hostTo = expandUriComponent (this .host , uriVariables , this .variableEncoder );
429
435
String portTo = expandUriComponent (this .port , uriVariables , this .variableEncoder );
430
436
PathComponent pathTo = this .path .expand (uriVariables , this .variableEncoder );
431
- MultiValueMap <String , String > paramsTo = expandQueryParams (uriVariables );
437
+ MultiValueMap <String , String > queryParamsTo = expandQueryParams (uriVariables );
432
438
433
439
return new HierarchicalUriComponents (schemeTo , fragmentTo , userInfoTo ,
434
- hostTo , portTo , pathTo , paramsTo , this .encodeState , this .variableEncoder );
440
+ hostTo , portTo , pathTo , queryParamsTo , this .encodeState , this .variableEncoder );
435
441
}
436
442
437
443
private MultiValueMap <String , String > expandQueryParams (UriTemplateVariables variables ) {
@@ -446,7 +452,7 @@ private MultiValueMap<String, String> expandQueryParams(UriTemplateVariables var
446
452
}
447
453
result .put (name , expandedValues );
448
454
});
449
- return result ;
455
+ return CollectionUtils . unmodifiableMultiValueMap ( result ) ;
450
456
}
451
457
452
458
@ Override
0 commit comments