1717
1818import org .apache .commons .io .IOUtils ;
1919import org .apache .commons .lang .StringUtils ;
20+ import org .apache .http .Header ;
2021import org .apache .http .HttpResponse ;
2122import org .apache .http .auth .AuthScope ;
2223import org .apache .http .auth .UsernamePasswordCredentials ;
@@ -63,6 +64,8 @@ public class JenkinsHttpClient {
6364
6465 private ObjectMapper mapper ;
6566 private String context ;
67+
68+ private String jenkinsVersion ;
6669
6770 /**
6871 * Create an unauthenticated Jenkins HTTP client
@@ -82,6 +85,7 @@ public JenkinsHttpClient(URI uri, CloseableHttpClient client) {
8285 this .client = client ;
8386 this .httpResponseValidator = new HttpResponseValidator ();
8487 // this.contentExtractor = new HttpResponseContentExtractor();
88+ this .jenkinsVersion = null ;
8589 }
8690
8791 /**
@@ -153,6 +157,7 @@ public JenkinsHttpClient(URI uri, String username, String password) {
153157 public <T extends BaseModel > T get (String path , Class <T > cls ) throws IOException {
154158 HttpGet getMethod = new HttpGet (api (path ));
155159 HttpResponse response = client .execute (getMethod , localContext );
160+ getJenkinsVersionFromHeader (response );
156161 try {
157162 httpResponseValidator .validateResponse (response );
158163 return objectFromResponse (cls , response );
@@ -175,7 +180,7 @@ public <T extends BaseModel> T get(String path, Class<T> cls) throws IOException
175180 public String get (String path ) throws IOException {
176181 HttpGet getMethod = new HttpGet (api (path ));
177182 HttpResponse response = client .execute (getMethod , localContext );
178-
183+ getJenkinsVersionFromHeader ( response );
179184 try {
180185 httpResponseValidator .validateResponse (response );
181186 return IOUtils .toString (response .getEntity ().getContent ());
@@ -220,6 +225,7 @@ public <T extends BaseModel> T getQuietly(String path, Class<T> cls) {
220225 public InputStream getFile (URI path ) throws IOException {
221226 HttpGet getMethod = new HttpGet (path );
222227 HttpResponse response = client .execute (getMethod , localContext );
228+ getJenkinsVersionFromHeader (response );
223229 httpResponseValidator .validateResponse (response );
224230 return new RequestReleasingInputStream (response .getEntity ().getContent (), getMethod );
225231 }
@@ -260,6 +266,7 @@ public <R extends BaseModel, D> R post(String path, D data, Class<R> cls, boolea
260266 request .setEntity (stringEntity );
261267 }
262268 HttpResponse response = client .execute (request , localContext );
269+ getJenkinsVersionFromHeader (response );
263270
264271 try {
265272 httpResponseValidator .validateResponse (response );
@@ -330,6 +337,7 @@ public void post_form(String path, Map<String, String> data, boolean crumbFlag)
330337 }
331338
332339 HttpResponse response = client .execute (request , localContext );
340+ getJenkinsVersionFromHeader (response );
333341
334342 try {
335343 httpResponseValidator .validateResponse (response );
@@ -368,6 +376,7 @@ public String post_xml(String path, String xml_data, boolean crumbFlag) throws I
368376 request .setEntity (new StringEntity (xml_data , ContentType .create ("text/xml" , "utf-8" )));
369377 }
370378 HttpResponse response = client .execute (request , localContext );
379+ getJenkinsVersionFromHeader (response );
371380 httpResponseValidator .validateResponse (response );
372381 try {
373382 return IOUtils .toString (response .getEntity ().getContent ());
@@ -413,6 +422,7 @@ public String post_text(String path, String textData, ContentType contentType, b
413422 request .setEntity (new StringEntity (textData , contentType ));
414423 }
415424 HttpResponse response = client .execute (request , localContext );
425+ getJenkinsVersionFromHeader (response );
416426 httpResponseValidator .validateResponse (response );
417427 try {
418428 return IOUtils .toString (response .getEntity ().getContent ());
@@ -471,7 +481,6 @@ private URI noapi(String path) {
471481 private <T extends BaseModel > T objectFromResponse (Class <T > cls , HttpResponse response ) throws IOException {
472482 InputStream content = response .getEntity ().getContent ();
473483 byte [] bytes = ByteStreams .toByteArray (content );
474- String bytestring = new String (bytes );
475484 T result = mapper .readValue (bytes , cls );
476485 // TODO: original:
477486 // T result = mapper.readValue(content, cls);
@@ -485,6 +494,20 @@ private ObjectMapper getDefaultMapper() {
485494 return mapper ;
486495 }
487496
497+ /**
498+ * @return the version string.
499+ */
500+ public String getJenkinsVersion () {
501+ return this .jenkinsVersion ;
502+ }
503+
504+ private void getJenkinsVersionFromHeader (HttpResponse response ) {
505+ Header [] headers = response .getHeaders ("X-Jenkins" );
506+ if (headers .length == 1 ) {
507+ this .jenkinsVersion = headers [0 ].getValue ();
508+ }
509+ }
510+
488511 private void releaseConnection (HttpRequestBase httpRequestBase ) {
489512 httpRequestBase .releaseConnection ();
490513 }
0 commit comments