From: Julien HENRY Date: Thu, 26 Nov 2015 08:31:09 +0000 (+0100) Subject: Fix quality flaws X-Git-Tag: 5.3-RC1~151 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fpull%2F655%2Fhead;p=sonarqube.git Fix quality flaws --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index a292451e818..ec34bfb7cef 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -19,7 +19,6 @@ */ package org.sonar.batch.bootstrap; -import org.sonar.api.utils.MessageException; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.base.Strings; @@ -42,6 +41,7 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.BatchSide; import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.MessageException; import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.core.util.DefaultHttpDownloader; @@ -81,7 +81,7 @@ public class ServerClient { InputStream is = load(pathStartingWithSlash, GET, false, connectTimeoutMillis, readTimeoutMillis); Files.copy(is, toFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (HttpDownloader.HttpException he) { - throw handleHttpException(he); + throw handleHttpException(he.getUri().toString(), he.getResponseCode(), he.getResponseContent(), he); } catch (IOException e) { throw new IllegalStateException(String.format("Unable to download '%s' to: %s", pathStartingWithSlash, toFile), e); } @@ -114,26 +114,26 @@ public class ServerClient { } else { return downloader.newInputSupplier(uri, requestMethod, getLogin(), getPassword(), connectTimeoutMs, readTimeoutMs).getInput(); } - } catch (HttpDownloader.HttpException e) { + } catch (HttpDownloader.HttpException he) { if (wrapHttpException) { - throw handleHttpException(e); + throw handleHttpException(he.getUri().toString(), he.getResponseCode(), he.getResponseContent(), he); } else { - throw e; + throw he; } } catch (IOException e) { throw new IllegalStateException(String.format("Unable to request: %s", uri), e); } } - public RuntimeException handleHttpException(HttpDownloader.HttpException he) { - if (he.getResponseCode() == 401) { + public RuntimeException handleHttpException(String url, int responseCode, String responseContent, Exception he) { + if (responseCode == 401) { return MessageException.of(String.format(getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD), he); } - if (he.getResponseCode() == 403) { + if (responseCode == 403) { // SONAR-4397 Details are in response content - return MessageException.of(tryParseAsJsonError(he.getResponseContent()), he); + return MessageException.of(tryParseAsJsonError(responseContent), he); } - return MessageException.of(String.format("Fail to execute request [code=%s, url=%s]: %s", he.getResponseCode(), he.getUri(), he.getResponseContent()), he); + return MessageException.of(String.format("Fail to execute request [code=%s, url=%s]: %s", responseCode, url, responseContent), he); } private static String tryParseAsJsonError(String responseContent) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java index 17331448b10..36c3324374b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ReportPublisher.java @@ -19,12 +19,9 @@ */ package org.sonar.batch.report; -import org.sonar.api.utils.text.JsonWriter; - import com.github.kevinsawicki.http.HttpRequest; import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; - import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; @@ -36,10 +33,8 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Date; - import javax.annotation.CheckForNull; import javax.annotation.Nullable; - import org.apache.commons.io.FileUtils; import org.picocontainer.Startable; import org.slf4j.Logger; @@ -51,11 +46,13 @@ import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; import org.sonar.api.utils.TempFolder; import org.sonar.api.utils.ZipUtils; +import org.sonar.api.utils.text.JsonWriter; import org.sonar.batch.analysis.DefaultAnalysisMode; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.protocol.output.BatchReportWriter; import org.sonar.batch.scan.ImmutableProjectReactor; import org.sonar.batch.util.BatchUtils; + import static java.lang.String.format; @BatchSide @@ -181,15 +178,7 @@ public class ReportPublisher implements Startable { request.basic(serverClient.getLogin(), serverClient.getPassword()); request.part("report", null, "application/octet-stream", report); if (!request.ok()) { - int responseCode = request.code(); - if (responseCode == 401) { - throw new IllegalStateException(format(serverClient.getMessageWhenNotAuthorized(), CoreProperties.LOGIN, CoreProperties.PASSWORD)); - } - if (responseCode == 403) { - // SONAR-4397 Details are in response content - throw new IllegalStateException(request.body()); - } - throw new IllegalStateException(format("Fail to execute request [code=%s, url=%s]: %s", responseCode, url, request.body())); + throw serverClient.handleHttpException(url.toString(), request.code(), request.body(), null); } long stopTime = System.currentTimeMillis(); LOG.info("Analysis reports sent to server in " + (stopTime - startTime) + "ms");