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();
}
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;
}
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;
}
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);
}
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;
}
Aggregations