]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6662 add ServerId to HTTP user agent of UPC client 1234/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 12 Sep 2016 08:54:28 +0000 (10:54 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 12 Sep 2016 15:12:12 +0000 (17:12 +0200)
sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java
sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java

index 09d7991a06d3001880f710161a1faa9d856fafc3..ed174aff4c90b59ab6d6b909ad6174371cd60ee8 100644 (file)
@@ -37,10 +37,12 @@ import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Optional;
 import java.util.zip.GZIPInputStream;
 import javax.annotation.Nullable;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.io.IOUtils;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.platform.Server;
 import org.sonar.api.utils.HttpDownloader;
@@ -189,7 +191,7 @@ public class DefaultHttpDownloader extends HttpDownloader {
 
     BaseHttpDownloader(SystemFacade system, Settings settings, @Nullable String userAgent) {
       initProxy(system, settings);
-      initUserAgent(userAgent);
+      initUserAgent(userAgent, settings);
     }
 
     private void initProxy(SystemFacade system, Settings settings) {
@@ -216,8 +218,9 @@ public class DefaultHttpDownloader extends HttpDownloader {
       }
     }
 
-    private void initUserAgent(@Nullable String sonarVersion) {
-      userAgent = sonarVersion == null ? "SonarQube" : String.format("SonarQube %s", sonarVersion);
+    private void initUserAgent(@Nullable String sonarVersion, Settings settings) {
+      String serverId = settings.getString(CoreProperties.SERVER_ID);
+      userAgent = sonarVersion == null ? "SonarQube" : String.format("SonarQube %s # %s", sonarVersion, Optional.ofNullable(serverId).orElse(""));
       System.setProperty("http.agent", userAgent);
     }
 
index 60884967562ae017830aa4a88c630a74c308469c..3dbd4c888003c5f06b87442bd429773db132c2fd 100644 (file)
@@ -51,6 +51,7 @@ import org.simpleframework.http.Request;
 import org.simpleframework.http.Response;
 import org.simpleframework.http.core.Container;
 import org.simpleframework.transport.connect.SocketConnection;
+import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.platform.Server;
@@ -214,7 +215,22 @@ public class DefaultHttpDownloaderTest {
   }
 
   @Test
-  public void userAgentIsSonarVersion() throws URISyntaxException, IOException {
+  public void userAgent_includes_version_and_SERVER_ID_when_server_is_provided() throws URISyntaxException, IOException {
+    Server server = mock(Server.class);
+    when(server.getVersion()).thenReturn("2.2");
+    MapSettings settings = new MapSettings();
+    settings.setProperty(CoreProperties.SERVER_ID, "blablabla");
+
+    InputStream stream = new DefaultHttpDownloader(server, settings).openStream(new URI(baseUrl));
+    Properties props = new Properties();
+    props.load(stream);
+    stream.close();
+
+    assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2 # blablabla");
+  }
+
+  @Test
+  public void userAgent_includes_only_version_when_there_is_no_SERVER_ID_and_server_is_provided() throws URISyntaxException, IOException {
     Server server = mock(Server.class);
     when(server.getVersion()).thenReturn("2.2");
 
@@ -223,7 +239,17 @@ public class DefaultHttpDownloaderTest {
     props.load(stream);
     stream.close();
 
-    assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2");
+    assertThat(props.getProperty("agent")).isEqualTo("SonarQube 2.2 #");
+  }
+
+  @Test
+  public void userAgent_is_static_value_when_server_is_not_provided() throws URISyntaxException, IOException {
+    InputStream stream = new DefaultHttpDownloader(new MapSettings()).openStream(new URI(baseUrl));
+    Properties props = new Properties();
+    props.load(stream);
+    stream.close();
+
+    assertThat(props.getProperty("agent")).isEqualTo("SonarQube");
   }
 
   @Test