Issue: SPR-16387
This commit is contained in:
Rossen Stoyanchev
2018-05-31 15:35:53 -04:00
parent ffbc75ae47
commit a3216432b5
15 changed files with 125 additions and 104 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -35,16 +35,15 @@ public interface ClientHttpConnector {
/**
* Connect to the origin server using the given {@code HttpMethod} and
* {@code URI}, then apply the given {@code requestCallback} on the
* {@link ClientHttpRequest} once the connection has been established.
* <p>Return a publisher of the {@link ClientHttpResponse}.
* {@code URI} and apply the given {@code requestCallback} when the HTTP
* request of the underlying API can be initialized and written to.
* @param method the HTTP request method
* @param uri the HTTP request URI
* @param requestCallback a function that prepares and writes the request,
* returning a publisher that signals when it's done interacting with the
* request. Implementations should return a {@code Mono<Void>} by calling
* @param requestCallback a function that prepares and writes to the request,
* returning a publisher that signals when it's done writing.
* Implementations can return a {@code Mono<Void>} by calling
* {@link ClientHttpRequest#writeWith} or {@link ClientHttpRequest#setComplete}.
* @return a publisher of the {@link ClientHttpResponse}
* @return publisher for the {@link ClientHttpResponse}
*/
Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,
Function<? super ClientHttpRequest, Mono<Void>> requestCallback);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -44,16 +44,17 @@ public class ReactorClientHttpConnector implements ClientHttpConnector {
/**
* Create a Reactor Netty {@link ClientHttpConnector}
* with a default configuration and HTTP compression support enabled.
* with default configuration and HTTP compression support enabled.
*/
public ReactorClientHttpConnector() {
this.httpClient = HttpClient.create()
.compress();
this.httpClient = HttpClient.create().compress();
}
/**
* Create a Reactor Netty {@link ClientHttpConnector} with the given
* {@link HttpClient}
* Create a Reactor Netty {@link ClientHttpConnector} with a fully
* configured {@code HttpClient}.
* @param httpClient the client instance to use
* @since 5.1
*/
public ReactorClientHttpConnector(HttpClient httpClient) {
this.httpClient = httpClient;
@@ -69,24 +70,23 @@ public class ReactorClientHttpConnector implements ClientHttpConnector {
}
return this.httpClient
.request(adaptHttpMethod(method))
.request(io.netty.handler.codec.http.HttpMethod.valueOf(method.name()))
.uri(uri.toString())
.send((req, out) -> requestCallback.apply(adaptRequest(method, uri, req, out)))
.send((request, outbound) -> requestCallback.apply(adaptRequest(method, uri, request, outbound)))
.responseConnection((res, con) -> Mono.just(adaptResponse(res, con.inbound(), con.outbound().alloc())))
.next();
}
private io.netty.handler.codec.http.HttpMethod adaptHttpMethod(HttpMethod method) {
return io.netty.handler.codec.http.HttpMethod.valueOf(method.name());
}
private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request,
NettyOutbound nettyOutbound) {
private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound out) {
return new ReactorClientHttpRequest(method, uri, request, out);
return new ReactorClientHttpRequest(method, uri, request, nettyOutbound);
}
private ClientHttpResponse adaptResponse(HttpClientResponse response, NettyInbound nettyInbound,
ByteBufAllocator alloc) {
return new ReactorClientHttpResponse(response, nettyInbound, alloc);
ByteBufAllocator allocator) {
return new ReactorClientHttpResponse(response, nettyInbound, allocator);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -47,20 +47,19 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
private final URI uri;
private final HttpClientRequest httpRequest;
private final HttpClientRequest request;
private final NettyOutbound out;
private final NettyOutbound outbound;
private final NettyDataBufferFactory bufferFactory;
public ReactorClientHttpRequest(HttpMethod httpMethod, URI uri,
HttpClientRequest httpRequest, NettyOutbound out) {
this.httpMethod = httpMethod;
public ReactorClientHttpRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound outbound) {
this.httpMethod = method;
this.uri = uri;
this.httpRequest = httpRequest;
this.out = out;
this.bufferFactory = new NettyDataBufferFactory(out.alloc());
this.request = request;
this.outbound = outbound;
this.bufferFactory = new NettyDataBufferFactory(outbound.alloc());
}
@@ -81,14 +80,16 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
@Override
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
return doCommit(() -> this.out
.send(Flux.from(body).map(NettyDataBufferFactory::toByteBuf)).then());
return doCommit(() -> {
Flux<ByteBuf> byteBufFlux = Flux.from(body).map(NettyDataBufferFactory::toByteBuf);
return this.outbound.send(byteBufFlux).then();
});
}
@Override
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
Publisher<Publisher<ByteBuf>> byteBufs = Flux.from(body).map(ReactorClientHttpRequest::toByteBufs);
return doCommit(() -> this.out.sendGroups(byteBufs).then());
return doCommit(() -> this.outbound.sendGroups(byteBufs).then());
}
private static Publisher<ByteBuf> toByteBufs(Publisher<? extends DataBuffer> dataBuffers) {
@@ -97,24 +98,24 @@ class ReactorClientHttpRequest extends AbstractClientHttpRequest implements Zero
@Override
public Mono<Void> writeWith(File file, long position, long count) {
return doCommit(() -> this.out.sendFile(file.toPath(), position, count).then());
return doCommit(() -> this.outbound.sendFile(file.toPath(), position, count).then());
}
@Override
public Mono<Void> setComplete() {
return doCommit(() -> out.then());
return doCommit(this.outbound::then);
}
@Override
protected void applyHeaders() {
getHeaders().entrySet().forEach(e -> this.httpRequest.requestHeaders().set(e.getKey(), e.getValue()));
getHeaders().forEach((key, value) -> this.request.requestHeaders().set(key, value));
}
@Override
protected void applyCookies() {
getCookies().values().stream().flatMap(Collection::stream)
.map(cookie -> new DefaultCookie(cookie.getName(), cookie.getValue()))
.forEach(this.httpRequest::addCookie);
.forEach(this.request::addCookie);
}
}

View File

@@ -42,28 +42,26 @@ import io.netty.buffer.ByteBufAllocator;
*/
class ReactorClientHttpResponse implements ClientHttpResponse {
private final NettyDataBufferFactory dataBufferFactory;
private final NettyDataBufferFactory bufferFactory;
private final HttpClientResponse response;
private final NettyInbound nettyInbound;
private final NettyInbound inbound;
public ReactorClientHttpResponse(HttpClientResponse response, NettyInbound nettyInbound,
ByteBufAllocator alloc) {
public ReactorClientHttpResponse(HttpClientResponse response, NettyInbound inbound, ByteBufAllocator alloc) {
this.response = response;
this.nettyInbound = nettyInbound;
this.dataBufferFactory = new NettyDataBufferFactory(alloc);
this.inbound = inbound;
this.bufferFactory = new NettyDataBufferFactory(alloc);
}
@Override
public Flux<DataBuffer> getBody() {
return nettyInbound
.receive()
.map(buf -> {
buf.retain();
return dataBufferFactory.wrap(buf);
return this.inbound.receive()
.map(byteBuf -> {
byteBuf.retain();
return this.bufferFactory.wrap(byteBuf);
});
}