Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Jenkins proxy configuration support #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package io.jenkins.plugins.servicenow.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import hudson.ProxyConfiguration;
import hudson.util.Secret;
import io.jenkins.plugins.servicenow.api.model.Error;
import io.jenkins.plugins.servicenow.api.model.Response;
import io.jenkins.plugins.servicenow.api.model.Result;
import io.jenkins.plugins.servicenow.api.model.TableResponse;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
Expand Down Expand Up @@ -385,7 +385,7 @@ private Result getResult(String endpoint, Response response) {
}

private Response get(final String endpointPath, final List<NameValuePair> parameters) {
try(CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(getCredentials()).build()) {
try(CloseableHttpClient client = (CloseableHttpClient) getHttpClient(getCredentials())) {

HttpGet request = new HttpGet();
HttpResponse response = sendRequest(client, request, endpointPath, parameters, null); //client.execute(request);
Expand All @@ -407,7 +407,7 @@ private Response get(final String endpointPath, final List<NameValuePair> parame
}

private TableResponse getTable(final String endpointPath, final List<NameValuePair> parameters) {
try(CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(getCredentials()).build()) {
try(CloseableHttpClient client = (CloseableHttpClient) getHttpClient(getCredentials())) {

HttpGet request = new HttpGet();
HttpResponse response = sendRequest(client, request, endpointPath, parameters, null); //client.execute(request);
Expand All @@ -432,7 +432,7 @@ private TableResponse getTable(final String endpointPath, final List<NameValuePa

private Response post(final String endpointPath, final List<NameValuePair> parameters, final String jsonBody) throws URISyntaxException, IOException {
this.lastActionProgressUrl = StringUtils.EMPTY;
try(CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(getCredentials()).build()) {
try(CloseableHttpClient client = (CloseableHttpClient) getHttpClient(getCredentials())) {

HttpPost request = new HttpPost();
HttpResponse response = sendRequest(client, request, endpointPath, parameters, jsonBody);
Expand All @@ -452,6 +452,38 @@ private Response post(final String endpointPath, final List<NameValuePair> param
}
}

private HttpClient getHttpClient(CredentialsProvider credentialsProvider) throws IOException {

HttpClientBuilder clientBuilder = HttpClientBuilder.create();

ProxyConfiguration proxyConfiguration = null;
try {
proxyConfiguration = Jenkins.get().proxy;
} catch (IllegalStateException ex) {
// No Jenkins instance, continue without proxy
}

if(proxyConfiguration != null) {

String name = proxyConfiguration.name;
int port = proxyConfiguration.port;
String userName = proxyConfiguration.getUserName();

if(userName != null) {
LOG.info("Setting credentials");
credentialsProvider.setCredentials(
new AuthScope(name, port),
new UsernamePasswordCredentials(userName, proxyConfiguration.getPassword()));
}

clientBuilder.setProxy(new HttpHost(name, port));
}

clientBuilder.setDefaultCredentialsProvider(credentialsProvider);

return clientBuilder.build();
}

private Response getResponse(HttpResponse response) throws IOException {
String responseJSON = EntityUtils.toString(response.getEntity());
LOG.debug(responseJSON);
Expand Down