-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathGitlabHTTPRequestor.java
449 lines (394 loc) · 16.5 KB
/
GitlabHTTPRequestor.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package org.gitlab.api.http;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.lang.reflect.Field;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.*;
import org.apache.commons.io.IOUtils;
import org.gitlab.api.AuthMethod;
import org.gitlab.api.GitlabAPI;
import org.gitlab.api.GitlabAPIException;
import org.gitlab.api.TokenType;
import static org.gitlab.api.http.Method.GET;
import static org.gitlab.api.http.Method.POST;
import static org.gitlab.api.http.Method.PUT;
/**
* Gitlab HTTP Requestor
* Responsible for handling HTTP requests to the Gitlab API
*
* @author @timols (Tim O)
*/
public class GitlabHTTPRequestor {
private static final Pattern PAGE_PATTERN = Pattern.compile("([&|?])page=(\\d+)");
private final GitlabAPI root;
private Method method = GET; // Default to GET requests
private Map<String, Object> data = new HashMap<>();
private Map<String, File> attachments = new HashMap<>();
private String apiToken;
private TokenType tokenType;
private AuthMethod authMethod;
public GitlabHTTPRequestor(GitlabAPI root) {
this.root = root;
}
/**
* Sets authentication data for the request.
* Has a fluent api for method chaining.
*
* @param token The token value
* @param type The type of the token
* @param method The authentication method
* @return this
*/
public GitlabHTTPRequestor authenticate(String token, TokenType type, AuthMethod method) {
this.apiToken = token;
this.tokenType = type;
this.authMethod = method;
return this;
}
/**
* Sets the HTTP Request method for the request.
* Has a fluent api for method chaining.
*
* @param method The HTTP method
* @return this
*/
public GitlabHTTPRequestor method(Method method) {
this.method = method;
return this;
}
/**
* Sets the HTTP Form Post parameters for the request
* Has a fluent api for method chaining
*
* @param key Form parameter Key
* @param value Form parameter Value
* @return this
*/
public GitlabHTTPRequestor with(String key, Object value) {
if (value != null && key != null) {
data.put(key, value);
}
return this;
}
/**
* Sets the HTTP Form Post parameters for the request
* Has a fluent api for method chaining
*
* @param key Form parameter Key
* @param file File data
* @return this
*/
public GitlabHTTPRequestor withAttachment(String key, File file) {
if (file != null && key != null) {
attachments.put(key, file);
}
return this;
}
public <T> T to(String tailAPIUrl, T instance) throws IOException {
return to(tailAPIUrl, null, instance);
}
public <T> T to(String tailAPIUrl, Class<T> type) throws IOException {
return to(tailAPIUrl, type, null);
}
/**
* Opens the HTTP(S) connection, submits any data and parses the response.
* Will throw an error
*
* @param <T> The return type of the method
* @param tailAPIUrl The url to open a connection to (after the host and namespace)
* @param type The type of the response to be deserialized from
* @param instance The instance to update from the response
* @return An object of type T
* @throws java.io.IOException on gitlab api error
*/
public <T> T to(String tailAPIUrl, Class<T> type, T instance) throws IOException {
HttpURLConnection connection = null;
try {
connection = setupConnection(root.getAPIUrl(tailAPIUrl));
if (hasAttachments()) {
submitAttachments(connection);
} else if (hasOutput()) {
submitData(connection);
} else if (PUT.equals(method)) {
// PUT requires Content-Length: 0 even when there is no body (eg: API for protecting a branch)
connection.setDoOutput(true);
connection.setFixedLengthStreamingMode(0);
}
try {
return parse(connection, type, instance);
} catch (IOException e) {
handleAPIError(e, connection);
}
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
public <T> List<T> getAll(final String tailUrl, final Class<T[]> type) throws IOException {
try {
List<T> results = new ArrayList<>();
Iterator<T[]> iterator = asIterator(tailUrl, type);
while (iterator.hasNext()) {
T[] requests = iterator.next();
if (requests.length > 0) {
results.addAll(Arrays.asList(requests));
}
}
return results;
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
public <T> Iterator<T> asIterator(final String tailApiUrl, final Class<T> type) {
method(GET); // Ensure we only use iterators for GET requests
// Ensure that we don't submit any data and alert the user
if (!data.isEmpty()) {
throw new IllegalStateException();
}
return new Iterator<T>() {
T next;
URL url;
{
try {
url = root.getAPIUrl(tailApiUrl);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public boolean hasNext() {
fetch();
if (next != null && next.getClass().isArray()) {
Object[] arr = (Object[]) next;
return arr.length != 0;
} else {
return next != null;
}
}
@Override
public T next() {
fetch();
T record = next;
if (record == null) {
throw new NoSuchElementException();
}
next = null;
return record;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
private void fetch() {
if (next != null) {
return;
}
if (url == null) {
return;
}
try {
HttpURLConnection connection = setupConnection(url);
try {
next = parse(connection, type, null);
assert next != null;
findNextUrl();
} catch (IOException e) {
handleAPIError(e, connection);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private void findNextUrl() throws MalformedURLException {
String url = this.url.toString();
this.url = null;
/* Increment the page number for the url if a "page" property exists,
* otherwise, add the page property and increment it.
* The Gitlab API is not a compliant hypermedia REST api, so we use
* a naive implementation.
*/
Matcher matcher = PAGE_PATTERN.matcher(url);
if (matcher.find()) {
Integer page = Integer.parseInt(matcher.group(2)) + 1;
this.url = new URL(matcher.replaceAll(matcher.group(1) + "page=" + page));
} else {
// Since the page query was not present, its safe to assume that we just
// currently used the first page, so we can default to page 2
this.url = new URL(url + (url.indexOf('?') > 0 ? '&' : '?') + "page=2");
}
}
};
}
private void submitAttachments(HttpURLConnection connection) throws IOException {
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
String charset = "UTF-8";
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
OutputStream output = connection.getOutputStream();
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true)) {
for (Map.Entry<String, Object> paramEntry : data.entrySet()) {
String paramName = paramEntry.getKey();
String param = GitlabAPI.MAPPER.writeValueAsString(paramEntry.getValue());
writer.append("--").append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"").append(paramName).append("\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=").append(charset).append(CRLF);
writer.append(CRLF).append(param).append(CRLF).flush();
}
for (Map.Entry<String, File> attachMentEntry : attachments.entrySet()) {
File binaryFile = attachMentEntry.getValue();
writer.append("--").append(boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"").append(attachMentEntry.getKey())
.append("\"; filename=\"").append(binaryFile.getName()).append("\"").append(CRLF);
writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
try (Reader fileReader = new FileReader(binaryFile)) {
IOUtils.copy(fileReader, output);
}
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end of boundary.
}
writer.append("--").append(boundary).append("--").append(CRLF).flush();
}
}
private void submitData(HttpURLConnection connection) throws IOException {
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
GitlabAPI.MAPPER.writeValue(connection.getOutputStream(), data);
}
private boolean hasAttachments() {
return !attachments.isEmpty();
}
private boolean hasOutput() {
return method.equals(POST) || method.equals(PUT) && !data.isEmpty();
}
private HttpURLConnection setupConnection(URL url) throws IOException {
if (root.isIgnoreCertificateErrors()) {
ignoreCertificateErrors();
}
if (apiToken != null && authMethod == AuthMethod.URL_PARAMETER) {
String urlWithAuth = url.toString();
urlWithAuth = urlWithAuth + (urlWithAuth.indexOf('?') > 0 ? '&' : '?') +
tokenType.getTokenParamName() + "=" + apiToken;
url = new URL(urlWithAuth);
}
HttpURLConnection connection = root.getProxy() != null ?
(HttpURLConnection) url.openConnection(root.getProxy()) : (HttpURLConnection) url.openConnection();
if (apiToken != null && authMethod == AuthMethod.HEADER) {
connection.setRequestProperty(tokenType.getTokenHeaderName(),
String.format(tokenType.getTokenHeaderFormat(), apiToken));
}
connection.setReadTimeout(root.getResponseReadTimeout());
connection.setConnectTimeout(root.getConnectionTimeout());
try {
connection.setRequestMethod(method.name());
} catch (ProtocolException e) {
// Hack in case the API uses a non-standard HTTP verb
try {
Field methodField = connection.getClass().getDeclaredField("method");
methodField.setAccessible(true);
methodField.set(connection, method.name());
} catch (Exception x) {
throw new IOException("Failed to set the custom verb", x);
}
}
connection.setRequestProperty("User-Agent", root.getUserAgent());
connection.setRequestProperty("Accept-Encoding", "gzip");
return connection;
}
private <T> T parse(HttpURLConnection connection, Class<T> type, T instance) throws IOException {
InputStreamReader reader = null;
try {
if (byte[].class == type) {
return type.cast(IOUtils.toByteArray(wrapStream(connection, connection.getInputStream())));
}
reader = new InputStreamReader(wrapStream(connection, connection.getInputStream()), "UTF-8");
String json = IOUtils.toString(reader);
if (type != null && type == String.class) {
return type.cast(json);
}
if (type != null && type != Void.class) {
return GitlabAPI.MAPPER.readValue(json, type);
} else if (instance != null) {
return GitlabAPI.MAPPER.readerForUpdating(instance).readValue(json);
} else {
return null;
}
} catch (SSLHandshakeException e) {
throw new SSLException("You can disable certificate checking by setting ignoreCertificateErrors " +
"on GitlabHTTPRequestor.", e);
} finally {
IOUtils.closeQuietly(reader);
}
}
private InputStream wrapStream(HttpURLConnection connection, InputStream inputStream) throws IOException {
String encoding = connection.getContentEncoding();
if (encoding == null || inputStream == null) {
return inputStream;
} else if (encoding.equals("gzip")) {
return new GZIPInputStream(inputStream);
} else {
throw new UnsupportedOperationException("Unexpected Content-Encoding: " + encoding);
}
}
private void handleAPIError(IOException e, HttpURLConnection connection) throws IOException {
if (e instanceof FileNotFoundException || // pass through 404 Not Found to allow the caller to handle it intelligently
e instanceof SocketTimeoutException ||
e instanceof ConnectException) {
throw e;
}
InputStream es = wrapStream(connection, connection.getErrorStream());
try {
String error = null;
if (es != null) {
error = IOUtils.toString(es, "UTF-8");
}
throw new GitlabAPIException(error, connection.getResponseCode(), e);
} finally {
IOUtils.closeQuietly(es);
}
}
private void ignoreCertificateErrors() {
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Added per https://github.com/timols/java-gitlab-api/issues/44
HostnameVerifier nullVerifier = (hostname, session) -> true;
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Added per https://github.com/timols/java-gitlab-api/issues/44
HttpsURLConnection.setDefaultHostnameVerifier(nullVerifier);
} catch (Exception ignore) {}
}
}