/
HttpClient.java
172 lines (145 loc) · 6.04 KB
/
HttpClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package net.authorize.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import net.authorize.Environment;
/**
* Transportation object used to facilitate the communication with the
* respective gateway.
*
*/
public class HttpClient {
private static Logger logger = LogManager.getLogger(HttpClient.class);
public static final String ENCODING = "UTF-8";
static boolean proxySet = false;
static boolean UseProxy = Environment.getBooleanProperty(Constants.HTTPS_USE_PROXY);
static String ProxyHost = Environment.getProperty(Constants.HTTPS_PROXY_HOST);
static int ProxyPort = Environment.getIntProperty(Constants.HTTPS_PROXY_PORT);
static String proxyUsername = Environment.getProperty(Constants.HTTPS_PROXY_USERNAME);
static String proxyPassword = Environment.getProperty(Constants.HTTPS_PROXY_PASSWORD);
static int httpConnectionTimeout = Environment.getIntProperty(Constants.HTTP_CONNECTION_TIME_OUT);
static int httpReadTimeout = Environment.getIntProperty(Constants.HTTP_READ_TIME_OUT);
static {
LogHelper.info(logger, "Use Proxy: '%s'", UseProxy);
httpConnectionTimeout = (httpConnectionTimeout == 0 ? Constants.HTTP_CONNECTION_TIME_OUT_DEFAULT_VALUE
: httpConnectionTimeout);
httpReadTimeout = (httpReadTimeout == 0 ? Constants.HTTP_READ_TIME_OUT_DEFAULT_VALUE : httpReadTimeout);
}
/**
* Converts a response inputstream into a string.
*
* @param is
* @return String
*/
@Deprecated
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
LogHelper.warn(logger, "Exception reading data from Stream: '%s'", e.getMessage());
} finally {
if (null != reader) {
try {
reader.close();
} catch (IOException e) {
LogHelper.warn(logger, "Exception closing BufferedReader: '%s'", e.getMessage());
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
LogHelper.warn(logger, "Exception closing InputStream: '%s'", e.getMessage());
}
}
}
return sb.toString();
}
/**
* @return returns an SSL context with TLSv1.2 protocol instance to be used in
* the call
*/
private static SSLContext getSSLContext() {
try {
final SSLContext sc = SSLContext.getInstance("TLSv1.2");
final TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
sc.init(null, trustManagerFactory.getTrustManagers(), new java.security.SecureRandom());
return sc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Returns a HTTPClient instance which enforce TLSv1.2 protocol for all the
* calls
*
* @return CloseableHttpClient instance
* @throws Exception
*/
static CloseableHttpClient getHttpsClient() throws Exception {
SSLContext sslcontext = getSSLContext();
try {
LayeredConnectionSocketFactory sslSocketFactory = new org.apache.http.conn.ssl.SSLConnectionSocketFactory(
sslcontext, SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(httpConnectionTimeout).build();
CloseableHttpClient httpClient;
if (UseProxy && ProxyHost != null) {
HttpClientBuilder hcBuilder;
if (proxyUsername != null && proxyPassword != null) {
LogHelper.info(logger, "Setting up proxy to URL with Authentication: '%s://%s@%s:%d'",
Constants.PROXY_PROTOCOL, proxyUsername, ProxyHost, ProxyPort);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
AuthScope proxyScope = new AuthScope(ProxyHost, ProxyPort);
Credentials proxyCreds = new UsernamePasswordCredentials(proxyUsername, proxyPassword);
credsProvider.setCredentials(proxyScope, proxyCreds);
hcBuilder = HttpClients.custom().setSSLSocketFactory(sslSocketFactory)
.setDefaultRequestConfig(requestConfig).setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultCredentialsProvider(credsProvider);
} else {
LogHelper.info(logger, "Setting up proxy to URL: '%s://%s:%d'", Constants.PROXY_PROTOCOL, ProxyHost,
ProxyPort);
hcBuilder = HttpClients.custom().setSSLSocketFactory(sslSocketFactory)
.setDefaultRequestConfig(requestConfig).setRedirectStrategy(new LaxRedirectStrategy());
}
HttpHost httpProxy = new HttpHost(ProxyHost, ProxyPort, Constants.PROXY_PROTOCOL);
hcBuilder.setProxy(httpProxy);
httpClient = hcBuilder.build();
proxySet = true;
} else {
LogHelper.warn(logger, "Defaulting to non-proxy environment");
httpClient = HttpClients.custom().setSSLSocketFactory(sslSocketFactory)
.setDefaultRequestConfig(requestConfig).setRedirectStrategy(new LaxRedirectStrategy()).build();
}
return httpClient;
} catch (Exception e) {
return null;
}
}
}