Minor refactoring in JettyClientHttpConnector
See gh-25849
This commit is contained in:
parent
86f2ebec1b
commit
e44b08f1fc
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2019 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -21,7 +21,9 @@ import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.client.api.Request;
|
||||||
import org.eclipse.jetty.reactive.client.ContentChunk;
|
import org.eclipse.jetty.reactive.client.ContentChunk;
|
||||||
|
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
||||||
import reactor.core.publisher.Flux;
|
import reactor.core.publisher.Flux;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@ -81,7 +83,8 @@ public class JettyClientHttpConnector implements ClientHttpConnector {
|
||||||
* Constructor with an {@link JettyResourceFactory} that will manage shared resources.
|
* Constructor with an {@link JettyResourceFactory} that will manage shared resources.
|
||||||
* @param resourceFactory the {@link JettyResourceFactory} to use
|
* @param resourceFactory the {@link JettyResourceFactory} to use
|
||||||
* @param customizer the lambda used to customize the {@link HttpClient}
|
* @param customizer the lambda used to customize the {@link HttpClient}
|
||||||
* @deprecated as of 5.2, in favor of {@link JettyClientHttpConnector#JettyClientHttpConnector(HttpClient, JettyResourceFactory)}
|
* @deprecated as of 5.2, in favor of
|
||||||
|
* {@link JettyClientHttpConnector#JettyClientHttpConnector(HttpClient, JettyResourceFactory)}
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public JettyClientHttpConnector(JettyResourceFactory resourceFactory, @Nullable Consumer<HttpClient> customizer) {
|
public JettyClientHttpConnector(JettyResourceFactory resourceFactory, @Nullable Consumer<HttpClient> customizer) {
|
||||||
|
@ -114,13 +117,13 @@ public class JettyClientHttpConnector implements ClientHttpConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JettyClientHttpRequest clientHttpRequest = new JettyClientHttpRequest(
|
Request request = this.httpClient.newRequest(uri).method(method.toString());
|
||||||
this.httpClient.newRequest(uri).method(method.toString()), this.bufferFactory);
|
|
||||||
|
|
||||||
return requestCallback.apply(clientHttpRequest).then(Mono.from(
|
return requestCallback.apply(new JettyClientHttpRequest(request, this.bufferFactory))
|
||||||
clientHttpRequest.getReactiveRequest().response((response, chunks) -> {
|
.then(Mono.from(ReactiveRequest.newBuilder(request).build()
|
||||||
Flux<DataBuffer> content = Flux.from(chunks).map(this::toDataBuffer);
|
.response((reactiveResponse, chunkPublisher) -> {
|
||||||
return Mono.just(new JettyClientHttpResponse(response, content));
|
Flux<DataBuffer> content = Flux.from(chunkPublisher).map(this::toDataBuffer);
|
||||||
|
return Mono.just(new JettyClientHttpResponse(reactiveResponse, content));
|
||||||
})));
|
})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2020 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -24,6 +24,7 @@ import java.util.function.Function;
|
||||||
import org.eclipse.jetty.client.api.Request;
|
import org.eclipse.jetty.client.api.Request;
|
||||||
import org.eclipse.jetty.reactive.client.ContentChunk;
|
import org.eclipse.jetty.reactive.client.ContentChunk;
|
||||||
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
import org.eclipse.jetty.reactive.client.ReactiveRequest;
|
||||||
|
import org.eclipse.jetty.reactive.client.internal.PublisherContentProvider;
|
||||||
import org.eclipse.jetty.util.Callback;
|
import org.eclipse.jetty.util.Callback;
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import reactor.core.Exceptions;
|
import reactor.core.Exceptions;
|
||||||
|
@ -37,7 +38,6 @@ import org.springframework.core.io.buffer.PooledDataBuffer;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,9 +53,6 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
|
|
||||||
private final DataBufferFactory bufferFactory;
|
private final DataBufferFactory bufferFactory;
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private ReactiveRequest reactiveRequest;
|
|
||||||
|
|
||||||
|
|
||||||
public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory) {
|
public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory) {
|
||||||
this.jettyRequest = jettyRequest;
|
this.jettyRequest = jettyRequest;
|
||||||
|
@ -87,20 +84,21 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
|
||||||
Flux<ContentChunk> chunks = Flux.from(body).map(this::toContentChunk);
|
ReactiveRequest.Content content = Flux.from(body)
|
||||||
ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
|
.map(this::toContentChunk)
|
||||||
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
|
.as(chunks -> ReactiveRequest.Content.fromPublisher(chunks, getContentType()));
|
||||||
|
this.jettyRequest.content(new PublisherContentProvider(content));
|
||||||
return doCommit(this::completes);
|
return doCommit(this::completes);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
|
||||||
Flux<ContentChunk> chunks = Flux.from(body)
|
ReactiveRequest.Content content = Flux.from(body)
|
||||||
.flatMap(Function.identity())
|
.flatMap(Function.identity())
|
||||||
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
|
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
|
||||||
.map(this::toContentChunk);
|
.map(this::toContentChunk)
|
||||||
ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
|
.as(chunks -> ReactiveRequest.Content.fromPublisher(chunks, getContentType()));
|
||||||
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
|
this.jettyRequest.content(new PublisherContentProvider(content));
|
||||||
return doCommit(this::completes);
|
return doCommit(this::completes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,11 +143,4 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ReactiveRequest getReactiveRequest() {
|
|
||||||
if (this.reactiveRequest == null) {
|
|
||||||
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).build();
|
|
||||||
}
|
|
||||||
return this.reactiveRequest;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue