52
52
import net .sf .json .JSONObject ;
53
53
54
54
public class JenkinsHttpClient {
55
- private final Logger LOGGER = LoggerFactory .getLogger ( getClass () );
55
+ private final Logger LOGGER = LoggerFactory .getLogger (getClass ());
56
56
57
57
private URI uri ;
58
58
private CloseableHttpClient client ;
@@ -62,16 +62,14 @@ public class JenkinsHttpClient {
62
62
63
63
private ObjectMapper mapper ;
64
64
private String context ;
65
-
65
+
66
66
private String jenkinsVersion ;
67
67
68
68
/**
69
69
* Create an unauthenticated Jenkins HTTP client
70
70
*
71
- * @param uri
72
- * Location of the jenkins server (ex. http://localhost:8080)
73
- * @param client
74
- * Configured CloseableHttpClient to be used
71
+ * @param uri Location of the jenkins server (ex. http://localhost:8080)
72
+ * @param client Configured CloseableHttpClient to be used
75
73
*/
76
74
public JenkinsHttpClient (URI uri , CloseableHttpClient client ) {
77
75
this .context = uri .getPath ();
@@ -90,10 +88,8 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) {
90
88
/**
91
89
* Create an unauthenticated Jenkins HTTP client
92
90
*
93
- * @param uri
94
- * Location of the jenkins server (ex. http://localhost:8080)
95
- * @param builder
96
- * Configured HttpClientBuilder to be used
91
+ * @param uri Location of the jenkins server (ex. http://localhost:8080)
92
+ * @param builder Configured HttpClientBuilder to be used
97
93
*/
98
94
public JenkinsHttpClient (URI uri , HttpClientBuilder builder ) {
99
95
this (uri , builder .build ());
@@ -102,8 +98,7 @@ public JenkinsHttpClient(URI uri, HttpClientBuilder builder) {
102
98
/**
103
99
* Create an unauthenticated Jenkins HTTP client
104
100
*
105
- * @param uri
106
- * Location of the jenkins server (ex. http://localhost:8080)
101
+ * @param uri Location of the jenkins server (ex. http://localhost:8080)
107
102
*/
108
103
public JenkinsHttpClient (URI uri ) {
109
104
this (uri , HttpClientBuilder .create ());
@@ -112,12 +107,9 @@ public JenkinsHttpClient(URI uri) {
112
107
/**
113
108
* Create an authenticated Jenkins HTTP client
114
109
*
115
- * @param uri
116
- * Location of the jenkins server (ex. http://localhost:8080)
117
- * @param username
118
- * Username to use when connecting
119
- * @param password
120
- * Password or auth token to use when connecting
110
+ * @param uri Location of the jenkins server (ex. http://localhost:8080)
111
+ * @param username Username to use when connecting
112
+ * @param password Password or auth token to use when connecting
121
113
*/
122
114
public JenkinsHttpClient (URI uri , String username , String password ) {
123
115
this (uri , addAuthentication (HttpClientBuilder .create (), uri , username , password ));
@@ -130,19 +122,15 @@ public JenkinsHttpClient(URI uri, String username, String password) {
130
122
/**
131
123
* Perform a GET request and parse the response to the given class
132
124
*
133
- * @param path
134
- * path to request, can be relative or absolute
135
- * @param cls
136
- * class of the response
137
- * @param <T>
138
- * type of the response
125
+ * @param path path to request, can be relative or absolute
126
+ * @param cls class of the response
127
+ * @param <T> type of the response
139
128
* @return an instance of the supplied class
140
- * @throws IOException,
141
- * HttpResponseException
129
+ * @throws IOException in case of an error.
142
130
*/
143
131
public <T extends BaseModel > T get (String path , Class <T > cls ) throws IOException {
144
132
HttpGet getMethod = new HttpGet (api (path ));
145
-
133
+
146
134
HttpResponse response = client .execute (getMethod , localContext );
147
135
getJenkinsVersionFromHeader (response );
148
136
try {
@@ -158,17 +146,16 @@ public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException
158
146
* Perform a GET request and parse the response and return a simple string
159
147
* of the content
160
148
*
161
- * @param path
162
- * path to request, can be relative or absolute
149
+ * @param path path to request, can be relative or absolute
163
150
* @return the entity text
164
- * @throws IOException,
165
- * HttpResponseException
151
+ * @throws IOException in case of an error.
166
152
*/
167
153
public String get (String path ) throws IOException {
168
154
HttpGet getMethod = new HttpGet (api (path ));
169
155
HttpResponse response = client .execute (getMethod , localContext );
170
156
getJenkinsVersionFromHeader (response );
171
- LOGGER .debug ("get({}), version={}, responseCode={}" , path , this .jenkinsVersion , response .getStatusLine ().getStatusCode ());
157
+ LOGGER .debug ("get({}), version={}, responseCode={}" , path , this .jenkinsVersion ,
158
+ response .getStatusLine ().getStatusCode ());
172
159
try {
173
160
httpResponseValidator .validateResponse (response );
174
161
return IOUtils .toString (response .getEntity ().getContent ());
@@ -183,12 +170,9 @@ public String get(String path) throws IOException {
183
170
* Perform a GET request and parse the response to the given class, logging
184
171
* any IOException that is thrown rather than propagating it.
185
172
*
186
- * @param path
187
- * path to request, can be relative or absolute
188
- * @param cls
189
- * class of the response
190
- * @param <T>
191
- * type of the response
173
+ * @param path path to request, can be relative or absolute
174
+ * @param cls class of the response
175
+ * @param <T> type of the response
192
176
* @return an instance of the supplied class
193
177
*/
194
178
public <T extends BaseModel > T getQuietly (String path , Class <T > cls ) {
@@ -198,19 +182,17 @@ public <T extends BaseModel> T getQuietly(String path, Class<T> cls) {
198
182
return value ;
199
183
} catch (IOException e ) {
200
184
LOGGER .debug ("getQuietly({}, {})" , path , cls .getName (), e );
201
- //TODO: Is returing null a good idea?
185
+ // TODO: Is returing null a good idea?
202
186
return null ;
203
187
}
204
188
}
205
189
206
190
/**
207
191
* Perform a GET request and return the response as InputStream
208
192
*
209
- * @param path
210
- * path to request, can be relative or absolute
193
+ * @param path path to request, can be relative or absolute
211
194
* @return the response stream
212
- * @throws IOException,
213
- * HttpResponseException
195
+ * @throws IOException in case of an error.
214
196
*/
215
197
public InputStream getFile (URI path ) throws IOException {
216
198
HttpGet getMethod = new HttpGet (path );
@@ -227,19 +209,14 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls) throws
227
209
/**
228
210
* Perform a POST request and parse the response to the given class
229
211
*
230
- * @param path
231
- * path to request, can be relative or absolute
232
- * @param data
233
- * data to post
234
- * @param cls
235
- * class of the response
236
- * @param <R>
237
- * type of the response
238
- * @param <D>
239
- * type of the data
212
+ * @param path path to request, can be relative or absolute
213
+ * @param data data to post
214
+ * @param cls class of the response
215
+ * @param <R> type of the response
216
+ * @param <D> type of the data
217
+ * @param crumbFlag true / false.
240
218
* @return an instance of the supplied class
241
- * @throws IOException,
242
- * HttpResponseException
219
+ * @throws IOException in case of an error.
243
220
*/
244
221
public <R extends BaseModel , D > R post (String path , D data , Class <R > cls , boolean crumbFlag ) throws IOException {
245
222
HttpPost request = new HttpPost (api (path ));
@@ -293,12 +270,10 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolea
293
270
* call required. It is unclear if any other jenkins APIs operate in this
294
271
* fashion.
295
272
*
296
- * @param path
297
- * path to request, can be relative or absolute
298
- * @param data
299
- * data to post
300
- * @throws IOException,
301
- * HttpResponseException
273
+ * @param path path to request, can be relative or absolute
274
+ * @param data data to post
275
+ * @param crumbFlag true / false.
276
+ * @throws IOException in case of an error.
302
277
*/
303
278
public void post_form (String path , Map <String , String > data , boolean crumbFlag ) throws IOException {
304
279
HttpPost request ;
@@ -341,13 +316,10 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
341
316
* Perform a POST request of XML (instead of using json mapper) and return a
342
317
* string rendering of the response entity.
343
318
*
344
- * @param path
345
- * path to request, can be relative or absolute
346
- * @param xml_data
347
- * data data to post
319
+ * @param path path to request, can be relative or absolute
320
+ * @param xml_data data data to post
348
321
* @return A string containing the xml response (if present)
349
- * @throws IOException,
350
- * HttpResponseException
322
+ * @throws IOException in case of an error.
351
323
*/
352
324
public String post_xml (String path , String xml_data ) throws IOException {
353
325
return post_xml (path , xml_data , true );
@@ -379,11 +351,11 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
379
351
/**
380
352
* Post a text entity to the given URL using the default content type
381
353
*
382
- * @param path
383
- * @param textData
384
- * @param crumbFlag
354
+ * @param path The path.
355
+ * @param textData data.
356
+ * @param crumbFlag true/false.
385
357
* @return resulting response
386
- * @throws IOException
358
+ * @throws IOException in case of an error.
387
359
*/
388
360
public String post_text (String path , String textData , boolean crumbFlag ) throws IOException {
389
361
return post_text (path , textData , ContentType .DEFAULT_TEXT , crumbFlag );
@@ -392,11 +364,12 @@ public String post_text(String path, String textData, boolean crumbFlag) throws
392
364
/**
393
365
* Post a text entity to the given URL with the given content type
394
366
*
395
- * @param path
396
- * @param textData
397
- * @param crumbFlag
367
+ * @param path The path.
368
+ * @param textData The data.
369
+ * @param contentType {@link ContentType}
370
+ * @param crumbFlag true or false.
398
371
* @return resulting response
399
- * @throws IOException
372
+ * @throws IOException in case of an error.
400
373
*/
401
374
public String post_text (String path , String textData , ContentType contentType , boolean crumbFlag )
402
375
throws IOException {
@@ -425,10 +398,8 @@ public String post_text(String path, String textData, ContentType contentType, b
425
398
/**
426
399
* Perform POST request that takes no parameters and returns no response
427
400
*
428
- * @param path
429
- * path to request
430
- * @throws IOException,
431
- * HttpResponseException
401
+ * @param path path to request
402
+ * @throws IOException in case of an error.
432
403
*/
433
404
public void post (String path ) throws IOException {
434
405
post (path , null , null , false );
@@ -458,7 +429,7 @@ private URI api(String path) {
458
429
String [] components = path .split ("\\ ?" , 2 );
459
430
path = urlJoin (components [0 ], "api/json" ) + "?" + components [1 ];
460
431
}
461
- return uri .resolve ("/" ).resolve (path .replace (" " ,"%20" ));
432
+ return uri .resolve ("/" ).resolve (path .replace (" " , "%20" ));
462
433
}
463
434
464
435
private URI noapi (String path ) {
@@ -495,7 +466,7 @@ private void getJenkinsVersionFromHeader(HttpResponse response) {
495
466
Header [] headers = response .getHeaders ("X-Jenkins" );
496
467
if (headers .length == 1 ) {
497
468
this .jenkinsVersion = headers [0 ].getValue ();
498
- }
469
+ }
499
470
}
500
471
501
472
private void releaseConnection (HttpRequestBase httpRequestBase ) {
@@ -517,10 +488,10 @@ protected static HttpClientBuilder addAuthentication(HttpClientBuilder builder,
517
488
}
518
489
519
490
protected HttpContext getLocalContext () {
520
- return localContext ;
491
+ return localContext ;
521
492
}
522
493
523
494
protected void setLocalContext (HttpContext localContext ) {
524
- this .localContext = localContext ;
495
+ this .localContext = localContext ;
525
496
}
526
497
}
0 commit comments