]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-2709 Send HTTP credentials
authorSimonBrandhof <simon.brandhof@gmail.com>
Mon, 8 Apr 2013 12:57:13 +0000 (14:57 +0200)
committerSimonBrandhof <simon.brandhof@gmail.com>
Mon, 8 Apr 2013 12:57:13 +0000 (14:57 +0200)
pom.xml
sonar-runner-api/pom.xml
sonar-runner-dist/pom.xml
sonar-runner-impl/pom.xml
sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
sonar-runner-impl/src/test/java/org/sonar/runner/impl/NetworkUtil.java [new file with mode: 0644]
sonar-runner-impl/src/test/java/org/sonar/runner/impl/ServerConnectionTest.java

diff --git a/pom.xml b/pom.xml
index badfebafee6aee0061fa984c40713c7281f868bb..60705487d157a08a8564cdfbc84a079aa80e8687 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <artifactId>fest-assert</artifactId>
         <version>1.4</version>
       </dependency>
+      <dependency>
+        <groupId>com.github.kevinsawicki</groupId>
+        <artifactId>http-request</artifactId>
+        <version>4.1</version>
+      </dependency>
       <dependency>
         <groupId>com.google.code.findbugs</groupId>
         <artifactId>jsr305</artifactId>
index f9b21b36cf675841e9655f76de77dfab42f6ec92..4fd6ff88f0eebbc1c6e11c31e36ac1615a6efc99 100644 (file)
             <configuration>
               <createDependencyReducedPom>true</createDependencyReducedPom>
               <minimizeJar>true</minimizeJar>
-              <relocations>
-                <relocation>
-                  <pattern>org.apache.commons.lang</pattern>
-                  <shadedPattern>org.sonar.runner.commonslang</shadedPattern>
-                </relocation>
-              </relocations>
             </configuration>
           </execution>
         </executions>
index 54d2e6d0e80ab1b8a74c814fe3c803ad5a6a598c..4a016d06a932f8c132975c24ffc0bc4f268ae97c 100644 (file)
             <configuration>
               <rules>
                 <requireFilesSize>
-                  <minsize>220000</minsize>
-                  <maxsize>240000</maxsize>
+                  <minsize>250000</minsize>
+                  <maxsize>300000</maxsize>
                   <files>
                     <file>${pom.build.directory}/sonar-runner-${pom.version}.zip</file>
                   </files>
index 688bb812a4dbe0451daef65a531a094fb3983f18..37ffe8d243968464d795191437c2c4cf3c824c07 100644 (file)
   <name>Sonar Runner - Implementation</name>
 
   <dependencies>
+    <dependency>
+      <groupId>com.github.kevinsawicki</groupId>
+      <artifactId>http-request</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.google.code.findbugs</groupId>
+      <artifactId>jsr305</artifactId>
+      <scope>provided</scope>
+    </dependency>
     <dependency>
       <groupId>org.codehaus.sonar</groupId>
       <artifactId>sonar-home</artifactId>
       <version>${pom.version}</version>
       <scope>provided</scope>
     </dependency>
-    <dependency>
-      <groupId>com.google.code.findbugs</groupId>
-      <artifactId>jsr305</artifactId>
-      <scope>provided</scope>
-    </dependency>
 
     <!-- unit tests -->
     <dependency>
             <configuration>
               <createDependencyReducedPom>true</createDependencyReducedPom>
               <minimizeJar>true</minimizeJar>
+              <relocations>
+                <relocation>
+                  <pattern>com.github.kevinsawicki.http</pattern>
+                  <shadedPattern>org.sonar.runner.kevinsawicki</shadedPattern>
+                </relocation>
+              </relocations>
             </configuration>
           </execution>
         </executions>
index 3985bff6513c3e88b7d8dc27d9f54a4d8393b153..3ea0f1b622effdab05c4395ea37dcdb656e4f79d 100644 (file)
  */
 package org.sonar.runner.impl;
 
