Search in sources :

Example 1 with WsResponse

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

the class NotificationChecker method isSupported.

/**
 * Checks whether a server supports notifications
 */
public boolean isSupported() {
    String path = getWsPath(Collections.emptyMap());
    WsResponse wsResponse = wsClient.rawGet(path);
    return wsResponse.isSuccessful();
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Example 2 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, InputStream inputStream) {
    WsResponse wsResponse = mock(WsResponse.class);
    when(wsClient.get(url)).thenReturn(wsResponse);
    when(wsClient.rawGet(url)).thenReturn(wsResponse);
    when(wsResponse.code()).thenReturn(200);
    when(wsResponse.requestUrl()).thenReturn(url);
    when(wsResponse.contentStream()).thenReturn(inputStream);
    when(wsResponse.isSuccessful()).thenReturn(true);
    return wsClient;
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Example 3 with WsResponse

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

the class WsClientTestUtils method addStreamResponse.

public static SonarLintWsClient addStreamResponse(SonarLintWsClient wsClient, String url, String resourcePath) {
    WsResponse wsResponse = mock(WsResponse.class);
    when(wsClient.get(url)).thenReturn(wsResponse);
    when(wsClient.rawGet(url)).thenReturn(wsResponse);
    when(wsResponse.requestUrl()).thenReturn(url);
    when(wsResponse.contentStream()).thenReturn(requireNonNull(WsClientTestUtils.class.getResourceAsStream(resourcePath)));
    when(wsResponse.isSuccessful()).thenReturn(true);
    return wsClient;
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Example 4 with WsResponse

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

the class SonarLintWsClient method getPaginated.

// static to allow mocking SonarLintWsClient while still using this method
/**
 * @param responseParser ProtoBuf parser
 * @param getPaging extract {@link Paging} from the protobuf message
 * @param responseConsummer consume the protobuf message and return <code>true</code> if some elements where present (or <code>false</code> is response is empty)
 */
public static <G, F> void getPaginated(SonarLintWsClient client, String baseUrl, CheckedFunction<InputStream, G> responseParser, Function<G, Paging> getPaging, Function<G, List<F>> itemExtractor, Consumer<F> itemConsumer, ProgressWrapper progress) {
    int page = 0;
    boolean stop = false;
    int loaded = 0;
    do {
        page++;
        WsResponse response = client.get(baseUrl + (baseUrl.contains("?") ? "&" : "?") + "ps=" + PAGE_SIZE + "&p=" + page);
        try (InputStream stream = response.contentStream()) {
            G protoBufResponse = responseParser.apply(stream);
            List<F> items = itemExtractor.apply(protoBufResponse);
            for (F item : items) {
                itemConsumer.accept(item);
                loaded++;
            }
            boolean isEmpty = items.isEmpty();
            Paging paging = getPaging.apply(protoBufResponse);
            // SONAR-9150 Some WS used to miss the paging information, so iterate until response is empty
            stop = isEmpty || (paging.getTotal() > 0 && page * PAGE_SIZE >= paging.getTotal());
            if (!stop && page >= MAX_PAGES) {
                stop = true;
                LOG.debug("Limiting number of requested pages from '{}' to {}. Some of the data won't be fetched", baseUrl, MAX_PAGES);
            }
            progress.setProgressAndCheckCancel("Page " + page, loaded / (float) paging.getTotal());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to process paginated WS", e);
        }
    } while (!stop);
}
Also used : InputStream(java.io.InputStream) Paging(org.sonarqube.ws.Common.Paging) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) IOException(java.io.IOException)

Example 5 with WsResponse

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

the class SonarLintWsClient method rawGet.

/**
 * Execute GET and don't check response
 */
public WsResponse rawGet(String path) {
    long startTime = System2.INSTANCE.now();
    GetRequest request = new GetRequest(path);
    WsResponse response = client.call(request);
    long duration = System2.INSTANCE.now() - startTime;
    if (LOG.isDebugEnabled()) {
        LOG.debug("{} {} {} | time={}ms", request.getMethod(), response.code(), response.requestUrl(), duration);
    }
    return response;
}
Also used : GetRequest(org.sonarsource.sonarlint.core.util.ws.GetRequest) 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