Skip to content

Commit

Permalink
Switch OkHttpConnector to OkHttpGitHubConnector
Browse files Browse the repository at this point in the history
To preserve backward compatibility, OkHttpConnector must continue to be based on HttpConnector.
  • Loading branch information
bitwiseman committed Nov 9, 2021
1 parent e9abaa7 commit 1031179
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 135 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@
</limit>
</limits>
<excludes>
<!-- Code implemented externally -->
<exclude>org.kohsuke.github.extras.okhttp3.ObsoleteUrlFactory.**</exclude>
<exclude>org.kohsuke.github.extras.okhttp3.ObsoleteUrlFactory</exclude>

<!-- Sample only -->
<exclude>org.kohsuke.github.example.*</exclude>


<!-- Deprecated -->
<exclude>org.kohsuke.github.extras.OkHttpConnector</exclude>
<exclude>org.kohsuke.github.extras.okhttp3.OkHttpConnector</exclude>
<exclude>org.kohsuke.github.extras.OkHttp3Connector</exclude>
<exclude>org.kohsuke.github.extras.OkHttp3Connector</exclude>
<exclude>org.kohsuke.github.EnforcementLevel</exclude>
<exclude>org.kohsuke.github.GHPerson.1</exclude>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/github/extras/OkHttp3Connector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import okhttp3.OkHttpClient;
import okhttp3.OkUrlFactory;
import org.kohsuke.github.HttpConnector;
import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector;

