Skip to content

Commit 5fd8e3a

Browse files
author
stoesselt
committed
Added support for proxy
- Implementation basing on org.apache.http.client.HttpClient was chosen, because this works with Jenkins StashNotifier as well.
1 parent 29bb738 commit 5fd8e3a

File tree

8 files changed

+204
-46
lines changed

8 files changed

+204
-46
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33

44
Changelog of Violation comments to bitbucket server lib.
55

6+
## 1.48
7+
### No issue
8+
9+
**PCLint**
10+
11+
12+
[68c09c2d9836709](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/68c09c2d9836709) Tomas Bjerre *2018-02-13 18:59:38*
13+
14+
15+
## 1.47
16+
### No issue
17+
18+
**ViolationCommentsToBitbucketServerApi**
19+
20+
21+
[e8cd87fdd59f777](https://github.com/tomasbjerre/violation-comments-to-bitbucket-server-lib/commit/e8cd87fdd59f777) Tomas Bjerre *2018-02-10 07:55:37*
22+
23+
624
## 1.46
725
### GitHub #47
826

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
compile 'com.jayway.jsonpath:json-path:2.0.0'
2424
compile 'com.google.code.gson:gson:2.8.0'
2525
compile 'com.google.guava:guava:20.0'
26+
compile 'org.apache.httpcomponents:httpclient:4.5.5'
2627

2728
testCompile 'junit:junit:4.12'
2829
testCompile 'org.assertj:assertj-core:2.3.0'

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/BitbucketServerCommentsProvider.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public BitbucketServerCommentsProvider(
6161
final String bitbucketServerPassword = violationCommentsToBitbucketApi.getPassword();
6262
final String bitbucketPersonalAccessToken =
6363
violationCommentsToBitbucketApi.getPersonalAccessToken();
64+
final String proxyHostNameOrIp = violationCommentsToBitbucketApi.getProxyHostNameOrIp();
65+
final Integer proxyHostPort = violationCommentsToBitbucketApi.getProxyHostPort();
66+
final String proxyUser = violationCommentsToBitbucketApi.getProxyUser();
67+
final String proxyPassword = violationCommentsToBitbucketApi.getProxyPassword();
6468
client =
6569
new BitbucketServerClient(
6670
bitbucketServerBaseUrl,
@@ -69,7 +73,11 @@ public BitbucketServerCommentsProvider(
6973
bitbucketServerPullRequestId,
7074
bitbucketServerUser,
7175
bitbucketServerPassword,
72-
bitbucketPersonalAccessToken);
76+
bitbucketPersonalAccessToken,
77+
proxyHostNameOrIp,
78+
proxyHostPort,
79+
proxyUser,
80+
proxyPassword);
7381
this.violationCommentsToBitbucketApi = violationCommentsToBitbucketApi;
7482
}
7583

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/ViolationCommentsToBitbucketServerApi.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public static ViolationCommentsToBitbucketServerApi violationCommentsToBitbucket
3535
private int commentOnlyChangedContentContext;
3636
private boolean shouldKeepOldComments;
3737
private String personalAccessToken;
38+
private String proxyHostNameOrIp;
39+
private Integer proxyHostPort = 0;
40+
private String proxyUser;
41+
private String proxyPassword;
3842

3943
private ViolationCommentsToBitbucketServerApi() {}
4044

@@ -113,6 +117,30 @@ public String getUsername() {
113117
return username;
114118
}
115119

120+
public boolean getShouldKeepOldComments() {
121+
return shouldKeepOldComments;
122+
}
123+
124+
public String getPersonalAccessToken() {
125+
return personalAccessToken;
126+
}
127+
128+
public String getProxyHostNameOrIp() {
129+
return proxyHostNameOrIp;
130+
}
131+
132+
public Integer getProxyHostPort() {
133+
return proxyHostPort;
134+
}
135+
136+
public String getProxyUser() {
137+
return proxyUser;
138+
}
139+
140+
public String getProxyPassword() {
141+
return proxyPassword;
142+
}
143+
116144
private void populateFromEnvironmentVariables() {
117145
if (System.getProperty(propUsername) != null) {
118146
username = firstNonNull(username, System.getProperty(propUsername));
@@ -219,11 +247,24 @@ public ViolationCommentsToBitbucketServerApi withShouldKeepOldComments(
219247
return this;
220248
}
221249

222-
public boolean getShouldKeepOldComments() {
223-
return shouldKeepOldComments;
250+
public ViolationCommentsToBitbucketServerApi withProxyHostNameOrIp(
251+
final String proxyHostNameOrIp) {
252+
this.proxyHostNameOrIp = proxyHostNameOrIp;
253+
return this;
224254
}
225255

226-
public String getPersonalAccessToken() {
227-
return personalAccessToken;
256+
public ViolationCommentsToBitbucketServerApi withProxyHostPort(final Integer proxyHostPort) {
257+
this.proxyHostPort = proxyHostPort;
258+
return this;
259+
}
260+
261+
public ViolationCommentsToBitbucketServerApi withProxyUser(final String proxyUser) {
262+
this.proxyUser = proxyUser;
263+
return this;
264+
}
265+
266+
public ViolationCommentsToBitbucketServerApi withProxyPassword(final String proxyPassword) {
267+
this.proxyPassword = proxyPassword;
268+
return this;
228269
}
229270
}

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerClient.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static void setBitbucketServerInvoker(
3232
private final String bitbucketServerRepo;
3333
private final String bitbucketServerUser;
3434
private final String bitbucketPersonalAccessToken;
35+
private final ProxyConfig proxyInformation;
3536

3637
public BitbucketServerClient(
3738
final String bitbucketServerBaseUrl,
@@ -40,7 +41,11 @@ public BitbucketServerClient(
4041
final Integer bitbucketServerPullRequestId,
4142
final String bitbucketServerUser,
4243
final String bitbucketServerPassword,
43-
final String bitbucketPersonalAccessToken) {
44+
final String bitbucketPersonalAccessToken,
45+
final String proxyHostNameOrIp,
46+
final Integer proxyHostPort,
47+
final String proxyUser,
48+
final String proxyPassword) {
4449
if (bitbucketServerBaseUrl.endsWith("/")) {
4550
this.bitbucketServerBaseUrl =
4651
bitbucketServerBaseUrl.substring(0, bitbucketServerBaseUrl.length() - 1);
@@ -53,6 +58,8 @@ public BitbucketServerClient(
5358
this.bitbucketServerUser = bitbucketServerUser;
5459
this.bitbucketServerPassword = bitbucketServerPassword;
5560
this.bitbucketPersonalAccessToken = bitbucketPersonalAccessToken;
61+
this.proxyInformation =
62+
new ProxyConfig(proxyHostNameOrIp, proxyHostPort, proxyUser, proxyPassword);
5663
}
5764

5865
private String getBitbucketServerPulLRequestBase() {
@@ -92,10 +99,10 @@ public void pullRequestComment(final String message) {
9299
private String doInvokeUrl(final String string, final Method method, final String postContent) {
93100
if (isNullOrEmpty(bitbucketServerUser) || isNullOrEmpty(bitbucketServerPassword)) {
94101
return bitbucketServerInvoker.invokeUrl(
95-
string, method, postContent, bitbucketPersonalAccessToken);
102+
string, method, postContent, bitbucketPersonalAccessToken, proxyInformation);
96103
} else {
97-
return bitbucketServerInvoker.invokeUrl(
98-
string, method, postContent, bitbucketServerUser, bitbucketServerPassword);
104+
return bitbucketServerInvoker.invokeUrl(string, method, postContent, bitbucketServerUser,
105+
bitbucketServerPassword, proxyInformation);
99106
}
100107
}
101108

src/main/java/se/bjurr/violations/comments/bitbucketserver/lib/client/BitbucketServerInvoker.java

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33
import static com.google.common.base.Charsets.UTF_8;
44
import static com.google.common.base.Strings.isNullOrEmpty;
55

6-
import com.google.common.base.Throwables;
76
import java.io.BufferedReader;
87
import java.io.IOException;
98
import java.io.InputStreamReader;
10-
import java.io.OutputStream;
119
import java.io.UnsupportedEncodingException;
1210
import java.net.CookieHandler;
1311
import java.net.CookieManager;
1412
import java.net.CookiePolicy;
15-
import java.net.HttpURLConnection;
16-
import java.net.URL;
13+
import java.net.URI;
14+
1715
import javax.xml.bind.DatatypeConverter;
1816

17+
import org.apache.http.HttpResponse;
18+
import org.apache.http.client.HttpClient;
19+
import org.apache.http.client.config.RequestConfig;
20+
import org.apache.http.client.methods.HttpDelete;
21+
import org.apache.http.client.methods.HttpGet;
22+
import org.apache.http.client.methods.HttpPost;
23+
import org.apache.http.client.methods.HttpRequestBase;
24+
import org.apache.http.entity.StringEntity;
25+
import org.apache.http.impl.client.HttpClientBuilder;
26+
27+
import com.google.common.base.Throwables;
28+
1929
public class BitbucketServerInvoker {
2030

2131
public enum Method {
@@ -25,19 +35,24 @@ public enum Method {
2535
}
2636

2737
public String invokeUrl(
28-
final String url, final Method method, final String postContent, final String bearer) {
38+
final String url,
39+
final Method method,
40+
final String postContent,
41+
final String bearer,
42+
final ProxyConfig proxyConfig) {
2943

3044
final String authorizationValue = "Bearer " + bearer;
3145

32-
return doInvokeUrl(url, method, postContent, authorizationValue);
46+
return doInvokeUrl(url, method, postContent, authorizationValue, proxyConfig);
3347
}
3448

3549
public String invokeUrl(
3650
final String url,
3751
final Method method,
3852
final String postContent,
3953
final String bitbucketServerUser,
40-
final String bitbucketServerPassword) {
54+
final String bitbucketServerPassword,
55+
final ProxyConfig proxyConfig) {
4156

4257
final String userAndPass = bitbucketServerUser + ":" + bitbucketServerPassword;
4358
String authString;
@@ -48,38 +63,59 @@ public String invokeUrl(
4863
}
4964
final String authorizationValue = "Basic " + authString;
5065

51-
return doInvokeUrl(url, method, postContent, authorizationValue);
66+
return doInvokeUrl(url, method, postContent, authorizationValue, proxyConfig);
5267
}
5368

5469
private String doInvokeUrl(
5570
final String url,
5671
final Method method,
5772
final String postContent,
58-
final String authorizationValue) {
59-
HttpURLConnection conn = null;
60-
OutputStream output = null;
61-
BufferedReader reader = null;
73+
final String authorizationValue,
74+
final ProxyConfig proxyConfig) {
75+
BufferedReader bufferedReader = null;
6276
try {
6377
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
64-
conn = (HttpURLConnection) new URL(url).openConnection();
65-
conn.setReadTimeout(30000);
66-
conn.setConnectTimeout(30000);
67-
conn.setRequestProperty("Authorization", authorizationValue);
68-
conn.setRequestMethod(method.name());
69-
final String charset = "UTF-8";
70-
conn.setDoOutput(true);
71-
conn.setRequestProperty("X-Atlassian-Token", "no-check");
72-
conn.setRequestProperty("Content-Type", "application/json");
73-
conn.setRequestProperty("Accept", "application/json");
74-
conn.connect();
75-
if (!isNullOrEmpty(postContent)) {
76-
output = conn.getOutputStream();
77-
output.write(postContent.getBytes(charset));
78+
// Preparation of the request
79+
HttpRequestBase request;
80+
switch (method) {
81+
case DELETE:
82+
request = new HttpDelete();
83+
break;
84+
case GET:
85+
request = new HttpGet();
86+
break;
87+
case POST:
88+
request = new HttpPost();
89+
break;
90+
default:
91+
throw new IllegalArgumentException(
92+
"Unsupported http method:\n" + url + "\n" + method + "\n" + postContent);
7893
}
79-
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8));
94+
request.setURI(new URI(url));
95+
RequestConfig.Builder requestBuilder =
96+
RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000);
97+
request.setConfig(requestBuilder.build());
98+
request.addHeader("Authorization", authorizationValue);
99+
request.addHeader("X-Atlassian-Token", "no-check");
100+
request.addHeader("Content-Type", "application/json");
101+
request.addHeader("Accept", "application/json");
102+
103+
if (request instanceof HttpPost && !isNullOrEmpty(postContent)) {
104+
StringEntity entity = new StringEntity(postContent, UTF_8);
105+
((HttpPost) request).setEntity(entity);
106+
}
107+
108+
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
109+
proxyConfig.addTo(httpClientBuilder);
110+
HttpClient httpClient = httpClientBuilder.build();
111+
112+
// Execute the request and get the response
113+
HttpResponse response = httpClient.execute(request);
114+
bufferedReader =
115+
new BufferedReader(new InputStreamReader(response.getEntity().getContent(), UTF_8));
80116
final StringBuilder stringBuilder = new StringBuilder();
81117
String line = null;
82-
while ((line = reader.readLine()) != null) {
118+
while ((line = bufferedReader.readLine()) != null) {
83119
stringBuilder.append(line + "\n");
84120
}
85121
final String json = stringBuilder.toString();
@@ -88,15 +124,10 @@ private String doInvokeUrl(
88124
throw new RuntimeException("Error calling:\n" + url + "\n" + method + "\n" + postContent, e);
89125
} finally {
90126
try {
91-
if (conn != null) {
92-
conn.disconnect();
93-
}
94-
if (reader != null) {
95-
reader.close();
96-
}
97-
if (output != null) {
98-
output.close();
127+
if (bufferedReader != null) {
128+
bufferedReader.close();
99129
}
130+
100131
} catch (final IOException e) {
101132
throw Throwables.propagate(e);
102133
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package se.bjurr.violations.comments.bitbucketserver.lib.client;
2+
3+
import static com.google.common.base.Strings.isNullOrEmpty;
4+
5+
import org.apache.http.HttpHost;
6+
import org.apache.http.auth.AuthScope;
7+
import org.apache.http.auth.UsernamePasswordCredentials;
8+
import org.apache.http.client.CredentialsProvider;
9+
import org.apache.http.impl.client.BasicCredentialsProvider;
10+
import org.apache.http.impl.client.HttpClientBuilder;
11+
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
12+
13+
public class ProxyConfig {
14+
15+
private final String proxyHostNameOrIp;
16+
private final Integer proxyHostPort;
17+
private final String proxyUser;
18+
private final String proxyPassword;
19+
20+
public ProxyConfig(
21+
String proxyHostNameOrIp, Integer proxyHostPort, String proxyUser, String proxyPassword) {
22+
this.proxyHostNameOrIp = proxyHostNameOrIp;
23+
this.proxyHostPort = proxyHostPort;
24+
this.proxyUser = proxyUser;
25+
this.proxyPassword = proxyPassword;
26+
}
27+
28+
public HttpClientBuilder addTo(HttpClientBuilder builder) {
29+
if (!isNullOrEmpty(proxyHostNameOrIp)) {
30+
HttpHost proxyHost = new HttpHost(proxyHostNameOrIp, proxyHostPort);
31+
builder = builder.setProxy(proxyHost);
32+
33+
if (!isNullOrEmpty(proxyUser)) {
34+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
35+
credsProvider.setCredentials(
36+
new AuthScope(proxyHostNameOrIp, proxyHostPort),
37+
new UsernamePasswordCredentials(proxyUser, proxyPassword));
38+
39+
builder =
40+
builder
41+
.setDefaultCredentialsProvider(credsProvider)
42+
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
43+
}
44+
}
45+
return builder;
46+
}
47+
}

0 commit comments

Comments
 (0)