Skip to content

Commit

Permalink
OkHttp3
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwiseman committed May 6, 2019
1 parent d6bc799 commit 6594cf5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 11 deletions.
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
<java.level>8</java.level>
<jenkins.version>2.121.3</jenkins.version>
<scm-api.version>2.3.0</scm-api.version>
<okhttp3.version>3.12.2</okhttp3.version>
<okio.version>2.2.2</okio.version>
</properties>

<scm>
Expand Down Expand Up @@ -70,6 +72,23 @@
<artifactId>display-url-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>${okio.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp3.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp-urlconnection</artifactId>
<version>${okhttp3.version}</version>
</dependency>


<!-- Currently just here for interactive testing via hpi:run: -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -110,6 +111,8 @@ protected boolean removeEldestEntry(Map.Entry<String,Long> 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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ConnectionSpec> TlsConnectionSpecs() {
return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
}

}

0 comments on commit 6594cf5

Please sign in to comment.