From 2012b86cad97d60d0a0310e5059625a29740a5e0 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sat, 3 Sep 2011 17:42:21 +0200 Subject: [PATCH] added support for proxy servers with authentication --- .../sonia/scm/config/ScmConfiguration.java | 56 +++++++++++++++++++ .../java/sonia/scm/net/URLHttpClient.java | 30 ++++++++++ .../js/config/sonia.config.scmconfigpanel.js | 19 +++++++ 3 files changed, 105 insertions(+) diff --git a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java index c73c53436b..8a1704bff3 100644 --- a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -158,6 +158,8 @@ public class ScmConfiguration implements ListenerSupport this.enableProxy = other.enableProxy; this.proxyPort = other.proxyPort; this.proxyServer = other.proxyServer; + this.proxyUser = other.proxyUser; + this.proxyPassword = other.proxyPassword; this.forceBaseUrl = other.forceBaseUrl; this.baseUrl = other.baseUrl; @@ -259,6 +261,18 @@ public class ScmConfiguration implements ListenerSupport return pluginUrl; } + /** + * Method description + * + * + * @return + * @since 1.7 + */ + public String getProxyPassword() + { + return proxyPassword; + } + /** * Returns the proxy port. * @@ -281,6 +295,18 @@ public class ScmConfiguration implements ListenerSupport return proxyServer; } + /** + * Method description + * + * + * @return + * @since 1.7 + */ + public String getProxyUser() + { + return proxyUser; + } + /** * Returns the servername of the SCM-Manager host. * @@ -496,6 +522,18 @@ public class ScmConfiguration implements ListenerSupport this.pluginUrl = pluginUrl; } + /** + * Method description + * + * + * @param proxyPassword + * @since 1.7 + */ + public void setProxyPassword(String proxyPassword) + { + this.proxyPassword = proxyPassword; + } + /** * Method description * @@ -518,6 +556,18 @@ public class ScmConfiguration implements ListenerSupport this.proxyServer = proxyServer; } + /** + * Method description + * + * + * @param proxyUser + * @since 1.7 + */ + public void setProxyUser(String proxyUser) + { + this.proxyUser = proxyUser; + } + /** * Method description * @@ -574,12 +624,18 @@ public class ScmConfiguration implements ListenerSupport @XmlElement(name = "plugin-url") private String pluginUrl = DEFAULT_PLUGINURL; + /** Field description */ + private String proxyPassword; + /** Field description */ private int proxyPort = 8080; /** Field description */ private String proxyServer = "proxy.mydomain.com"; + /** Field description */ + private String proxyUser; + /** @deprecated use {@link #baseUrl} */ private String servername = "localhost"; diff --git a/scm-webapp/src/main/java/sonia/scm/net/URLHttpClient.java b/scm-webapp/src/main/java/sonia/scm/net/URLHttpClient.java index 38fc39e4b5..3f298fe6cc 100644 --- a/scm-webapp/src/main/java/sonia/scm/net/URLHttpClient.java +++ b/scm-webapp/src/main/java/sonia/scm/net/URLHttpClient.java @@ -47,6 +47,8 @@ import sonia.scm.util.Util; //~--- JDK imports ------------------------------------------------------------ +import com.sun.jersey.core.util.Base64; + import java.io.IOException; import java.io.OutputStreamWriter; @@ -69,6 +71,9 @@ import java.util.Map; public class URLHttpClient implements HttpClient { + /** Field description */ + public static final String CREDENTIAL_SEPARATOR = ":"; + /** Field description */ public static final String ENCODING = "UTF-8"; @@ -78,6 +83,9 @@ public class URLHttpClient implements HttpClient /** Field description */ public static final String HEADER_ACCEPT_ENCODING_VALUE = "gzip"; + /** Field description */ + public static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization"; + /** Field description */ public static final String HEADER_USERAGENT = "User-Agent"; @@ -87,6 +95,9 @@ public class URLHttpClient implements HttpClient /** Field description */ public static final String METHOD_POST = "POST"; + /** Field description */ + public static final String PREFIX_BASIC_AUTHENTICATION = "Basic "; + /** Field description */ public static final int TIMEOUT_CONNECTION = 30000; @@ -353,6 +364,25 @@ public class URLHttpClient implements HttpClient connection.setRequestProperty( HEADER_USERAGENT, HEADER_USERAGENT_VALUE.concat(context.getVersion())); + String username = configuration.getProxyUser(); + String password = configuration.getProxyPassword(); + + if (Util.isNotEmpty(username) || Util.isNotEmpty(password)) + { + if (logger.isDebugEnabled()) + { + logger.debug("enable proxy authentication for user '{}'", + Util.nonNull(username)); + } + + String auth = Util.nonNull(username).concat(CREDENTIAL_SEPARATOR).concat( + Util.nonNull(password)); + + auth = PREFIX_BASIC_AUTHENTICATION.concat( + new String(Base64.encode(auth.getBytes()))); + connection.setRequestProperty(HEADER_PROXY_AUTHORIZATION, auth); + } + return connection; } diff --git a/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js b/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js index 7ddc8fa317..508b16d59e 100644 --- a/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js +++ b/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js @@ -125,6 +125,8 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ check: function(){ Ext.getCmp('proxyServer').setDisabled( ! this.checked ); Ext.getCmp('proxyPort').setDisabled( ! this.checked ); + Ext.getCmp('proxyUser').setDisabled( ! this.checked ); + Ext.getCmp('proxyPassword').setDisabled( ! this.checked ); } } },{ @@ -143,6 +145,23 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ disabled: true, allowBlank: false, helpText: this.proxyPortHelpText + },{ + id: 'proxyUser', + xtype: 'textfield', + fieldLabel: this.proxyUserText, + name: 'proxyUser', + disabled: true, + helpText: this.proxyUserHelpText, + allowBlank: true + },{ + id: 'proxyPassword', + xtype: 'textfield', + inputType: 'password', + fieldLabel: this.proxyPasswordText, + name: 'proxyPassword', + disabled: true, + helpText: this.proxyPasswordHelpText, + allowBlank: true },{ xtype : 'textfield', fieldLabel : this.adminGroupsText,