summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java36
-rw-r--r--sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java28
2 files changed, 60 insertions, 4 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
index f46baa4f42c..84e2c8707ca 100644
--- a/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
+++ b/sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
@@ -19,22 +19,30 @@
*/
package org.sonar.server.plugins;
+import com.google.common.base.Joiner;
+import com.google.common.collect.Lists;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.ServerComponent;
import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.Logs;
+import org.sonar.api.utils.SonarException;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.UpdateCenterDeserializer;
import java.io.InputStream;
+import java.net.Proxy;
+import java.net.ProxySelector;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.Date;
+import java.util.List;
import java.util.Properties;
/**
* HTTP client to load data from the remote update center hosted at http://update.sonarsource.org.
+ *
* @since 2.4
*/
public class UpdateCenterClient implements ServerComponent {
@@ -54,7 +62,7 @@ public class UpdateCenterClient implements ServerComponent {
UpdateCenterClient(HttpDownloader downloader, String url) {
this.downloader = downloader;
this.url = url;
- Logs.INFO.info("Update center: " + url);
+ Logs.INFO.info("Update center: " + url + " (" + sumUpProxyConfiguration(url) + ")");
}
public UpdateCenterClient(HttpDownloader downloader, Configuration configuration) {
@@ -74,7 +82,7 @@ public class UpdateCenterClient implements ServerComponent {
}
public Date getLastRefreshDate() {
- return lastRefreshDate >0 ? new Date(lastRefreshDate) : null;
+ return lastRefreshDate > 0 ? new Date(lastRefreshDate) : null;
}
private boolean needsRefresh() {
@@ -99,4 +107,28 @@ public class UpdateCenterClient implements ServerComponent {
}
return null;
}
+
+ private static String sumUpProxyConfiguration(String url) {
+ return sumUpProxyConfiguration(url, ProxySelector.getDefault());
+ }
+
+ static String sumUpProxyConfiguration(String url, ProxySelector proxySelector) {
+ try {
+ List<String> descriptions = Lists.newArrayList();
+ List<Proxy> proxies = proxySelector.select(new URI(url));
+ if (proxies.size() == 1 && proxies.get(0).type().equals(Proxy.Type.DIRECT)) {
+ descriptions.add("no HTTP proxy");
+ } else {
+ for (Proxy proxy : proxies) {
+ if (!proxy.type().equals(Proxy.Type.DIRECT)) {
+ descriptions.add("proxy: " + proxy.address().toString());
+ }
+ }
+ }
+ return Joiner.on(", ").join(descriptions);
+
+ } catch (URISyntaxException e) {
+ throw new SonarException("Can not load configuration of HTTP proxies");
+ }
+ }
}
diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java b/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java
index 6d206f3d9d3..1186e26e252 100644
--- a/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java
@@ -28,10 +28,11 @@ import org.sonar.server.plugins.UpdateCenterClient;
import org.sonar.updatecenter.common.UpdateCenter;
import org.sonar.updatecenter.common.Version;
-import java.net.URI;
-import java.net.URISyntaxException;
+import java.net.*;
+import java.util.Arrays;
import static junit.framework.Assert.assertNull;
+import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
import static org.mockito.Matchers.anyObject;
@@ -83,4 +84,27 @@ public class UpdateCenterClientTest {
verify(downloader, times(2)).openStream(new URI(BASE_URL));
}
+
+ @Test
+ public void shouldSumUpDirectProxyConfiguration() {
+ ProxySelector proxySelector = mock(ProxySelector.class);
+ when(proxySelector.select((URI)anyObject())).thenReturn(Arrays.asList(Proxy.NO_PROXY));
+ assertThat(UpdateCenterClient.sumUpProxyConfiguration("http://updatecenter", proxySelector), is("no HTTP proxy"));
+ }
+
+ @Test
+ public void shouldSumUpProxyConfiguration() {
+ ProxySelector proxySelector = mock(ProxySelector.class);
+ when(proxySelector.select((URI)anyObject())).thenReturn(Arrays.asList((Proxy)new FakeProxy()));
+ assertThat(UpdateCenterClient.sumUpProxyConfiguration("http://updatecenter", proxySelector), is("proxy: http://updatecenter:80"));
+ }
+
+
+}
+
+class FakeProxy extends Proxy {
+
+ public FakeProxy() {
+ super(Type.HTTP, new InetSocketAddress("http://updatecenter", 80));
+ }
}