Skip to content

Commit ac9c0ad

Browse files
committed
Change OkHttpConnector to OkHttpGitHubConnector
To preserve backward compatibility, OkHttpConnector must continue to be based on HttpConnector.
1 parent e9abaa7 commit ac9c0ad

14 files changed

+234
-139
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,17 @@
153153
</limit>
154154
</limits>
155155
<excludes>
156+
<!-- Code implemented externally -->
157+
<exclude>org.kohsuke.github.extras.okhttp3.ObsoleteUrlFactory.**</exclude>
158+
<exclude>org.kohsuke.github.extras.okhttp3.ObsoleteUrlFactory</exclude>
159+
156160
<!-- Sample only -->
157161
<exclude>org.kohsuke.github.example.*</exclude>
158162

159163

160164
<!-- Deprecated -->
161-
<exclude>org.kohsuke.github.extras.OkHttpConnector</exclude>
165+
<exclude>org.kohsuke.github.extras.okhttp3.OkHttpConnector</exclude>
166+
<exclude>org.kohsuke.github.extras.OkHttp3Connector</exclude>
162167
<exclude>org.kohsuke.github.extras.OkHttp3Connector</exclude>
163168
<exclude>org.kohsuke.github.EnforcementLevel</exclude>
164169
<exclude>org.kohsuke.github.GHPerson.1</exclude>

src/main/java/org/kohsuke/github/extras/OkHttp3Connector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import okhttp3.OkHttpClient;
55
import okhttp3.OkUrlFactory;
66
import org.kohsuke.github.HttpConnector;
7+
import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector;
78

