use of org.sonarsource.sonarlint.core.commons.http.HttpClient in project sonarlint-core by SonarSource.
the class ConnectedSonarLintEngineImpl method getActiveRuleDetails.
@Override
public CompletableFuture<ConnectedRuleDetails> getActiveRuleDetails(EndpointParams endpoint, HttpClient client, String ruleKey, @Nullable String projectKey) {
var allRulesDefinitionsByKey = analysisContext.get().allRulesDefinitionsByKey;
var ruleDefFromPlugin = allRulesDefinitionsByKey.get(ruleKey);
if (ruleDefFromPlugin != null && (globalConfig.getExtraPluginsPathsByKey().containsKey(ruleDefFromPlugin.getLanguage().getPluginKey()) || projectKey == null)) {
// if no project key, or for rules from extra plugins there will be no rules metadata in the storage
return CompletableFuture.completedFuture(new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), ""));
}
if (projectKey != null) {
var analyzerConfiguration = projectStorage.getAnalyzerConfiguration(projectKey);
var storageActiveRule = analyzerConfiguration.getRuleSetByLanguageKey().values().stream().flatMap(s -> s.getRules().stream()).filter(r -> r.getRuleKey().equals(ruleKey)).findFirst();
if (storageActiveRule.isPresent()) {
var activeRuleFromStorage = storageActiveRule.get();
var serverSeverity = activeRuleFromStorage.getSeverity();
if (StringUtils.isNotBlank(activeRuleFromStorage.getTemplateKey())) {
var templateRuleDefFromPlugin = Optional.ofNullable(allRulesDefinitionsByKey.get(activeRuleFromStorage.getTemplateKey())).orElseThrow(() -> new IllegalStateException("Unable to find rule definition for rule template " + activeRuleFromStorage.getTemplateKey()));
return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, serverRule.getName(), serverRule.getHtmlDesc(), serverSeverity, templateRuleDefFromPlugin.getType(), templateRuleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
} else {
if (ruleDefFromPlugin == null) {
throw new IllegalStateException("Unable to find rule definition for rule " + ruleKey);
}
return new ServerApi(new ServerApiHelper(endpoint, client)).rules().getRule(ruleKey).thenApply(serverRule -> new DefaultRuleDetails(ruleKey, ruleDefFromPlugin.getName(), ruleDefFromPlugin.getHtmlDescription(), serverSeverity != null ? serverSeverity : ruleDefFromPlugin.getSeverity(), ruleDefFromPlugin.getType(), ruleDefFromPlugin.getLanguage(), serverRule.getHtmlNote()));
}
}
}
throw new IllegalStateException("Unable to find rule details for '" + ruleKey + "'");
}
use of org.sonarsource.sonarlint.core.commons.http.HttpClient in project sonarlint-core by SonarSource.
the class MockWebServerExtension method httpClient.
public static HttpClient httpClient() {
return new HttpClient() {
private final OkHttpClient okClient = SHARED_CLIENT.newBuilder().build();
@Override
public Response post(String url, String contentType, String bodyContent) {
var body = RequestBody.create(MediaType.get(contentType), bodyContent);
var request = new Request.Builder().url(url).post(body).build();
return executeRequest(request);
}
@Override
public Response get(String url) {
var request = new Request.Builder().url(url).build();
return executeRequest(request);
}
@Override
public CompletableFuture<Response> getAsync(String url) {
var request = new Request.Builder().url(url).build();
return executeRequestAsync(request);
}
@Override
public Response delete(String url, String contentType, String bodyContent) {
var body = RequestBody.create(MediaType.get(contentType), bodyContent);
var request = new Request.Builder().url(url).delete(body).build();
return executeRequest(request);
}
private Response executeRequest(Request request) {
try {
return wrap(okClient.newCall(request).execute());
} catch (IOException e) {
throw new IllegalStateException("Unable to execute request: " + e.getMessage(), e);
}
}
private CompletableFuture<Response> executeRequestAsync(Request request) {
var call = okClient.newCall(request);
var futureResponse = new CompletableFuture<Response>().whenComplete((response, error) -> {
if (error instanceof CancellationException) {
call.cancel();
}
});
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
futureResponse.completeExceptionally(e);
}
@Override
public void onResponse(Call call, okhttp3.Response response) {
futureResponse.complete(wrap(response));
}
});
return futureResponse;
}
private Response wrap(okhttp3.Response wrapped) {
return new Response() {
@Override
public String url() {
return wrapped.request().url().toString();
}
@Override
public int code() {
return wrapped.code();
}
@Override
public void close() {
wrapped.close();
}
@Override
public String bodyAsString() {
try (var body = wrapped.body()) {
return body.string();
} catch (IOException e) {
throw new IllegalStateException("Unable to read response body: " + e.getMessage(), e);
}
}
@Override
public InputStream bodyAsStream() {
return wrapped.body().byteStream();
}
@Override
public String toString() {
return wrapped.toString();
}
};
}
};
}
Aggregations