Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MandrillRequestDispatcher.java -->TLSv1.2 #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
*/
package com.microtripit.mandrillapp.lutung.model;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

Expand All @@ -19,13 +23,16 @@
import com.microtripit.mandrillapp.lutung.model.MandrillApiError.MandrillError;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.net.ssl.SSLContext;

/**
* @author rschreijer
* @since Feb 21, 2013
Expand Down Expand Up @@ -53,18 +60,37 @@ public final class MandrillRequestDispatcher {
private static CloseableHttpClient httpClient;
private static PoolingHttpClientConnectionManager connexionManager;
private static RequestConfig defaultRequestConfig;

private static SSLContext sslContext;
private static SSLConnectionSocketFactory sslSocketFactory;
private static Registry<ConnectionSocketFactory> socketFactoryRegistry;

static {
connexionManager = new PoolingHttpClientConnectionManager();
connexionManager.setDefaultMaxPerRoute(50);
defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(SOCKET_TIMEOUT_MILLIS)
.setConnectTimeout(CONNECTION_TIMEOUT_MILLIS)
.setConnectionRequestTimeout(CONNECTION_TIMEOUT_MILLIS).build();
httpClient = HttpClients.custom().setUserAgent("/Lutung-0.1")

sslContext = createSSLContext();
sslSocketFactory = new SSLConnectionSocketFactory(
sslContext,
new String[] { "TLSv1.2" },
null,
new DefaultHostnameVerifier());

socketFactoryRegistry = RegistryBuilder
.<ConnectionSocketFactory> create().register("https", sslSocketFactory)
.build();

connexionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connexionManager.setDefaultMaxPerRoute(50);


httpClient = HttpClientBuilder.create()
.setUserAgent("/Lutung-0.1")
.setConnectionManager(connexionManager)
.setDefaultRequestConfig(defaultRequestConfig)
.setConnectionManager(connexionManager).useSystemProperties()
.build();
.setSSLSocketFactory(sslSocketFactory)
.build();
}

public static final <T> T execute(final RequestModel<T> requestModel) throws MandrillApiError, IOException {
Expand Down Expand Up @@ -116,7 +142,6 @@ public static final <T> T execute(final RequestModel<T> requestModel) throws Man
"Unexpected http status in response: "
+status.getStatusCode()+ " ("
+status.getReasonPhrase()+ ")").withError(error);

}

} finally {
Expand Down Expand Up @@ -150,6 +175,20 @@ private static final ProxyData detectProxyServer(final String url) {
}
}

private static SSLContext createSSLContext() {
SSLContext sslContext = null;
try{
sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sslContext;
}

private static final class ProxyData {
String host;
int port;
Expand Down