-
Notifications
You must be signed in to change notification settings - Fork 735
/
GitHubHttpUrlConnectionClient.java
240 lines (212 loc) · 9.72 KB
/
GitHubHttpUrlConnectionClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.kohsuke.github;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import javax.annotation.Nonnull;
import static java.util.logging.Level.*;
import static org.apache.commons.lang3.StringUtils.defaultString;
/**
* A GitHub API Client for HttpUrlConnection
* <p>
* A GitHubClient can be used to send requests and retrieve their responses. GitHubClient is thread-safe and can be used
* to send multiple requests. GitHubClient also track some GitHub API information such as {@link #rateLimit()}.
* </p>
* <p>
* GitHubHttpUrlConnectionClient gets a new {@link HttpURLConnection} for each call to send.
* </p>
*/
class GitHubHttpUrlConnectionClient extends GitHubClient {
GitHubHttpUrlConnectionClient(String apiUrl,
String login,
String oauthAccessToken,
String jwtToken,
String password,
HttpConnector connector,
RateLimitHandler rateLimitHandler,
AbuseLimitHandler abuseLimitHandler,
GitHubRateLimitChecker rateLimitChecker,
Consumer<GHMyself> myselfConsumer) throws IOException {
super(apiUrl,
login,
oauthAccessToken,
jwtToken,
password,
connector,
rateLimitHandler,
abuseLimitHandler,
rateLimitChecker,
myselfConsumer);
}
@Nonnull
protected GitHubResponse.ResponseInfo getResponseInfo(GitHubRequest request) throws IOException {
HttpURLConnection connection;
try {
connection = HttpURLConnectionResponseInfo.setupConnection(this, request);
} catch (IOException e) {
// An error in here should be wrapped to bypass http exception wrapping.
throw new GHIOException(e.getMessage(), e);
}
// HttpUrlConnection is nuts. This call opens the connection and gets a response.
// Putting this on it's own line for ease of debugging if needed.
int statusCode = connection.getResponseCode();
Map<String, List<String>> headers = connection.getHeaderFields();
return new HttpURLConnectionResponseInfo(request, statusCode, headers, connection);
}
protected void handleLimitingErrors(@Nonnull GitHubResponse.ResponseInfo responseInfo) throws IOException {
if (isRateLimitResponse(responseInfo)) {
GHIOException e = new HttpException("Rate limit violation",
responseInfo.statusCode(),
responseInfo.headerField("Status"),
responseInfo.url().toString()).withResponseHeaderFields(responseInfo.headers());
rateLimitHandler.onError(e, ((HttpURLConnectionResponseInfo) responseInfo).connection);
} else if (isAbuseLimitResponse(responseInfo)) {
GHIOException e = new HttpException("Abuse limit violation",
responseInfo.statusCode(),
responseInfo.headerField("Status"),
responseInfo.url().toString()).withResponseHeaderFields(responseInfo.headers());
abuseLimitHandler.onError(e, ((HttpURLConnectionResponseInfo) responseInfo).connection);
}
}
/**
* Initial response information supplied to a {@link GitHubResponse.BodyHandler} when a response is initially
* received and before the body is processed.
*
* Implementation specific to {@link HttpURLConnection}.
*/
static class HttpURLConnectionResponseInfo extends GitHubResponse.ResponseInfo {
@Nonnull
private final HttpURLConnection connection;
HttpURLConnectionResponseInfo(@Nonnull GitHubRequest request,
int statusCode,
@Nonnull Map<String, List<String>> headers,
@Nonnull HttpURLConnection connection) {
super(request, statusCode, headers);
this.connection = connection;
}
@Nonnull
static HttpURLConnection setupConnection(@Nonnull GitHubClient client, @Nonnull GitHubRequest request)
throws IOException {
HttpURLConnection connection = client.getConnector().connect(request.url());
// if the authentication is needed but no credential is given, try it anyway (so that some calls
// that do work with anonymous access in the reduced form should still work.)
if (client.encodedAuthorization != null)
connection.setRequestProperty("Authorization", client.encodedAuthorization);
setRequestMethod(request.method(), connection);
buildRequest(request, connection);
return connection;
}
/**
* Set up the request parameters or POST payload.
*/
private static void buildRequest(GitHubRequest request, HttpURLConnection connection) throws IOException {
for (Map.Entry<String, String> e : request.headers().entrySet()) {
String v = e.getValue();
if (v != null)
connection.setRequestProperty(e.getKey(), v);
}
connection.setRequestProperty("Accept-Encoding", "gzip");
if (request.inBody()) {
connection.setDoOutput(true);
try (InputStream body = request.body()) {
if (body != null) {
connection.setRequestProperty("Content-type",
defaultString(request.contentType(), "application/x-www-form-urlencoded"));
byte[] bytes = new byte[32768];
int read;
while ((read = body.read(bytes)) != -1) {
connection.getOutputStream().write(bytes, 0, read);
}
} else {
connection.setRequestProperty("Content-type",
defaultString(request.contentType(), "application/json"));
Map<String, Object> json = new HashMap<>();
for (GitHubRequest.Entry e : request.args()) {
json.put(e.key, e.value);
}
getMappingObjectWriter().writeValue(connection.getOutputStream(), json);
}
}
}
}
private static void setRequestMethod(String method, HttpURLConnection connection) throws IOException {
try {
connection.setRequestMethod(method);
} catch (ProtocolException e) {
// JDK only allows one of the fixed set of verbs. Try to override that
try {
Field $method = HttpURLConnection.class.getDeclaredField("method");
$method.setAccessible(true);
$method.set(connection, method);
} catch (Exception x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
// sun.net.www.protocol.https.DelegatingHttpsURLConnection delegates to another HttpURLConnection
try {
Field $delegate = connection.getClass().getDeclaredField("delegate");
$delegate.setAccessible(true);
Object delegate = $delegate.get(connection);
if (delegate instanceof HttpURLConnection) {
HttpURLConnection nested = (HttpURLConnection) delegate;
setRequestMethod(method, nested);
}
} catch (NoSuchFieldException x) {
// no problem
} catch (IllegalAccessException x) {
throw (IOException) new IOException("Failed to set the custom verb").initCause(x);
}
}
if (!connection.getRequestMethod().equals(method))
throw new IllegalStateException("Failed to set the request method to " + method);
}
/**
* {@inheritDoc}
*/
InputStream bodyStream() throws IOException {
return wrapStream(connection.getInputStream());
}
/**
* {@inheritDoc}
*/
String errorMessage() {
String result = null;
InputStream stream = null;
try {
stream = connection.getErrorStream();
if (stream != null) {
result = IOUtils.toString(wrapStream(stream), StandardCharsets.UTF_8);
}
} catch (Exception e) {
LOGGER.log(FINER, "Ignored exception get error message", e);
} finally {
IOUtils.closeQuietly(stream);
}
return result;
}
/**
* Handles the "Content-Encoding" header.
*
* @param stream
* the stream to possibly wrap
*
*/
private InputStream wrapStream(InputStream stream) throws IOException {
String encoding = headerField("Content-Encoding");
if (encoding == null || stream == null)
return stream;
if (encoding.equals("gzip"))
return new GZIPInputStream(stream);
throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
}
private static final Logger LOGGER = Logger.getLogger(GitHubClient.class.getName());
}
}