co.tuzza.clicksend4j.http.HttpClientUtils.java Source code

Java tutorial

Introduction

Here is the source code for co.tuzza.clicksend4j.http.HttpClientUtils.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.tuzza.clicksend4j.http;

import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
 *
 * @author dylan
 */
public class HttpClientUtils {

    private final int connectionTimeout;
    private final int socketTimeout;
    private final PoolingHttpClientConnectionManager clientConnectionManager;
    private final static Map<String, HttpClientUtils> instances = new HashMap<>();

    /**
     * Return an existing or instantiate a new HttpClient factory instance with
     * explicitly specified connection and read timeout values
     *
     * @param connectionTimeout the timeout value in milliseconds to use when
     * establishing a new http socket
     * @param socketTimeout the timeout value in milliseconds to wait for a http
     * response before closing the socket
     *
     * @return HttpClientUtils an instance of the HttpClient factory primed with
     * the requested timeout values
     */
    public static HttpClientUtils getInstance(int connectionTimeout, int socketTimeout) {
        String key = "ct-" + connectionTimeout + "-st-" + socketTimeout;
        HttpClientUtils instance = instances.get(key);
        if (instance == null) {
            instance = new HttpClientUtils(connectionTimeout, socketTimeout);
            instances.put(key, instance);
        }
        return instance;
    }

    public HttpClientUtils(int connectionTimeout, int socketTimeout) {
        this.connectionTimeout = connectionTimeout;
        this.socketTimeout = socketTimeout;

        this.clientConnectionManager = new PoolingHttpClientConnectionManager();
        this.clientConnectionManager.setDefaultMaxPerRoute(200);
        this.clientConnectionManager.setMaxTotal(200);
    }

    public HttpClient getHttpClient() {

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setUserAgent("ClickSend4J 0.0.1");
        httpClientBuilder.setConnectionManager(clientConnectionManager);
        httpClientBuilder.setDefaultRequestConfig(buildRequestConfig());

        return httpClientBuilder.build();

    }

    private RequestConfig buildRequestConfig() {
        Builder builder = RequestConfig.custom();

        builder.setSocketTimeout(socketTimeout);
        builder.setConnectTimeout(connectionTimeout);
        builder.setAuthenticationEnabled(true);

        return builder.build();
    }
}