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

Commit

Permalink
Adding HTTP 1.1 support #257
Browse files Browse the repository at this point in the history
Hopefully solves OpenShift integration problem.
  • Loading branch information
tomasbjerre committed Nov 18, 2017
1 parent cdc5e60 commit 843d859
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 219 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ target
node_modules
opt
atlassian-plugin-sdk-tgz

.idea
*iml
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Changelog of Pull Request Notifier for Bitbucket.

## Unreleased
### No issue
Doc

[cdc5e6047039e40](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/cdc5e6047039e40) Tomas Bjerre *2017-11-01 16:44:23*

## 3.16
### No issue
Encoding HTML newline as <br/>
Expand Down
101 changes: 59 additions & 42 deletions src/main/java/se/bjurr/prnfb/http/UrlInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static com.google.common.collect.Lists.newArrayList;
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.xml.bind.DatatypeConverter.printBase64Binary;
import static org.apache.http.HttpVersion.HTTP_1_0;
import static org.apache.http.HttpVersion.HTTP_1_1;
import static org.slf4j.LoggerFactory.getLogger;
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.GET;
import static se.bjurr.prnfb.http.UrlInvoker.HTTP_METHOD.POST;
Expand All @@ -24,6 +26,8 @@

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
Expand Down Expand Up @@ -73,7 +77,7 @@ public enum HTTP_METHOD {
private static final Logger LOG = getLogger(UrlInvoker.class);

@VisibleForTesting
public static String getHeaderValue(PrnfbHeader header) {
public static String getHeaderValue(final PrnfbHeader header) {
return header.getValue();
}

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

private String urlParam;
private ProtocolVersion httpVersion = HttpVersion.HTTP_1_0;

UrlInvoker() {}

public UrlInvoker appendBasicAuth(PrnfbNotification notification) {
public UrlInvoker appendBasicAuth(final PrnfbNotification notification) {
if (notification.getUser().isPresent() && notification.getPassword().isPresent()) {
final String userpass = notification.getUser().get() + ":" + notification.getPassword().get();
final String basicAuth = "Basic " + new String(printBase64Binary(userpass.getBytes(UTF_8)));
Expand Down Expand Up @@ -155,14 +160,26 @@ public String getUrlParam() {
return this.urlParam;
}

public UrlInvoker setHttpVersion(final String httpVersion) {
if (httpVersion == null || httpVersion.equals("HTTP_1_0")) {
this.httpVersion = HTTP_1_0;
} else if (httpVersion.equals("HTTP_1_1")) {
this.httpVersion = HTTP_1_1;
} else {
this.httpVersion = HTTP_1_0;
}
return this;
}

public HttpResponse invoke() {
LOG.info("Url: \"" + this.urlParam + "\"");

HttpRequestBase httpRequestBase = newHttpRequestBase();
final HttpRequestBase httpRequestBase = newHttpRequestBase();
configureUrl(httpRequestBase);
addHeaders(httpRequestBase);
httpRequestBase.setProtocolVersion(httpVersion);

HttpClientBuilder builder = HttpClientBuilder.create();
final HttpClientBuilder builder = HttpClientBuilder.create();
configureSsl(builder);
configureProxy(builder);

Expand All @@ -176,7 +193,7 @@ public HttpResponse invoke() {
return this.response;
}

public void setResponse(HttpResponse response) {
public void setResponse(final HttpResponse response) {
this.response = response;
}

Expand All @@ -185,7 +202,7 @@ public boolean shouldAcceptAnyCertificate() {
return this.shouldAcceptAnyCertificate;
}

public UrlInvoker shouldAcceptAnyCertificate(boolean shouldAcceptAnyCertificate) {
public UrlInvoker shouldAcceptAnyCertificate(final boolean shouldAcceptAnyCertificate) {
this.shouldAcceptAnyCertificate = shouldAcceptAnyCertificate;
return this;
}
Expand All @@ -205,58 +222,58 @@ public boolean shouldUseProxy() {
return getProxyHost().isPresent() && getProxyPort().isPresent() && getProxyPort().get() > 0;
}

public UrlInvoker withClientKeyStore(ClientKeyStore clientKeyStore) {
public UrlInvoker withClientKeyStore(final ClientKeyStore clientKeyStore) {
this.clientKeyStore = clientKeyStore;
return this;
}

public UrlInvoker withHeader(String name, String value) {
public UrlInvoker withHeader(final String name, final String value) {
this.headers.add(new PrnfbHeader(name, value));
return this;
}

public UrlInvoker withMethod(HTTP_METHOD method) {
public UrlInvoker withMethod(final HTTP_METHOD method) {
this.method = method;
return this;
}

public UrlInvoker withPostContent(Optional<String> postContent) {
public UrlInvoker withPostContent(final Optional<String> postContent) {
this.postContent = postContent;
return this;
}

public UrlInvoker withProxyPassword(Optional<String> proxyPassword) {
public UrlInvoker withProxyPassword(final Optional<String> proxyPassword) {
this.proxyPassword = proxyPassword;
return this;
}

public UrlInvoker withProxyPort(Integer proxyPort) {
public UrlInvoker withProxyPort(final Integer proxyPort) {
this.proxyPort = fromNullable(proxyPort);
return this;
}

public UrlInvoker withProxySchema(Optional<String> proxySchema) {
public UrlInvoker withProxySchema(final Optional<String> proxySchema) {
this.proxySchema = proxySchema;
return this;
}

public UrlInvoker withProxyServer(Optional<String> proxyHost) {
public UrlInvoker withProxyServer(final Optional<String> proxyHost) {
this.proxyHost = proxyHost;
return this;
}

public UrlInvoker withProxyUser(Optional<String> proxyUser) {
public UrlInvoker withProxyUser(final Optional<String> proxyUser) {
this.proxyUser = proxyUser;
return this;
}

public UrlInvoker withUrlParam(String urlParam) {
public UrlInvoker withUrlParam(final String urlParam) {
this.urlParam = urlParam.replaceAll("\\s", "%20");
return this;
}

private void addHeaders(HttpRequestBase httpRequestBase) {
for (PrnfbHeader header : this.headers) {
private void addHeaders(final HttpRequestBase httpRequestBase) {
for (final PrnfbHeader header : this.headers) {

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

private void configureUrl(HttpRequestBase httpRequestBase) {
private void configureUrl(final HttpRequestBase httpRequestBase) {
try {
httpRequestBase.setURI(new URI(this.urlParam));
} catch (URISyntaxException e) {
} catch (final URISyntaxException e) {
propagate(e);
}
}

private SSLContextBuilder doAcceptAnyCertificate(SSLContextBuilder customContext)
throws Exception {
TrustStrategy easyStrategy =
final TrustStrategy easyStrategy =
new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) {
public boolean isTrusted(final X509Certificate[] chain, final String authType) {
return true;
}
};
Expand All @@ -291,7 +308,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType) {
}

private SSLContext newSslContext() throws Exception {
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
final SSLContextBuilder sslContextBuilder = SSLContexts.custom();
if (this.shouldAcceptAnyCertificate) {
doAcceptAnyCertificate(sslContextBuilder);
if (this.clientKeyStore.getKeyStore().isPresent()) {
Expand All @@ -308,16 +325,16 @@ private boolean shouldUseSsl() {
}

@VisibleForTesting
HttpClientBuilder configureProxy(HttpClientBuilder builder) {
HttpClientBuilder configureProxy(final HttpClientBuilder builder) {
if (!shouldUseProxy()) {
return builder;
}

if (this.proxyUser.isPresent() && this.proxyPassword.isPresent()) {
String username = this.proxyUser.get();
String password = this.proxyPassword.get();
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
final String username = this.proxyUser.get();
final String password = this.proxyPassword.get();
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(this.proxyHost.get(), this.proxyPort.get()), creds);
builder.setDefaultCredentialsProvider(credsProvider);
Expand All @@ -331,44 +348,44 @@ HttpClientBuilder configureProxy(HttpClientBuilder builder) {
}

@VisibleForTesting
HttpClientBuilder configureSsl(HttpClientBuilder builder) {
HttpClientBuilder configureSsl(final HttpClientBuilder builder) {
if (shouldUseSsl()) {
try {
SSLContext s = newSslContext();
SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(s);
final SSLContext s = newSslContext();
final SSLConnectionSocketFactory sslConnSocketFactory = new SSLConnectionSocketFactory(s);
builder.setSSLSocketFactory(sslConnSocketFactory);

Registry<ConnectionSocketFactory> registry =
final Registry<ConnectionSocketFactory> registry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnSocketFactory)
.build();

HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
final HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);

builder.setConnectionManager(ccm);
} catch (Exception e) {
} catch (final Exception e) {
propagate(e);
}
}
return builder;
}

@VisibleForTesting
HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder) {
HttpResponse doInvoke(final HttpRequestBase httpRequestBase, final HttpClientBuilder builder) {
CloseableHttpResponse httpResponse = null;
try {
httpResponse =
builder //
.build() //
.execute(httpRequestBase);

HttpEntity entity = httpResponse.getEntity();
final HttpEntity entity = httpResponse.getEntity();
String entityString = "";
if (entity != null) {
entityString = EntityUtils.toString(entity, UTF_8);
}
URI uri = httpRequestBase.getURI();
int statusCode = httpResponse.getStatusLine().getStatusCode();
final URI uri = httpRequestBase.getURI();
final int statusCode = httpResponse.getStatusLine().getStatusCode();
return new HttpResponse(uri, statusCode, entityString);
} catch (final Exception e) {
LOG.error("", e);
Expand All @@ -377,7 +394,7 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
} catch (final IOException e) {
propagate(e);
}
}
Expand All @@ -386,8 +403,8 @@ HttpResponse doInvoke(HttpRequestBase httpRequestBase, HttpClientBuilder builder

@VisibleForTesting
HttpEntityEnclosingRequestBase newHttpEntityEnclosingRequestBase(
HTTP_METHOD method, String entity) {
HttpEntityEnclosingRequestBase entityEnclosing =
final HTTP_METHOD method, final String entity) {
final HttpEntityEnclosingRequestBase entityEnclosing =
new HttpEntityEnclosingRequestBase() {
@Override
public String getMethod() {
Expand All @@ -409,7 +426,7 @@ HttpRequestBase newHttpRequestBase() {
}

@VisibleForTesting
HttpRequestBase newHttpRequestBase(HTTP_METHOD method) {
HttpRequestBase newHttpRequestBase(final HTTP_METHOD method) {
return new HttpRequestBase() {
@Override
public String getMethod() {
Expand Down
Loading

0 comments on commit 843d859

Please sign in to comment.