|
1 | 1 | package org.kohsuke.github.extras.okhttp3; |
2 | 2 |
|
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; |
9 | 7 |
|
10 | | -import java.io.FileNotFoundException; |
11 | 8 | import java.io.IOException; |
12 | | -import java.io.InputStream; |
| 9 | +import java.net.HttpURLConnection; |
| 10 | +import java.net.URL; |
13 | 11 | import java.util.Arrays; |
14 | 12 | import java.util.List; |
15 | | -import java.util.Map; |
16 | 13 | 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; |
25 | 14 |
|
26 | 15 | /** |
27 | 16 | * {@link HttpConnector} for {@link OkHttpClient}. |
|
31 | 20 | * |
32 | 21 | * @author Liam Newman |
33 | 22 | * @author Kohsuke Kawaguchi |
| 23 | + * @deprecated Use OkHttpGitHubConnector instead. |
34 | 24 | */ |
35 | | -public class OkHttpConnector implements GitHubConnector { |
| 25 | +@Deprecated |
| 26 | +public class OkHttpConnector implements HttpConnector { |
36 | 27 | private static final String HEADER_NAME = "Cache-Control"; |
37 | 28 | private final String maxAgeHeaderValue; |
38 | 29 |
|
39 | 30 | private final OkHttpClient client; |
| 31 | + private final ObsoleteUrlFactory urlFactory; |
40 | 32 |
|
41 | 33 | /** |
42 | 34 | * Instantiates a new Ok http connector. |
@@ -67,117 +59,26 @@ public OkHttpConnector(OkHttpClient client, int cacheMaxAge) { |
67 | 59 | } else { |
68 | 60 | maxAgeHeaderValue = null; |
69 | 61 | } |
| 62 | + this.urlFactory = new ObsoleteUrlFactory(this.client); |
70 | 63 | } |
71 | 64 |
|
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) { |
76 | 68 | // By default OkHttp honors max-age, meaning it will use local cache |
77 | 69 | // without checking the network within that timeframe. |
78 | 70 | // However, that can result in stale data being returned during that time so |
79 | 71 | // we force network-based checking no matter how often the query is made. |
80 | 72 | // OkHttp still automatically does ETag checking and returns cached data when |
81 | 73 | // 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); |
90 | 75 | } |
91 | 76 |
|
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; |
101 | 78 | } |
102 | 79 |
|
103 | 80 | /** Returns connection spec with TLS v1.2 in it */ |
104 | 81 | private List<ConnectionSpec> TlsConnectionSpecs() { |
105 | 82 | return Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT); |
106 | 83 | } |
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 | | - } |
183 | 84 | } |
0 commit comments