26
26
import java .io .IOException ;
27
27
import java .io .InputStream ;
28
28
import java .io .UnsupportedEncodingException ;
29
+ import java .net .InetSocketAddress ;
30
+ import java .net .Proxy ;
29
31
import java .net .URLEncoder ;
30
32
import java .util .ArrayList ;
31
33
import java .util .Collections ;
32
34
import java .util .List ;
33
35
import java .util .logging .Level ;
34
36
import java .util .logging .Logger ;
35
37
38
+ import org .apache .commons .httpclient .HostConfiguration ;
36
39
import org .apache .commons .httpclient .HttpClient ;
37
40
import org .apache .commons .httpclient .HttpException ;
38
41
import org .apache .commons .httpclient .HttpStatus ;
42
+ import org .apache .commons .httpclient .HttpMethod ;
43
+ import org .apache .commons .httpclient .HttpState ;
39
44
import org .apache .commons .httpclient .MultiThreadedHttpConnectionManager ;
40
45
import org .apache .commons .httpclient .NameValuePair ;
41
46
import org .apache .commons .httpclient .UsernamePasswordCredentials ;
47
+ import org .apache .commons .httpclient .URIException ;
42
48
import org .apache .commons .httpclient .auth .AuthScope ;
43
49
import org .apache .commons .httpclient .methods .DeleteMethod ;
44
50
import org .apache .commons .httpclient .methods .GetMethod ;
@@ -85,10 +91,10 @@ public class BitbucketCloudApiClient implements BitbucketApi {
85
91
private static final String V2_TEAMS_API_BASE_URL = "https://bitbucket.org/api/2.0/teams/" ;
86
92
private static final int MAX_PAGES = 100 ;
87
93
private HttpClient client ;
88
- private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
89
- private String owner ;
90
- private String repositoryName ;
91
- private UsernamePasswordCredentials credentials ;
94
+ private static final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager ();
95
+ private final String owner ;
96
+ private final String repositoryName ;
97
+ private final UsernamePasswordCredentials credentials ;
92
98
93
99
static {
94
100
connectionManager .getParams ().setDefaultMaxConnectionsPerHost (20 );
@@ -98,6 +104,8 @@ public class BitbucketCloudApiClient implements BitbucketApi {
98
104
public BitbucketCloudApiClient (String username , String password , String owner , String repositoryName ) {
99
105
if (!StringUtils .isBlank (username ) && !StringUtils .isBlank (password )) {
100
106
this .credentials = new UsernamePasswordCredentials (username , password );
107
+ } else {
108
+ this .credentials = null ;
101
109
}
102
110
this .owner = owner ;
103
111
this .repositoryName = repositoryName ;
@@ -106,6 +114,8 @@ public BitbucketCloudApiClient(String username, String password, String owner, S
106
114
public BitbucketCloudApiClient (String owner , String repositoryName , StandardUsernamePasswordCredentials creds ) {
107
115
if (creds != null ) {
108
116
this .credentials = new UsernamePasswordCredentials (creds .getUsername (), Secret .toString (creds .getPassword ()));
117
+ } else {
118
+ this .credentials = null ;
109
119
}
110
120
this .owner = owner ;
111
121
this .repositoryName = repositoryName ;
@@ -391,41 +401,62 @@ public List<BitbucketCloudRepository> getRepositories() {
391
401
}
392
402
393
403
private synchronized HttpClient getHttpClient () {
404
+ if (this .client == null ) {
405
+ HttpClient client = new HttpClient (connectionManager );
406
+ client .getParams ().setConnectionManagerTimeout (10 * 1000 );
407
+ client .getParams ().setSoTimeout (60 * 1000 );
394
408
395
- if (this .client != null ) return this .client ;
409
+ client .getState ().setCredentials (AuthScope .ANY , credentials );
410
+ client .getParams ().setAuthenticationPreemptive (true );
396
411
397
- this .client = new HttpClient (connectionManager );
398
- this .client .getParams ().setConnectionManagerTimeout (10 * 1000 );
399
- this .client .getParams ().setSoTimeout (60 * 1000 );
412
+ this .client = client ;
413
+ }
400
414
415
+ return this .client ;
416
+ }
417
+
418
+ private static int executeMethod (HttpClient client , HttpMethod method ) throws IOException {
401
419
Jenkins jenkins = Jenkins .getInstance ();
402
- ProxyConfiguration proxy = null ;
420
+ ProxyConfiguration proxyConfig = null ;
403
421
if (jenkins != null ) {
404
- proxy = jenkins .proxy ;
422
+ proxyConfig = jenkins .proxy ;
405
423
}
406
- if (proxy != null ) {
407
- LOGGER .info ("Jenkins proxy: " + proxy .name + ":" + proxy .port );
408
- this .client .getHostConfiguration ().setProxy (proxy .name , proxy .port );
409
- String username = proxy .getUserName ();
410
- String password = proxy .getPassword ();
424
+
425
+ Proxy proxy = Proxy .NO_PROXY ;
426
+ if (proxyConfig != null ) {
427
+ proxy = proxyConfig .createProxy (getMethodHost (method ));
428
+ }
429
+
430
+ if (proxy .type () != Proxy .Type .DIRECT ) {
431
+ final InetSocketAddress proxyAddress = (InetSocketAddress )proxy .address ();
432
+ LOGGER .fine ("Jenkins proxy: " + proxy .address ());
433
+
434
+ final HostConfiguration hc = new HostConfiguration (client .getHostConfiguration ());
435
+ hc .setProxy (proxyAddress .getHostString (), proxyAddress .getPort ());
436
+
437
+ String username = proxyConfig .getUserName ();
438
+ String password = proxyConfig .getPassword ();
411
439
if (username != null && !"" .equals (username .trim ())) {
412
- LOGGER .info ("Using proxy authentication (user=" + username + ")" );
413
- this .client .getState ().setProxyCredentials (AuthScope .ANY ,
414
- new UsernamePasswordCredentials (username , password ));
440
+ LOGGER .fine ("Using proxy authentication (user=" + username + ")" );
441
+
442
+ final HttpState state = new HttpState ();
443
+ state .setProxyCredentials (AuthScope .ANY , new UsernamePasswordCredentials (username , password ));
444
+ return client .executeMethod (hc , method , state );
445
+ } else {
446
+ return client .executeMethod (hc , method );
415
447
}
448
+ } else {
449
+ return client .executeMethod (method );
416
450
}
417
- return this .client ;
418
451
}
419
452
420
453
private String getRequest (String path ) {
421
- HttpClient client = getHttpClient ();
422
- client .getState ().setCredentials (AuthScope .ANY , credentials );
423
454
GetMethod httpget = new GetMethod (path );
424
- client . getParams (). setAuthenticationPreemptive ( true );
455
+ HttpClient client = getHttpClient ( );
425
456
String response = null ;
426
457
InputStream responseBodyAsStream = null ;
427
458
try {
428
- client . executeMethod (httpget );
459
+ executeMethod (client , httpget );
429
460
responseBodyAsStream = httpget .getResponseBodyAsStream ();
430
461
response = IOUtils .toString (responseBodyAsStream , "UTF-8" );
431
462
if (httpget .getStatusCode () != HttpStatus .SC_OK ) {
@@ -448,12 +479,10 @@ private String getRequest(String path) {
448
479
}
449
480
450
481
private int getRequestStatus (String path ) {
451
- HttpClient client = getHttpClient ();
452
- client .getState ().setCredentials (AuthScope .ANY , credentials );
453
482
GetMethod httpget = new GetMethod (path );
454
- client . getParams (). setAuthenticationPreemptive ( true );
483
+ HttpClient client = getHttpClient ( );
455
484
try {
456
- client . executeMethod (httpget );
485
+ executeMethod (client , httpget );
457
486
return httpget .getStatusCode ();
458
487
} catch (HttpException e ) {
459
488
LOGGER .log (Level .SEVERE , "Communication error" , e );
@@ -465,13 +494,19 @@ private int getRequestStatus(String path) {
465
494
return -1 ;
466
495
}
467
496
497
+ private static String getMethodHost (HttpMethod method ) {
498
+ try {
499
+ return method .getURI ().getHost ();
500
+ } catch (URIException e ) {
501
+ throw new IllegalStateException ("Could not obtain host part for method " + method , e );
502
+ }
503
+ }
504
+
468
505
private void deleteRequest (String path ) {
469
- HttpClient client = getHttpClient ();
470
- client .getState ().setCredentials (AuthScope .ANY , credentials );
471
506
DeleteMethod httppost = new DeleteMethod (path );
472
- client . getParams (). setAuthenticationPreemptive ( true );
507
+ HttpClient client = getHttpClient ( );
473
508
try {
474
- client . executeMethod (httppost );
509
+ executeMethod (client , httppost );
475
510
if (httppost .getStatusCode () != HttpStatus .SC_NO_CONTENT ) {
476
511
throw new BitbucketRequestException (httppost .getStatusCode (), "HTTP request error. Status: " + httppost .getStatusCode () + ": " + httppost .getStatusText ());
477
512
}
@@ -484,12 +519,10 @@ private void deleteRequest(String path) {
484
519
485
520
private String postRequest (PostMethod httppost ) throws UnsupportedEncodingException {
486
521
HttpClient client = getHttpClient ();
487
- client .getState ().setCredentials (AuthScope .ANY , credentials );
488
- client .getParams ().setAuthenticationPreemptive (true );
489
522
String response = "" ;
490
523
InputStream responseBodyAsStream = null ;
491
524
try {
492
- client . executeMethod (httppost );
525
+ executeMethod (client , httppost );
493
526
responseBodyAsStream = httppost .getResponseBodyAsStream ();
494
527
response = IOUtils .toString (responseBodyAsStream , "UTF-8" );
495
528
if (httppost .getStatusCode () != HttpStatus .SC_OK && httppost .getStatusCode () != HttpStatus .SC_CREATED ) {
0 commit comments