Skip to content

Commit

Permalink
[FIXED JENKINS-20191] Add Test button to check proxy connection
Browse files Browse the repository at this point in the history
  • Loading branch information
ssogabe committed Oct 29, 2013
1 parent ad93581 commit c853c47
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
65 changes: 52 additions & 13 deletions core/src/main/java/hudson/ProxyConfiguration.java
Expand Up @@ -24,32 +24,30 @@
package hudson;

import com.google.common.collect.Lists;
import com.thoughtworks.xstream.XStream;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.util.FormValidation;
import jenkins.model.Jenkins;
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;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

import com.thoughtworks.xstream.XStream;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import jenkins.model.Jenkins;
import org.jvnet.robust_http_client.RetryableHttpStream;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -65,7 +63,7 @@
* (as described in the Java 6 tech note
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/net/http-auth.html">
* Http Authentication</a>).
*
*
* @see jenkins.model.Jenkins#proxy
*/
public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfiguration> implements Saveable, Serializable {
Expand All @@ -80,7 +78,7 @@ public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfi
/**
* List of host names that shouldn't use proxy, as typed by users.
*
* @see #getNoProxyHostPatterns()
* @see #getNoProxyHostPatterns()
*/
public final String noProxyHost;

Expand All @@ -91,6 +89,8 @@ public final class ProxyConfiguration extends AbstractDescribableImpl<ProxyConfi
* encrypted password
*/
private Secret secretPassword;

private String testUrl;

public ProxyConfiguration(String name, int port) {
this(name,port,null,null);
Expand All @@ -100,13 +100,18 @@ public ProxyConfiguration(String name, int port, String userName, String passwor
this(name,port,userName,password,null);
}

@DataBoundConstructor
public ProxyConfiguration(String name, int port, String userName, String password, String noProxyHost) {
this(name,port,userName,password,noProxyHost,null);
}

@DataBoundConstructor
public ProxyConfiguration(String name, int port, String userName, String password, String noProxyHost, String testUrl) {
this.name = Util.fixEmptyAndTrim(name);
this.port = port;
this.userName = Util.fixEmptyAndTrim(userName);
this.secretPassword = Secret.fromString(password);
this.noProxyHost = Util.fixEmptyAndTrim(noProxyHost);
this.testUrl =Util.fixEmptyAndTrim(testUrl);
}

public String getUserName() {
Expand All @@ -126,6 +131,10 @@ public String getEncryptedPassword() {
return (secretPassword == null) ? null : secretPassword.getEncryptedValue();
}

public String getTestUrl() {
return testUrl;
}

/**
* Returns the list of properly formatted no proxy host names.
*/
Expand Down Expand Up @@ -207,7 +216,7 @@ public static URLConnection open(URL url) throws IOException {

URLConnection con = url.openConnection(p.createProxy(url.getHost()));
if(p.getUserName()!=null) {
// Add an authenticator which provides the credentials for proxy authentication
// Add an authenticator which provides the credentials for proxy authentication
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
Expand All @@ -224,10 +233,14 @@ public PasswordAuthentication getPasswordAuthentication() {

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);

Expand All @@ -245,7 +258,7 @@ public PasswordAuthentication getPasswordAuthentication() {
}
});
}

return is;
}

Expand Down Expand Up @@ -280,5 +293,31 @@ public FormValidation doCheckPort(@QueryParameter String value) {
}
return FormValidation.ok();
}

public FormValidation doValidateProxy(
@QueryParameter("testUrl") String testUrl, @QueryParameter("name") String name, @QueryParameter("port") int port,
@QueryParameter("userName") String userName, @QueryParameter("password") String password,
@QueryParameter("noProxyHost") String noProxyHost) {

if (Util.fixEmptyAndTrim(testUrl) == null) {
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;
try {
is = ProxyConfiguration.getInputStream(new URL(testUrl), pc);
} catch (IOException e) {
return FormValidation.error(e, Messages.ProxyConfiguration_FailedToConnectViaProxy(testUrl));
} finally {
IOUtils.closeQuietly(is);
}

return FormValidation.ok(Messages.ProxyConfiguration_Success());
}
}
}
4 changes: 4 additions & 0 deletions core/src/main/resources/hudson/Messages.properties
Expand Up @@ -63,3 +63,7 @@ PluginManager.ConfigureUpdateCenterPermission.Description=\

AboutJenkins.DisplayName=About Jenkins
AboutJenkins.Description=See the version and license information.

ProxyConfiguration.TestUrlRequired=Test URL is required.
ProxyConfiguration.FailedToConnectViaProxy=Failed to connect to {0}.
ProxyConfiguration.Success=Success
6 changes: 5 additions & 1 deletion core/src/main/resources/hudson/Messages_ja.properties
Expand Up @@ -62,4 +62,8 @@ PluginManager.UploadPluginsPermission.Description=\
PluginManager.ConfigureUpdateCenterPermission.Description=\
\u6a29\u9650"\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u30bb\u30f3\u30bf\u30fc\u306e\u66f4\u65b0"\u304c\u3001\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u30b5\u30a4\u30c8\u3084\u30d7\u30ed\u30ad\u30b7\u30fc\u306e\u8a2d\u5b9a\u3092\u8a31\u53ef\u3059\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002
AboutJenkins.DisplayName=Jenkins\u306b\u3064\u3044\u3066
AboutJenkins.Description=\u30d0\u30fc\u30b8\u30e7\u30f3\u3068\u30e9\u30a4\u30bb\u30f3\u30b9\u60c5\u5831\u3092\u8868\u793a\u3057\u307e\u3059\u3002
AboutJenkins.Description=\u30d0\u30fc\u30b8\u30e7\u30f3\u3068\u30e9\u30a4\u30bb\u30f3\u30b9\u60c5\u5831\u3092\u8868\u793a\u3057\u307e\u3059\u3002

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.Success=\u6210\u529f
Expand Up @@ -17,3 +17,10 @@ f.entry(title:_("Password"),field:"password") {
f.entry(title:_("No Proxy Host"),field:"noProxyHost") {
f.textarea()
}
f.advanced(){
f.entry(title:_("Test URL"),field:"testUrl") {
f.textbox()
}
f.validateButton(title:_("Validate Proxy"),
method:"validateProxy", with:"testUrl,name,port,userName,passwrod,noProxyHost")
}
@@ -1,6 +1,6 @@
# The MIT License
#
# Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
# Copyright (c) 2004-2013, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,4 +24,6 @@ Server=\u30b5\u30fc\u30d0\u30fc
Port=\u30dd\u30fc\u30c8\u756a\u53f7
User\ name=\u30e6\u30fc\u30b6\u30fc\u540d
Password=\u30d1\u30b9\u30ef\u30fc\u30c9
No\ Proxy\ Host=\u5bfe\u8c61\u5916\u30db\u30b9\u30c8
No\ Proxy\ Host=\u5bfe\u8c61\u5916\u30db\u30b9\u30c8
Test\ URL=\u30c6\u30b9\u30c8URL
Validate\ Proxy=Proxy\u306e\u63a5\u7d9a\u78ba\u8a8d

0 comments on commit c853c47

Please sign in to comment.