package com.github.arteam.dropwizard.http2.client;

import io.dropwizard.setup.Environment;
import org.eclipse.jetty.client.HttpClient;

/**
 * Date: 11/26/15
 * Time: 10:22 AM
 * <p>
 * A builder for {@link HttpClient}. It builds an instance from an external
 * configuration and ties it to Dropwizard environment.
 *
 * @author Artem Prigoda
 */
public class JettyHttpClientBuilder {

    private Environment environment;
    private Http2ClientConfiguration configuration = new Http2ClientConfiguration();

    public JettyHttpClientBuilder(Environment environment) {
        this.environment = environment;
    }

    public JettyHttpClientBuilder using(Http2ClientConfiguration configuration) {
        this.configuration = configuration;
        return this;
    }

    public HttpClient build(String name) {
        ClientTransportFactory connectionFactoryBuilder = configuration.getConnectionFactoryBuilder();
        HttpClient httpClient = new HttpClient(
                connectionFactoryBuilder.httpClientTransport(environment.metrics(), name),
                connectionFactoryBuilder.sslContextFactory());
        httpClient.setConnectTimeout(configuration.getConnectionTimeout().toMilliseconds());
        httpClient.setIdleTimeout(configuration.getIdleTimeout().toMilliseconds());
        environment.lifecycle().manage(httpClient);
        return httpClient;
    }

    public HttpClient build() {
        return build("");
    }
}

@@ -45,16 +45,55 @@
 lient;%0A%0A
+import com.google.common.base.Strings;%0A
 import i
@@ -167,16 +167,99 @@
 pClient;
+%0Aimport org.eclipse.jetty.http.HttpField;%0Aimport org.eclipse.jetty.http.HttpHeader;
 %0A%0A/**%0A *
@@ -1404,32 +1404,172 @@
 illiseconds());%0A
+        if (!Strings.isNullOrEmpty(name)) %7B%0A            httpClient.setUserAgentField(new HttpField(HttpHeader.USER_AGENT, name));%0A        %7D%0A
         environm

Set user-agent from the client's name