use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.
the class ReportPublisherTest method parse_upload_error_message.
@Test
public void parse_upload_error_message() {
HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}");
WsResponse response = mock(WsResponse.class);
when(response.failIfNotSuccessful()).thenThrow(ex);
when(wsClient.call(any(WsRequest.class))).thenReturn(response);
assertThatThrownBy(() -> underTest.upload(reportTempFolder.newFile())).isInstanceOf(MessageException.class).hasMessage("Server failed to process report. Please check server logs: Organization with key 'MyOrg' does not exist");
}
use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.
the class ReportPublisherTest method upload_error_message.
@Test
public void upload_error_message() {
HttpException ex = new HttpException("url", 404, "{\"errors\":[{\"msg\":\"Organization with key 'MyOrg' does not exist\"}]}");
WsResponse response = mock(WsResponse.class);
when(response.failIfNotSuccessful()).thenThrow(ex);
when(wsClient.call(any(WsRequest.class))).thenThrow(new IllegalStateException("timeout"));
assertThatThrownBy(() -> underTest.upload(reportTempFolder.newFile())).isInstanceOf(IllegalStateException.class).hasMessage("Failed to upload report: timeout");
}
use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.
the class DefaultProjectRepositoriesLoaderTest method failFastHttpError.
@Test(expected = IllegalStateException.class)
public void failFastHttpError() {
HttpException http = new HttpException("url", 403, null);
IllegalStateException e = new IllegalStateException("http error", http);
WsTestUtil.mockException(wsClient, e);
loader.load(PROJECT_KEY, null);
}
use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.
the class AbstractSettingsLoader method load.
Map<String, String> load(@Nullable String componentKey) {
String url = "api/settings/values.protobuf";
Profiler profiler = Profiler.create(LOG);
if (componentKey != null) {
url += "?component=" + ScannerUtils.encodeForUrl(componentKey);
profiler.startInfo(String.format("Load project settings for component key: '%s'", componentKey));
} else {
profiler.startInfo("Load global settings");
}
try (InputStream is = wsClient.call(new GetRequest(url)).contentStream()) {
Settings.ValuesWsResponse values = Settings.ValuesWsResponse.parseFrom(is);
profiler.stopInfo();
return toMap(values.getSettingsList());
} catch (HttpException e) {
if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) {
return Collections.emptyMap();
}
throw e;
} catch (IOException e) {
throw new IllegalStateException("Unable to load settings", e);
}
}
use of org.sonarqube.ws.client.HttpException in project sonarqube by SonarSource.
the class PluginFiles method download.
private Optional<File> download(InstalledPlugin plugin) {
GetRequest request = new GetRequest("api/plugins/download").setParam("plugin", plugin.key).setTimeOutInMs(5 * 60_000);
try {
Class.forName("java.util.jar.Pack200");
request.setParam("acceptCompressions", PACK200);
} catch (ClassNotFoundException e) {
// ignore and don't use any compression
}
File downloadedFile = newTempFile();
LOGGER.debug("Download plugin '{}' to '{}'", plugin.key, downloadedFile);
try (WsResponse response = wsClient.call(request)) {
Optional<String> expectedMd5 = response.header(MD5_HEADER);
if (!expectedMd5.isPresent()) {
throw new IllegalStateException(format("Fail to download plugin [%s]. Request to %s did not return header %s", plugin.key, response.requestUrl(), MD5_HEADER));
}
downloadBinaryTo(plugin, downloadedFile, response);
// verify integrity
String effectiveTempMd5 = computeMd5(downloadedFile);
if (!expectedMd5.get().equals(effectiveTempMd5)) {
throw new IllegalStateException(format("Fail to download plugin [%s]. File %s was expected to have checksum %s but had %s", plugin.key, downloadedFile, expectedMd5.get(), effectiveTempMd5));
}
// un-compress if needed
String cacheMd5;
File tempJar;
Optional<String> compression = response.header(COMPRESSION_HEADER);
if (compression.isPresent() && PACK200.equals(compression.get())) {
tempJar = unpack200(plugin.key, downloadedFile);
cacheMd5 = response.header(UNCOMPRESSED_MD5_HEADER).orElseThrow(() -> new IllegalStateException(format("Fail to download plugin [%s]. Request to %s did not return header %s.", plugin.key, response.requestUrl(), UNCOMPRESSED_MD5_HEADER)));
} else {
tempJar = downloadedFile;
cacheMd5 = expectedMd5.get();
}
// put in cache
File jarInCache = jarInCache(plugin.key, cacheMd5);
mkdir(jarInCache.getParentFile());
moveFile(tempJar, jarInCache);
return Optional.of(jarInCache);
} catch (HttpException e) {
if (e.code() == HttpURLConnection.HTTP_NOT_FOUND) {
// uninstalled.
return Optional.empty();
}
// not 2xx nor 404
throw new IllegalStateException(format("Fail to download plugin [%s]. Request to %s returned code %d.", plugin.key, e.url(), e.code()));
}
}
Aggregations