*/
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;
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;
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);
}
} 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) {
*/
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;
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;
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
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");