Search in sources :

Example 1 with HttpException

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");
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) MessageException(org.sonar.api.utils.MessageException) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException) Test(org.junit.Test)

Example 2 with HttpException

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");
}
Also used : WsRequest(org.sonarqube.ws.client.WsRequest) MockWsResponse(org.sonarqube.ws.client.MockWsResponse) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException) Test(org.junit.Test)

Example 3 with HttpException

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);
}
Also used : HttpException(org.sonarqube.ws.client.HttpException) Test(org.junit.Test)

Example 4 with HttpException

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);
    }
}
Also used : Profiler(org.sonar.api.utils.log.Profiler) InputStream(java.io.InputStream) GetRequest(org.sonarqube.ws.client.GetRequest) HttpException(org.sonarqube.ws.client.HttpException) IOException(java.io.IOException) Settings(org.sonarqube.ws.Settings)

Example 5 with HttpException

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()));
    }
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) HttpException(org.sonarqube.ws.client.HttpException) File(java.io.File)

Aggregations

HttpException (org.sonarqube.ws.client.HttpException)17 Test (org.junit.Test)11 WsResponse (org.sonarqube.ws.client.WsResponse)6 MessageException (org.sonar.api.utils.MessageException)5 GetRequest (org.sonarqube.ws.client.GetRequest)4 MockWsResponse (org.sonarqube.ws.client.MockWsResponse)3 WsRequest (org.sonarqube.ws.client.WsRequest)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Profiler (org.sonar.api.utils.log.Profiler)1 Ce (org.sonarqube.ws.Ce)1 Settings (org.sonarqube.ws.Settings)1 PostRequest (org.sonarqube.ws.client.PostRequest)1 OrganizationService (org.sonarqube.ws.client.organization.OrganizationService)1