From 6594cf571a8d33efa4480775731cbbe71c3cf4bc Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Thu, 25 Apr 2019 15:23:25 -0700 Subject: [PATCH] OkHttp3 --- pom.xml | 19 +++++ .../github_branch_source/Connector.java | 28 +++++--- .../OkHttp3Connector.java | 72 +++++++++++++++++++ 3 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/github_branch_source/OkHttp3Connector.java diff --git a/pom.xml b/pom.xml index d6018824c6..e84729026f 100644 --- a/pom.xml +++ b/pom.xml @@ -26,6 +26,8 @@ 8 2.121.3 2.3.0 + 3.12.2 + 2.2.2 @@ -70,6 +72,23 @@ display-url-api 2.0 + + com.squareup.okio + okio + ${okio.version} + + + com.squareup.okhttp3 + okhttp + ${okhttp3.version} + + + com.squareup.okhttp3 + okhttp-urlconnection + ${okhttp3.version} + + + org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java index 68c69239fc..b749925987 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java @@ -35,10 +35,10 @@ import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; import com.cloudbees.plugins.credentials.domains.DomainRequirement; import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; -import com.squareup.okhttp.Cache; -import com.squareup.okhttp.CacheControl; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.OkUrlFactory; +import okhttp3.Cache; +import okhttp3.CacheControl; +import okhttp3.OkHttpClient; +import okhttp3.OkUrlFactory; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.AbortException; import hudson.Extension; @@ -86,6 +86,7 @@ import org.kohsuke.github.HttpConnector; import org.kohsuke.github.RateLimitHandler; import org.kohsuke.github.extras.OkHttpConnector; +import org.jenkinsci.plugins.github_branch_source.OkHttp3Connector; import static java.util.logging.Level.FINE; @@ -110,6 +111,8 @@ protected boolean removeEldestEntry(Map.Entry eldest) { private static final double MILLIS_PER_HOUR = TimeUnit.HOURS.toMillis(1); private static final Random ENTROPY = new Random(); private static final String SALT = Long.toHexString(ENTROPY.nextLong()); + private static final OkHttpClient baseClient = new OkHttpClient(); + private Connector() { throw new IllegalAccessError("Utility class"); @@ -370,7 +373,8 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta gb.withEndpoint(apiUrl); gb.withRateLimitHandler(CUSTOMIZED); - OkHttpClient client = new OkHttpClient().setProxy(getProxy(host)); + OkHttpClient.Builder clientBuilder = baseClient.newBuilder(); + clientBuilder.proxy(getProxy(host)); int cacheSize = GitHubSCMSource.getCacheSize(); if (cacheSize > 0) { @@ -392,14 +396,16 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta } if (cacheDir != null) { Cache cache = new Cache(cacheDir, cacheSize * 1024L * 1024L); - client.setCache(cache); + clientBuilder.cache(cache); } } - if (client.getCache() != null) { + OkHttpClient client = clientBuilder.build(); + + if (client.cache() != null) { gb.withConnector(new ForceValidationOkHttpConnector(new OkUrlFactory(client))); } else { - gb.withConnector(new OkHttpConnector(new OkUrlFactory(client))); + gb.withConnector(new OkHttp3Connector(new OkUrlFactory(client))); } if (username != null) { @@ -735,13 +741,13 @@ public String toString() { .build() .toString(); private static final String HEADER_NAME = "Cache-Control"; - private final OkHttpConnector delegate; + private final OkHttp3Connector delegate; public ForceValidationOkHttpConnector(OkUrlFactory okUrlFactory) { - this.delegate = new OkHttpConnector(okUrlFactory); + this.delegate = new OkHttp3Connector(okUrlFactory); } - /*package*/ OkHttpConnector getDelegate() { + /*package*/ HttpConnector getDelegate() { return delegate; } diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/OkHttp3Connector.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/OkHttp3Connector.java new file mode 100644 index 0000000000..302a5ab491 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/OkHttp3Connector.java @@ -0,0 +1,72 @@ +package org.jenkinsci.plugins.github_branch_source; + + +import okhttp3.ConnectionSpec; +import okhttp3.OkHttpClient; +import okhttp3.OkUrlFactory; + +import org.kohsuke.github.HttpConnector; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +import java.util.Arrays; +import java.util.List; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocketFactory; + + +/** + * {@link HttpConnector} for {@link OkHttpClient}. + * + * Unlike {@link #DEFAULT}, OkHttp does response caching. + * Making a conditional request against GitHubAPI and receiving a 304 + * response does not count against the rate limit. + * See http://developer.github.com/v3/#conditional-requests + * + * @author Roberto Tyley + * @author Kohsuke Kawaguchi + */ +public class OkHttp3Connector implements HttpConnector { + private final OkUrlFactory urlFactory; + + public OkHttp3Connector(OkUrlFactory urlFactory) { + OkHttpClient.Builder builder = urlFactory.client().newBuilder(); + builder.sslSocketFactory(TlsSocketFactory()); + builder.connectionSpecs(TlsConnectionSpecs()); + urlFactory.setClient(builder.build()); + + this.urlFactory = urlFactory; + } + + public HttpURLConnection connect(URL url) throws IOException { + return urlFactory.open(url); + } + + /** Returns TLSv1.2 only SSL Socket Factory. */ + private SSLSocketFactory TlsSocketFactory() { + SSLContext sc; + try { + sc = SSLContext.getInstance("TLSv1.2"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e.getMessage(), e); + } + try { + sc.init(null, null, null); + return sc.getSocketFactory(); + } catch (KeyManagementException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + /** Returns connection spec with TLS v1.2 in it */ + private List TlsConnectionSpecs() { + return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT); + } + +}