Polish HttpRequestBuilder API
This commit makes messageEncoders a required argument for building a client request - those are needed to actually encode the body object as a reactive stream to be written to the HTTP request body. Removed raw types usage in DefaultHttpRequestBuilder. DefaultHttpRequestBuilder now uses a UriTemplateHandler to expand URI templates + variables into a concrete URI. Fixes #80, fixes #85, fixes #86
This commit is contained in:
parent
8ed2925ce3
commit
ddf996cfeb
|
|
@ -18,7 +18,6 @@ package org.springframework.web.client.reactive;
|
||||||
|
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -40,6 +39,8 @@ import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
|
import org.springframework.web.util.DefaultUriTemplateHandler;
|
||||||
|
import org.springframework.web.util.UriTemplateHandler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a {@link ClientHttpRequest}
|
* Builds a {@link ClientHttpRequest}
|
||||||
|
|
@ -51,6 +52,8 @@ import org.springframework.web.client.RestClientException;
|
||||||
*/
|
*/
|
||||||
public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
|
|
||||||
|
private final UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
|
||||||
|
|
||||||
protected HttpMethod httpMethod;
|
protected HttpMethod httpMethod;
|
||||||
|
|
||||||
protected HttpHeaders httpHeaders;
|
protected HttpHeaders httpHeaders;
|
||||||
|
|
@ -59,8 +62,6 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
|
|
||||||
protected Publisher contentPublisher;
|
protected Publisher contentPublisher;
|
||||||
|
|
||||||
protected List<Encoder<?>> messageEncoders;
|
|
||||||
|
|
||||||
protected final List<HttpCookie> cookies = new ArrayList<HttpCookie>();
|
protected final List<HttpCookie> cookies = new ArrayList<HttpCookie>();
|
||||||
|
|
||||||
protected DefaultHttpRequestBuilder() {
|
protected DefaultHttpRequestBuilder() {
|
||||||
|
|
@ -69,7 +70,7 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
public DefaultHttpRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) throws RestClientException {
|
public DefaultHttpRequestBuilder(HttpMethod httpMethod, String urlTemplate, Object... urlVariables) throws RestClientException {
|
||||||
this.httpMethod = httpMethod;
|
this.httpMethod = httpMethod;
|
||||||
this.httpHeaders = new HttpHeaders();
|
this.httpHeaders = new HttpHeaders();
|
||||||
this.url = parseURI(urlTemplate);
|
this.url = this.uriTemplateHandler.expand(urlTemplate, urlVariables);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultHttpRequestBuilder(HttpMethod httpMethod, URI url) {
|
public DefaultHttpRequestBuilder(HttpMethod httpMethod, URI url) {
|
||||||
|
|
@ -78,24 +79,6 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
this.url = url;
|
this.url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected DefaultHttpRequestBuilder setMessageEncoders(List<Encoder<?>> messageEncoders) {
|
|
||||||
this.messageEncoders = messageEncoders;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private URI parseURI(String uri) throws RestClientException {
|
|
||||||
try {
|
|
||||||
return new URI(uri);
|
|
||||||
}
|
|
||||||
catch (URISyntaxException e) {
|
|
||||||
throw new RestClientException("could not parse URL template", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultHttpRequestBuilder param(String name, String... values) {
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultHttpRequestBuilder header(String name, String... values) {
|
public DefaultHttpRequestBuilder header(String name, String... values) {
|
||||||
Arrays.stream(values).forEach(value -> this.httpHeaders.add(name, value));
|
Arrays.stream(values).forEach(value -> this.httpHeaders.add(name, value));
|
||||||
return this;
|
return this;
|
||||||
|
|
@ -133,7 +116,7 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DefaultHttpRequestBuilder contentStream(Publisher content) {
|
public DefaultHttpRequestBuilder contentStream(Publisher<?> content) {
|
||||||
this.contentPublisher = Flux.from(content);
|
this.contentPublisher = Flux.from(content);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
@ -153,7 +136,7 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClientHttpRequest build(ClientHttpRequestFactory factory) {
|
public ClientHttpRequest build(ClientHttpRequestFactory factory, List<Encoder<?>> messageEncoders) {
|
||||||
ClientHttpRequest request = factory.createRequest(this.httpMethod, this.url, this.httpHeaders);
|
ClientHttpRequest request = factory.createRequest(this.httpMethod, this.url, this.httpHeaders);
|
||||||
request.getHeaders().putAll(this.httpHeaders);
|
request.getHeaders().putAll(this.httpHeaders);
|
||||||
|
|
||||||
|
|
@ -161,7 +144,10 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
ResolvableType requestBodyType = ResolvableType.forInstance(this.contentPublisher);
|
ResolvableType requestBodyType = ResolvableType.forInstance(this.contentPublisher);
|
||||||
MediaType mediaType = request.getHeaders().getContentType();
|
MediaType mediaType = request.getHeaders().getContentType();
|
||||||
|
|
||||||
Optional<Encoder<?>> messageEncoder = resolveEncoder(requestBodyType, mediaType);
|
Optional<Encoder<?>> messageEncoder = messageEncoders
|
||||||
|
.stream()
|
||||||
|
.filter(e -> e.canEncode(requestBodyType, mediaType))
|
||||||
|
.findFirst();
|
||||||
|
|
||||||
if (messageEncoder.isPresent()) {
|
if (messageEncoder.isPresent()) {
|
||||||
DataBufferAllocator allocator = request.allocator();
|
DataBufferAllocator allocator = request.allocator();
|
||||||
|
|
@ -175,13 +161,7 @@ public class DefaultHttpRequestBuilder implements HttpRequestBuilder {
|
||||||
"' for content-type '" + mediaType.toString() + "'");
|
"' for content-type '" + mediaType.toString() + "'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Optional<Encoder<?>> resolveEncoder(ResolvableType type, MediaType mediaType) {
|
|
||||||
return this.messageEncoders.stream()
|
|
||||||
.filter(e -> e.canEncode(type, mediaType)).findFirst();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -16,6 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.web.client.reactive;
|
package org.springframework.web.client.reactive;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.core.codec.Encoder;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
||||||
|
|
||||||
|
|
@ -29,6 +32,9 @@ public interface HttpRequestBuilder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build a {@link ClientHttpRequest}
|
* Build a {@link ClientHttpRequest}
|
||||||
|
*
|
||||||
|
* @param factory the factory that creates the actual {@link ClientHttpRequest}
|
||||||
|
* @param messageEncoders the {@link Encoder}s to use for encoding the request body
|
||||||
*/
|
*/
|
||||||
ClientHttpRequest build(ClientHttpRequestFactory factory);
|
ClientHttpRequest build(ClientHttpRequestFactory factory, List<Encoder<?>> messageEncoders);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,6 @@ import org.springframework.core.codec.support.JacksonJsonEncoder;
|
||||||
import org.springframework.core.codec.support.JsonObjectDecoder;
|
import org.springframework.core.codec.support.JsonObjectDecoder;
|
||||||
import org.springframework.core.codec.support.StringDecoder;
|
import org.springframework.core.codec.support.StringDecoder;
|
||||||
import org.springframework.core.codec.support.StringEncoder;
|
import org.springframework.core.codec.support.StringEncoder;
|
||||||
import org.springframework.core.io.buffer.DataBufferAllocator;
|
|
||||||
import org.springframework.core.io.buffer.DefaultDataBufferAllocator;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequest;
|
import org.springframework.http.client.reactive.ClientHttpRequest;
|
||||||
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
import org.springframework.http.client.reactive.ClientHttpRequestFactory;
|
||||||
|
|
@ -85,7 +83,6 @@ public final class WebClient {
|
||||||
*/
|
*/
|
||||||
public WebClient(ClientHttpRequestFactory requestFactory) {
|
public WebClient(ClientHttpRequestFactory requestFactory) {
|
||||||
this.requestFactory = requestFactory;
|
this.requestFactory = requestFactory;
|
||||||
DataBufferAllocator allocator = new DefaultDataBufferAllocator();
|
|
||||||
this.messageEncoders = Arrays.asList(new ByteBufferEncoder(), new StringEncoder(),
|
this.messageEncoders = Arrays.asList(new ByteBufferEncoder(), new StringEncoder(),
|
||||||
new JacksonJsonEncoder());
|
new JacksonJsonEncoder());
|
||||||
this.messageDecoders = Arrays.asList(new ByteBufferDecoder(), new StringDecoder(),
|
this.messageDecoders = Arrays.asList(new ByteBufferDecoder(), new StringDecoder(),
|
||||||
|
|
@ -116,9 +113,9 @@ public final class WebClient {
|
||||||
* <li>returning the response with a publisher of the body</li>
|
* <li>returning the response with a publisher of the body</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*/
|
*/
|
||||||
public WebResponseActions perform(DefaultHttpRequestBuilder builder) {
|
public WebResponseActions perform(HttpRequestBuilder builder) {
|
||||||
|
|
||||||
ClientHttpRequest request = builder.setMessageEncoders(messageEncoders).build(requestFactory);
|
ClientHttpRequest request = builder.build(this.requestFactory, this.messageEncoders);
|
||||||
final Mono<ClientHttpResponse> clientResponse = request.execute()
|
final Mono<ClientHttpResponse> clientResponse = request.execute()
|
||||||
.log("org.springframework.http.client.reactive");
|
.log("org.springframework.http.client.reactive");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue