]> source.dussan.org Git - sonarqube.git/commitdiff
UpdateCenter: log HTTP proxy configuration
authorsimonbrandhof <simon.brandhof@gmail.com>
Thu, 4 Nov 2010 15:50:19 +0000 (15:50 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Thu, 4 Nov 2010 15:50:19 +0000 (15:50 +0000)
sonar-server/src/main/java/org/sonar/server/plugins/UpdateCenterClient.java
sonar-server/src/test/java/org/sonar/server/plugins/UpdateCenterClientTest.java

index f46baa4f42cb5c29b6c66b23bac94c8b313e49c9..84e2c8707ca8c0da5ae9e2c608d09c42f190e912 100644 (file)
  */
 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");
+    }
+  }
 }
index 6d206f3d9d3c79b000a1e1714e327cb552b3f122..1186e26e25204a7333fa5c574f11c06366451267 100644 (file)
@@ -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));
+  }
 }