import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -17,7 +18,7 @@
*
* @author Roberto Tyley
* @author Kohsuke Kawaguchi
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
* @see OkHttpGitHubConnector
*/
@Deprecated
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Deprecated")
Expand All @@ -31,7 +32,7 @@ public class OkHttp3Connector implements HttpConnector {
* the url factory
*/
/*
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
* @see org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector
*/
@Deprecated
public OkHttp3Connector(OkUrlFactory urlFactory) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kohsuke/github/extras/OkHttpConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import org.kohsuke.github.HttpConnector;
import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector;

import java.io.IOException;
import java.net.HttpURLConnection;
Expand All @@ -27,8 +28,8 @@
* @author Roberto Tyley
* @author Kohsuke Kawaguchi
* @deprecated This class depends on an unsupported version of OkHttp. Switch to
* {@link org.kohsuke.github.extras.okhttp3.OkHttpConnector}.
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
* {@link OkHttpGitHubConnector}.
* @see OkHttpGitHubConnector
*/
@Deprecated
public class OkHttpConnector implements HttpConnector {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* code will be shorter, easier to read, and you'll be able to use interceptors.
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP", "EI_EXPOSE_REP2" }, justification = "Deprecated external code")
@Deprecated
public final class ObsoleteUrlFactory implements URLStreamHandlerFactory, Cloneable {
static final String SELECTED_PROTOCOL = "ObsoleteUrlFactory-Selected-Protocol";

Expand Down
131 changes: 16 additions & 115 deletions src/main/java/org/kohsuke/github/extras/okhttp3/OkHttpConnector.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
package org.kohsuke.github.extras.okhttp3;

import okhttp3.*;
import org.apache.commons.io.IOUtils;
import org.kohsuke.github.*;
import org.kohsuke.github.connector.GitHubConnector;
import org.kohsuke.github.connector.GitHubConnectorRequest;
import org.kohsuke.github.connector.GitHubConnectorResponse;
import okhttp3.CacheControl;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import org.kohsuke.github.HttpConnector;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;

import javax.annotation.Nonnull;

import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.util.logging.Level.FINER;

/**
* {@link HttpConnector} for {@link OkHttpClient}.
Expand All @@ -31,12 +20,15 @@
*
* @author Liam Newman
* @author Kohsuke Kawaguchi
* @deprecated Use OkHttpGitHubConnector instead.
*/
public class OkHttpConnector implements GitHubConnector {
@Deprecated
public class OkHttpConnector implements HttpConnector {
private static final String HEADER_NAME = "Cache-Control";
private final String maxAgeHeaderValue;

private final OkHttpClient client;
private final ObsoleteUrlFactory urlFactory;

/**
* Instantiates a new Ok http connector.
Expand Down Expand Up @@ -67,117 +59,26 @@ public OkHttpConnector(OkHttpClient client, int cacheMaxAge) {
} else {
maxAgeHeaderValue = null;
}
this.urlFactory = new ObsoleteUrlFactory(this.client);
}

@Override
public GitHubConnectorResponse send(GitHubConnectorRequest request) throws IOException {
Request.Builder builder = new Request.Builder().url(request.url());
if (maxAgeHeaderValue != null && request.header(HEADER_NAME) == null) {
public HttpURLConnection connect(URL url) throws IOException {
HttpURLConnection urlConnection = urlFactory.open(url);
if (maxAgeHeaderValue != null) {
// By default OkHttp honors max-age, meaning it will use local cache
// without checking the network within that timeframe.
// However, that can result in stale data being returned during that time so
// we force network-based checking no matter how often the query is made.
// OkHttp still automatically does ETag checking and returns cached data when
// GitHub reports 304, but those do not count against rate limit.
builder.header(HEADER_NAME, maxAgeHeaderValue);
}

for (Map.Entry<String, List<String>> e : request.allHeaders().entrySet()) {
List<String> v = e.getValue();
if (v != null) {
builder.addHeader(e.getKey(), String.join(", ", v));
}
urlConnection.setRequestProperty(HEADER_NAME, maxAgeHeaderValue);
}

RequestBody body = null;
if (request.hasBody()) {
body = RequestBody.create(IOUtils.toByteArray(request.body()));
}
builder.method(request.method(), body);
Request okhttpRequest = builder.build();
Response okhttpResponse = client.newCall(okhttpRequest).execute();

return new OkHttpGitHubConnectorResponse(request, okhttpResponse);
return urlConnection;
}

/** Returns connection spec with TLS v1.2 in it */
private List<ConnectionSpec> TlsConnectionSpecs() {
return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
}

/**
* Initial response information when a response is initially received and before the body is processed.
*
* Implementation specific to {@link okhttp3.Response}.
*/
static class OkHttpGitHubConnectorResponse extends GitHubConnectorResponse {

@Nonnull
private final Response response;

OkHttpGitHubConnectorResponse(@Nonnull GitHubConnectorRequest request, @Nonnull Response response) {
super(request, response.code(), response.headers().toMultimap());
this.response = response;
}

/**
* {@inheritDoc}
*/
public InputStream bodyStream() throws IOException {
if (response.code() >= HTTP_BAD_REQUEST) {
if (response.code() == HTTP_NOT_FOUND) {
throw new FileNotFoundException(request().url().toString());
} else {
throw new HttpException(errorMessage(),
response.code(),
response.message(),
request().url().toString());
}
}

ResponseBody body = response.body();
InputStream bytes = body != null ? body.byteStream() : null;
return wrapStream(bytes);
}

/**
* {@inheritDoc}
*/
public String errorMessage() {
String result = null;
try {
if (!response.isSuccessful()) {
ResponseBody body = response.body();
result = body != null ? body.string() : null;
}
} catch (Exception e) {
LOGGER.log(FINER, "Ignored exception get error message", e);
}
return result;
}

/**
* Handles the "Content-Encoding" header.
*
* @param stream
* the stream to possibly wrap
*
*/
private InputStream wrapStream(InputStream stream) throws IOException {
String encoding = header("Content-Encoding");
if (encoding == null || stream == null)
return stream;
if (encoding.equals("gzip"))
return new GZIPInputStream(stream);

throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
}

@Override
public void close() throws IOException {
response.close();
}

private static final Logger LOGGER = Logger.getLogger(OkHttpConnector.class.getName());
}
}
Loading

0 comments on commit 1031179

Please sign in to comment.