Polishing contribution
Closes gh-30869
This commit is contained in:
@@ -31,24 +31,26 @@ import org.springframework.web.service.invoker.HttpRequestValues;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
|
||||
/**
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} to use
|
||||
* {@link RestClient} for request execution.
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
|
||||
* to use {@link RestClient} for request execution.
|
||||
*
|
||||
* <p>
|
||||
* Use static factory methods in this class to create an {@link HttpServiceProxyFactory}
|
||||
* configured with a given {@link RestClient}.
|
||||
* <p>Use static factory methods in this class to create an
|
||||
* {@link HttpServiceProxyFactory} configured with the given {@link RestClient}.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 6.1
|
||||
*/
|
||||
public final class RestClientAdapter implements HttpExchangeAdapter {
|
||||
|
||||
private final RestClient restClient;
|
||||
|
||||
|
||||
private RestClientAdapter(RestClient restClient) {
|
||||
this.restClient = restClient;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supportsRequestAttributes() {
|
||||
return true;
|
||||
@@ -60,66 +62,66 @@ public final class RestClientAdapter implements HttpExchangeAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders exchangeForHeaders(HttpRequestValues requestValues) {
|
||||
return newRequest(requestValues).retrieve().toBodilessEntity().getHeaders();
|
||||
public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
|
||||
return newRequest(values).retrieve().toBodilessEntity().getHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T exchangeForBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
|
||||
return newRequest(requestValues).retrieve().body(bodyType);
|
||||
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
|
||||
return newRequest(values).retrieve().body(bodyType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues requestValues) {
|
||||
return newRequest(requestValues).retrieve().toBodilessEntity();
|
||||
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
|
||||
return newRequest(values).retrieve().toBodilessEntity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues requestValues,
|
||||
ParameterizedTypeReference<T> bodyType) {
|
||||
return newRequest(requestValues).retrieve().toEntity(bodyType);
|
||||
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
|
||||
return newRequest(values).retrieve().toEntity(bodyType);
|
||||
}
|
||||
|
||||
private RestClient.RequestBodySpec newRequest(HttpRequestValues requestValues) {
|
||||
private RestClient.RequestBodySpec newRequest(HttpRequestValues values) {
|
||||
|
||||
HttpMethod httpMethod = requestValues.getHttpMethod();
|
||||
HttpMethod httpMethod = values.getHttpMethod();
|
||||
Assert.notNull(httpMethod, "HttpMethod is required");
|
||||
|
||||
RestClient.RequestBodyUriSpec uriSpec = this.restClient.method(httpMethod);
|
||||
|
||||
RestClient.RequestBodySpec bodySpec;
|
||||
if (requestValues.getUri() != null) {
|
||||
bodySpec = uriSpec.uri(requestValues.getUri());
|
||||
if (values.getUri() != null) {
|
||||
bodySpec = uriSpec.uri(values.getUri());
|
||||
}
|
||||
else if (requestValues.getUriTemplate() != null) {
|
||||
bodySpec = uriSpec.uri(requestValues.getUriTemplate(), requestValues.getUriVariables());
|
||||
else if (values.getUriTemplate() != null) {
|
||||
bodySpec = uriSpec.uri(values.getUriTemplate(), values.getUriVariables());
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Neither full URL nor URI template");
|
||||
}
|
||||
|
||||
bodySpec.headers(headers -> headers.putAll(requestValues.getHeaders()));
|
||||
bodySpec.headers(headers -> headers.putAll(values.getHeaders()));
|
||||
|
||||
if (!requestValues.getCookies().isEmpty()) {
|
||||
if (!values.getCookies().isEmpty()) {
|
||||
List<String> cookies = new ArrayList<>();
|
||||
requestValues.getCookies().forEach((name, values) -> values.forEach(value -> {
|
||||
values.getCookies().forEach((name, cookieValues) -> cookieValues.forEach(value -> {
|
||||
HttpCookie cookie = new HttpCookie(name, value);
|
||||
cookies.add(cookie.toString());
|
||||
}));
|
||||
bodySpec.header(HttpHeaders.COOKIE, String.join("; ", cookies));
|
||||
}
|
||||
|
||||
bodySpec.attributes(attributes -> attributes.putAll(requestValues.getAttributes()));
|
||||
bodySpec.attributes(attributes -> attributes.putAll(values.getAttributes()));
|
||||
|
||||
if (requestValues.getBodyValue() != null) {
|
||||
bodySpec.body(requestValues.getBodyValue());
|
||||
if (values.getBodyValue() != null) {
|
||||
bodySpec.body(values.getBodyValue());
|
||||
}
|
||||
|
||||
return bodySpec;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link RestClientAdapter} with the given {@link RestClient}.
|
||||
* Create a {@link RestClientAdapter} for the given {@link RestClient}.
|
||||
*/
|
||||
public static RestClientAdapter create(RestClient restClient) {
|
||||
return new RestClientAdapter(restClient);
|
||||
|
||||
@@ -34,11 +34,11 @@ import org.springframework.web.service.invoker.HttpRequestValues;
|
||||
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
||||
|
||||
/**
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory} to use
|
||||
* {@link RestTemplate} for request execution.
|
||||
* {@link HttpExchangeAdapter} that enables an {@link HttpServiceProxyFactory}
|
||||
* to use {@link RestTemplate} for request execution.
|
||||
*
|
||||
* <p>Use static factory methods in this class to create an
|
||||
* {@link HttpServiceProxyFactory} configured with a given {@link RestTemplate}.
|
||||
* {@link HttpServiceProxyFactory} configured with the given {@link RestTemplate}.
|
||||
*
|
||||
* @author Olga Maciaszek-Sharma
|
||||
* @since 6.1
|
||||
@@ -59,63 +59,61 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exchange(HttpRequestValues requestValues) {
|
||||
this.restTemplate.exchange(newRequest(requestValues), Void.class);
|
||||
public void exchange(HttpRequestValues values) {
|
||||
this.restTemplate.exchange(newRequest(values), Void.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders exchangeForHeaders(HttpRequestValues requestValues) {
|
||||
return this.restTemplate.exchange(newRequest(requestValues), Void.class).getHeaders();
|
||||
public HttpHeaders exchangeForHeaders(HttpRequestValues values) {
|
||||
return this.restTemplate.exchange(newRequest(values), Void.class).getHeaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T exchangeForBody(HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
|
||||
return this.restTemplate.exchange(newRequest(requestValues), bodyType).getBody();
|
||||
public <T> T exchangeForBody(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
|
||||
return this.restTemplate.exchange(newRequest(values), bodyType).getBody();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues requestValues) {
|
||||
return this.restTemplate.exchange(newRequest(requestValues), Void.class);
|
||||
public ResponseEntity<Void> exchangeForBodilessEntity(HttpRequestValues values) {
|
||||
return this.restTemplate.exchange(newRequest(values), Void.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ResponseEntity<T> exchangeForEntity(
|
||||
HttpRequestValues requestValues, ParameterizedTypeReference<T> bodyType) {
|
||||
|
||||
return this.restTemplate.exchange(newRequest(requestValues), bodyType);
|
||||
public <T> ResponseEntity<T> exchangeForEntity(HttpRequestValues values, ParameterizedTypeReference<T> bodyType) {
|
||||
return this.restTemplate.exchange(newRequest(values), bodyType);
|
||||
}
|
||||
|
||||
private RequestEntity<?> newRequest(HttpRequestValues requestValues) {
|
||||
private RequestEntity<?> newRequest(HttpRequestValues values) {
|
||||
URI uri;
|
||||
if (requestValues.getUri() != null) {
|
||||
uri = requestValues.getUri();
|
||||
if (values.getUri() != null) {
|
||||
uri = values.getUri();
|
||||
}
|
||||
else if (requestValues.getUriTemplate() != null) {
|
||||
String uriTemplate = requestValues.getUriTemplate();
|
||||
Map<String, String> variables = requestValues.getUriVariables();
|
||||
else if (values.getUriTemplate() != null) {
|
||||
String uriTemplate = values.getUriTemplate();
|
||||
Map<String, String> variables = values.getUriVariables();
|
||||
uri = this.restTemplate.getUriTemplateHandler().expand(uriTemplate, variables);
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Neither full URL nor URI template");
|
||||
}
|
||||
|
||||
HttpMethod httpMethod = requestValues.getHttpMethod();
|
||||
HttpMethod httpMethod = values.getHttpMethod();
|
||||
Assert.notNull(httpMethod, "HttpMethod is required");
|
||||
|
||||
RequestEntity.BodyBuilder builder = RequestEntity.method(httpMethod, uri);
|
||||
builder.headers(requestValues.getHeaders());
|
||||
builder.headers(values.getHeaders());
|
||||
|
||||
if (!requestValues.getCookies().isEmpty()) {
|
||||
if (!values.getCookies().isEmpty()) {
|
||||
List<String> cookies = new ArrayList<>();
|
||||
requestValues.getCookies().forEach((name, values) -> values.forEach(value -> {
|
||||
values.getCookies().forEach((name, cookieValues) -> cookieValues.forEach(value -> {
|
||||
HttpCookie cookie = new HttpCookie(name, value);
|
||||
cookies.add(cookie.toString());
|
||||
}));
|
||||
builder.header(HttpHeaders.COOKIE, String.join("; ", cookies));
|
||||
}
|
||||
|
||||
if (requestValues.getBodyValue() != null) {
|
||||
return builder.body(requestValues.getBodyValue());
|
||||
if (values.getBodyValue() != null) {
|
||||
return builder.body(values.getBodyValue());
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
@@ -123,7 +121,7 @@ public final class RestTemplateAdapter implements HttpExchangeAdapter {
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@link RestTemplateAdapter} with the given {@link RestTemplate}.
|
||||
* Create a {@link RestTemplateAdapter} for the given {@link RestTemplate}.
|
||||
*/
|
||||
public static RestTemplateAdapter create(RestTemplate restTemplate) {
|
||||
return new RestTemplateAdapter(restTemplate);
|
||||
|
||||
@@ -53,9 +53,9 @@ import org.springframework.web.service.annotation.HttpExchange;
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 6.0
|
||||
* @see org.springframework.web.client.support.RestTemplateAdapter
|
||||
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
|
||||
* @see org.springframework.web.client.support.RestClientAdapter
|
||||
* @see org.springframework.web.reactive.function.client.support.WebClientAdapter
|
||||
* @see org.springframework.web.client.support.RestTemplateAdapter
|
||||
*/
|
||||
public final class HttpServiceProxyFactory {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user