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 static final String CLIENT_NAME = "nexmo-java";
040    private static final String CLIENT_VERSION = "5.6.0";
041    private static final String JAVA_VERSION = System.getProperty("java.version");
042
043    private AuthCollection authCollection;
044    private HttpClient httpClient = null;
045    private HttpConfig httpConfig;
046
047    public HttpWrapper(AuthCollection authCollection) {
048        this(HttpConfig.builder().build(), authCollection);
049    }
050
051    public HttpWrapper(HttpConfig httpConfig, AuthCollection authCollection) {
052        this.authCollection = authCollection;
053        this.httpConfig = httpConfig;
054    }
055
056    public HttpWrapper(AuthMethod... authMethods) {
057        this(HttpConfig.builder().build(), authMethods);
058    }
059
060    public HttpWrapper(HttpConfig httpConfig, AuthMethod... authMethods) {
061        this(new AuthCollection());
062        for (AuthMethod authMethod : authMethods) {
063            authCollection.add(authMethod);
064        }
065
066        this.httpConfig = httpConfig;
067    }
068
069    public HttpClient getHttpClient() {
070        if (this.httpClient == null) {
071            this.httpClient = createHttpClient();
072        }
073        return this.httpClient;
074    }
075
076    public void setHttpClient(HttpClient httpClient) {
077        this.httpClient = httpClient;
078    }
079
080    public AuthCollection getAuthCollection() {
081        return authCollection;
082    }
083
084    public void setAuthCollection(AuthCollection authCollection) {
085        this.authCollection = authCollection;
086    }
087
088    protected HttpClient createHttpClient() {
089        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
090        connectionManager.setDefaultMaxPerRoute(200);
091        connectionManager.setMaxTotal(200);
092        connectionManager.setDefaultConnectionConfig(ConnectionConfig
093                .custom()
094                .setCharset(Charset.forName("UTF-8"))
095                .build());
096        connectionManager.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).build());
097
098        // Need to work out a good value for the following:
099        // threadSafeClientConnManager.setValidateAfterInactivity();
100
101        RequestConfig requestConfig = RequestConfig.custom().build();
102
103        return HttpClientBuilder
104                .create()
105                .setConnectionManager(connectionManager)
106                .setUserAgent(String.format("%s/%s java/%s", CLIENT_NAME, CLIENT_VERSION, JAVA_VERSION))
107                .setDefaultRequestConfig(requestConfig)
108                .useSystemProperties()
109                .build();
110    }
111
112    public HttpConfig getHttpConfig() {
113        return httpConfig;
114    }
115}