Skip to content

Commit 93b7a48

Browse files
committed
UriComponentsBuilder method to configure URI variables
See Javadoc on UriComponentsBuilder#uriVariables for details. This helps to prepare for SPR-17027 where the MvcUriComponentsBuilder already does a partial expand but was forced to build UriComonents and then create a new UriComponentsBuilder from it to continue. This change makes it possible to stay with the same builder instance. Issue: SPR-17027
1 parent 28cd697 commit 93b7a48

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

+34-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.charset.Charset;
2121
import java.nio.charset.StandardCharsets;
2222
import java.util.ArrayList;
23+
import java.util.HashMap;
2324
import java.util.LinkedList;
2425
import java.util.List;
2526
import java.util.Map;
@@ -35,6 +36,7 @@
3536
import org.springframework.util.ObjectUtils;
3637
import org.springframework.util.StringUtils;
3738
import org.springframework.web.util.HierarchicalUriComponents.PathComponent;
39+
import org.springframework.web.util.UriComponents.UriTemplateVariables;
3840

3941
/**
4042
* Builder for {@link UriComponents}.
@@ -121,6 +123,8 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
121123
@Nullable
122124
private String fragment;
123125

126+
private final Map<String, Object> uriVariables = new HashMap<>(4);
127+
124128
private boolean encodeTemplate;
125129

126130
private Charset charset = StandardCharsets.UTF_8;
@@ -381,15 +385,20 @@ public UriComponents build() {
381385
* @return the URI components
382386
*/
383387
public UriComponents build(boolean encoded) {
388+
UriComponents result;
384389
if (this.ssp != null) {
385-
return new OpaqueUriComponents(this.scheme, this.ssp, this.fragment);
390+
result = new OpaqueUriComponents(this.scheme, this.ssp, this.fragment);
386391
}
387392
else {
388-
HierarchicalUriComponents uriComponents =
389-
new HierarchicalUriComponents(this.scheme, this.fragment, this.userInfo,
390-
this.host, this.port, this.pathBuilder.build(), this.queryParams, encoded);
391-
return (this.encodeTemplate ? uriComponents.encodeTemplate(this.charset) : uriComponents);
393+
HierarchicalUriComponents uric = new HierarchicalUriComponents(this.scheme, this.fragment,
394+
this.userInfo, this.host, this.port, this.pathBuilder.build(), this.queryParams, encoded);
395+
396+
result = this.encodeTemplate ? uric.encodeTemplate(this.charset) : uric;
397+
}
398+
if (!this.uriVariables.isEmpty()) {
399+
result = result.expand(name -> this.uriVariables.getOrDefault(name, UriTemplateVariables.SKIP_VALUE));
392400
}
401+
return result;
393402
}
394403

395404
/**
@@ -433,7 +442,7 @@ public URI build(Map<String, ?> uriVariables) {
433442
* @see UriComponents#toUriString()
434443
*/
435444
public String toUriString() {
436-
return build().encode().toUriString();
445+
return encode().build().toUriString();
437446
}
438447

439448

@@ -752,6 +761,25 @@ public UriComponentsBuilder fragment(@Nullable String fragment) {
752761
return this;
753762
}
754763

