Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 2665e48

Browse files
committed
Adding HTTP 1.1 support #257
Hopefully solves OpenShift integration problem.
1 parent cdc5e60 commit 2665e48

File tree

10 files changed

+334
-221
lines changed

10 files changed

+334
-221
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ target
77
node_modules
88
opt
99
atlassian-plugin-sdk-tgz
10-
10+
.idea
11+
*iml

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### GitHub [#257](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/257) Bitbucket - OpenShift Integration
7+
Adding HTTP 1.1 support
8+
9+
Hopefully solves OpenShift integration problem.
10+
11+
[843d85974970009](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/843d85974970009) Tomas Bjerre *2017-11-18 17:25:41*
12+
13+
### No issue
14+
Doc
15+
16+
[cdc5e6047039e40](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/cdc5e6047039e40) Tomas Bjerre *2017-11-01 16:44:23*
17+
518
## 3.16
619
### No issue
720
Encoding HTML newline as <br/>

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<plugin>
142142
<groupId>se.bjurr.gitchangelog</groupId>
143143
<artifactId>git-changelog-maven-plugin</artifactId>
144-
<version>1.40</version>
144+
<version>1.50</version>
145145
<executions>
146146
<execution>
147147
<id>GenerateGitChangelog</id>
@@ -286,4 +286,4 @@ Changelog of Pull Request Notifier for Bitbucket.
286286
<quick.reload.version>2.0.0</quick.reload.version>
287287
<amps.version>6.3.0</amps.version>
288288
</properties>
289-
</project>
289+
</project>

src/main/java/se/bjurr/prnfb/http/UrlInvoker.java

+59-42
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import static com.google.common.collect.Lists.newArrayList;
88
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
99
import static javax.xml.bind.DatatypeConverter.printBase64Binary;
10+
import static org.apache.http.HttpVersion.HTTP_1_0;
11+
import static org.apache.http.HttpVersion.HTTP_1_1;
1012
import static org.slf4j.LoggerFactory.getLogger;
1113
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.GET;
1214
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.POST;
@@ -24,6 +26,8 @@
2426

