com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java Source code

Java tutorial

Introduction

Here is the source code for com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java

Source

/*
 * Copyright 2015 Ras Kasa Williams
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.raskasa.dropwizard.okhttp;

import com.codahale.metrics.MetricRegistry;
import com.raskasa.metrics.okhttp.InstrumentedOkHttpClients;
import com.squareup.okhttp.Cache;
import com.squareup.okhttp.OkHttpClient;
import io.dropwizard.lifecycle.Managed;
import io.dropwizard.setup.Environment;
import java.util.concurrent.TimeUnit;

/** A convenience class for building {@link OkHttpClient} instances. */
public final class OkHttpClientBuilder {
    private final MetricRegistry registry;
    private Environment environment;
    private OkHttpClientConfiguration configuration;
    private String name;

    public OkHttpClientBuilder(MetricRegistry registry) {
        this.registry = registry;
    }

    public OkHttpClientBuilder(Environment environment) {
        this(environment.metrics());
        this.environment = environment;
    }

    /** Use the given {@link OkHttpClientConfiguration} instance. */
    public OkHttpClientBuilder using(OkHttpClientConfiguration configuration) {
        if (configuration == null)
            throw new IllegalArgumentException("configuration == null");
        this.configuration = configuration;
        return this;
    }

    /** Provide an identifier for the {@link OkHttpClient} instance. */
    public OkHttpClientBuilder withName(String name) {
        if (name == null)
            throw new IllegalArgumentException("name == null");
        this.name = name;
        return this;
    }

    /** Builds the {@link OkHttpClient}. */
    public OkHttpClient build() {
        final OkHttpClient rawClient = new OkHttpClient();
        if (configuration.getConnectTimeout() > 0L) {
            rawClient.setConnectTimeout(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS);
        }
        if (configuration.getReadTimeout() > 0L) {
            rawClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS);
        }
        if (configuration.getWriteTimeout() > 0L) {
            rawClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.MILLISECONDS);
        }
        if (configuration.getCacheDir() != null && configuration.getCacheSize() > 0L) {
            rawClient.setCache(new Cache(configuration.getCacheDir(), configuration.getCacheSize()));
        }
        final OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, name);

        // If the environment is present, we tie the client with the server lifecycle
        if (environment != null) {
            environment.lifecycle().manage(new Managed() {
                @Override
                public void start() throws Exception {
                }

                @Override
                public void stop() throws Exception {
                    client.getConnectionPool().evictAll();
                }
            });
        }
        return client;
    }
}