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