From 24a49907b94212826a2f1d270371a9e156774f60 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 12 Sep 2016 10:54:28 +0200 Subject: [PATCH] SONAR-6662 add ServerId to HTTP user agent of UPC client --- .../core/util/DefaultHttpDownloader.java | 9 ++++-- .../core/util/DefaultHttpDownloaderTest.java | 30 +++++++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java index 09d7991a06d..ed174aff4c9 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java +++ b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java @@ -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); } diff --git a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java index 60884967562..3dbd4c88800 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java @@ -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 -- 2.39.5