89
import java.io.IOException;
910
import java.net.HttpURLConnection;
@@ -17,7 +18,7 @@
1718
*
1819
* @author Roberto Tyley
1920
* @author Kohsuke Kawaguchi
20-
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
21+
* @see OkHttpGitHubConnector
2122
*/
2223
@Deprecated
2324
@SuppressFBWarnings(value = { "EI_EXPOSE_REP2" }, justification = "Deprecated")
@@ -31,7 +32,7 @@ public class OkHttp3Connector implements HttpConnector {
3132
* the url factory
3233
*/
3334
/*
34-
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
35+
* @see org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector
3536
*/
3637
@Deprecated
3738
public OkHttp3Connector(OkUrlFactory urlFactory) {

src/main/java/org/kohsuke/github/extras/OkHttpConnector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.squareup.okhttp.OkHttpClient;
66
import com.squareup.okhttp.OkUrlFactory;
77
import org.kohsuke.github.HttpConnector;
8+
import org.kohsuke.github.extras.okhttp3.OkHttpGitHubConnector;
89

910
import java.io.IOException;
1011
import java.net.HttpURLConnection;
@@ -26,9 +27,8 @@
2627
*
2728
* @author Roberto Tyley
2829
* @author Kohsuke Kawaguchi
29-
* @deprecated This class depends on an unsupported version of OkHttp. Switch to
30-
* {@link org.kohsuke.github.extras.okhttp3.OkHttpConnector}.
31-
* @see org.kohsuke.github.extras.okhttp3.OkHttpConnector
30+
* @deprecated This class depends on an unsupported version of OkHttp. Switch to {@link OkHttpGitHubConnector}.
31+
* @see OkHttpGitHubConnector
3232
*/
3333
@Deprecated
3434
public class OkHttpConnector implements HttpConnector {

src/main/java/org/kohsuke/github/extras/okhttp3/ObsoleteUrlFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
* code will be shorter, easier to read, and you'll be able to use interceptors.
8181
*/
8282
@SuppressFBWarnings(value = { "EI_EXPOSE_REP", "EI_EXPOSE_REP2" }, justification = "Deprecated external code")
83+
@Deprecated
8384
public final class ObsoleteUrlFactory implements URLStreamHandlerFactory, Cloneable {
8485
static final String SELECTED_PROTOCOL = "ObsoleteUrlFactory-Selected-Protocol";
8586

Lines changed: 16 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
package org.kohsuke.github.extras.okhttp3;
22

3-
import okhttp3.*;
4-
import org.apache.commons.io.IOUtils;
5-
import org.kohsuke.github.*;
6-
import org.kohsuke.github.connector.GitHubConnector;
7-
import org.kohsuke.github.connector.GitHubConnectorRequest;
8-
import org.kohsuke.github.connector.GitHubConnectorResponse;
3+
import okhttp3.CacheControl;
4+
import okhttp3.ConnectionSpec;
5+
import okhttp3.OkHttpClient;
6+
import org.kohsuke.github.HttpConnector;
97

10-
import java.io.FileNotFoundException;
118
import java.io.IOException;
12-
import java.io.InputStream;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
1311
import java.util.Arrays;
1412
import java.util.List;
15-
import java.util.Map;
1613
import java.util.concurrent.TimeUnit;
17-
import java.util.logging.Logger;
18-
import java.util.zip.GZIPInputStream;
19-
20-
import javax.annotation.Nonnull;
21-
22-
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
23-
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
24-
import static java.util.logging.Level.FINER;
2514

2615
/**
2716
* {@link HttpConnector} for {@link OkHttpClient}.
@@ -31,12 +20,15 @@
3120
*
3221
* @author Liam Newman
3322
* @author Kohsuke Kawaguchi
23+
* @deprecated Use OkHttpGitHubConnector instead.
3424
*/
35-
public class OkHttpConnector implements GitHubConnector {
25+
@Deprecated
26+
public class OkHttpConnector implements HttpConnector {
3627
private static final String HEADER_NAME = "Cache-Control";
3728
private final String maxAgeHeaderValue;
3829

3930
private final OkHttpClient client;
31+
private final ObsoleteUrlFactory urlFactory;
4032

4133
/**
4234
* Instantiates a new Ok http connector.
@@ -67,117 +59,26 @@ public OkHttpConnector(OkHttpClient client, int cacheMaxAge) {
6759
} else {
6860
maxAgeHeaderValue = null;
6961
}
62+
this.urlFactory = new ObsoleteUrlFactory(this.client);
7063
}
7164

72-
@Override
73-
public GitHubConnectorResponse send(GitHubConnectorRequest request) throws IOException {
74-
Request.Builder builder = new Request.Builder().url(request.url());
75-
if (maxAgeHeaderValue != null && request.header(HEADER_NAME) == null) {
65+
public HttpURLConnection connect(URL url) throws IOException {
66+
HttpURLConnection urlConnection = urlFactory.open(url);
67+
if (maxAgeHeaderValue != null) {
7668
// By default OkHttp honors max-age, meaning it will use local cache
7769
// without checking the network within that timeframe.
7870
// However, that can result in stale data being returned during that time so
7971
// we force network-based checking no matter how often the query is made.
8072
// OkHttp still automatically does ETag checking and returns cached data when
8173
// GitHub reports 304, but those do not count against rate limit.
82-
builder.header(HEADER_NAME, maxAgeHeaderValue);
83-
}
84-
85-
for (Map.Entry<String, List<String>> e : request.allHeaders().entrySet()) {
86-
List<String> v = e.getValue();
87-
if (v != null) {
88-
builder.addHeader(e.getKey(), String.join(", ", v));
89-
}
74+
urlConnection.setRequestProperty(HEADER_NAME, maxAgeHeaderValue);
9075
}
9176

92-
RequestBody body = null;
93-
if (request.hasBody()) {
94-
body = RequestBody.create(IOUtils.toByteArray(request.body()));
95-
}
96-
builder.method(request.method(), body);
97-
Request okhttpRequest = builder.build();
98-
Response okhttpResponse = client.newCall(okhttpRequest).execute();
99-
100-
return new OkHttpGitHubConnectorResponse(request, okhttpResponse);
77+
return urlConnection;
10178
}
10279

10380
/** Returns connection spec with TLS v1.2 in it */
10481
private List<ConnectionSpec> TlsConnectionSpecs() {
10582
return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT);
10683
}
107-
108-
/**
109-
* Initial response information when a response is initially received and before the body is processed.
110-
*
111-
* Implementation specific to {@link okhttp3.Response}.
112-
*/
113-
static class OkHttpGitHubConnectorResponse extends GitHubConnectorResponse {
114-
115-
@Nonnull
116-
private final Response response;
117-
118-
OkHttpGitHubConnectorResponse(@Nonnull GitHubConnectorRequest request, @Nonnull Response response) {
119-
super(request, response.code(), response.headers().toMultimap());
120-
this.response = response;
121-
}
122-
123-
/**
124-
* {@inheritDoc}
125-
*/
126-
public InputStream bodyStream() throws IOException {
127-
if (response.code() >= HTTP_BAD_REQUEST) {
128-
if (response.code() == HTTP_NOT_FOUND) {
129-
throw new FileNotFoundException(request().url().toString());
130-
} else {
131-
throw new HttpException(errorMessage(),
132-
response.code(),
133-
response.message(),
134-
request().url().toString());
135-
}
136-
}
137-
138-
ResponseBody body = response.body();
139-
InputStream bytes = body != null ? body.byteStream() : null;
140-
return wrapStream(bytes);
141-
}
142-
143-
/**
144-
* {@inheritDoc}
145-
*/
146-
public String errorMessage() {
147-
String result = null;
148-
try {
149-
if (!response.isSuccessful()) {
150-
ResponseBody body = response.body();
151-
result = body != null ? body.string() : null;
152-
}
153-
} catch (Exception e) {
154-
LOGGER.log(FINER, "Ignored exception get error message", e);
155-
}
156-
return result;
157-
}
158-
159-
/**
160-
* Handles the "Content-Encoding" header.
161-
*
162-
* @param stream
163-
* the stream to possibly wrap
164-
*
165-
*/
166-
private InputStream wrapStream(InputStream stream) throws IOException {
167-
String encoding = header("Content-Encoding");
168-
if (encoding == null || stream == null)
169-
return stream;
170-
if (encoding.equals("gzip"))
171-
return new GZIPInputStream(stream);
172-
173-
throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
174-
}
175-
176-
@Override
177-
public void close() throws IOException {
178-
response.close();
179-
}
180-
181-
private static final Logger LOGGER = Logger.getLogger(OkHttpConnector.class.getName());
182-
}
18384
}

0 commit comments

Comments
 (0)