Skip to content

Commit

Permalink
0005567: Added and renamed methods in HttpConnection classes
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-miller-jumpmind committed Nov 4, 2022
1 parent 940e4f5 commit bfd90f4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static synchronized void initHttps(final String httpSslVerifiedServerName
if (!StringUtils.isBlank(httpSslVerifiedServerNames)) {
HostnameVerifier hostnameVerifier = new SimpleHostnameVerifier(httpSslVerifiedServerNames);
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
Http2Connection.setHostnameVerifier(hostnameVerifier);
Http2Connection.setDefaultHostnameVerifier(hostnameVerifier);
}
if (allowSelfSignedCerts) {
initSelfSignedSocketFactory(enableHttps2);
Expand Down Expand Up @@ -165,7 +165,7 @@ private static void initSelfSignedSocketFactory(boolean enableHttps2)
context.init(keyManagers, new TrustManager[] { trustManager }, new SecureRandom());
SSLSocketFactory sslSocketFactory = context.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
Http2Connection.setSslSocketFactory(sslSocketFactory);
Http2Connection.setTrustManager(trustManager);
Http2Connection.setDefaultSslSocketFactory(sslSocketFactory);
Http2Connection.setDefaultTrustManager(trustManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,32 @@ public static X509TrustManager getTrustManager() {
return trustManager;
}

public static void setTrustManager(X509TrustManager trustManager) {
public static void setDefaultTrustManager(X509TrustManager trustManager) {
Http2Connection.trustManager = trustManager;
}

public static SSLSocketFactory getSslSocketFactory() {
return sslSocketFactory;
}

public static void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
public void setSslSocketFactoryAndTrustManager(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager) {
clientBuilder.sslSocketFactory(sslSocketFactory, trustManager);
}

public static void setDefaultSslSocketFactory(SSLSocketFactory sslSocketFactory) {
Http2Connection.sslSocketFactory = sslSocketFactory;
}

public static HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}

public static void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
@Override
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
clientBuilder.hostnameVerifier(hostnameVerifier);
}

public static void setDefaultHostnameVerifier(HostnameVerifier hostnameVerifier) {
Http2Connection.hostnameVerifier = hostnameVerifier;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.security.cert.Certificate;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocketFactory;

public class HttpConnection implements Closeable {
public static final int HTTP_OK = HttpURLConnection.HTTP_OK;
public static final int HTTP_NOT_MODIFIED = HttpURLConnection.HTTP_NOT_MODIFIED;
Expand Down Expand Up @@ -120,4 +126,28 @@ public void setRequestMethod(String method) throws ProtocolException {
public int getResponseCode() throws IOException {
return conn.getResponseCode();
}

public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setHostnameVerifier(hostnameVerifier);
}
}

public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
if (conn instanceof HttpsURLConnection) {
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
}
}

public Certificate[] getServerCertificates() {
if (conn instanceof HttpsURLConnection) {
try {
conn.connect();
return ((HttpsURLConnection) conn).getServerCertificates();
} catch (SSLPeerUnverifiedException e) {
} catch (IOException e) {
}
}
return new Certificate[] {};
}
}

0 comments on commit bfd90f4

Please sign in to comment.