From 12604bc22402fdc41a0eef463af6c53463de26da Mon Sep 17 00:00:00 2001
From: "maksym.grebenets" <maksym.grebenets@fairfaxmedia.com.au>
Date: Fri, 16 Jan 2015 15:14:17 +1100
Subject: Add support for proxy configuration

---
 .../bitbucket/BitbucketApiClient.java              | 29 +++++++++++++++++++---
 1 file changed, 26 insertions(+), 3 deletions(-)

(limited to 'src/main/java/bitbucketpullrequestbuilder')

diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java
index af43408..5906f4c 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/BitbucketApiClient.java
@@ -14,6 +14,9 @@ import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import jenkins.model.Jenkins;
+import hudson.ProxyConfiguration;
+
 /**
  * Created by nishio
  */
@@ -76,8 +79,28 @@ public class BitbucketApiClient {
         return null;
     }
 
-    private String getRequest(String path) {
+    private HttpClient getHttpClient() {
         HttpClient client = new HttpClient();
+        if (Jenkins.getInstance() != null) {
+            ProxyConfiguration proxy = Jenkins.getInstance().proxy;
+            logger.info("Jenins proxy: " + proxy.name + ":" + proxy.port);
+            if (proxy != null) {
+                client.getHostConfiguration().setProxy(proxy.name, proxy.port);
+                String username = proxy.getUserName();
+                String password = proxy.getPassword();
+                // Consider it to be passed if username specified. Sufficient?
+                if (username != null && !"".equals(username.trim())) {
+                    logger.info("Using proxy authentication (user=" + username + ")");
+                    client.getState().setProxyCredentials(AuthScope.ANY,
+                        new UsernamePasswordCredentials(username, password));
+                }
+            }
+        }
+        return client;
+    }
+
+    private String getRequest(String path) {
+        HttpClient client = getHttpClient();
         client.getState().setCredentials(AuthScope.ANY, credentials);
         GetMethod httpget = new GetMethod(path);
         client.getParams().setAuthenticationPreemptive(true);
@@ -94,7 +117,7 @@ public class BitbucketApiClient {
     }
 
     public void deleteRequest(String path) {
-        HttpClient client = new HttpClient();
+        HttpClient client = getHttpClient();
         client.getState().setCredentials(AuthScope.ANY, credentials);
         DeleteMethod httppost = new DeleteMethod(path);
         client.getParams().setAuthenticationPreemptive(true);
@@ -107,7 +130,7 @@ public class BitbucketApiClient {
     }
 
     private String postRequest(String path, NameValuePair[] params) throws UnsupportedEncodingException {
-        HttpClient client = new HttpClient();
+        HttpClient client = getHttpClient();
         client.getState().setCredentials(AuthScope.ANY, credentials);
         PostMethod httppost = new PostMethod(path);
         httppost.setRequestBody(params);
-- 
cgit v1.2.3