package com.yammer.dropwizard.client;

import com.sun.jersey.api.client.filter.GZIPContentEncodingFilter;
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
import com.sun.jersey.client.apache4.config.ApacheHttpClient4Config;
import com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.jersey.JacksonMessageBodyProvider;
import org.apache.http.client.HttpClient;

import java.util.concurrent.TimeUnit;

public class JerseyClientFactory {
    private final JerseyClientConfiguration configuration;
    private final HttpClientFactory factory;

    public JerseyClientFactory(JerseyClientConfiguration configuration) {
        this.configuration = configuration;
        this.factory = new HttpClientFactory(configuration);
    }

    public JerseyClient build(Environment environment) {
        final HttpClient client = factory.build();

        final ApacheHttpClient4Handler handler = new ApacheHttpClient4Handler(client, null, true);

        final ApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
        config.getSingletons().add(new JacksonMessageBodyProvider(environment.getService().getJson()));

        final JerseyClient jerseyClient = new JerseyClient(handler, config);
        jerseyClient.setExecutorService(environment.managedExecutorService("jersey-client-%d",
                                                                           configuration.getMinThreads(),
                                                                           configuration.getMaxThreads(),
                                                                           60,
                                                                           TimeUnit.SECONDS));

        if (configuration.isGzipEnabled()) {
            jerseyClient.addFilter(new GZIPContentEncodingFilter());
        }

        return jerseyClient;
    }
}

@@ -28,24 +28,89 @@
 rd.client;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A
 import com.s
@@ -523,25 +523,24 @@
 HttpClient;%0A
-%0A
 import java.
@@ -704,16 +704,66 @@
 factory;
+%0A    private final ApacheHttpClient4Config config;
 %0A%0A    pu
@@ -935,16 +935,735 @@
 ation);%0A
+        this.config = new DefaultApacheHttpClient4Config();%0A    %7D%0A%0A    /**%0A     * Adds the given object as a Jersey provider.%0A     *%0A     * @param provider    a Jersey provider%0A     */%0A    public JerseyClientFactory addProvider(Object provider) %7B%0A        config.getSingletons().add(checkNotNull(provider));%0A        return this;%0A    %7D%0A%0A    /**%0A     * Adds the given class as a Jersey provider.%0A     * %3Cp/%3E%3Cb%3EN.B.:%3C/b%3E This class must either have a no-args constructor or use Jersey's built-in%0A     * dependency injection.%0A     *%0A     * @param klass    a Jersey provider class%0A     */%0A    public JerseyClientFactory addProvider(Class%3C?%3E klass) %7B%0A        config.getClasses().add(checkNotNull(klass));%0A        return this;%0A
     %7D%0A%0A 
@@ -1872,94 +1872,8 @@
 ue);
-%0A%0A        final ApacheHttpClient4Config config = new DefaultApacheHttpClient4Config();
 %0A   

Add addProvider to JerseyClientFactory