Search in sources :

Example 31 with GetRequest

use of org.sonarqube.ws.client.GetRequest 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)

Example 32 with GetRequest

use of org.sonarqube.ws.client.GetRequest in project sonarqube by SonarSource.

the class RestartTest method restart_in_prod_mode_requires_sysadmin_permission_and_restarts.

@Test
public void restart_in_prod_mode_requires_sysadmin_permission_and_restarts() throws Exception {
    // server classloader locks Jar files on Windows
    if (!SystemUtils.IS_OS_WINDOWS) {
        orchestrator = Orchestrator.builderEnv().setOrchestratorProperty("orchestrator.keepWorkspace", "true").build();
        orchestrator.start();
        verifyFailWith403(() -> newWsClient(orchestrator).system().restart());
        createNonSystemAdministrator("john", "doe");
        verifyFailWith403(() -> ItUtils.newUserWsClient(orchestrator, "john", "doe").system().restart());
        createSystemAdministrator("big", "boss");
        ItUtils.newUserWsClient(orchestrator, "big", "boss").system().restart();
        WsResponse wsResponse = newAdminWsClient(orchestrator).wsConnector().call(new GetRequest("/api/system/status")).failIfNotSuccessful();
        assertThat(wsResponse.content()).contains("RESTARTING");
        // we just wait five seconds, for a lack of a better approach to waiting for the restart process to start in SQ
        Thread.sleep(5000);
        assertThat(FileUtils.readFileToString(orchestrator.getServer().getWebLogs())).contains("SonarQube restart requested by big");
    }
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) Test(org.junit.Test)

Example 33 with GetRequest

use of org.sonarqube.ws.client.GetRequest in project sonarqube by SonarSource.

the class ServerSystemTest method download_system_info.

@Test
public void download_system_info() throws Exception {
    waitForComputeEngineToBeUp(orchestrator);
    WsResponse response = newAdminWsClient(orchestrator).wsConnector().call(new GetRequest("api/system/info"));
    assertThat(response.code()).isEqualTo(200);
    assertThat(response.content()).contains(// SONAR-7436 monitor ES and CE
    "\"Compute Engine Database Connection\":", "\"Compute Engine State\":", "\"Compute Engine Tasks\":", "\"Elasticsearch\":", "\"State\":\"GREEN\"", // SONAR-7271 get settings
    "\"Settings\":", "\"sonar.jdbc.url\":", "\"sonar.path.data\":");
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse) ShowWsResponse(org.sonarqube.ws.ServerId.ShowWsResponse) Test(org.junit.Test)

Example 34 with GetRequest

use of org.sonarqube.ws.client.GetRequest in project sonarqube by SonarSource.

the class QualityGateTest method getAnalysisId.

private String getAnalysisId(String taskId) throws IOException {
    WsResponse activity = wsClient.wsConnector().call(new GetRequest("api/ce/task").setParam("id", taskId).setMediaType(MediaTypes.PROTOBUF));
    WsCe.TaskResponse activityWsResponse = WsCe.TaskResponse.parseFrom(activity.contentStream());
    return activityWsResponse.getTask().getAnalysisId();
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsCe(org.sonarqube.ws.WsCe) WsResponse(org.sonarqube.ws.client.WsResponse) ProjectStatusWsResponse(org.sonarqube.ws.WsQualityGates.ProjectStatusWsResponse)

Example 35 with GetRequest

use of org.sonarqube.ws.client.GetRequest in project sonarqube by SonarSource.

the class UserRule method getUsers.

public Users getUsers() {
    WsResponse response = adminWsClient().wsConnector().call(new GetRequest("api/users/search"));
    assertThat(response.code()).isEqualTo(200);
    return Users.parse(response.content());
}
Also used : GetRequest(org.sonarqube.ws.client.GetRequest) WsResponse(org.sonarqube.ws.client.WsResponse)

Aggregations

GetRequest (org.sonarqube.ws.client.GetRequest)59 Test (org.junit.Test)34 WsResponse (org.sonarqube.ws.client.WsResponse)27 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 Profiler (org.sonar.api.utils.log.Profiler)5 Reader (java.io.Reader)4 HttpException (org.sonarqube.ws.client.HttpException)4 WsClient (org.sonarqube.ws.client.WsClient)4 ItUtils.newAdminWsClient (util.ItUtils.newAdminWsClient)3 File (java.io.File)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 ShowWsResponse (org.sonarqube.ws.ServerId.ShowWsResponse)2 ItUtils.newWsClient (util.ItUtils.newWsClient)2 Gson (com.google.gson.Gson)1 LinkedList (java.util.LinkedList)1 CheckForNull (javax.annotation.CheckForNull)1 LoadedActiveRule (org.sonar.api.batch.rule.LoadedActiveRule)1 Metric (org.sonar.api.measures.Metric)1