Created
April 6, 2017 15:52
-
-
Save ptaylor/fb5a3abce5c455fbec87f6bfd6386814 to your computer and use it in GitHub Desktop.
Groovy HTTPBuilder supporting TLS 1.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovyx.net.http.HTTPBuilder | |
import org.apache.http.client.HttpClient | |
import org.apache.http.impl.client.DefaultHttpClient | |
import org.apache.http.impl.conn.PoolingClientConnectionManager | |
import org.apache.http.impl.conn.SchemeRegistryFactory | |
import org.apache.http.params.HttpParams | |
import javax.net.ssl.SSLContext | |
import javax.net.ssl.SSLSocket | |
import java.security.SecureRandom | |
/** | |
* HTTPBuilder that allows the list of supported TLS protocols to be specified. | |
*/ | |
class TlsHttpBuilder extends HTTPBuilder { | |
List sslProtocols | |
TlsHttpBuilder(List sslProtocols) { | |
super() | |
this.sslProtocols = sslProtocols | |
} | |
protected HttpClient createClient(HttpParams params) { | |
def sslContext = SSLContext.getInstance("TLS") | |
sslContext.init(null, null, new SecureRandom()) | |
def sf = new org.apache.http.conn.ssl.SSLSocketFactory(sslContext) { | |
protected void prepareSocket(final SSLSocket socket) throws IOException { | |
if (sslProtocols) { | |
log.debug("Setting protocols: ${sslProtocols}") | |
socket.setEnabledProtocols(sslProtocols as String[]) | |
} | |
} | |
} | |
def schemeRegistry = SchemeRegistryFactory.createDefault() | |
schemeRegistry.register(new org.apache.http.conn.scheme.Scheme("https", sf, 443)) | |
new DefaultHttpClient(new PoolingClientConnectionManager(schemeRegistry), params) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
TlsHttpBuilder tlsHttpBuilder = new TlsHttpBuilder(['TLSv1', 'TLSv1.1', 'TLSv1.2'])