Skip to content

Commit afe911f

Browse files
committed
Merge remote-tracking branch 'origin/4.0.x' into 4.1.x
2 parents 31f4df2 + a90ff3f commit afe911f

File tree

5 files changed

+111
-38
lines changed

5 files changed

+111
-38
lines changed

src/main/java/org/eclipse/jetty/reactive/client/ReactiveRequest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import java.nio.ByteBuffer;
1919
import java.nio.charset.Charset;
2020
import java.util.function.BiFunction;
21-
2221
import org.eclipse.jetty.client.HttpClient;
2322
import org.eclipse.jetty.client.Request;
2423
import org.eclipse.jetty.io.Content.Chunk;
2524
import org.eclipse.jetty.reactive.client.internal.AdapterRequestContent;
25+
import org.eclipse.jetty.reactive.client.internal.BytesContent;
2626
import org.eclipse.jetty.reactive.client.internal.PublisherContent;
2727
import org.eclipse.jetty.reactive.client.internal.RequestEventPublisher;
2828
import org.eclipse.jetty.reactive.client.internal.ResponseEventPublisher;
@@ -79,7 +79,7 @@ protected ReactiveRequest(Request request) {
7979
}
8080

8181
private ReactiveRequest(Request request, boolean abortOnCancel) {
82-
this.request = request.listener(requestEvents)
82+
this.request = request.onRequestListener(requestEvents)
8383
.onResponseBegin(r -> {
8484
this.response = new ReactiveResponse(this, r);
8585
})
@@ -311,6 +311,25 @@ public default boolean rewind() {
311311
return false;
312312
}
313313

314+
/**
315+
* <p>Creates a Content from the given {@code byte[]}.</p>
316+
*
317+
* @param bytes the request content
318+
* @param contentType the request content type
319+
* @return a Content wrapping the given {@code byte[]}
320+
*/
321+
public static Content fromBytes(byte[] bytes, String contentType) {
322+
return new BytesContent(bytes, contentType);
323+
}
324+
325+
/**
326+
* <p>Creates a Content from the given String.</p>
327+
*
328+
* @param string the request content
329+
* @param mediaType the request content media type
330+
* @param charset the request content charset
331+
* @return a Content wrapping the given String
332+
*/
314333
public static Content fromString(String string, String mediaType, Charset charset) {
315334
return new StringContent(string, mediaType, charset);
316335
}

src/main/java/org/eclipse/jetty/reactive/client/internal/AdapterRequestContent.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private Bridge() {
115115
@Override
116116
public void onSubscribe(Subscription s) {
117117
subscription = s;
118+
// Do not demand here, the application controls the demand.
118119
}
119120

120121
@Override
@@ -130,7 +131,9 @@ public void onNext(Content.Chunk c) {
130131
demand = null;
131132
}
132133

133-
invoker.run(() -> invokeDemand(onDemand));
134+
if (onDemand != null) {
135+
invoker.run(() -> invokeDemand(onDemand));
136+
}
134137
}
135138

136139
@Override
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.eclipse.jetty.reactive.client.internal;
17+
18+
import java.nio.ByteBuffer;
19+
import org.eclipse.jetty.io.Content;
20+
import org.eclipse.jetty.reactive.client.ReactiveRequest;
21+
import org.reactivestreams.Subscriber;
22+
23+
/**
24+
* <p>Utility class that provides a {@code byte[]} as reactive content.</p>
25+
*/
26+
public class BytesContent extends AbstractSinglePublisher<Content.Chunk> implements ReactiveRequest.Content {
27+
private final byte[] bytes;
28+
private final String contentType;
29+
30+
public BytesContent(byte[] bytes, String contentType) {
31+
this.bytes = bytes;
32+
this.contentType = contentType;
33+
}
34+
35+
@Override
36+
public long getLength() {
37+
return bytes.length;
38+
}
39+
40+
@Override
41+
public String getContentType() {
42+
return contentType;
43+
}
44+
45+
@Override
46+
public boolean rewind() {
47+
return true;
48+
}
49+
50+
@Override
51+
protected void onRequest(Subscriber<? super Content.Chunk> subscriber, long n) {
52+
// The whole byte[] is sent at once, so this is the last chunk.
53+
emitOnNext(subscriber, Content.Chunk.from(ByteBuffer.wrap(bytes), true));
54+
emitOnComplete(subscriber);
55+
}
56+
}

src/main/java/org/eclipse/jetty/reactive/client/internal/StringContent.java

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,13 @@
1515
*/
1616
package org.eclipse.jetty.reactive.client.internal;
1717

18-
import java.nio.ByteBuffer;
1918
import java.nio.charset.Charset;
20-
import java.util.Objects;
21-
import org.eclipse.jetty.io.Content;
22-
import org.eclipse.jetty.reactive.client.ReactiveRequest;
23-
import org.reactivestreams.Subscriber;
2419

2520
/**
2621
* <p>Utility class that provides a String as reactive content.</p>
2722
*/
28-
public class StringContent extends AbstractSinglePublisher<Content.Chunk> implements ReactiveRequest.Content {
29-
private final String mediaType;
30-
private final Charset encoding;
31-
private final byte[] bytes;
32-
23+
public class StringContent extends BytesContent {
3324
public StringContent(String string, String mediaType, Charset encoding) {
34-
this.mediaType = Objects.requireNonNull(mediaType);
35-
this.encoding = Objects.requireNonNull(encoding);
36-
this.bytes = string.getBytes(encoding);
37-
}
38-
39-
@Override
40-
public long getLength() {
41-
return bytes.length;
42-
}
43-
44-
@Override
45-
public String getContentType() {
46-
return mediaType + ";charset=" + encoding.name();
47-
}
48-
49-
@Override
50-
public boolean rewind() {
51-
return true;
52-
}
53-
54-
@Override
55-
protected void onRequest(Subscriber<? super Content.Chunk> subscriber, long n) {
56-
// The whole string is sent at once, so this is the last chunk.
57-
emitOnNext(subscriber, Content.Chunk.from(ByteBuffer.wrap(bytes), true));
58-
emitOnComplete(subscriber);
25+
super(string.getBytes(encoding), mediaType + ";charset=" + encoding.name());
5926
}
6027
}

src/test/java/org/eclipse/jetty/reactive/client/ReactorTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.reactivestreams.Subscriber;
3737
import org.reactivestreams.Subscription;
3838
import org.springframework.http.HttpStatusCode;
39+
import org.springframework.http.MediaType;
3940
import org.springframework.http.client.reactive.JettyClientHttpConnector;
4041
import org.springframework.web.reactive.function.client.WebClient;
42+
import reactor.core.publisher.Flux;
4143
import reactor.core.publisher.Hooks;
4244
import reactor.core.publisher.Mono;
4345

@@ -70,6 +72,32 @@ public boolean handle(Request request, Response response, Callback callback) {
7072
assertArrayEquals(data, responseContent);
7173
}
7274

75+
@ParameterizedTest
76+
@MethodSource("protocols")
77+
public void testRequestWithContentResponseWithContent(String protocol) throws Exception {
78+
byte[] data = new byte[1024];
79+
new Random().nextBytes(data);
80+
prepare(protocol, new Handler.Abstract() {
81+
@Override
82+
public boolean handle(Request request, Response response, Callback callback) {
83+
Content.copy(request, response, callback);
84+
return true;
85+
}
86+
});
87+
88+
ReactiveRequest.Content requestContent = ReactiveRequest.Content.fromBytes(data, "application/octet-stream");
89+
WebClient client = WebClient.builder().clientConnector(new JettyClientHttpConnector(httpClient())).build();
90+
byte[] responseContent = client.post()
91+
.uri(uri())
92+
.contentType(MediaType.parseMediaType(requestContent.getContentType()))
93+
.body(Flux.from(requestContent).map(Content.Chunk::getByteBuffer), ByteBuffer.class)
94+
.retrieve()
95+
.bodyToMono(byte[].class)
96+
.block();
97+
assertNotNull(responseContent);
98+
assertArrayEquals(data, responseContent);
99+
}
100+
73101
@ParameterizedTest
74102
@MethodSource("protocols")
75103
public void testTotalTimeout(String protocol) throws Exception {

0 commit comments

Comments
 (0)