Skip to content

Instantly share code, notes, and snippets.

@aruld aruld/ChunkFactory
Last active Dec 31, 2015

Embed
What would you like to do?
Chunk factory to override default chunk size in Apache HttpClient.
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.ContentLengthStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultManagedHttpClientConnection;
import org.apache.http.impl.conn.ManagedHttpClientConnectionFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.io.ChunkedOutputStream;
import org.apache.http.io.SessionOutputBuffer;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicLong;
/**
* Created by arul on 12/19/13.
*/
public class ChunkFactory {
public static class CustomManagedHttpClientConnection extends DefaultManagedHttpClientConnection {
private final int chunkSize;
public CustomManagedHttpClientConnection(final String id, final int buffersize, final int chunkSize) {
super(id, buffersize);
this.chunkSize = chunkSize;
}
@Override
protected OutputStream createOutputStream(long len, SessionOutputBuffer outbuffer) {
if (len == ContentLengthStrategy.CHUNKED) {
return new ChunkedOutputStream(chunkSize, outbuffer);
}
return super.createOutputStream(len, outbuffer);
}
}
public static class CustomManagedHttpClientConnectionFactory extends ManagedHttpClientConnectionFactory {
private static final AtomicLong COUNTER = new AtomicLong();
private final int chunkSize;
public CustomManagedHttpClientConnectionFactory(int chunkSize) {
this.chunkSize = chunkSize;
}
@Override
public ManagedHttpClientConnection create(HttpRoute route, ConnectionConfig config) {
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
return new CustomManagedHttpClientConnection(id, config.getBufferSize(), chunkSize);
}
}
public static void main(String[] args) throws Exception {
int chunkSize = 1024;
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new CustomManagedHttpClientConnectionFactory(chunkSize);
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(connFactory);
CloseableHttpClient client = HttpClients.createMinimal(connManager);
HttpPost httpPost = new HttpPost("http://.../resource");
StringEntity entity = new StringEntity("a very long string .....", ContentType.create("plain/text", Consts.UTF_8));
entity.setChunked(true);
httpPost.setEntity(entity);
CloseableHttpResponse response = client.execute(httpPost);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.