Skip to content

Commit

Permalink
JENKINS-51478 Retrieve and apply proxy configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgroot committed May 31, 2018
1 parent ea67eed commit 1b225d7
Showing 1 changed file with 21 additions and 1 deletion.
Expand Up @@ -26,12 +26,15 @@

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import hudson.ProxyConfiguration;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.publishoverdropbox.impl.Messages;

import javax.annotation.Nonnull;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -140,7 +143,7 @@ public T execute() throws IOException {
InputStream errorStream = null;
try {
// Prepare
connection = (HttpURLConnection) url.openConnection();
connection = openConnection(url);
connection.setReadTimeout(timeout);
connection.setConnectTimeout(timeout);
for (Map.Entry<String, String> entry : headers.entrySet()) {
Expand Down Expand Up @@ -193,6 +196,23 @@ public T execute() throws IOException {
return model;
}

private HttpURLConnection openConnection(URL url) throws IOException {
Proxy proxy = getProxy(url);
if (proxy != null) {
return (HttpURLConnection) url.openConnection(proxy);
} else {
return (HttpURLConnection) url.openConnection();
}
}

private Proxy getProxy(URL url) {
ProxyConfiguration proxyConfig = Jenkins.getInstance().proxy;
if (proxyConfig != null) {
return proxyConfig.createProxy(url.getHost());
}
return null;
}

public static String httpHeaderEncode(String text) {
StringBuilder sb = new StringBuilder(text.length());
for (int i = 0; i < text.length(); i++) {
Expand Down

0 comments on commit 1b225d7

Please sign in to comment.