764+
/**
765+
* Configure URI variables to be expanded at build time.
766+
* <p>The provided variables may be a subset of all required ones. At build
767+
* time, the available ones are expanded, while unresolved URI placeholders
768+
* are left in place and can still be expanded later.
769+
* <p>In contrast to {@link UriComponents#expand(Map)} or
770+
* {@link #buildAndExpand(Map)}, this method is useful when you need to
771+
* supply URI variables without building the {@link UriComponents} instance
772+
* just yet, or perhaps pre-expand some shared default values such as host
773+
* and port.
774+
* @param uriVariables the URI variables to use
775+
* @return this UriComponentsBuilder
776+
* @since 5.0.8
777+
*/
778+
public UriComponentsBuilder uriVariables(Map<String, Object> uriVariables) {
779+
this.uriVariables.putAll(uriVariables);
780+
return this;
781+
}
782+
755783
/**
756784
* Adapt this builder's scheme+host+port from the given headers, specifically
757785
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>,

spring-web/src/test/java/org/springframework/web/util/UriComponentsTests.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,12 @@ public void encodeAndExpand() {
7070
@Test
7171
public void encodeAndExpandPartially() {
7272

73-
Map<String, Object> uriVars = new HashMap<>();
74-
uriVars.put("city", "Z\u00fcrich");
75-
7673
UriComponents uri = UriComponentsBuilder
77-
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build()
78-
.expand(name -> uriVars.getOrDefault(name, UriTemplateVariables.SKIP_VALUE));
79-
80-
assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q={value}", uri.toString());
74+
.fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode()
75+
.uriVariables(Collections.singletonMap("city", "Z\u00fcrich"))
76+
.build();
8177

82-
uriVars.put("value", "a+b");
83-
uri = uri.expand(uriVars);
84-
85-
assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.toString());
78+
assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.expand("a+b").toString());
8679
}
8780

8881
@Test

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import org.springframework.web.servlet.DispatcherServlet;
6464
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
6565
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
66-
import org.springframework.web.util.UriComponents;
6766
import org.springframework.web.util.UriComponentsBuilder;
6867

6968
/**
@@ -547,8 +546,7 @@ private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBu
547546
String path = pathMatcher.combine(typePath, methodPath);
548547
builder.path(path);
549548

550-
UriComponents uriComponents = applyContributors(builder, method, args);
551-
return UriComponentsBuilder.newInstance().uriComponents(uriComponents);
549+
return applyContributors(builder, method, args);
552550
}
553551

554552
private static UriComponentsBuilder getBaseUrlToUse(@Nullable UriComponentsBuilder baseUrl) {
@@ -626,7 +624,7 @@ else if (methods.size() > 1) {
626624
}
627625
}
628626

629-
private static UriComponents applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
627+
private static UriComponentsBuilder applyContributors(UriComponentsBuilder builder, Method method, Object... args) {
630628
CompositeUriComponentsContributor contributor = getUriComponentsContributor();
631629

632630
int paramCount = method.getParameterCount();
@@ -643,9 +641,8 @@ private static UriComponents applyContributors(UriComponentsBuilder builder, Met
643641
contributor.contributeMethodArgument(param, args[i], builder, uriVars);
644642
}
645643

646-
// We may not have all URI var values, expand only what we have
647-
return builder.build().expand(name ->
648-
uriVars.getOrDefault(name, UriComponents.UriTemplateVariables.SKIP_VALUE));
644+
// This may not be all the URI variables, supply what we have so far..
645+
return builder.uriVariables(uriVars);
649646
}
650647

651648
private static CompositeUriComponentsContributor getUriComponentsContributor() {
@@ -851,7 +848,7 @@ public MethodArgumentBuilder(Class<?> controllerType, Method method) {
851848
public MethodArgumentBuilder(@Nullable UriComponentsBuilder baseUrl, Class<?> controllerType, Method method) {
852849
Assert.notNull(controllerType, "'controllerType' is required");
853850
Assert.notNull(method, "'method' is required");
854-
this.baseUrl = (baseUrl != null ? baseUrl : initBaseUrl());
851+
this.baseUrl = baseUrl != null ? baseUrl : UriComponentsBuilder.fromPath(getPath());
855852
this.controllerType = controllerType;
856853
this.method = method;
857854
this.argumentValues = new Object[method.getParameterCount()];
@@ -860,10 +857,10 @@ public MethodArgumentBuilder(@Nullable UriComponentsBuilder baseUrl, Class<?> co
860857
}
861858
}
862859

863-
private static UriComponentsBuilder initBaseUrl() {
860+
private static String getPath() {
864861
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentServletMapping();
865862
String path = builder.build().getPath();
866-
return (path != null ? UriComponentsBuilder.fromPath(path) : UriComponentsBuilder.newInstance());
863+
return path != null ? path : "";
867864
}
868865

869866
public MethodArgumentBuilder arg(int index, Object value) {
@@ -872,13 +869,13 @@ public MethodArgumentBuilder arg(int index, Object value) {
872869
}
873870

874871
public String build() {
875-
return fromMethodInternal(this.baseUrl, this.controllerType, this.method,
876-
this.argumentValues).build(false).encode().toUriString();
872+
return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues)
873+
.build(false).encode().toUriString();
877874
}
878875

879876
public String buildAndExpand(Object... uriVars) {
880-
return fromMethodInternal(this.baseUrl, this.controllerType, this.method,
881-
this.argumentValues).build(false).expand(uriVars).encode().toString();
877+
return fromMethodInternal(this.baseUrl, this.controllerType, this.method, this.argumentValues)
878+
.build(false).expand(uriVars).encode().toString();
882879
}
883880
}
884881

0 commit comments

Comments
 (0)