2527
import org.apache.http.HttpEntity;
2628
import org.apache.http.HttpHost;
29+
import org.apache.http.HttpVersion;
30+
import org.apache.http.ProtocolVersion;
2731
import org.apache.http.auth.AuthScope;
2832
import org.apache.http.auth.UsernamePasswordCredentials;
2933
import org.apache.http.client.CredentialsProvider;
@@ -73,7 +77,7 @@ public enum HTTP_METHOD {
7377
private static final Logger LOG = getLogger(UrlInvoker.class);
7478

7579
@VisibleForTesting
76-
public static String getHeaderValue(PrnfbHeader header) {
80+
public static String getHeaderValue(final PrnfbHeader header) {
7781
return header.getValue();
7882
}
7983

@@ -95,10 +99,11 @@ public static UrlInvoker urlInvoker() {
9599
private boolean shouldAcceptAnyCertificate;
96100

97101
private String urlParam;
102+
private ProtocolVersion httpVersion = HttpVersion.HTTP_1_0;
98103

99104
UrlInvoker() {}
100105

101-
public UrlInvoker appendBasicAuth(PrnfbNotification notification) {
106+
public UrlInvoker appendBasicAuth(final PrnfbNotification notification) {
102107
if (notification.getUser().isPresent() && notification.getPassword().isPresent()) {
103108
final String userpass = notification.getUser().get() + ":" + notification.getPassword().get();
104109
final String basicAuth = "Basic " + new String(printBase64Binary(userpass.getBytes(UTF_8)));
@@ -155,14 +160,26 @@ public String getUrlParam() {
155160
return this.urlParam;
156161
}
157162

163+
public UrlInvoker setHttpVersion(final String httpVersion) {
164+
if (httpVersion == null || httpVersion.equals("HTTP_1_0")) {
165+
this.httpVersion = HTTP_1_0;
166+
} else if (httpVersion.equals("HTTP_1_1")) {
167+
this.httpVersion = HTTP_1_1;
168+
} else {
169+
this.httpVersion = HTTP_1_0;
170+
}
171+
return this;
172+
}
173+
158174
public HttpResponse invoke() {
159175
LOG.info("Url: \"" + this.urlParam + "\"");
160176

161-
HttpRequestBase httpRequestBase = newHttpRequestBase();
177+
final HttpRequestBase httpRequestBase = newHttpRequestBase();
162178
configureUrl(httpRequestBase);
163179
addHeaders(httpRequestBase);
180+
httpRequestBase.setProtocolVersion(httpVersion);
164181

165-
HttpClientBuilder builder = HttpClientBuilder.create();
182+
final HttpClientBuilder builder = HttpClientBuilder.create();
166183
configureSsl(builder);
167184
configureProxy(builder);
168185

@@ -176,7 +193,7 @@ public HttpResponse invoke() {
176193
return this.response;
177194
}
178195

179-
public void setResponse(HttpResponse response) {
196+
public void setResponse(final HttpResponse response) {
180197
this.response = response;
181198
}
182199

@@ -185,7 +202,7 @@ public boolean shouldAcceptAnyCertificate() {
185202
return this.shouldAcceptAnyCertificate;
186203
}
187204

188-
public UrlInvoker shouldAcceptAnyCertificate(boolean shouldAcceptAnyCertificate) {
205+
public UrlInvoker shouldAcceptAnyCertificate(final boolean shouldAcceptAnyCertificate) {
189206
this.shouldAcceptAnyCertificate = shouldAcceptAnyCertificate;
190207
return this;
191208
}
@@ -205,58 +222,58 @@ public boolean shouldUseProxy() {
205222
return getProxyHost().isPresent() && getProxyPort().isPresent() && getProxyPort().get() > 0;
206223
}
207224

208-
public UrlInvoker withClientKeyStore(ClientKeyStore clientKeyStore) {
225+
public UrlInvoker withClientKeyStore(final ClientKeyStore clientKeyStore) {
209226
this.clientKeyStore = clientKeyStore;
210227
return this;
211228
}
212229

213-
public UrlInvoker withHeader(String name, String value) {
230+
public UrlInvoker withHeader(final String name, final String value) {
214231
this.headers.add(new PrnfbHeader(name, value));
215232
return this;
216233
}
217234

218-
public UrlInvoker withMethod(HTTP_METHOD method) {
235+
public UrlInvoker withMethod(final HTTP_METHOD method) {
219236
this.method = method;
220237
return this;
221238
}
222239

223-
public UrlInvoker withPostContent(Optional<String> postContent) {
240+
public UrlInvoker withPostContent(final Optional<String> postContent) {
224241
this.postContent = postContent;
225242
return this;
226243
}
227244

228-
public UrlInvoker withProxyPassword(Optional<String> proxyPassword) {
245+
public UrlInvoker withProxyPassword(final Optional<String> proxyPassword) {
229246
this.proxyPassword = proxyPassword;
230247
return this;
231248
}
232249

233-
public UrlInvoker withProxyPort(Integer proxyPort) {
250+
public UrlInvoker withProxyPort(final Integer proxyPort) {
234251
this.proxyPort = fromNullable(proxyPort);
235252
return this;
236253
}
237254

238-
public UrlInvoker withProxySchema(Optional<String> proxySchema) {
255+
public UrlInvoker withProxySchema(final Optional<String> proxySchema) {
239256
this.proxySchema = proxySchema;
240257
return this;
241258
}
242259

243-
public UrlInvoker withProxyServer(Optional<String> proxyHost) {
260+
public UrlInvoker withProxyServer(final Optional<String> proxyHost) {
244261
this.proxyHost = proxyHost;
245262
return this;
246263
}
247264

248-
public UrlInvoker withProxyUser(Optional<String> proxyUser) {
265+
public UrlInvoker withProxyUser(final Optional<String> proxyUser) {
249266
this.proxyUser = proxyUser;
250267
return this;
251268
}
252269

253-
public UrlInvoker withUrlParam(String urlParam) {
270+
public UrlInvoker withUrlParam(final String urlParam) {
254271
this.urlParam = urlParam.replaceAll("\\s", "%20");
255272
return this;
256273
}
257274

258-
private void addHeaders(HttpRequestBase httpRequestBase) {
259-
for (PrnfbHeader header : this.headers) {
275+
private void addHeaders(final HttpRequestBase httpRequestBase) {
276+
for (final PrnfbHeader header : this.headers) {
260277

261278
if (header.getName().equals(AUTHORIZATION)) {
262279
LOG.debug("header: \"" + header.getName() + "\" value: \"**********\"");
@@ -268,20 +285,20 @@ private void addHeaders(HttpRequestBase httpRequestBase) {
268285
}
269286
}
270287

271-
private void configureUrl(HttpRequestBase httpRequestBase) {
288+
private void configureUrl(final HttpRequestBase httpRequestBase) {
272289
try {
273290
httpRequestBase.setURI(new URI(this.urlParam));
274-
} catch (URISyntaxException e) {
291+
} catch (final URISyntaxException e) {
275292
propagate(e);
276293
}
277294
}
278295

279296
private SSLContextBuilder doAcceptAnyCertificate(SSLContextBuilder customContext)
280297
throws Exception {
281-
TrustStrategy easyStrategy =
298+
final TrustStrategy easyStrategy =
282299
new TrustStrategy() {
283300
@Override
284-
public boolean isTrusted(X509Certificate[] chain, String authType) {
301+
public boolean isTrusted(final X509Certificate[] chain, final String authType) {
285302
return true;
286303
}
287304
};
@@ -291,7 +308,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) {
291308
}
292309

293310
private SSLContext newSslContext() throws Exception {
294-
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
311+
final SSLContextBuilder sslContextBuilder = SSLContexts.custom();
295312
if (this.shouldAcceptAnyCertificate) {
296313
doAcceptAnyCertificate(sslContextBuilder);
297314
if (this.clientKeyStore.getKeyStore().isPresent()) {
@@ -308,16 +325,16 @@ private boolean shouldUseSsl() {
308325
}
309326

310327
@VisibleForTesting
311-
HttpClientBuilder configureProxy(HttpClientBuilder builder) {
328+
HttpClientBuilder configureProxy(final HttpClientBuilder builder) {
312329
if (!shouldUseProxy()) {
313330
return builder;
314331
}
315332

316333
if (this.proxyUser.isPresent() && this.proxyPassword.isPresent()) {
317-
String username = this.proxyUser.get();
318-
String password = this.proxyPassword.get();
319-
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
320-
CredentialsProvider credsProvider = new BasicCredentialsProvider();
334+
final String username = this.proxyUser.get();
335+
final String password = this.proxyPassword.get();
336+
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
337+
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
321338
credsProvider.setCredentials(
322339
new AuthScope(this.proxyHost.get(), this.proxyPort.get()), creds);
323340
builder.setDefaultCredentialsProvider(credsProvider);
@@ -331,44 +348,44 @@ HttpClientBuilder configureProxy(HttpClientBuilder builder) {
331348
}
332349

333350
@VisibleForTesting
334-
HttpClientBuilder configureSsl(HttpClientBuilder builder) {
351+
HttpClientBuilder configureSsl(final HttpClientBuilder builder) {
335352
if (shouldUseSsl()) {
336353
try {
337-
SSLContext s = newSslContext();
338-
SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(s);
354+
final SSLContext s = newSslContext();
355+
final SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(s);
339356
builder.setSSLSocketFactory(sslConnSocketFactory);
340357

341-
Registry<ConnectionSocketFactory> registry =
358+
final Registry<ConnectionSocketFactory> registry =
342359
RegistryBuilder.<ConnectionSocketFactory>create()
343360
.register("https", sslConnSocketFactory)
344361
.build();
345362

346-
HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
363+
final HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
347364

348365
builder.setConnectionManager(ccm);
349-
} catch (Exception e) {
366+
} catch (final Exception e) {
350367
propagate(e);
351368
}
352369
}
353370
return builder;
354371
}
355372

356373
@VisibleForTesting
357-
HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder) {
374+
HttpResponse doInvoke(final HttpRequestBase httpRequestBase, final HttpClientBuilder builder) {
358375
CloseableHttpResponse httpResponse = null;
359376
try {
360377
httpResponse =
361378
builder //
362379
.build() //
363380
.execute(httpRequestBase);
364381

365-
HttpEntity entity = httpResponse.getEntity();
382+
final HttpEntity entity = httpResponse.getEntity();
366383
String entityString = "";
367384
if (entity != null) {
368385
entityString = EntityUtils.toString(entity, UTF_8);
369386
}
370-
URI uri = httpRequestBase.getURI();
371-
int statusCode = httpResponse.getStatusLine().getStatusCode();
387+
final URI uri = httpRequestBase.getURI();
388+
final int statusCode = httpResponse.getStatusLine().getStatusCode();
372389
return new HttpResponse(uri, statusCode, entityString);
373390
} catch (final Exception e) {
374391
LOG.error("", e);
@@ -377,7 +394,7 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder
377394
if (httpResponse != null) {
378395
httpResponse.close();
379396
}
380-
} catch (IOException e) {
397+
} catch (final IOException e) {
381398
propagate(e);
382399
}
383400
}
@@ -386,8 +403,8 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder
386403

387404
@VisibleForTesting
388405
HttpEntityEnclosingRequestBase newHttpEntityEnclosingRequestBase(
389-
HTTP_METHOD method, String entity) {
390-
HttpEntityEnclosingRequestBase entityEnclosing =
406+
final HTTP_METHOD method, final String entity) {
407+
final HttpEntityEnclosingRequestBase entityEnclosing =
391408
new HttpEntityEnclosingRequestBase() {
392409
@Override
393410
public String getMethod() {
@@ -409,7 +426,7 @@ HttpRequestBase newHttpRequestBase() {
409426
}
410427

411428
@VisibleForTesting
412-
HttpRequestBase newHttpRequestBase(HTTP_METHOD method) {
429+
HttpRequestBase newHttpRequestBase(final HTTP_METHOD method) {
413430
return new HttpRequestBase() {
414431
@Override
415432
public String getMethod() {

0 commit comments

Comments
 (0)