]> source.dussan.org Git - sonarqube.git/commitdiff
Bootstrapper should send User-Agent request-header field
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 9 Feb 2011 21:15:48 +0000 (00:15 +0300)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 9 Feb 2011 21:15:48 +0000 (00:15 +0300)
sonar-batch-bootstrapper/pom.xml
sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/Bootstrapper.java
sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/BootstrapperVersion.java [new file with mode: 0644]
sonar-batch-bootstrapper/src/main/resources/org/sonar/batch/bootstrapper/version.txt [new file with mode: 0644]
sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java
sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperVersionTest.java [new file with mode: 0644]

index 75a7498f4566811c1f661bd881b32fb58be11b18..dae9cde4f602273a6d8872667c06b86442fd1ebc 100644 (file)
       <scope>test</scope>
     </dependency>
   </dependencies>
+  
+  <build>
+    <resources>
+      <resource>
+        <directory>src/main/resources</directory>
+        <filtering>true</filtering>
+      </resource>
+    </resources>
+  </build>
 </project>
index 5dbd4f6edcac83836d00a85fa830abf890eac524..b5612713503fe8f68a0dd96628457c3294f96560 100644 (file)
@@ -36,9 +36,14 @@ public class Bootstrapper {
 
   private File bootDir;
   private String serverUrl;
+  private String productToken;
   private String serverVersion;
 
-  public Bootstrapper(String serverUrl, File workDir) {
+  /**
+   * @param productToken part of User-Agent request-header field - see http://tools.ietf.org/html/rfc1945#section-10.15
+   */
+  public Bootstrapper(String productToken, String serverUrl, File workDir) {
+    this.productToken = productToken;
     bootDir = new File(workDir, "batch");
     bootDir.mkdirs();
     if (serverUrl.endsWith("/")) {
@@ -128,13 +133,20 @@ public class Bootstrapper {
     }
   }
 
-  static HttpURLConnection newHttpConnection(URL url) throws IOException {
+  /**
+   * By convention, the product tokens are listed in order of their significance for identifying the application.
+   */
+  String getUserAgent() {
+    return "sonar-bootstrapper/" + BootstrapperVersion.getVersion() + " " + productToken;
+  }
+
+  HttpURLConnection newHttpConnection(URL url) throws IOException {
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
     connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS);
     connection.setInstanceFollowRedirects(true);
     connection.setRequestMethod("GET");
-    // TODO connection.setRequestProperty("User-Agent", userAgent);
+    connection.setRequestProperty("User-Agent", getUserAgent());
     return connection;
   }
 
diff --git a/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/BootstrapperVersion.java b/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/BootstrapperVersion.java
new file mode 100644 (file)
index 0000000..0f46384
--- /dev/null
@@ -0,0 +1,33 @@
+package org.sonar.batch.bootstrapper;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+public enum BootstrapperVersion {
+
+  INSTANCE;
+
+  private static final String PROPERTIES_PATH = "/org/sonar/batch/bootstrapper/version.txt";
+  private String version;
+
+  public static String getVersion() {
+    return INSTANCE.version;
+  }
+
+  private BootstrapperVersion() {
+    InputStream input = getClass().getResourceAsStream(PROPERTIES_PATH);
+    try {
+      Properties properties = new Properties();
+      properties.load(input);
+      this.version = properties.getProperty("version");
+
+    } catch (IOException e) {
+      // Can not load the version
+      this.version = "";
+
+    } finally {
+      BootstrapperIOUtils.closeQuietly(input);
+    }
+  }
+}
diff --git a/sonar-batch-bootstrapper/src/main/resources/org/sonar/batch/bootstrapper/version.txt b/sonar-batch-bootstrapper/src/main/resources/org/sonar/batch/bootstrapper/version.txt
new file mode 100644 (file)
index 0000000..defbd48
--- /dev/null
@@ -0,0 +1 @@
+version=${project.version}
index 698db7d9de46434c90c8289f6b0ddbebdc399ed5..79d03f73fac8ef19502662a4de42b149c1813fe3 100644 (file)
  */
 package org.sonar.batch.bootstrapper;
 
-import org.junit.Test;
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.assertThat;
 
 import java.io.File;
 import java.io.IOException;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
+import org.junit.Test;
 
 public class BootstrapperTest {
 
   @Test
   public void shouldRemoveLastUrlSlash() {
-    Bootstrapper bootstrapper = new Bootstrapper("http://test/", new File("target"));
+    Bootstrapper bootstrapper = new Bootstrapper("", "http://test/", new File("target"));
     assertThat(bootstrapper.getServerUrl(), is("http://test"));
   }
 
   @Test(expected = Exception.class)
   public void shouldFailIfCanNotConnectServer() {
-    Bootstrapper bootstrapper = new Bootstrapper("http://unknown.foo", new File("target"));
+    Bootstrapper bootstrapper = new Bootstrapper("", "http://unknown.foo", new File("target"));
     bootstrapper.getServerVersion();
   }
 
+  @Test
+  public void shouldReturnUserAgent() {
+    Bootstrapper bootstrapper = new Bootstrapper("test/0.1", "http://unknown.foo", new File("target"));
+    String userAgent = bootstrapper.getUserAgent();
+    assertThat(userAgent.length(), greaterThan(0));
+    assertThat(userAgent, allOf(startsWith("sonar-bootstrapper/"), endsWith(" test/0.1")));
+  }
+
   @Test
   public void shouldReturnValidVersion() {
-    Bootstrapper bootstrapper = new Bootstrapper("http://test", new File("target")) {
+    Bootstrapper bootstrapper = new Bootstrapper("", "http://test", new File("target")) {
       @Override
       String remoteContent(String path) throws IOException {
         return "2.6";
diff --git a/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperVersionTest.java b/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperVersionTest.java
new file mode 100644 (file)
index 0000000..843023c
--- /dev/null
@@ -0,0 +1,18 @@
+package org.sonar.batch.bootstrapper;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class BootstrapperVersionTest {
+
+  @Test
+  public void shouldLoadVersion() {
+    String version = BootstrapperVersion.getVersion();
+    assertThat(version, containsString("."));
+    assertThat(version, not(containsString("$")));
+  }
+
+}