Search in sources :

Example 16 with WsResponse

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

the class NotificationCheckerTest method testIsSupported.

@Test
public void testIsSupported() {
    String expectedUrl = "api/developers/search_events?projects=&from=";
    SonarLintWsClient client = WsClientTestUtils.createMock();
    WsResponse wsResponse = mock(WsResponse.class);
    when(client.rawGet(startsWith(expectedUrl))).thenReturn(wsResponse);
    when(wsResponse.isSuccessful()).thenReturn(true);
    NotificationChecker checker = new NotificationChecker(client);
    assertThat(checker.isSupported()).isTrue();
}
Also used : WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) SonarLintWsClient(org.sonarsource.sonarlint.core.container.connected.SonarLintWsClient) Test(org.junit.Test)

Example 17 with WsResponse

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

the class SonarLintWsClient method rawPost.

/**
 * Execute POST and don't check response
 */
public WsResponse rawPost(String path) {
    long startTime = System2.INSTANCE.now();
    PostRequest request = new PostRequest(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 : PostRequest(org.sonarsource.sonarlint.core.util.ws.PostRequest) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Example 18 with WsResponse

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

the class IssueDownloaderImpl method apply.

/**
 * Fetch all issues of the component with specified key.
 * If the component doesn't exist or it exists but has no issues, an empty iterator is returned.
 * See also sonarqube:/web_api/batch
 *
 * @param key project key, module key, or file key.
 * @return Iterator of issues. It can be empty but never null.
 */
@Override
public List<ScannerInput.ServerIssue> apply(String key) {
    try (WsResponse response = wsClient.rawGet(getIssuesUrl(key))) {
        if (response.code() == 403 || response.code() == 404) {
            return Collections.emptyList();
        } else if (response.code() != 200) {
            throw SonarLintWsClient.handleError(response);
        }
        InputStream input = response.contentStream();
        Parser<ScannerInput.ServerIssue> parser = ScannerInput.ServerIssue.parser();
        return ProtobufUtil.readMessages(input, parser);
    }
}
Also used : InputStream(java.io.InputStream) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse)

Example 19 with WsResponse

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

the class ModuleListDownloader method fetchModulesListBefore6dot3.

private void fetchModulesListBefore6dot3(Path dest) {
    try (WsResponse response = wsClient.get("api/projects/index?format=json&subprojects=true")) {
        try (Reader contentReader = response.contentReader()) {
            DefaultModule[] results = new Gson().fromJson(contentReader, DefaultModule[].class);
            ModuleList.Builder moduleListBuilder = ModuleList.newBuilder();
            Builder moduleBuilder = ModuleList.Module.newBuilder();
            for (DefaultModule module : results) {
                moduleBuilder.clear();
                moduleListBuilder.putModulesByKey(module.k, moduleBuilder.setKey(module.k).setName(module.nm).setQu(module.qu).build());
            }
            ProtobufUtil.writeToFile(moduleListBuilder.build(), dest.resolve(StoragePaths.MODULE_LIST_PB));
        } catch (IOException e) {
            throw new IllegalStateException("Failed to load module list", e);
        }
    }
}
Also used : Builder(org.sonarsource.sonarlint.core.proto.Sonarlint.ModuleList.Module.Builder) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) Reader(java.io.Reader) Gson(com.google.gson.Gson) ModuleList(org.sonarsource.sonarlint.core.proto.Sonarlint.ModuleList) IOException(java.io.IOException)

Example 20 with WsResponse

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

the class PluginListDownloader method downloadPluginListBefore66.

public List<SonarAnalyzer> downloadPluginListBefore66(Version serverVersion) {
    List<SonarAnalyzer> analyzers = new LinkedList<>();
    boolean compatibleFlagPresent = serverVersion.compareToIgnoreQualifier(Version.create("6.0")) >= 0;
    String responseStr;
    try (WsResponse response = wsClient.get(WS_PATH_LTS)) {
        responseStr = response.content();
    }
    Scanner scanner = new Scanner(responseStr);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        String[] fields = StringUtils.split(line, ",");
        String[] nameAndHash = StringUtils.split(fields[fields.length - 1], "|");
        String key = fields[0];
        String filename = nameAndHash[0];
        String hash = nameAndHash[1];
        String version = VersionUtils.getJarVersion(filename);
        String minVersion = pluginVersionChecker.getMinimumVersion(key);
        boolean sonarlintCompatible = PluginCacheLoader.isWhitelisted(key) || !compatibleFlagPresent || "true".equals(fields[1]);
        DefaultSonarAnalyzer analyzer = new DefaultSonarAnalyzer(key, filename, hash, version, sonarlintCompatible, minVersion);
        analyzers.add(analyzer);
    }
    scanner.close();
    return analyzers;
}
Also used : Scanner(java.util.Scanner) DefaultSonarAnalyzer(org.sonarsource.sonarlint.core.container.model.DefaultSonarAnalyzer) SonarAnalyzer(org.sonarsource.sonarlint.core.client.api.connected.SonarAnalyzer) DefaultSonarAnalyzer(org.sonarsource.sonarlint.core.container.model.DefaultSonarAnalyzer) WsResponse(org.sonarsource.sonarlint.core.util.ws.WsResponse) LinkedList(java.util.LinkedList)

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