Search in sources :

Example 6 with WsResponse

use of org.sonarsource.sonarlint.core.util.ws.WsResponse in project sonarlint-core by SonarSource.

the class ModuleHierarchyDownloader method fetchAncestorId.

@CheckForNull
private String fetchAncestorId(String moduleId) {
    try (WsResponse response = wsClient.get("api/components/show.protobuf?id=" + StringUtils.urlEncode(moduleId))) {
        InputStream stream = response.contentStream();
        ShowWsResponse showResponse = WsComponents.ShowWsResponse.parseFrom(stream);
        return showResponse.getAncestorsList().stream().map(Component::getId).findFirst().orElse(null);
    } catch (IOException e) {
        throw new IllegalStateException("Failed to load module hierarchy", e);
    }
}
Also used : InputStream(java.io.InputStream) ShowWsResponse(org.sonarqube.ws.WsComponents.ShowWsResponse) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) ShowWsResponse(org.sonarqube.ws.WsComponents.ShowWsResponse) IOException(java.io.IOException) Component(org.sonarqube.ws.WsComponents.Component) CheckForNull(javax.annotation.CheckForNull)

Example 7 with WsResponse

use of org.sonarsource.sonarlint.core.util.ws.WsResponse in project sonarlint-core by SonarSource.

the class AuthenticationChecker method validateCredentials.

public ValidationResult validateCredentials() {
    try (WsResponse response = wsClient.rawGet("api/authentication/validate?format=json")) {
        int code = response.code();
        if (response.isSuccessful()) {
            String responseStr = response.content();
            ValidateResponse 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.content());
        }
    }
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) Gson(com.google.gson.Gson)

Example 8 with WsResponse

use of org.sonarsource.sonarlint.core.util.ws.WsResponse in project sonarlint-core by SonarSource.

the class ServerVersionAndStatusChecker method fetchServerInfos.

private ServerInfos fetchServerInfos() {
    try (WsResponse response = wsClient.rawGet("api/system/status")) {
        if (!response.isSuccessful()) {
            if (response.code() == HttpURLConnection.HTTP_NOT_FOUND) {
                return tryFromDeprecatedApi(response);
            } else {
                throw SonarLintWsClient.handleError(response);
            }
        } else {
            String responseStr = response.content();
            try {
                ServerInfos.Builder builder = ServerInfos.newBuilder();
                JsonFormat.parser().merge(responseStr, builder);
                return builder.build();
            } catch (InvalidProtocolBufferException e) {
                throw new IllegalStateException("Unable to parse server infos from: " + StringUtils.abbreviate(responseStr, 100), e);
            }
        }
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) ServerInfos(org.sonarsource.sonarlint.core.proto.Sonarlint.ServerInfos)

Example 9 with WsResponse

use of org.sonarsource.sonarlint.core.util.ws.WsResponse in project sonarlint-core by SonarSource.

the class NotificationChecker method request.

/**
 * Get all notification events for a set of projects after a given timestamp.
 * Returns an empty list if an error occurred making the request or parsing the response.
 */
@CheckForNull
public List<SonarQubeNotification> request(Map<String, ZonedDateTime> projectTimestamps) {
    String path = getWsPath(projectTimestamps);
    WsResponse wsResponse = wsClient.rawGet(path);
    if (!wsResponse.isSuccessful()) {
        LOG.debug("Failed to get notifications: {}, {}", wsResponse.code(), wsResponse.content());
        return Collections.emptyList();
    }
    return parseResponse(wsResponse.content());
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) CheckForNull(javax.annotation.CheckForNull)

Example 10 with WsResponse

use of org.sonarsource.sonarlint.core.util.ws.WsResponse in project sonarlint-core by SonarSource.

the class WsClientTestUtils method addResponse.

public static SonarLintWsClient addResponse(SonarLintWsClient wsClient, String url, String response) {
    WsResponse wsResponse = mock(WsResponse.class);
    when(wsClient.get(url)).thenReturn(wsResponse);
    when(wsClient.rawGet(url)).thenReturn(wsResponse);
    when(wsResponse.requestUrl()).thenReturn(url);
    when(wsResponse.content()).thenReturn(response).thenThrow(new IllegalStateException("Should not call content() twice"));
    when(wsResponse.contentStream()).thenReturn(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
    when(wsResponse.contentReader()).thenReturn(new StringReader(response));
    when(wsResponse.isSuccessful()).thenReturn(true);
    return wsClient;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Aggregations

WsResponse (org.sonarsource.sonarlint.core.util.ws.WsResponse)23 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 Gson (com.google.gson.Gson)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CheckForNull (javax.annotation.CheckForNull)2 Test (org.junit.Test)2 ValuesWsResponse (org.sonarqube.ws.Settings.ValuesWsResponse)2 SonarLintWsClient (org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient)2 ServerInfos (org.sonarsource.sonarlint.core.proto.Sonarlint.ServerInfos)2 JsonReader (com.google.gson.stream.JsonReader)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 Scanner (java.util.Scanner)1 Paging (org.sonarqube.ws.Common.Paging)1 Setting (org.sonarqube.ws.Settings.Setting)1