import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
public class HttpsUtils {
private static final String HTTP = "http";
private static final String HTTPS = "https";
- private static final int DEFUALT_TIMEOUT = 30000;
private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
private static PoolingHttpClientConnectionManager connectionManager = null;
private static SSLContextBuilder sslContextBuilder = null;
+ public static final int DEFUALT_TIMEOUT = 30000;
static{
try {
}
});
sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(),
- new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null,
+ new String[]{"SSLv3", "TLSv1", "TLSv1.2"}, null,
NoopHostnameVerifier.INSTANCE);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register(HTTP, new PlainConnectionSocketFactory())
}
}
- public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity) throws CorrelationException {
- return post(url, header, param, entity, DEFUALT_TIMEOUT);
+ public static HttpResponse get(HttpGet httpGet, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpGet, header, httpClient);
}
- public static HttpResponse post(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpPost httpPost = new HttpPost(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpPost);
- addParams(param, httpPost);
- if (entity != null) {
- httpPost.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPost);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through POST method!");
- }
- return response;
- }
-
- public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity) throws CorrelationException {
- return put(url, header, param, entity, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse put(String url, Map<String, String> header, Map<String, String> param,
- HttpEntity entity, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpPut httpPut = new HttpPut(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpPut);
- addParams(param, httpPut);
- if (entity != null) {
- httpPut.setEntity(entity);
- }
- response = executeRequest(httpClient, httpPut);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through PUT method!");
- }
- return response;
- }
-
- public static HttpResponse get(String url, Map<String, String> header) throws CorrelationException {
- return get(url, header, DEFUALT_TIMEOUT);
+ public static HttpResponse post(HttpPost httpPost, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPost, header, param, entity, httpClient);
}
- public static HttpResponse get(String url, Map<String, String> header, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpGet httpGet = new HttpGet(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpGet);
- response = executeRequest(httpClient, httpGet);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through GET method!");
- }
- return response;
+ public static HttpResponse put(HttpPut httpPut, Map<String, String> header, Map<String, String> param,
+ HttpEntity entity, CloseableHttpClient httpClient) throws CorrelationException {
+ return getPostAndPutResponse(httpPut, header, param, entity, httpClient);
}
- public static HttpResponse delete(String url, Map<String, String> header) throws CorrelationException {
- return delete(url, header, DEFUALT_TIMEOUT);
- }
-
- public static HttpResponse delete(String url, Map<String, String> header, int timeout) throws CorrelationException {
- HttpResponse response;
- HttpDelete httpDelete = new HttpDelete(url);
- try {
- CloseableHttpClient httpClient = getHttpClient(timeout);
- addHeaders(header, httpDelete);
- response = executeRequest(httpClient, httpDelete);
- } catch (Exception e) {
- throw new CorrelationException("Failed to query data from server through DELETE method!");
- }
- return response;
+ public static HttpResponse delete(HttpDelete httpDelete, Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ return getGetAndDeleteResponse(httpDelete, header, httpClient);
}
private static void addParams(Map<String, String> param, HttpEntityEnclosingRequestBase requestBase) {
return httpRequestBase;
}
+ private static HttpResponse getPostAndPutResponse(HttpEntityEnclosingRequestBase requestBase,
+ Map<String, String> header, Map<String, String> param, HttpEntity entity,
+ CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ addParams(param, requestBase);
+ if (entity != null) {
+ requestBase.setEntity(entity);
+ }
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
+ private static HttpResponse getGetAndDeleteResponse(HttpRequestBase requestBase,
+ Map<String, String> header, CloseableHttpClient httpClient) throws CorrelationException {
+ try {
+ addHeaders(header, requestBase);
+ return executeRequest(httpClient, requestBase);
+ } catch (Exception e) {
+ throw new CorrelationException("Failed to connect to server", e);
+ }
+ }
+
public static String extractResponseEntity(HttpResponse httpResponse)
throws CorrelationException, IOException {
String result = "";
HttpEntity resEntity = httpResponse.getEntity();
result = EntityUtils.toString(resEntity);
} else {
- throw new CorrelationException("Get a error status from server : " + statusCode);
+ throw new CorrelationException("Get an error status from server : " + statusCode);
}
}
return result;
}
private static HttpResponse executeRequest(CloseableHttpClient httpClient, HttpRequestBase httpRequest)
- throws Exception {
+ throws CorrelationException, IOException {
HttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpRequest);
} catch (Exception e) {
- throw new CorrelationException("Failed to get data from server");
- } finally {
- if (httpRequest != null) {
- httpRequest.releaseConnection();
- }
- if (httpClient != null) {
- httpClient.close();
- }
+ throw new CorrelationException("Failed to get data from server" ,e);
}
return httpResponse;
}
- private static CloseableHttpClient getHttpClient(int timeout) throws Exception {
+ public static CloseableHttpClient getHttpClient(int timeout) {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(timeout)
.setConnectTimeout(timeout)