1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2024 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.
24
24
import java .util .Optional ;
25
25
26
26
import org .springframework .lang .Nullable ;
27
+ import org .springframework .util .CollectionUtils ;
27
28
import org .springframework .util .MultiValueMap ;
28
29
import org .springframework .util .ObjectUtils ;
29
30
import org .springframework .util .StringUtils ;
@@ -46,7 +47,8 @@ public class DefaultUriBuilderFactory implements UriBuilderFactory {
46
47
47
48
private EncodingMode encodingMode = EncodingMode .TEMPLATE_AND_VALUES ;
48
49
49
- private final Map <String , Object > defaultUriVariables = new HashMap <>();
50
+ @ Nullable
51
+ private Map <String , Object > defaultUriVariables ;
50
52
51
53
private boolean parsePath = true ;
52
54
@@ -108,17 +110,31 @@ public EncodingMode getEncodingMode() {
108
110
* @param defaultUriVariables default URI variable values
109
111
*/
110
112
public void setDefaultUriVariables (@ Nullable Map <String , ?> defaultUriVariables ) {
111
- this .defaultUriVariables .clear ();
112
113
if (defaultUriVariables != null ) {
113
- this .defaultUriVariables .putAll (defaultUriVariables );
114
+ if (this .defaultUriVariables == null ) {
115
+ this .defaultUriVariables = new HashMap <>(defaultUriVariables );
116
+ }
117
+ else {
118
+ this .defaultUriVariables .putAll (defaultUriVariables );
119
+ }
120
+ }
121
+ else {
122
+ if (this .defaultUriVariables != null ) {
123
+ this .defaultUriVariables .clear ();
124
+ }
114
125
}
115
126
}
116
127
117
128
/**
118
129
* Return the configured default URI variable values.
119
130
*/
120
131
public Map <String , ?> getDefaultUriVariables () {
121
- return Collections .unmodifiableMap (this .defaultUriVariables );
132
+ if (this .defaultUriVariables != null ) {
133
+ return Collections .unmodifiableMap (this .defaultUriVariables );
134
+ }
135
+ else {
136
+ return Collections .emptyMap ();
137
+ }
122
138
}
123
139
124
140
/**
@@ -141,6 +157,20 @@ public boolean shouldParsePath() {
141
157
return this .parsePath ;
142
158
}
143
159
160
+ /**
161
+ * Indicates whether this {@code DefaultUriBuilderFactory} uses the default
162
+ * {@link org.springframework.web.client.RestTemplate RestTemplate}
163
+ * settings.
164
+ * @since 6.1.4
165
+ */
166
+ public boolean hasRestTemplateDefaults () {
167
+ // see RestTemplate::initUriTemplateHandler
168
+ return this .baseUri == null &&
169
+ this .encodingMode == EncodingMode .URI_COMPONENT &&
170
+ CollectionUtils .isEmpty (this .defaultUriVariables ) &&
171
+ this .parsePath ;
172
+ }
173
+
144
174
145
175
// UriTemplateHandler
146
176
@@ -379,8 +409,8 @@ public DefaultUriBuilder fragment(@Nullable String fragment) {
379
409
380
410
@ Override
381
411
public URI build (Map <String , ?> uriVars ) {
382
- if (!defaultUriVariables .isEmpty ()) {
383
- Map <String , Object > map = new HashMap <>();
412
+ if (!CollectionUtils .isEmpty (defaultUriVariables )) {
413
+ Map <String , Object > map = new HashMap <>(defaultUriVariables . size () + uriVars . size () );
384
414
map .putAll (defaultUriVariables );
385
415
map .putAll (uriVars );
386
416
uriVars = map ;
@@ -394,7 +424,7 @@ public URI build(Map<String, ?> uriVars) {
394
424
395
425
@ Override
396
426
public URI build (Object ... uriVars ) {
397
- if (ObjectUtils .isEmpty (uriVars ) && !defaultUriVariables .isEmpty ()) {
427
+ if (ObjectUtils .isEmpty (uriVars ) && !CollectionUtils .isEmpty (defaultUriVariables )) {
398
428
return build (Collections .emptyMap ());
399
429
}
400
430
if (encodingMode .equals (EncodingMode .VALUES_ONLY )) {
0 commit comments