Add UriBuilderFactoryArgumentResolver

See gh-31413
This commit is contained in:
Olga MaciaszekSharma
2023-10-10 16:53:11 +02:00
committed by rstoyanchev
parent 364186e699
commit 0cd196e3dd
15 changed files with 589 additions and 24 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.web.client.support;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@@ -92,9 +93,19 @@ public final class RestClientAdapter implements HttpExchangeAdapter {
if (values.getUri() != null) {
bodySpec = uriSpec.uri(values.getUri());
}
else if (values.getUriTemplate() != null) {
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
if (values.getUriBuilderFactory() != null) {
URI expanded = values.getUriBuilderFactory()
.expand(values.getUriTemplate(), values.getUriVariables());
bodySpec = uriSpec.uri(expanded);
}
else {
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
}
}
else {
throw new IllegalStateException("Neither full URL nor URI template");
}

View File

@@ -16,6 +16,7 @@
package org.springframework.web.client.support;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
@@ -91,9 +92,19 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
if (values.getUri() != null) {
builder = RequestEntity.method(httpMethod, values.getUri());
}
else if (values.getUriTemplate() != null) {
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
if (values.getUriBuilderFactory() != null) {
URI expanded = values.getUriBuilderFactory()
.expand(values.getUriTemplate(), values.getUriVariables());
builder = RequestEntity.method(httpMethod, expanded);
}
else {
builder = RequestEntity.method(httpMethod, values.getUriTemplate(), values.getUriVariables());
}
}
else {
throw new IllegalStateException("Neither full URL nor URI template");
}

View File

@@ -26,6 +26,7 @@ import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.util.UriBuilderFactory;
/**
* Annotation to declare a method on an HTTP service interface as an HTTP
@@ -61,6 +62,13 @@ import org.springframework.web.bind.annotation.Mapping;
* <td>{@link org.springframework.web.service.invoker.UrlArgumentResolver}</td>
* </tr>
* <tr>
* <td>{@link UriBuilderFactory}</td>
* <td>Dynamically set the {@code base URI} for the request, overriding the
* one from the annotation's {@link #url()} attribute, while keeping the
* subsequent path segments as defined there</td>
* <td>{@link org.springframework.web.service.invoker.UriBuilderFactoryArgumentResolver}</td>
* </tr>
* <tr>
* <td>{@link org.springframework.http.HttpMethod HttpMethod}</td>
* <td>Dynamically set the HTTP method for the request, overriding the annotation's
* {@link #method()} attribute</td>

View File

@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
@@ -46,6 +47,7 @@ import org.springframework.web.util.UriUtils;
* {@link HttpExchangeAdapter} to adapt to the underlying HTTP client.
*
* @author Rossen Stoyanchev
* @author Olga Maciaszek-Sharma
* @since 6.0
*/
public class HttpRequestValues {
@@ -63,6 +65,9 @@ public class HttpRequestValues {
@Nullable
private final String uriTemplate;
@Nullable
private final UriBuilderFactory uriBuilderFactory;
private final Map<String, String> uriVariables;
private final HttpHeaders headers;
@@ -75,8 +80,27 @@ public class HttpRequestValues {
private final Object bodyValue;
/**
* Construct {@link HttpRequestValues}.
*
* @deprecated in favour of {@link HttpRequestValues#HttpRequestValues(
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
* MultiValueMap, Map, Object)} to be removed in 6.2.
*/
@Deprecated(since = "6.1", forRemoval = true)
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
@Nullable URI uri, @Nullable String uriTemplate,
Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
this(httpMethod, uri, uriTemplate, null, uriVariables,
headers, cookies, attributes, bodyValue);
}
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
@@ -85,6 +109,7 @@ public class HttpRequestValues {
this.httpMethod = httpMethod;
this.uri = uri;
this.uriTemplate = uriTemplate;
this.uriBuilderFactory = uriBuilderFactory;
this.uriVariables = uriVariables;
this.headers = headers;
this.cookies = cookies;
@@ -106,7 +131,6 @@ public class HttpRequestValues {
* <p>Typically, this comes from a {@link URI} method argument, which provides
* the caller with the option to override the {@link #getUriTemplate()
* uriTemplate} from class and method {@code HttpExchange} annotations.
* annotation.
*/
@Nullable
public URI getUri() {
@@ -122,6 +146,19 @@ public class HttpRequestValues {
return this.uriTemplate;
}
/**
* Return the {@link UriBuilderFactory} to expand
* the {@link HttpRequestValues#uriTemplate} with.
* <p>This comes from a {@link UriBuilderFactory} method argument.
* It allows you to override the {@code baseUri}, while keeping the
* path as defined in class and method
* {@code HttpExchange} annotations.
*/
@Nullable
public UriBuilderFactory getUriBuilderFactory() {
return this.uriBuilderFactory;
}
/**
* Return the URL template variables, or an empty map.
*/
@@ -202,6 +239,9 @@ public class HttpRequestValues {
@Nullable
private String uriTemplate;
@Nullable
private UriBuilderFactory uriBuilderFactory;
@Nullable
private Map<String, String> uriVars;
@@ -249,6 +289,15 @@ public class HttpRequestValues {
return this;
}
/**
* Set the {@link UriBuilderFactory} that
* will be used to expand the URI.
*/
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
this.uriBuilderFactory = uriBuilderFactory;
return this;
}
/**
* Add a URI variable name-value pair.
*/
@@ -379,6 +428,7 @@ public class HttpRequestValues {
URI uri = this.uri;
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory;
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
Object bodyValue = this.bodyValue;
@@ -420,7 +470,8 @@ public class HttpRequestValues {
new HashMap<>(this.attributes) : Collections.emptyMap());
return createRequestValues(
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVars, headers, cookies, attributes, bodyValue);
}
protected boolean hasParts() {
@@ -459,14 +510,37 @@ public class HttpRequestValues {
return uriComponentsBuilder.build().toUriString();
}
/**
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
* @deprecated in favour of {@link Builder#createRequestValues(
* HttpMethod, URI, String, UriBuilderFactory, Map, HttpHeaders,
* MultiValueMap, Map, Object)} to be removed in 6.2.
*/
@Deprecated(since = "6.1", forRemoval = true)
protected HttpRequestValues createRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVars,
@Nullable URI uri, @Nullable String uriTemplate,
Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return createRequestValues(httpMethod, uri, uriTemplate, null,
uriVars, headers, cookies, attributes, bodyValue);
}
/**
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
*/
protected HttpRequestValues createRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return new HttpRequestValues(
this.httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVars, headers, cookies, attributes, bodyValue);
}
}

View File

@@ -273,6 +273,7 @@ public final class HttpServiceProxyFactory {
// Specific type
resolvers.add(new UrlArgumentResolver());
resolvers.add(new UriBuilderFactoryArgumentResolver());
resolvers.add(new HttpMethodArgumentResolver());
return resolvers;

View File

@@ -31,11 +31,13 @@ import org.springframework.http.client.MultipartBodyBuilder;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriBuilderFactory;
/**
* {@link HttpRequestValues} extension for use with {@link ReactorHttpExchangeAdapter}.
*
* @author Rossen Stoyanchev
* @author Olga Maciaszek-Sharma
* @since 6.1
*/
public final class ReactiveHttpRequestValues extends HttpRequestValues {
@@ -49,11 +51,13 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
private ReactiveHttpRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVariables,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
super(httpMethod, uri, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue);
super(httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVariables, headers, cookies, attributes, bodyValue);
this.body = body;
this.bodyElementType = elementType;
@@ -136,6 +140,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
return this;
}
@Override
public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) {
super.setUriBuilderFactory(uriBuilderFactory);
return this;
}
@Override
public Builder setUriVariable(String name, String value) {
super.setUriVariable(name, value);
@@ -261,12 +271,14 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
@Override
protected ReactiveHttpRequestValues createRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate, Map<String, String> uriVars,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return new ReactiveHttpRequestValues(
httpMethod, uri, uriTemplate, uriVars, headers, cookies, attributes,
httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVars, headers, cookies, attributes,
bodyValue, this.body, this.bodyElementType);
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.service.invoker;
import java.net.URL;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.util.UriBuilderFactory;
import org.springframework.web.util.UriTemplate;
/**
* An {@link HttpServiceArgumentResolver} that uses the provided
* {@link UriBuilderFactory} to expand the {@link UriTemplate}.
* <p>Unlike with the {@link UrlArgumentResolver},
* if the {@link UriBuilderFactoryArgumentResolver} is provided,
* it will not override the entire {@link URL}, but just the {@code baseUri}.
* <p>This allows for dynamically setting the {@code baseUri},
* while keeping the {@code path} specified through class
* and method annotations.
*
* @author Olga Maciaszek-Sharma
* @since 6.1
*/
public class UriBuilderFactoryArgumentResolver implements HttpServiceArgumentResolver {
@Override
public boolean resolve(
@Nullable Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
if (!parameter.getParameterType().equals(UriBuilderFactory.class)) {
return false;
}
if (argument != null) {
requestValues.setUriBuilderFactory((UriBuilderFactory) argument);
}
return true;
}
}