Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>apache-httpcomponents-client-5-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>gson-api</artifactId>
Expand All @@ -90,10 +94,6 @@
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
Expand Down
87 changes: 49 additions & 38 deletions src/main/java/jenkins/plugins/office365connector/HttpWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,28 @@
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;

/**
* Makes http post requests in a separate thread.
Expand Down Expand Up @@ -82,53 +86,60 @@
// log("Posted JSON: %s", data);
post.setEntity(new StringEntity(data, ContentType.APPLICATION_JSON));

try (CloseableHttpResponse httpResponse = client.execute(post)) {
int responseCode = httpResponse.getStatusLine().getStatusCode();
try (ClassicHttpResponse httpResponse = client.execute(post, classicHttpResponse -> classicHttpResponse)) {
int responseCode = httpResponse.getCode();
if (responseCode >= HttpStatus.SC_BAD_REQUEST) {
log("Posting data to %s may have failed. Webhook responded with status code - %s", url, responseCode);
String response =
EntityUtils.toString(httpResponse.getEntity(), StandardCharsets.UTF_8);
log("Message from webhook - %s", response);

} else {
success = true;
}
} catch (IOException e) {
} catch (IOException | ParseException e) {
log("Failed to post data to webhook - %s", url);
e.printStackTrace(logger);
}
} while (tried < RETRIES && !success);

}

private CloseableHttpClient getHttpClient() {
HttpClientBuilder builder = HttpClientBuilder.create();
Jenkins jenkins = Jenkins.get();
if (jenkins != null) {
ProxyConfiguration proxy = jenkins.proxy;
if (proxy != null) {
List<Pattern> noHostProxyPatterns = proxy.getNoProxyHostPatterns();
if (!isNoProxyHost(this.url, noHostProxyPatterns)) {
builder.setProxy(new HttpHost(proxy.name, proxy.port));
String username = proxy.getUserName();
String password = proxy.getPassword();
// Consider it to be passed if username specified. Sufficient?
if (StringUtils.isNotBlank(username)) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxy.name, proxy.port),
new UsernamePasswordCredentials(username, password));
builder.setDefaultCredentialsProvider(credsProvider);
}
ProxyConfiguration proxy = jenkins.proxy;
if (proxy != null) {
List<Pattern> noHostProxyPatterns = proxy.getNoProxyHostPatterns();
if (!isNoProxyHost(this.url, noHostProxyPatterns)) {
builder.setProxy(new HttpHost(proxy.name, proxy.port));
String username = proxy.getUserName();
String password = proxy.getSecretPassword().getPlainText();
// Consider it to be passed if username specified. Sufficient?
if (StringUtils.isNotBlank(username)) {
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxy.name, proxy.port),
new UsernamePasswordCredentials(username, password.toCharArray()));

Check warning on line 123 in src/main/java/jenkins/plugins/office365connector/HttpWorker.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 89-123 are not covered by tests
builder.setDefaultCredentialsProvider(credsProvider);
}
}
}
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setSocketTimeout(timeout)
.setConnectionRequestTimeout(timeout)
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeout, TimeUnit.MILLISECONDS)
.build();
builder.setDefaultRequestConfig(config);

ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setConnectTimeout(timeout, TimeUnit.MILLISECONDS)
.setSocketTimeout(timeout, TimeUnit.MILLISECONDS)
.build();

PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setDefaultConnectionConfig(connectionConfig)
.build();

builder.setDefaultRequestConfig(requestConfig);
builder.setConnectionManager(connectionManager);

return builder.build();
}
Expand Down
Loading