diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-10 00:15:48 +0300 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2011-02-10 00:15:48 +0300 |
commit | 0ad2ba4fc87d0534bf274c54a5f7d63937671186 (patch) | |
tree | 373641b691bd07e195cb46692435c6b9a534dd29 /sonar-batch-bootstrapper | |
parent | 964f6ca40e2e0ddef9a3b8532f64e707349719ef (diff) | |
download | sonarqube-0ad2ba4fc87d0534bf274c54a5f7d63937671186.tar.gz sonarqube-0ad2ba4fc87d0534bf274c54a5f7d63937671186.zip |
Bootstrapper should send User-Agent request-header field
Diffstat (limited to 'sonar-batch-bootstrapper')
6 files changed, 90 insertions, 9 deletions
diff --git a/sonar-batch-bootstrapper/pom.xml b/sonar-batch-bootstrapper/pom.xml index 75a7498f456..dae9cde4f60 100644 --- a/sonar-batch-bootstrapper/pom.xml +++ b/sonar-batch-bootstrapper/pom.xml @@ -24,4 +24,13 @@ <scope>test</scope> </dependency> </dependencies> + + <build> + <resources> + <resource> + <directory>src/main/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + </build> </project> diff --git a/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/Bootstrapper.java b/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/Bootstrapper.java index 5dbd4f6edca..b5612713503 100644 --- a/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/Bootstrapper.java +++ b/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/Bootstrapper.java @@ -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 index 00000000000..0f4638498e6 --- /dev/null +++ b/sonar-batch-bootstrapper/src/main/java/org/sonar/batch/bootstrapper/BootstrapperVersion.java @@ -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 index 00000000000..defbd48204e --- /dev/null +++ b/sonar-batch-bootstrapper/src/main/resources/org/sonar/batch/bootstrapper/version.txt @@ -0,0 +1 @@ +version=${project.version} diff --git a/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java b/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java index 698db7d9de4..79d03f73fac 100644 --- a/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java +++ b/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperTest.java @@ -19,31 +19,39 @@ */ 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 index 00000000000..843023c0c75 --- /dev/null +++ b/sonar-batch-bootstrapper/src/test/java/org/sonar/batch/bootstrapper/BootstrapperVersionTest.java @@ -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("$"))); + } + +} |