001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client;
023
024import com.nexmo.client.auth.AuthCollection;
025import com.nexmo.client.auth.AuthMethod;
026import org.apache.http.client.HttpClient;
027import org.apache.http.client.config.RequestConfig;
028import org.apache.http.config.ConnectionConfig;
029import org.apache.http.config.SocketConfig;
030import org.apache.http.impl.client.HttpClientBuilder;
031import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
032
033import java.nio.charset.Charset;
034
035/**
036 * Internal class that holds available authentication methods and a shared HttpClient.
037 */
038public class HttpWrapper {
039    private AuthCollection authCollection;
040    private HttpClient httpClient = null;
041
042    public HttpWrapper(AuthCollection authCollection) {
043        this.authCollection = authCollection;
044    }
045
046    public HttpWrapper(AuthMethod... authMethods) {
047        this(new AuthCollection());
048        for (AuthMethod authMethod : authMethods) {
049            authCollection.add(authMethod);
050        }
051
052    }
053
054    public HttpClient getHttpClient() {
055        if (this.httpClient == null) {
056            this.httpClient = createHttpClient();
057        }
058        return this.httpClient;
059    }
060
061    public void setHttpClient(HttpClient httpClient) {
062        this.httpClient = httpClient;
063    }
064
065    public AuthCollection getAuthCollection() {
066        return authCollection;
067    }
068
069    public void setAuthCollection(AuthCollection authCollection) {
070        this.authCollection = authCollection;
071    }
072
073    protected HttpClient createHttpClient() {
074        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
075        connectionManager.setDefaultMaxPerRoute(200);
076        connectionManager.setMaxTotal(200);
077        connectionManager.setDefaultConnectionConfig(
078                ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build());
079        connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());
080
081        // Need to work out a good value for the following:
082        // threadSafeClientConnManager.setValidateAfterInactivity();
083
084        RequestConfig requestConfig = RequestConfig.custom().build();
085
086        return HttpClientBuilder.create()
087                .setConnectionManager(connectionManager)
088                .setUserAgent("nexmo-java/3.3.0")
089                .setDefaultRequestConfig(requestConfig)
090                .build();
091    }
092}