Skip to content

Commit

Permalink
make the read buffer max size configurable in jetty connector
Browse files Browse the repository at this point in the history
  • Loading branch information
cen1 committed Nov 30, 2020
1 parent 684bbf8 commit 680d63b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ private JettyClientProperties() {
public static final String ENABLE_SSL_HOSTNAME_VERIFICATION =
"jersey.config.jetty.client.enableSslHostnameVerification";

/**
* Size in bytes for max response buffer size.
* <p/>
* If the property is absent the value is default jetty response buffer size.
*/
public static final String RESPONSE_BUFFER_MAX_SIZE =
"jersey.config.jetty.client.responseBufferMaxSize";

/**
* Get the value of the specified property.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
Expand All @@ -45,6 +47,10 @@

import javax.net.ssl.SSLContext;

import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.FutureResponseListener;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.ClientRequest;
import org.glassfish.jersey.client.ClientResponse;
Expand All @@ -65,9 +71,6 @@
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.client.util.BasicAuthentication;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.client.util.OutputStreamContentProvider;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
Expand Down Expand Up @@ -130,6 +133,7 @@ class JettyConnector implements Connector {
private final HttpClient client;
private final CookieStore cookieStore;
private final Configuration configuration;
private final Integer responseBufferMaxSize;

/**
* Create the new Jetty client connector.
Expand Down Expand Up @@ -199,6 +203,12 @@ class JettyConnector implements Connector {
client.setCookieStore(new HttpCookieStore.Empty());
}

responseBufferMaxSize = (Integer) Optional.ofNullable(configuration.getProperties()
.get(JettyClientProperties.RESPONSE_BUFFER_MAX_SIZE)).orElse(2 * 1024 * 1024);
if (responseBufferMaxSize <= 0) {
throw new IllegalArgumentException(JettyClientProperties.RESPONSE_BUFFER_MAX_SIZE + " can not be negative.");
}

try {
client.start();
} catch (final Exception e) {
Expand Down Expand Up @@ -248,7 +258,20 @@ public ClientResponse apply(final ClientRequest jerseyRequest) throws Processing
}

try {
final ContentResponse jettyResponse = jettyRequest.send();
final FutureResponseListener listener = new FutureResponseListener(jettyRequest, responseBufferMaxSize);
jettyRequest.send(listener);
final ContentResponse jettyResponse;
try {
jettyResponse = listener.get();
}
catch (ExecutionException x) {
if (x.getCause() instanceof TimeoutException) {
TimeoutException t = (TimeoutException) (x.getCause());
throw t;
}
throw x;
}

HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, jerseyRequest.getHeaders(),
JettyConnector.this.getClass().getName(), jerseyRequest.getConfiguration());

Expand Down

0 comments on commit 680d63b

Please sign in to comment.