提交 553a3850 编写于 作者: S ssogabe

Use Common HttpClient instead of URLConnection

上级 1c89aed2
......@@ -30,7 +30,6 @@ import hudson.model.Descriptor;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
import hudson.util.FormValidation;
import hudson.util.IOUtils;
import hudson.util.Scrambler;
import hudson.util.Secret;
import hudson.util.XStream2;
......@@ -39,6 +38,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
......@@ -48,6 +48,11 @@ import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import jenkins.model.Jenkins;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jvnet.robust_http_client.RetryableHttpStream;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
......@@ -233,14 +238,10 @@ public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfi
return con;
}
public static InputStream getInputStream(URL url) throws IOException {
Jenkins h = Jenkins.getInstance(); // this code might run on slaves
final ProxyConfiguration p = (h != null) ? h.proxy : null;
return getInputStream(url, p);
}
public static InputStream getInputStream(URL url, final ProxyConfiguration p) throws IOException {
if (p == null)
return new RetryableHttpStream(url);
......@@ -303,18 +304,30 @@ public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfi
return FormValidation.error(Messages.ProxyConfiguration_TestUrlRequired());
}
ProxyConfiguration pc = null;
if (Util.fixEmptyAndTrim(name) != null) {
pc = new ProxyConfiguration(name, port, userName, password, noProxyHost);
}
InputStream is = null;
GetMethod method = null;
try {
is = ProxyConfiguration.getInputStream(new URL(testUrl), pc);
method = new GetMethod(testUrl);
method.getParams().setParameter("http.socket.timeout", new Integer(30 * 1000));
HttpClient client = new HttpClient();
if (Util.fixEmptyAndTrim(name) != null) {
client.getHostConfiguration().setProxy(name, port);
Credentials credentials =
new UsernamePasswordCredentials(userName, Secret.fromString(password).getPlainText());
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
client.getState().setProxyCredentials(scope, credentials);
}
int code = client.executeMethod(method);
if (code != HttpURLConnection.HTTP_OK) {
return FormValidation.error(Messages.ProxyConfiguration_FailedToConnect(testUrl, code));
}
} catch (IOException e) {
return FormValidation.error(e, Messages.ProxyConfiguration_FailedToConnectViaProxy(testUrl));
} finally {
IOUtils.closeQuietly(is);
if (method != null) {
method.releaseConnection();
}
}
return FormValidation.ok(Messages.ProxyConfiguration_Success());
......
......@@ -66,4 +66,5 @@ AboutJenkins.Description=See the version and license information.
ProxyConfiguration.TestUrlRequired=Test URL is required.
ProxyConfiguration.FailedToConnectViaProxy=Failed to connect to {0}.
ProxyConfiguration.FailedToConnect=Failed to connect to {0} (code {1}).
ProxyConfiguration.Success=Success
\ No newline at end of file
......@@ -66,4 +66,5 @@ AboutJenkins.Description=\u30d0\u30fc\u30b8\u30e7\u30f3\u3068\u30e9\u30a4\u30bb\
ProxyConfiguration.TestUrlRequired=\u30c6\u30b9\u30c8URL\u306f\u5fc5\u9808\u3067\u3059\u3002
ProxyConfiguration.FailedToConnectViaProxy={0} \u306b\u63a5\u7d9a\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002
ProxyConfiguration.FailedToConnect={0} \u306b\u63a5\u7d9a\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002(\u30b3\u30fc\u30c9 {1}).
ProxyConfiguration.Success=\u6210\u529f
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册