Skip to content

Commit 680d63b

Browse files
committed
make the read buffer max size configurable in jetty connector
1 parent 684bbf8 commit 680d63b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyClientProperties.java

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ private JettyClientProperties() {
7575
public static final String ENABLE_SSL_HOSTNAME_VERIFICATION =
7676
"jersey.config.jetty.client.enableSslHostnameVerification";
7777

78+
/**
79+
* Size in bytes for max response buffer size.
80+
* <p/>
81+
* If the property is absent the value is default jetty response buffer size.
82+
*/
83+
public static final String RESPONSE_BUFFER_MAX_SIZE =
84+
"jersey.config.jetty.client.responseBufferMaxSize";
85+
7886
/**
7987
* Get the value of the specified property.
8088
*

connectors/jetty-connector/src/main/java/org/glassfish/jersey/jetty/connector/JettyConnector.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import java.util.Optional;
3232
import java.util.concurrent.CancellationException;
3333
import java.util.concurrent.CompletableFuture;
34+
import java.util.concurrent.ExecutionException;
3435
import java.util.concurrent.Future;
3536
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
3638
import java.util.concurrent.atomic.AtomicBoolean;
3739
import java.util.concurrent.atomic.AtomicReference;
3840
import java.util.logging.Level;
@@ -45,6 +47,10 @@
4547

4648
import javax.net.ssl.SSLContext;
4749

50+
import org.eclipse.jetty.client.util.BasicAuthentication;
51+
import org.eclipse.jetty.client.util.BytesContentProvider;
52+
import org.eclipse.jetty.client.util.FutureResponseListener;
53+
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
4854
import org.glassfish.jersey.client.ClientProperties;
4955
import org.glassfish.jersey.client.ClientRequest;
5056
import org.glassfish.jersey.client.ClientResponse;
@@ -65,9 +71,6 @@
6571
import org.eclipse.jetty.client.api.Request;
6672
import org.eclipse.jetty.client.api.Response;
6773
import org.eclipse.jetty.client.api.Result;
68-
import org.eclipse.jetty.client.util.BasicAuthentication;
69-
import org.eclipse.jetty.client.util.BytesContentProvider;
70-
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
7174
import org.eclipse.jetty.http.HttpField;
7275
import org.eclipse.jetty.http.HttpFields;
7376
import org.eclipse.jetty.http.HttpHeader;
@@ -130,6 +133,7 @@ class JettyConnector implements Connector {
130133
private final HttpClient client;
131134
private final CookieStore cookieStore;
132135
private final Configuration configuration;
136+
private final Integer responseBufferMaxSize;
133137

134138
/**
135139
* Create the new Jetty client connector.
@@ -199,6 +203,12 @@ class JettyConnector implements Connector {
199203
client.setCookieStore(new HttpCookieStore.Empty());
200204
}
201205

206+
responseBufferMaxSize = (Integer) Optional.ofNullable(configuration.getProperties()
207+
.get(JettyClientProperties.RESPONSE_BUFFER_MAX_SIZE)).orElse(2 * 1024 * 1024);
208+
if (responseBufferMaxSize <= 0) {
209+
throw new IllegalArgumentException(JettyClientProperties.RESPONSE_BUFFER_MAX_SIZE + " can not be negative.");
210+
}
211+
202212
try {
203213
client.start();
204214
} catch (final Exception e) {
@@ -248,7 +258,20 @@ public ClientResponse apply(final ClientRequest jerseyRequest) throws Processing
248258
}
249259

250260
try {
251-
final ContentResponse jettyResponse = jettyRequest.send();
261+
final FutureResponseListener listener = new FutureResponseListener(jettyRequest, responseBufferMaxSize);
262+
jettyRequest.send(listener);
263+
final ContentResponse jettyResponse;
264+
try {
265+
jettyResponse = listener.get();
266+
}
267+
catch (ExecutionException x) {
268+
if (x.getCause() instanceof TimeoutException) {
269+
TimeoutException t = (TimeoutException) (x.getCause());
270+
throw t;
271+
}
272+
throw x;
273+
}
274+
252275
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(),
253276
JettyConnector.this.getClass().getName(), jerseyRequest.getConfiguration());
254277

0 commit comments

Comments
 (0)