]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3895 Improve Http error handling
authorDavid Gageot <david@gageot.net>
Wed, 7 Nov 2012 14:53:42 +0000 (15:53 +0100)
committerDavid Gageot <david@gageot.net>
Wed, 7 Nov 2012 14:53:42 +0000 (15:53 +0100)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/DryRunDatabase.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/DryRunDatabaseTest.java
sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java

index 0f4898263459ef569e92bc7fe53e9aa03e8173a7..837e60d0731a6499461cb244a8e0c14a1ea1ffce 100644 (file)
@@ -31,8 +31,8 @@ import org.sonar.api.database.DatabaseProperties;
 import org.sonar.api.utils.SonarException;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
+
+import static org.sonar.api.utils.HttpDownloader.HttpException;
 
 /**
  * @since 3.4
@@ -52,8 +52,8 @@ public class DryRunDatabase implements BatchComponent {
   private final ProjectReactor reactor;
 
   public DryRunDatabase(Settings settings, ServerClient server, TempDirectories tempDirectories, ProjectReactor reactor,
-                        // project reactor must be completely built
-                        ProjectReactorReady reactorReady) {
+      // project reactor must be completely built
+      ProjectReactorReady reactorReady) {
     this.settings = settings;
     this.server = server;
     this.tempDirectories = tempDirectories;
@@ -76,9 +76,7 @@ public class DryRunDatabase implements BatchComponent {
       server.download("/batch_bootstrap/db?project=" + projectKey, toFile);
     } catch (SonarException e) {
       Throwable rootCause = Throwables.getRootCause(e);
-      if (rootCause instanceof FileNotFoundException) {
-        throw new SonarException(String.format("Project [%s] doesn't exist on server", projectKey), e);
-      } else if ((rootCause instanceof IOException) && (StringUtils.contains(rootCause.getMessage(), "401"))) {
+      if ((rootCause instanceof HttpException) && (((HttpException) rootCause).getResponseCode() == 401)) {
         throw new SonarException(String.format("You don't have access rights to project [%s]", projectKey), e);
       }
       throw e;
@@ -87,11 +85,11 @@ public class DryRunDatabase implements BatchComponent {
 
   private void replaceSettings(String databasePath) {
     settings
-      .removeProperty("sonar.jdbc.schema")
-      .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT)
-      .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER)
-      .setProperty(DatabaseProperties.PROP_USER, USER)
-      .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD)
-      .setProperty(DatabaseProperties.PROP_URL, URL + databasePath);
+        .removeProperty("sonar.jdbc.schema")
+        .setProperty(DatabaseProperties.PROP_DIALECT, DIALECT)
+        .setProperty(DatabaseProperties.PROP_DRIVER, DRIVER)
+        .setProperty(DatabaseProperties.PROP_USER, USER)
+        .setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD)
+        .setProperty(DatabaseProperties.PROP_URL, URL + databasePath);
   }
 }
index ae4f2826f0102dd2be0b7c5a6998515af43d8573..40fef228a4aeda86db17b6bfd876ea64135211d8 100644 (file)
@@ -28,11 +28,10 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.config.Settings;
 import org.sonar.api.database.DatabaseProperties;
+import org.sonar.api.utils.HttpDownloader;
 import org.sonar.api.utils.SonarException;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.doThrow;
@@ -87,19 +86,9 @@ public class DryRunDatabaseTest {
     assertThat(settings.getString(DatabaseProperties.PROP_URL)).isEqualTo("jdbc:h2:/tmp/dryrun");
   }
 
-  @Test
-  public void should_fail_on_unknown_project() {
-    doThrow(new SonarException(new FileNotFoundException())).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
-
-    thrown.expect(SonarException.class);
-    thrown.expectMessage("Project [group:project] doesn't exist on server");
-
-    dryRunDatabase.start();
-  }
-
   @Test
   public void should_fail_on_invalid_role() {
-    doThrow(new SonarException(new IOException("HTTP 401"))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
+    doThrow(new SonarException(new HttpDownloader.HttpException(null, 401))).when(server).download("/batch_bootstrap/db?project=group:project", databaseFile);
 
     thrown.expect(SonarException.class);
     thrown.expectMessage("You don't have access rights to project [group:project]");
index 6e9bb112de47279690fa94d8b472bb5eca835ccf..2e739d1edbe3f522fb0ed0c60578c5cc6f79ed8c 100644 (file)
@@ -225,6 +225,12 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo
         connection.setUseCaches(true);
         connection.setInstanceFollowRedirects(true);
         connection.setRequestProperty("User-Agent", userAgent);
+
+        int responseCode = connection.getResponseCode();
+        if (responseCode >= 400) {
+          throw new HttpException(uri, responseCode);
+        }
+
         return connection.getInputStream();
       }
     }
@@ -242,4 +248,23 @@ public class HttpDownloader extends UriReader.SchemeProcessor implements BatchCo
       }
     }
   }
+
+  public static class HttpException extends IOException {
+    private final URI uri;
+    private final int responseCode;
+
+    public HttpException(URI uri, int responseCode) {
+      super("Fail to download [" + uri + "]. Response code: " + responseCode);
+      this.uri = uri;
+      this.responseCode = responseCode;
+    }
+
+    public int getResponseCode() {
+      return responseCode;
+    }
+
+    public URI getUri() {
+      return uri;
+    }
+  }
 }