diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java index f5a9382414..b0136e8275 100644 --- a/core/src/main/java/hudson/PluginManager.java +++ b/core/src/main/java/hudson/PluginManager.java @@ -186,19 +186,18 @@ public final class PluginManager extends AbstractModelObject { } public void doProxyConfigure(@QueryParameter("proxy.server") String server, @QueryParameter("proxy.port") String port, StaplerResponse rsp) throws IOException { - setProp("http.proxyHost", Util.fixEmptyAndTrim(server)); - setProp("http.proxyPort",Util.fixEmptyAndTrim(port)); - setProp("https.proxyHost", Util.fixEmptyAndTrim(server)); - setProp("https.proxyPort",Util.fixEmptyAndTrim(port)); + Hudson hudson = Hudson.getInstance(); + server = Util.fixEmptyAndTrim(server); + if(server==null) { + hudson.proxy = null; + ProxyConfiguration.getXmlFile().delete(); + } else { + hudson.proxy = new ProxyConfiguration(server,Integer.parseInt(Util.fixEmptyAndTrim(port))); + hudson.proxy.save(); + } rsp.sendRedirect("./advanced"); } - private void setProp(String key, String value) { - // System.setProperty causes NPE if t he value is null. - if(value==null) System.getProperties().remove(key); - else System.setProperty(key,value); - } - /** * Uploads a plugin. */ diff --git a/core/src/main/java/hudson/ProxyConfiguration.java b/core/src/main/java/hudson/ProxyConfiguration.java new file mode 100644 index 0000000000..2c57581941 --- /dev/null +++ b/core/src/main/java/hudson/ProxyConfiguration.java @@ -0,0 +1,51 @@ +package hudson; + +import com.thoughtworks.xstream.XStream; +import hudson.model.Hudson; +import hudson.util.XStream2; + +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Proxy; + +/** + * HTTP proxy configuration. + * + * @see Hudson#proxy + */ +public final class ProxyConfiguration { + public final String name; + public final int port; + + public ProxyConfiguration(String name, int port) { + this.name = name; + this.port = port; + } + + public Proxy createProxy() { + return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(name,port)); + } + + public void save() throws IOException { + getXmlFile().write(this); + } + + public static XmlFile getXmlFile() { + return new XmlFile(XSTREAM, new File(Hudson.getInstance().getRootDir(), "proxy.xml")); + } + + public static ProxyConfiguration load() throws IOException { + XmlFile f = getXmlFile(); + if(f.exists()) + return (ProxyConfiguration) f.read(); + else + return null; + } + + private static final XStream XSTREAM = new XStream2(); + + static { + XSTREAM.alias("proxy", ProxyConfiguration.class); + } +} diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 95b815ad6c..f9e4893e9c 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -14,6 +14,7 @@ import hudson.TcpSlaveAgentListener; import hudson.Util; import static hudson.Util.fixEmpty; import hudson.XmlFile; +import hudson.ProxyConfiguration; import hudson.slaves.ComputerLauncher; import hudson.slaves.RetentionStrategy; import hudson.model.Descriptor.FormException; @@ -102,6 +103,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.net.URL; +import java.net.Proxy; import java.security.SecureRandom; import java.text.NumberFormat; import java.text.ParseException; @@ -300,6 +302,12 @@ public final class Hudson extends View implements ItemGroup, Node, private transient final UpdateCenter updateCenter = new UpdateCenter(); + /** + * HTTP proxy configuration. + */ + public transient volatile ProxyConfiguration proxy; + + public Hudson(File root, ServletContext context) throws IOException { this.root = root; this.servletContext = context; @@ -328,6 +336,12 @@ public final class Hudson extends View implements ItemGroup, Node, secretFile.write(secretKey); } + try { + proxy = ProxyConfiguration.load(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Failed to load proxy configuration", e); + } + // load plugins. pluginManager = new PluginManager(context); @@ -368,6 +382,16 @@ public final class Hudson extends View implements ItemGroup, Node, return slaveAgentPort; } + /** + * Obtains the {@link Proxy} instance to talk to other HTTP servers. + */ + public Proxy createProxy() { + if(proxy==null) + return Proxy.NO_PROXY; + else + return proxy.createProxy(); + } + /** * If you are calling this on Hudson something is wrong. * diff --git a/core/src/main/java/hudson/model/UpdateCenter.java b/core/src/main/java/hudson/model/UpdateCenter.java index 996f4ed220..f5cbe28e6c 100644 --- a/core/src/main/java/hudson/model/UpdateCenter.java +++ b/core/src/main/java/hudson/model/UpdateCenter.java @@ -364,7 +364,7 @@ public class UpdateCenter implements ModelObject { } private void testConnection(URL url) throws IOException { - InputStream in = url.openStream(); + InputStream in = url.openConnection(Hudson.getInstance().createProxy()).getInputStream(); IOUtils.copy(in,new ByteArrayOutputStream()); in.close(); } @@ -404,7 +404,7 @@ public class UpdateCenter implements ModelObject { // In the future if we are to open up update center to 3rd party, we need more elaborate scheme // like signing to ensure the safety of the bits. - URLConnection con = new URL(plugin.url).openConnection(); + URLConnection con = new URL(plugin.url).openConnection(Hudson.getInstance().createProxy()); int total = con.getContentLength(); CountingInputStream in = new CountingInputStream(con.getInputStream()); byte[] buf = new byte[8192]; diff --git a/core/src/main/resources/hudson/PluginManager/advanced.jelly b/core/src/main/resources/hudson/PluginManager/advanced.jelly index 59949852eb..f165d0e4eb 100644 --- a/core/src/main/resources/hudson/PluginManager/advanced.jelly +++ b/core/src/main/resources/hudson/PluginManager/advanced.jelly @@ -15,10 +15,10 @@ - + - +