+import com.github.kevinsawicki.http.HttpRequest;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.net.ConnectException;
-import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.Properties;
@@ -58,70 +53,58 @@ class ServerConnection {
   }
 
   void download(String path, File toFile) {
-    InputStream input = null;
-    FileOutputStream output = null;
     String fullUrl = serverUrl + path;
     try {
       Logs.debug("Download " + fullUrl + " to " + toFile.getAbsolutePath());
-      HttpURLConnection connection = newHttpConnection(new URL(fullUrl));
-      int statusCode = connection.getResponseCode();
-      if (statusCode != HttpURLConnection.HTTP_OK) {
-        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode);
+      HttpRequest httpRequest = newHttpRequest(new URL(fullUrl));
+      if (!httpRequest.ok()) {
+        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code());
       }
-      output = new FileOutputStream(toFile, false);
-      input = connection.getInputStream();
-      IOUtils.copyLarge(input, output);
+      httpRequest.receive(toFile);
 
     } catch (Exception e) {
-      if (e instanceof ConnectException || e instanceof  UnknownHostException) {
+      if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
         Logs.error("Sonar server '" + serverUrl + "' can not be reached");
       }
-      IOUtils.closeQuietly(output);
       FileUtils.deleteQuietly(toFile);
       throw new IllegalStateException("Fail to download: " + fullUrl, e);
 
-    } finally {
-      IOUtils.closeQuietly(input);
-      IOUtils.closeQuietly(output);
     }
   }
 
   String downloadString(String path) throws IOException {
     String fullUrl = serverUrl + path;
-    HttpURLConnection conn = newHttpConnection(new URL(fullUrl));
-    String charset = getCharsetFromContentType(conn.getContentType());
-    if (charset == null || "".equals(charset)) {
-      charset = "UTF-8";
-    }
-    Reader reader = null;
+    HttpRequest httpRequest = newHttpRequest(new URL(fullUrl));
     try {
-      int statusCode = conn.getResponseCode();
-      if (statusCode != HttpURLConnection.HTTP_OK) {
-        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode);
+      String charset = getCharsetFromContentType(httpRequest.contentType());
+      if (charset == null || "".equals(charset)) {
+        charset = "UTF-8";
+      }
+      if (!httpRequest.ok()) {
+        throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code());
       }
-      reader = new InputStreamReader(conn.getInputStream(), charset);
-      return IOUtils.toString(reader);
-    } catch (IOException e) {
-      if (e instanceof ConnectException || e instanceof  UnknownHostException) {
+      return httpRequest.body(charset);
+
+    } catch (HttpRequest.HttpRequestException e) {
+      if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
         Logs.error("Sonar server '" + serverUrl + "' can not be reached");
       }
       throw e;
 
     } finally {
-      IOUtils.closeQuietly(reader);
-      conn.disconnect();
+      httpRequest.disconnect();
     }
   }
 
-  private HttpURLConnection newHttpConnection(URL url) throws IOException {
-    //TODO send credentials
-    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
-    connection.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS);
-    connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS);
-    connection.setInstanceFollowRedirects(true);
-    connection.setRequestMethod("GET");
-    connection.setRequestProperty("User-Agent", userAgent);
-    return connection;
+  private HttpRequest newHttpRequest(URL url) {
+    HttpRequest request = HttpRequest.get(url);
+    request.trustAllCerts().trustAllHosts();
+    request.acceptGzipEncoding().uncompress(true);
+    request.connectTimeout(CONNECT_TIMEOUT_MILLISECONDS).readTimeout(READ_TIMEOUT_MILLISECONDS);
+    request.userAgent(userAgent);
+
+    // TODO send credentials
+    return request;
   }
 
   /**
diff --git a/sonar-runner-impl/src/test/java/org/sonar/runner/impl/NetworkUtil.java b/sonar-runner-impl/src/test/java/org/sonar/runner/impl/NetworkUtil.java
new file mode 100644 (file)
index 0000000..ccc5a53
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Sonar Runner - Implementation
+ * Copyright (C) 2011 SonarSource
+ * dev@sonar.codehaus.org
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.runner.impl;
+
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.Arrays;
+import java.util.List;
+
+class NetworkUtil {
+  static int getNextAvailablePort() {
+    for (int index = 0; index < 5; index++) {
+      try {
+        ServerSocket socket = new ServerSocket(0);
+        int unusedPort = socket.getLocalPort();
+        socket.close();
+        if (isValidPort(unusedPort)) {
+          return unusedPort;
+        }
+
+      } catch (IOException e) {
+        throw new IllegalStateException("Can not find an open network port", e);
+      }
+    }
+    throw new IllegalStateException("Can not find an open network port");
+  }
+
+  // Firefox blocks some reserverd ports : http://www-archive.mozilla.org/projects/netlib/PortBanning.html
+  private static final List<Integer> BLOCKED_PORTS = Arrays.asList(2049, 4045, 6000);
+
+  static boolean isValidPort(int port) {
+    return port > 1023 && !BLOCKED_PORTS.contains(port);
+  }
+}
index ca722e32c7233a0335ac7f0f22b48b3e1dc768fe..378aed5941cdd691c572134e412bc99616ed5058 100644 (file)
@@ -28,6 +28,7 @@ import java.io.File;
 import java.util.Properties;
 
 import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
 
 public class ServerConnectionTest {
 
@@ -61,4 +62,34 @@ public class ServerConnectionTest {
 
     assertThat(FileUtils.readFileToString(toFile)).isEqualTo("abcde");
   }
+
+  @Test
+  public void should_not_download_file_when_host_is_down() throws Exception {
+    Properties props = new Properties();
+    props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort());
+
+    ServerConnection connection = ServerConnection.create(props);
+    File toFile = temp.newFile();
+    try {
+      connection.download("/batch/index.txt", toFile);
+      fail();
+    } catch (Exception e) {
+      // success
+    }
+  }
+
+  @Test
+  public void should_not_download_string_when_host_is_down() throws Exception {
+    Properties props = new Properties();
+    props.setProperty("sonar.host.url", "http://localhost:" + NetworkUtil.getNextAvailablePort());
+
+    ServerConnection connection = ServerConnection.create(props);
+    File toFile = temp.newFile();
+    try {
+      connection.download("/batch/index.txt", toFile);
+      fail();
+    } catch (Exception e) {
+      // success
+    }
+  }
 }