Search in sources :

Example 1 with DefaultValidationResult

use of org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult in project sonarlint-core by SonarSource.

the class ConnectionValidator method validateConnection.

public CompletableFuture<ValidationResult> validateConnection() {
    var serverChecker = new ServerVersionAndStatusChecker(helper);
    var authChecker = new AuthenticationChecker(helper);
    return serverChecker.checkVersionAndStatusAsync().thenApply(check -> {
        var validateCredentials = authChecker.validateCredentials();
        var organizationKey = helper.getOrganizationKey();
        if (validateCredentials.success() && organizationKey.isPresent()) {
            var organization = new ServerApi(helper).organization().fetchOrganization(organizationKey.get(), new ProgressMonitor(null));
            if (organization.isEmpty()) {
                return new DefaultValidationResult(false, "No organizations found for key: " + organizationKey.get());
            }
        }
        return validateCredentials;
    }).exceptionally(e -> new DefaultValidationResult(false, e.getCause().getMessage()));
}
Also used : AuthenticationChecker(org.sonarsource.sonarlint.core.serverapi.authentication.AuthenticationChecker) ServerVersionAndStatusChecker(org.sonarsource.sonarlint.core.serverapi.system.ServerVersionAndStatusChecker) ServerOrganization(org.sonarsource.sonarlint.core.serverapi.organization.ServerOrganization) ValidationResult(org.sonarsource.sonarlint.core.serverapi.system.ValidationResult) Optional(java.util.Optional) CompletableFuture(java.util.concurrent.CompletableFuture) ServerApiHelper(org.sonarsource.sonarlint.core.serverapi.ServerApiHelper) ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) DefaultValidationResult(org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult) ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerVersionAndStatusChecker(org.sonarsource.sonarlint.core.serverapi.system.ServerVersionAndStatusChecker) AuthenticationChecker(org.sonarsource.sonarlint.core.serverapi.authentication.AuthenticationChecker) DefaultValidationResult(org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi)

Example 2 with DefaultValidationResult

use of org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult in project sonarlint-core by SonarSource.

the class AuthenticationChecker method validateCredentials.

public ValidationResult validateCredentials() {
    try (var response = serverApiHelper.rawGet("api/authentication/validate?format=json")) {
        var code = response.code();
        if (response.isSuccessful()) {
            var responseStr = response.bodyAsString();
            var validateResponse = new Gson().fromJson(responseStr, ValidateResponse.class);
            return new DefaultValidationResult(validateResponse.valid, validateResponse.valid ? "Authentication successful" : "Authentication failed");
        } else {
            return new DefaultValidationResult(false, "HTTP Connection failed (" + code + "): " + response.bodyAsString());
        }
    }
}
Also used : DefaultValidationResult(org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult) Gson(com.google.gson.Gson)

Aggregations

DefaultValidationResult (org.sonarsource.sonarlint.core.serverapi.system.DefaultValidationResult)2 Gson (com.google.gson.Gson)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ProgressMonitor (org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor)1 ServerApi (org.sonarsource.sonarlint.core.serverapi.ServerApi)1 ServerApiHelper (org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)1 AuthenticationChecker (org.sonarsource.sonarlint.core.serverapi.authentication.AuthenticationChecker)1 ServerOrganization (org.sonarsource.sonarlint.core.serverapi.organization.ServerOrganization)1 ServerVersionAndStatusChecker (org.sonarsource.sonarlint.core.serverapi.system.ServerVersionAndStatusChecker)1 ValidationResult (org.sonarsource.sonarlint.core.serverapi.system.ValidationResult)1