Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SonarLint Core - Implementation
* Copyright (C) 2016-2021 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.core.container.connected.update;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonarsource.sonarlint.core.client.api.common.Version;
import org.sonarsource.sonarlint.core.client.api.connected.ConnectedGlobalConfiguration;
import org.sonarsource.sonarlint.core.client.api.connected.SonarAnalyzer;
import org.sonarsource.sonarlint.core.container.storage.ProtobufUtil;
import org.sonarsource.sonarlint.core.container.storage.StoragePaths;
import org.sonarsource.sonarlint.core.plugin.cache.PluginCache;
import org.sonarsource.sonarlint.core.proto.Sonarlint.PluginReferences;
import org.sonarsource.sonarlint.core.proto.Sonarlint.PluginReferences.Builder;
import org.sonarsource.sonarlint.core.proto.Sonarlint.PluginReferences.PluginReference;
import org.sonarsource.sonarlint.core.serverapi.ServerApiHelper;
import org.sonarsource.sonarlint.core.util.ProgressWrapper;
import static java.lang.String.format;
public class PluginReferencesDownloader {
private static final Logger LOG = Loggers.get(PluginReferencesDownloader.class);
private final PluginCache pluginCache;
private final ServerApiHelper serverApiHelper;
private final ConnectedGlobalConfiguration configuration;
public PluginReferencesDownloader(ServerApiHelper serverApiHelper, PluginCache pluginCache, ConnectedGlobalConfiguration configuration) {
this.serverApiHelper = serverApiHelper;
this.pluginCache = pluginCache;
this.configuration = configuration;
}
public PluginReferences toReferences(List analyzers) {
Builder builder = PluginReferences.newBuilder();
analyzers.stream()
.filter(this::analyzerFilter)
.map(analyzer -> PluginReference.newBuilder()
.setKey(analyzer.key())
.setHash(analyzer.hash())
.setFilename(analyzer.filename())
.build())
.forEach(builder::addReference);
return builder.build();
}
private boolean analyzerFilter(SonarAnalyzer analyzer) {
if (!analyzer.sonarlintCompatible()) {
LOG.debug("Code analyzer '{}' is not compatible with SonarLint. Skip downloading it.", analyzer.key());
return false;
} else if (!analyzer.versionSupported()) {
LOG.debug("Code analyzer '{}' version '{}' is not supported (minimal version is '{}'). Skip downloading it.",
analyzer.key(), analyzer.version(), analyzer.minimumVersion());
return false;
}
return true;
}
public void fetchPluginsTo(Version serverVersion, Path dest, List analyzers, ProgressWrapper progress) {
PluginReferences refs = toReferences(analyzers);
int i = 0;
float refCount = refs.getReferenceList().size();
for (PluginReference ref : refs.getReferenceList()) {
if (configuration.getEmbeddedPluginUrlsByKey().containsKey(ref.getKey())) {
LOG.debug("Code analyzer '{}' is embedded in SonarLint. Skip downloading it.", ref.getKey());
continue;
}
progress.setProgressAndCheckCancel("Loading analyzer " + ref.getKey(), i / refCount);
pluginCache.get(ref.getFilename(), ref.getHash(), new SonarQubeServerPluginDownloader(serverVersion, ref.getKey()));
}
ProtobufUtil.writeToFile(refs, dest.resolve(StoragePaths.PLUGIN_REFERENCES_PB));
}
private class SonarQubeServerPluginDownloader implements PluginCache.Copier {
private final String key;
private final Version serverVersion;
SonarQubeServerPluginDownloader(Version serverVersion, String key) {
this.serverVersion = serverVersion;
this.key = key;
}
@Override
public void copy(String filename, Path toFile) throws IOException {
String url = "api/plugins/download?plugin=" + key;
if (LOG.isDebugEnabled()) {
LOG.debug("Download plugin '{}' to '{}'...", filename, toFile);
} else {
LOG.info("Download '{}'...", filename);
}
ServerApiHelper.consumeTimed(
() -> serverApiHelper.get(url),
response -> FileUtils.copyInputStreamToFile(response.bodyAsStream(), toFile.toFile()),
duration -> LOG.info("Downloaded '{}' in {}ms", filename, duration));
}
}
}