Polishing

Closes gh-31413
This commit is contained in:
rstoyanchev
2023-10-12 17:45:37 +01:00
parent 0cd196e3dd
commit b5b9386be6
10 changed files with 118 additions and 131 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.web.client.RestClient;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
import org.springframework.web.service.invoker.HttpRequestValues;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.util.UriBuilderFactory;
/**
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
@@ -93,19 +94,16 @@ public final class RestClientAdapter implements HttpExchangeAdapter {
if (values.getUri() != null) {
bodySpec = uriSpec.uri(values.getUri());
}
else if (values.getUriTemplate() != null) {
if (values.getUriBuilderFactory() != null) {
URI expanded = values.getUriBuilderFactory()
.expand(values.getUriTemplate(), values.getUriVariables());
bodySpec = uriSpec.uri(expanded);
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
if (uriBuilderFactory != null) {
URI uri = uriBuilderFactory.expand(values.getUriTemplate(), values.getUriVariables());
bodySpec = uriSpec.uri(uri);
}
else {
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
}
}
else {
throw new IllegalStateException("Neither full URL nor URI template");
}

View File

@@ -31,6 +31,7 @@ import org.springframework.web.client.RestTemplate;
import org.springframework.web.service.invoker.HttpExchangeAdapter;
import org.springframework.web.service.invoker.HttpRequestValues;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.util.UriBuilderFactory;
/**
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
@@ -92,19 +93,16 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
if (values.getUri() != null) {
builder = RequestEntity.method(httpMethod, values.getUri());
}
else if (values.getUriTemplate() != null) {
if (values.getUriBuilderFactory() != null) {
URI expanded = values.getUriBuilderFactory()
.expand(values.getUriTemplate(), values.getUriVariables());
UriBuilderFactory uriBuilderFactory = values.getUriBuilderFactory();
if (uriBuilderFactory != null) {
URI expanded = uriBuilderFactory.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

@@ -63,10 +63,10 @@ public class HttpRequestValues {
private final URI uri;
@Nullable
private final String uriTemplate;
private final UriBuilderFactory uriBuilderFactory;
@Nullable
private final UriBuilderFactory uriBuilderFactory;
private final String uriTemplate;
private final Map<String, String> uriVariables;
@@ -81,11 +81,10 @@ public class HttpRequestValues {
/**
* 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.
* Constructor without UriBuilderFactory.
* @deprecated in favour of
* {@link HttpRequestValues#HttpRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
* to be removed in 6.2.
*/
@Deprecated(since = "6.1", forRemoval = true)
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
@@ -94,13 +93,16 @@ public class HttpRequestValues {
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
this(httpMethod, uri, uriTemplate, null, uriVariables,
headers, cookies, attributes, bodyValue);
this(httpMethod, uri, null, uriTemplate, uriVariables, headers, cookies, attributes, bodyValue);
}
/**
* Construct {@link HttpRequestValues}.
* @since 6.1
*/
protected HttpRequestValues(@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
@Nullable String uriTemplate, Map<String, String> uriVariables,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
@@ -108,8 +110,8 @@ public class HttpRequestValues {
this.httpMethod = httpMethod;
this.uri = uri;
this.uriTemplate = uriTemplate;
this.uriBuilderFactory = uriBuilderFactory;
this.uriTemplate = uriTemplate;
this.uriVariables = uriVariables;
this.headers = headers;
this.cookies = cookies;
@@ -137,6 +139,19 @@ public class HttpRequestValues {
return this.uri;
}
/**
* Return the {@link UriBuilderFactory} to expand
* the {@link HttpRequestValues#uriTemplate} and {@link #getUriVariables()} with.
* <p>The {@link UriBuilderFactory} is passed into the HTTP interface method
* in order to override the UriBuilderFactory (and its baseUrl) used by the
* underlying client.
* @since 6.1
*/
@Nullable
public UriBuilderFactory getUriBuilderFactory() {
return this.uriBuilderFactory;
}
/**
* Return the URL template for the request. This comes from the values in
* class and method {@code HttpExchange} annotations.
@@ -146,19 +161,6 @@ 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.
*/
@@ -237,10 +239,10 @@ public class HttpRequestValues {
private URI uri;
@Nullable
private String uriTemplate;
private UriBuilderFactory uriBuilderFactory;
@Nullable
private UriBuilderFactory uriBuilderFactory;
private String uriTemplate;
@Nullable
private Map<String, String> uriVars;
@@ -282,19 +284,20 @@ public class HttpRequestValues {
}
/**
* Set the request URL as a String template.
* Set the {@link UriBuilderFactory} that will be used to expand the
* {@link #getUriTemplate()}.
* @since 6.1
*/
public Builder setUriTemplate(String uriTemplate) {
this.uriTemplate = uriTemplate;
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
this.uriBuilderFactory = uriBuilderFactory;
return this;
}
/**
* Set the {@link UriBuilderFactory} that
* will be used to expand the URI.
* Set the request URL as a String template.
*/
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
this.uriBuilderFactory = uriBuilderFactory;
public Builder setUriTemplate(String uriTemplate) {
this.uriTemplate = uriTemplate;
return this;
}
@@ -427,8 +430,8 @@ public class HttpRequestValues {
public HttpRequestValues build() {
URI uri = this.uri;
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
UriBuilderFactory uriBuilderFactory = this.uriBuilderFactory;
String uriTemplate = (this.uriTemplate != null ? this.uriTemplate : "");
Map<String, String> uriVars = (this.uriVars != null ? new HashMap<>(this.uriVars) : Collections.emptyMap());
Object bodyValue = this.bodyValue;
@@ -470,8 +473,8 @@ public class HttpRequestValues {
new HashMap<>(this.attributes) : Collections.emptyMap());
return createRequestValues(
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVars, headers, cookies, attributes, bodyValue);
this.httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
headers, cookies, attributes, bodyValue);
}
protected boolean hasParts() {
@@ -512,9 +515,9 @@ public class HttpRequestValues {
/**
* 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 in favour of
* {@link Builder#createRequestValues(HttpMethod, URI, UriBuilderFactory, String, Map, HttpHeaders, MultiValueMap, Map, Object)}
* to be removed in 6.2.
*/
@Deprecated(since = "6.1", forRemoval = true)
protected HttpRequestValues createRequestValues(
@@ -524,22 +527,23 @@ public class HttpRequestValues {
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return createRequestValues(httpMethod, uri, uriTemplate, null,
return createRequestValues(httpMethod, uri, null, uriTemplate,
uriVars, headers, cookies, attributes, bodyValue);
}
/**
* Create {@link HttpRequestValues} from values passed to the {@link Builder}.
* @since 6.1
*/
protected HttpRequestValues createRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory, @Nullable String uriTemplate,
Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return new HttpRequestValues(
this.httpMethod, uri, uriTemplate, uriBuilderFactory,
this.httpMethod, uri, uriBuilderFactory, uriTemplate,
uriVars, headers, cookies, attributes, bodyValue);
}
}

View File

@@ -51,14 +51,12 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
private ReactiveHttpRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVariables,
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
@Nullable String uriTemplate, Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue, @Nullable Publisher<?> body, @Nullable ParameterizedTypeReference<?> elementType) {
super(httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVariables, headers, cookies, attributes, bodyValue);
super(httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars, headers, cookies, attributes, bodyValue);
this.body = body;
this.bodyElementType = elementType;
}
@@ -135,14 +133,14 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
}
@Override
public Builder setUriTemplate(String uriTemplate) {
super.setUriTemplate(uriTemplate);
public Builder setUriBuilderFactory(@Nullable UriBuilderFactory uriBuilderFactory) {
super.setUriBuilderFactory(uriBuilderFactory);
return this;
}
@Override
public Builder setUriBuilderFactory(UriBuilderFactory uriBuilderFactory) {
super.setUriBuilderFactory(uriBuilderFactory);
public Builder setUriTemplate(String uriTemplate) {
super.setUriTemplate(uriTemplate);
return this;
}
@@ -271,17 +269,15 @@ public final class ReactiveHttpRequestValues extends HttpRequestValues {
@Override
protected ReactiveHttpRequestValues createRequestValues(
@Nullable HttpMethod httpMethod,
@Nullable URI uri, @Nullable String uriTemplate,
@Nullable UriBuilderFactory uriBuilderFactory, Map<String, String> uriVars,
@Nullable URI uri, @Nullable UriBuilderFactory uriBuilderFactory,
@Nullable String uriTemplate, Map<String, String> uriVars,
HttpHeaders headers, MultiValueMap<String, String> cookies, Map<String, Object> attributes,
@Nullable Object bodyValue) {
return new ReactiveHttpRequestValues(
httpMethod, uri, uriTemplate, uriBuilderFactory,
uriVars, headers, cookies, attributes,
bodyValue, this.body, this.bodyElementType);
httpMethod, uri, uriBuilderFactory, uriTemplate, uriVars,
headers, cookies, attributes, bodyValue, this.body, this.bodyElementType);
}
}
}