Skip to content

Commit

Permalink
🎨 UriComponentsBuilder API
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKETODAY committed Jan 9, 2025
1 parent 6ffeab4 commit 0385894
Showing 43 changed files with 324 additions and 313 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -120,7 +120,7 @@ public void fromControllerNotMapped() {

@Test
public void fromControllerWithCustomBaseURIViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
UriComponents uriComponents = fromController(builder, PersonControllerImpl.class).build();

assertThat(uriComponents.toString()).isEqualTo("https://example.org:9090/base/people");
@@ -129,7 +129,7 @@ public void fromControllerWithCustomBaseURIViaStaticCall() {

@Test
public void fromControllerWithCustomBaseURIViaInstance() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = relativeTo(builder);
UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build();

@@ -202,7 +202,7 @@ public void fromMethodNameInUnmappedControllerMethod() {

@Test
public void fromMethodNameWithCustomBaseURIViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class,
"methodWithPathVariable", "1").build();

@@ -212,7 +212,7 @@ public void fromMethodNameWithCustomBaseURIViaStaticCall() {

@Test
public void fromMethodNameWithCustomBaseURIViaInstance() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = relativeTo(builder);
UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class,
"methodWithPathVariable", "1").build();
@@ -328,7 +328,7 @@ public void fromMethodCallWithPathVariableAndMultiValueRequestParams() {

@Test
public void fromMethodCallWithCustomBaseURIViaStaticCall() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
UriComponents uriComponents = fromMethodCall(builder, on(ControllerWithMethods.class).myMethod(null)).build();

assertThat(uriComponents.toString()).isEqualTo("https://example.org:9090/base/something/else");
@@ -337,7 +337,7 @@ public void fromMethodCallWithCustomBaseURIViaStaticCall() {

@Test
public void fromMethodCallWithCustomBaseURIViaInstance() {
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://example.org:9090/base");
UriComponentsBuilder builder = UriComponentsBuilder.forURIString("https://example.org:9090/base");
MvcUriComponentsBuilder mvcBuilder = relativeTo(builder);
UriComponents result = mvcBuilder.withMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();

@@ -350,23 +350,23 @@ public void fromMethodCallWithModelAndViewReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithModelAndView.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
assertThat(uriComponents.encode().toURI().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test
public void fromMethodCallWithObjectReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithObject.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
assertThat(uriComponents.encode().toURI().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test
public void fromMethodCallWithStringReturnType() {
assertThatIllegalStateException().isThrownBy(() -> {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithString.class).getBooking(21L)).buildAndExpand(42);
uriComponents.encode().toUri().toString();
uriComponents.encode().toURI().toString();
});
}

@@ -375,7 +375,7 @@ public void fromMethodNameWithStringReturnType() {
UriComponents uriComponents = fromMethodName(
BookingControllerWithString.class, "getBooking", 21L).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
assertThat(uriComponents.encode().toURI().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // gh-30210
@@ -384,15 +384,15 @@ public void fromMethodCallWithCharSequenceReturnType() {
on(BookingControllerWithCharSequence.class).getBooking(21L))
.buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
assertThat(uriComponents.encode().toURI().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test // gh-30210
public void fromMethodCallWithJdbc30115ReturnType() {
UriComponents uriComponents = fromMethodCall(
on(BookingControllerWithJdbcSavepoint.class).getBooking(21L)).buildAndExpand(42);

assertThat(uriComponents.encode().toUri().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
assertThat(uriComponents.encode().toURI().toString()).isEqualTo("http://localhost/hotels/42/bookings/21");
}

@Test
@@ -411,7 +411,7 @@ public void fromMappingNamePlain() {
public void fromMappingNameWithCustomBaseURI() {
initWebApplicationContext(WebConfig.class);

UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("https://example.org:9999/base");
UriComponentsBuilder baseUrl = UriComponentsBuilder.forURIString("https://example.org:9999/base");
MvcUriComponentsBuilder mvcBuilder = relativeTo(baseUrl);
String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
assertThat(url).isEqualTo("https://example.org:9999/base/people/123/addresses/DE");
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -361,7 +361,7 @@ public HttpMethod getMethod() {

@Override
public URI getURI() {
return UriComponentsBuilder.fromUriString("/").build().toUri();
return UriComponentsBuilder.forURIString("/").build().toURI();
}

@Override
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -60,7 +60,7 @@ public class MockClientHttpRequest extends AbstractClientHttpRequest {
private Function<Flux<DataBuffer>, Mono<Void>> writeHandler;

public MockClientHttpRequest(HttpMethod httpMethod, String urlTemplate, Object... vars) {
this(httpMethod, UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(vars).encode().toUri());
this(httpMethod, UriComponentsBuilder.forURIString(urlTemplate).buildAndExpand(vars).encode().toURI());
}

public MockClientHttpRequest(HttpMethod httpMethod, URI url) {
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -249,7 +249,7 @@ public static BodyBuilder method(String httpMethod, String uri, Object... vars)
}

private static URI toUri(String uri, Object[] vars) {
return UriComponentsBuilder.fromUriString(uri).buildAndExpand(vars).encode().toUri();
return UriComponentsBuilder.forURIString(uri).buildAndExpand(vars).encode().toURI();
}

/**
@@ -453,7 +453,7 @@ private static class DefaultBodyBuilder implements BodyBuilder {
@Nullable
private String contextPath;

private final UriComponentsBuilder queryParamsBuilder = UriComponentsBuilder.newInstance();
private final UriComponentsBuilder queryParamsBuilder = UriComponentsBuilder.create();

private final HttpHeaders headers = HttpHeaders.forWritable();

@@ -623,7 +623,7 @@ private URI getUrlToUse() {
MultiValueMap<String, String> params =
this.queryParamsBuilder.buildAndExpand().encode().getQueryParams();
if (!params.isEmpty()) {
return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri();
return UriComponentsBuilder.forURI(this.url).queryParams(params).build(true).toURI();
}
return this.url;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -103,7 +103,7 @@ public static RequestMatcher requestTo(String expectedUri) {
*/
public static RequestMatcher requestToUriTemplate(String expectedUri, Object... uriVars) {
Assert.notNull(expectedUri, "'uri' is required");
URI uri = UriComponentsBuilder.fromUriString(expectedUri).buildAndExpand(uriVars).encode().toUri();
URI uri = UriComponentsBuilder.forURIString(expectedUri).buildAndExpand(uriVars).encode().toURI();
return requestTo(uri);
}

@@ -146,7 +146,7 @@ public static RequestMatcher queryParam(String name, String... expectedValues) {
}

private static MultiValueMap<String, String> getQueryParams(ClientHttpRequest request) {
return UriComponentsBuilder.fromUri(request.getURI()).build().getQueryParams();
return UriComponentsBuilder.forURI(request.getURI()).build().getQueryParams();
}

private static void assertValueCount(
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -146,7 +146,7 @@ private static URI initUri(String url, Object[] vars) {
Assert.isTrue(url.isEmpty() || url.startsWith("/") || url.startsWith("http://") || url.startsWith("https://"),
() -> "'url' should start with a path or be a complete HTTP URL: " + url);
String uriString = (url.isEmpty() ? "/" : url);
return UriComponentsBuilder.fromUriString(uriString).buildAndExpand(vars).encode().toUri();
return UriComponentsBuilder.forURIString(uriString).buildAndExpand(vars).encode().toURI();
}

/**
@@ -706,13 +706,13 @@ public final HttpMockRequestImpl buildRequest(MockContext mockContext) {

String query = this.url.getRawQuery();
if (!this.queryParams.isEmpty()) {
String str = UriComponentsBuilder.newInstance().queryParams(this.queryParams).build().encode().getQuery();
String str = UriComponentsBuilder.create().queryParams(this.queryParams).build().encode().getQuery();
query = StringUtils.isNotEmpty(query) ? (query + "&" + str) : str;
}
if (query != null) {
request.setQueryString(query);
}
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
addRequestParams(request, UriComponentsBuilder.forURI(this.url).build().getQueryParams());

this.parameters.forEach((name, values) -> {
for (String value : values) {
Original file line number Diff line number Diff line change
@@ -98,10 +98,10 @@ public static ResultMatcher forwardedUrl(@Nullable String expectedUrl) {
*
* @param urlTemplate a URL template; the expanded URL will be encoded
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
* @see UriComponentsBuilder#forURIString(String)
*/
public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
String uri = UriComponentsBuilder.forURIString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return forwardedUrl(uri);
}

@@ -139,10 +139,10 @@ public static ResultMatcher redirectedUrl(String expectedUrl) {
*
* @param urlTemplate a URL template; the expanded URL will be encoded
* @param uriVars zero or more URI variables to populate the template
* @see UriComponentsBuilder#fromUriString(String)
* @see UriComponentsBuilder#forURIString(String)
*/
public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) {
String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
String uri = UriComponentsBuilder.forURIString(urlTemplate).buildAndExpand(uriVars).encode().toUriString();
return redirectedUrl(uri);
}

Original file line number Diff line number Diff line change
@@ -78,7 +78,7 @@ void httpMethodNotNullOrEmpty(ThrowingCallable callable) {
static Stream<Named<ThrowingCallable>> httpMethodNotNullOrEmpty() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
named("null HttpMethod, URI", () -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build()),
named("null HttpMethod, URI", () -> MockServerHttpRequest.method(null, UriComponentsBuilder.forURIString(uriTemplate).build("")).build()),
named("null HttpMethod, uriTemplate", () -> MockServerHttpRequest.method((HttpMethod) null, uriTemplate).build()),
named("null String, uriTemplate", () -> MockServerHttpRequest.method((String) null, uriTemplate).build()),
named("empty String, uriTemplate", () -> MockServerHttpRequest.method("", uriTemplate).build()),
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -219,7 +219,7 @@ void setCookies() throws IOException {
}

private static ClientHttpResponse createResponse(DefaultResponseCreator creator) throws IOException {
URI uri = UriComponentsBuilder.fromUriString("/foo/bar").build().toUri();
URI uri = UriComponentsBuilder.forURIString("/foo/bar").build().toURI();
return creator.createResponse(new MockClientHttpRequest(HttpMethod.POST, uri));
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -541,7 +541,7 @@ void mergeInvokesDefaultRequestPostProcessorFirst() {

void arbitraryMethod() {
String httpMethod = "REPort";
URI url = UriComponentsBuilder.fromPath("/foo/{bar}").buildAndExpand(42).toUri();
URI url = UriComponentsBuilder.forPath("/foo/{bar}").buildAndExpand(42).toURI();
this.builder = new MockHttpRequestBuilder(httpMethod, url);
HttpMockRequestImpl request = this.builder.buildRequest(this.mockContext);

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -75,7 +75,7 @@ public void setup() {
@Test
public void test() throws Exception {
String id = "a/b";
URI url = UriComponentsBuilder.fromUriString("/circuit").pathSegment(id).build().encode().toUri();
URI url = UriComponentsBuilder.forURIString("/circuit").pathSegment(id).build().encode().toURI();
ResultActions result = mockMvc.perform(get(url));
result.andExpect(status().isOk())
.andExpect(model().attribute("receivedId", is(id)));
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -165,7 +165,7 @@ protected boolean isRedirectModelForRequest(RedirectModel model, RequestContext

private MultiValueMap<String, String> getOriginatingRequestParams(RequestContext request) {
String query = request.getQueryString();
return UriComponentsBuilder.fromPath("/").query(query).build().getQueryParams();
return UriComponentsBuilder.forPath("/").query(query).build().getQueryParams();
}

@Override
4 changes: 2 additions & 2 deletions today-web/src/main/java/infra/web/RequestContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -946,7 +946,7 @@ public boolean isCorsRequest() {
corsRequestFlag = false;
}
else {
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
UriComponents originUrl = UriComponentsBuilder.forOriginHeader(origin).build();
String scheme = getScheme();
String host = getServerName();
int port = getServerPort();
2 changes: 1 addition & 1 deletion today-web/src/main/java/infra/web/RequestContextUtils.java
Original file line number Diff line number Diff line change
@@ -297,7 +297,7 @@ public static void saveRedirectModel(
manager = getRedirectModelManager(request);
}
if (manager != null) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString(location).build();
UriComponents uriComponents = UriComponentsBuilder.forURIString(location).build();
redirectModel.setTargetRequestPath(uriComponents.getPath());
redirectModel.addTargetRequestParams(uriComponents.getQueryParams());

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 - 2024 the original author or authors.
* Copyright 2017 - 2025 the original author or authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -27,7 +27,7 @@
* Resolvers argument values of type {@link UriComponentsBuilder}.
*
* <p>The returned instance is initialized via
* {@link UriComponentsBuilder#fromHttpRequest(HttpRequest)}
* {@link UriComponentsBuilder#forHttpRequest(HttpRequest)}
*
* @author Rossen Stoyanchev
* @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
@@ -43,7 +43,7 @@ public boolean supportsParameter(ResolvableMethodParameter resolvable) {
@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
return UriComponentsBuilder.fromHttpRequest(context);
return UriComponentsBuilder.forHttpRequest(context);
}

}
Loading

0 comments on commit 0385894

Please sign in to comment.