Skip to content

Commit e44b08f

Browse files
committed
Minor refactoring in JettyClientHttpConnector
See gh-25849
1 parent 86f2ebe commit e44b08f

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpConnector.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,9 @@
2121
import java.util.function.Function;
2222

2323
import org.eclipse.jetty.client.HttpClient;
24+
import org.eclipse.jetty.client.api.Request;
2425
import org.eclipse.jetty.reactive.client.ContentChunk;
26+
import org.eclipse.jetty.reactive.client.ReactiveRequest;
2527
import reactor.core.publisher.Flux;
2628
import reactor.core.publisher.Mono;
2729

@@ -81,7 +83,8 @@ public JettyClientHttpConnector(HttpClient httpClient, @Nullable JettyResourceFa
8183
* Constructor with an {@link JettyResourceFactory} that will manage shared resources.
8284
* @param resourceFactory the {@link JettyResourceFactory} to use
8385
* @param customizer the lambda used to customize the {@link HttpClient}
84-
* @deprecated as of 5.2, in favor of {@link JettyClientHttpConnector#JettyClientHttpConnector(HttpClient, JettyResourceFactory)}
86+
* @deprecated as of 5.2, in favor of
87+
* {@link JettyClientHttpConnector#JettyClientHttpConnector(HttpClient, JettyResourceFactory)}
8588
*/
8689
@Deprecated
8790
public JettyClientHttpConnector(JettyResourceFactory resourceFactory, @Nullable Consumer<HttpClient> customizer) {
@@ -114,14 +117,14 @@ public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri,
114117
}
115118
}
116119

117-
JettyClientHttpRequest clientHttpRequest = new JettyClientHttpRequest(
118-
this.httpClient.newRequest(uri).method(method.toString()), this.bufferFactory);
120+
Request request = this.httpClient.newRequest(uri).method(method.toString());
119121

120-
return requestCallback.apply(clientHttpRequest).then(Mono.from(
121-
clientHttpRequest.getReactiveRequest().response((response, chunks) -> {
122-
Flux<DataBuffer> content = Flux.from(chunks).map(this::toDataBuffer);
123-
return Mono.just(new JettyClientHttpResponse(response, content));
124-
})));
122+
return requestCallback.apply(new JettyClientHttpRequest(request, this.bufferFactory))
123+
.then(Mono.from(ReactiveRequest.newBuilder(request).build()
124+
.response((reactiveResponse, chunkPublisher) -> {
125+
Flux<DataBuffer> content = Flux.from(chunkPublisher).map(this::toDataBuffer);
126+
return Mono.just(new JettyClientHttpResponse(reactiveResponse, content));
127+
})));
125128
}
126129

127130
private DataBuffer toDataBuffer(ContentChunk chunk) {

spring-web/src/main/java/org/springframework/http/client/reactive/JettyClientHttpRequest.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.eclipse.jetty.client.api.Request;
2525
import org.eclipse.jetty.reactive.client.ContentChunk;
2626
import org.eclipse.jetty.reactive.client.ReactiveRequest;
27+
import org.eclipse.jetty.reactive.client.internal.PublisherContentProvider;
2728
import org.eclipse.jetty.util.Callback;
2829
import org.reactivestreams.Publisher;
2930
import reactor.core.Exceptions;
@@ -37,7 +38,6 @@
3738
import org.springframework.http.HttpHeaders;
3839
import org.springframework.http.HttpMethod;
3940
import org.springframework.http.MediaType;
40-
import org.springframework.lang.Nullable;
4141
import org.springframework.util.Assert;
4242

4343
/**
@@ -53,9 +53,6 @@ class JettyClientHttpRequest extends AbstractClientHttpRequest {
5353

5454
private final DataBufferFactory bufferFactory;
5555

56-
@Nullable
57-
private ReactiveRequest reactiveRequest;
58-
5956

6057
public JettyClientHttpRequest(Request jettyRequest, DataBufferFactory bufferFactory) {
6158
this.jettyRequest = jettyRequest;
@@ -87,20 +84,21 @@ public DataBufferFactory bufferFactory() {
8784

8885
@Override
8986
public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
90-
Flux<ContentChunk> chunks = Flux.from(body).map(this::toContentChunk);
91-
ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
92-
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
87+
ReactiveRequest.Content content = Flux.from(body)
88+
.map(this::toContentChunk)
89+
.as(chunks -> ReactiveRequest.Content.fromPublisher(chunks, getContentType()));
90+
this.jettyRequest.content(new PublisherContentProvider(content));
9391
return doCommit(this::completes);
9492
}
9593

9694
@Override
9795
public Mono<Void> writeAndFlushWith(Publisher<? extends Publisher<? extends DataBuffer>> body) {
98-
Flux<ContentChunk> chunks = Flux.from(body)
96+
ReactiveRequest.Content content = Flux.from(body)
9997
.flatMap(Function.identity())
10098
.doOnDiscard(PooledDataBuffer.class, DataBufferUtils::release)
101-
.map(this::toContentChunk);
102-
ReactiveRequest.Content content = ReactiveRequest.Content.fromPublisher(chunks, getContentType());
103-
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).content(content).build();
99+
.map(this::toContentChunk)
100+
.as(chunks -> ReactiveRequest.Content.fromPublisher(chunks, getContentType()));
101+
this.jettyRequest.content(new PublisherContentProvider(content));
104102
return doCommit(this::completes);
105103
}
106104

@@ -145,11 +143,4 @@ protected void applyHeaders() {
145143
}
146144
}
147145

148-
ReactiveRequest getReactiveRequest() {
149-
if (this.reactiveRequest == null) {
150-
this.reactiveRequest = ReactiveRequest.newBuilder(this.jettyRequest).build();
151-
}
152-
return this.reactiveRequest;
153-
}
154-
155146
}

0 commit comments

Comments
 (0)