1 package com.atlassian.httpclient.apache.httpcomponents;
2
3 import com.google.common.primitives.Ints;
4 import org.apache.http.HttpHost;
5 import org.apache.http.HttpRequest;
6 import org.apache.http.HttpResponse;
7 import org.apache.http.concurrent.FutureCallback;
8 import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
9 import org.apache.http.nio.client.methods.HttpAsyncMethods;
10 import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
11 import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
12 import org.apache.http.protocol.HttpContext;
13
14 import java.io.IOException;
15 import java.util.concurrent.Future;
16
17 public class BoundedHttpAsyncClient extends CloseableHttpAsyncClient {
18
19 private final CloseableHttpAsyncClient delegate;
20 private final int maxEntitySize;
21
22 public BoundedHttpAsyncClient(CloseableHttpAsyncClient delegate, int maxEntitySize) {
23 this.delegate = delegate;
24 this.maxEntitySize = maxEntitySize;
25 }
26
27 @Override
28 public boolean isRunning() {
29 return delegate.isRunning();
30 }
31
32 @Override
33 public void start() {
34 delegate.start();
35 }
36
37 @Override
38 public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer,
39 HttpAsyncResponseConsumer<T> responseConsumer,
40 HttpContext context,
41 FutureCallback<T> callback) {
42 return delegate.execute(requestProducer, responseConsumer, context, callback);
43 }
44
45 @Override
46 public Future<HttpResponse> execute(HttpHost target, HttpRequest request, HttpContext context,
47 FutureCallback<HttpResponse> callback) {
48 BoundedAsyncResponseConsumer consumer = new BoundedAsyncResponseConsumer(Ints.saturatedCast(maxEntitySize));
49 return delegate.execute(
50 HttpAsyncMethods.create(target, request),
51 consumer,
52 context, callback);
53 }
54
55 @Override
56 public void close() throws IOException {
57 delegate.close();
58 }
59 }