5252import net .sf .json .JSONObject ;
5353
5454public class JenkinsHttpClient {
55- private final Logger LOGGER = LoggerFactory .getLogger ( getClass () );
55+ private final Logger LOGGER = LoggerFactory .getLogger (getClass ());
5656
5757 private URI uri ;
5858 private CloseableHttpClient client ;
@@ -62,16 +62,14 @@ public class JenkinsHttpClient {
6262
6363 private ObjectMapper mapper ;
6464 private String context ;
65-
65+
6666 private String jenkinsVersion ;
6767
6868 /**
6969 * Create an unauthenticated Jenkins HTTP client
7070 *
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
7573 */
7674 public JenkinsHttpClient (URI uri , CloseableHttpClient client ) {
7775 this .context = uri .getPath ();
@@ -90,10 +88,8 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) {
9088 /**
9189 * Create an unauthenticated Jenkins HTTP client
9290 *
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
9793 */
9894 public JenkinsHttpClient (URI uri , HttpClientBuilder builder ) {
9995 this (uri , builder .build ());
@@ -102,8 +98,7 @@ public JenkinsHttpClient(URI uri, HttpClientBuilder builder) {
10298 /**
10399 * Create an unauthenticated Jenkins HTTP client
104100 *
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)
107102 */
108103 public JenkinsHttpClient (URI uri ) {
109104 this (uri , HttpClientBuilder .create ());
@@ -112,12 +107,9 @@ public JenkinsHttpClient(URI uri) {
112107 /**
113108 * Create an authenticated Jenkins HTTP client
114109 *
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
121113 */
122114 public JenkinsHttpClient (URI uri , String username , String password ) {
123115 this (uri , addAuthentication (HttpClientBuilder .create (), uri , username , password ));
@@ -130,19 +122,15 @@ public JenkinsHttpClient(URI uri, String username, String password) {
130122 /**
131123 * Perform a GET request and parse the response to the given class
132124 *
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
139128 * @return an instance of the supplied class
140- * @throws IOException,
141- * HttpResponseException
129+ * @throws IOException in case of an error.
142130 */
143131 public <T extends BaseModel > T get (String path , Class <T > cls ) throws IOException {
144132 HttpGet getMethod = new HttpGet (api (path ));
145-
133+
146134 HttpResponse response = client .execute (getMethod , localContext );
147135 getJenkinsVersionFromHeader (response );
148136 try {
@@ -158,17 +146,16 @@ public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException
158146 * Perform a GET request and parse the response and return a simple string
159147 * of the content
160148 *
161- * @param path
162- * path to request, can be relative or absolute
149+ * @param path path to request, can be relative or absolute
163150 * @return the entity text
164- * @throws IOException,
165- * HttpResponseException
151+ * @throws IOException in case of an error.
166152 */
167153 public String get (String path ) throws IOException {
168154 HttpGet getMethod = new HttpGet (api (path ));
169155 HttpResponse response = client .execute (getMethod , localContext );
170156 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 ());
172159 try {
173160 httpResponseValidator .validateResponse (response );
174161 return IOUtils .toString (response .getEntity ().getContent ());
@@ -183,12 +170,9 @@ public String get(String path) throws IOException {
183170 * Perform a GET request and parse the response to the given class, logging
184171 * any IOException that is thrown rather than propagating it.
185172 *
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
192176 * @return an instance of the supplied class
193177 */
194178 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) {
198182 return value ;
199183 } catch (IOException e ) {
200184 LOGGER .debug ("getQuietly({}, {})" , path , cls .getName (), e );
201- //TODO: Is returing null a good idea?
185+ // TODO: Is returing null a good idea?
202186 return null ;
203187 }
204188 }
205189
206190 /**
207191 * Perform a GET request and return the response as InputStream
208192 *
209- * @param path
210- * path to request, can be relative or absolute
193+ * @param path path to request, can be relative or absolute
211194 * @return the response stream
212- * @throws IOException,
213- * HttpResponseException
195+ * @throws IOException in case of an error.
214196 */
215197 public InputStream getFile (URI path ) throws IOException {
216198 HttpGet getMethod = new HttpGet (path );
@@ -227,19 +209,14 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls) throws
227209 /**
228210 * Perform a POST request and parse the response to the given class
229211 *
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.
240218 * @return an instance of the supplied class
241- * @throws IOException,
242- * HttpResponseException
219+ * @throws IOException in case of an error.
243220 */
244221 public <R extends BaseModel , D > R post (String path , D data , Class <R > cls , boolean crumbFlag ) throws IOException {
245222 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
293270 * call required. It is unclear if any other jenkins APIs operate in this
294271 * fashion.
295272 *
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.
302277 */
303278 public void post_form (String path , Map <String , String > data , boolean crumbFlag ) throws IOException {
304279 HttpPost request ;
@@ -341,13 +316,10 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
341316 * Perform a POST request of XML (instead of using json mapper) and return a
342317 * string rendering of the response entity.
343318 *
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
348321 * @return A string containing the xml response (if present)
349- * @throws IOException,
350- * HttpResponseException
322+ * @throws IOException in case of an error.
351323 */
352324 public String post_xml (String path , String xml_data ) throws IOException {
353325 return post_xml (path , xml_data , true );
@@ -379,11 +351,11 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
379351 /**
380352 * Post a text entity to the given URL using the default content type
381353 *
382- * @param path
383- * @param textData
384- * @param crumbFlag
354+ * @param path The path.
355+ * @param textData data.
356+ * @param crumbFlag true/false.
385357 * @return resulting response
386- * @throws IOException
358+ * @throws IOException in case of an error.
387359 */
388360 public String post_text (String path , String textData , boolean crumbFlag ) throws IOException {
389361 return post_text (path , textData , ContentType .DEFAULT_TEXT , crumbFlag );
@@ -392,11 +364,12 @@ public String post_text(String path, String textData, boolean crumbFlag) throws
392364 /**
393365 * Post a text entity to the given URL with the given content type
394366 *
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.
398371 * @return resulting response
399- * @throws IOException
372+ * @throws IOException in case of an error.
400373 */
401374 public String post_text (String path , String textData , ContentType contentType , boolean crumbFlag )
402375 throws IOException {
@@ -425,10 +398,8 @@ public String post_text(String path, String textData, ContentType contentType, b
425398 /**
426399 * Perform POST request that takes no parameters and returns no response
427400 *
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.
432403 */
433404 public void post (String path ) throws IOException {
434405 post (path , null , null , false );
@@ -458,7 +429,7 @@ private URI api(String path) {
458429 String [] components = path .split ("\\ ?" , 2 );
459430 path = urlJoin (components [0 ], "api/json" ) + "?" + components [1 ];
460431 }
461- return uri .resolve ("/" ).resolve (path .replace (" " ,"%20" ));
432+ return uri .resolve ("/" ).resolve (path .replace (" " , "%20" ));
462433 }
463434
464435 private URI noapi (String path ) {
@@ -495,7 +466,7 @@ private void getJenkinsVersionFromHeader(HttpResponse response) {
495466 Header [] headers = response .getHeaders ("X-Jenkins" );
496467 if (headers .length == 1 ) {
497468 this .jenkinsVersion = headers [0 ].getValue ();
498- }
469+ }
499470 }
500471
501472 private void releaseConnection (HttpRequestBase httpRequestBase ) {
@@ -517,10 +488,10 @@ protected static HttpClientBuilder addAuthentication(HttpClientBuilder builder,
517488 }
518489
519490 protected HttpContext getLocalContext () {
520- return localContext ;
491+ return localContext ;
521492 }
522493
523494 protected void setLocalContext (HttpContext localContext ) {
524- this .localContext = localContext ;
495+ this .localContext = localContext ;
525496 }
526497}
0 commit comments