diff options
43 files changed, 485 insertions, 444 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 7085d2a46a2..796372498fd 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -32,7 +32,7 @@ import org.sonar.api.rules.AnnotationRuleParser; import org.sonar.api.rules.XMLRuleParser; import org.sonar.api.server.rule.RulesDefinitionXmlLoader; import org.sonar.api.utils.Durations; -import org.sonar.api.utils.HttpDownloader; +import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; @@ -331,7 +331,7 @@ class ServerComponents { ThreadLocalDatabaseSessionFactory.class, new DatabaseSessionProvider(), ServerMetadataPersister.class, - HttpDownloader.class, + DefaultHttpDownloader.class, UriReader.class, ServerIdGenerator.class ); diff --git a/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java b/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java index 18de6b9e09c..295a58a916a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/plugins/PluginDownloader.java @@ -22,10 +22,10 @@ package org.sonar.server.plugins; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.picocontainer.Startable; -import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.HttpDownloader; import org.sonar.server.platform.DefaultServerFileSystem; import org.sonar.updatecenter.common.Release; import org.sonar.updatecenter.common.Version; diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java index bffee518e54..7aaf0f6be32 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java @@ -24,7 +24,7 @@ import org.sonar.api.config.EmailSettings; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.PluginMetadata; import org.sonar.api.utils.Durations; -import org.sonar.api.utils.HttpDownloader; +import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; import org.sonar.api.utils.internal.TempFolderCleaner; @@ -88,7 +88,7 @@ public class GlobalContainer extends ComponentContainer { DefaultServer.class, new TempFolderProvider(), TempFolderCleaner.class, - HttpDownloader.class, + DefaultHttpDownloader.class, UriReader.class, new FileCacheProvider(), System2.INSTANCE, diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index faeef8385d8..91eac2e7ebe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -33,8 +33,9 @@ import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; -import org.sonar.api.utils.HttpDownloader; +import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.batch.bootstrapper.EnvironmentInformation; +import org.sonar.api.utils.HttpDownloader; import javax.annotation.Nullable; @@ -57,11 +58,11 @@ public class ServerClient implements BatchComponent { private static final String GET = "GET"; private BootstrapProperties props; - private HttpDownloader.BaseHttpDownloader downloader; + private DefaultHttpDownloader.BaseHttpDownloader downloader; public ServerClient(BootstrapProperties settings, EnvironmentInformation env) { this.props = settings; - this.downloader = new HttpDownloader.BaseHttpDownloader(settings.properties(), env.toString()); + this.downloader = new DefaultHttpDownloader.BaseHttpDownloader(settings.properties(), env.toString()); } public String getURL() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java index 4ede40d4ed9..83be230b314 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/qualitygate/QualityGateProvider.java @@ -28,9 +28,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.config.Settings; import org.sonar.api.measures.MetricFinder; -import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.MessageException; import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.api.utils.HttpDownloader; import java.net.HttpURLConnection; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java index 8f32995522a..8193d3a3ebe 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java @@ -21,9 +21,9 @@ package org.sonar.batch.repository; import com.google.common.base.Function; import com.google.common.io.InputSupplier; -import org.sonar.api.utils.HttpDownloader; import org.sonar.batch.bootstrap.ServerClient; import org.sonar.batch.protocol.input.BatchInput.ServerIssue; +import org.sonar.api.utils.HttpDownloader; import java.io.IOException; import java.io.InputStream; diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java index 28d9b251ec9..2986564bbb0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/tracking/DefaultServerLineHashesLoaderTest.java @@ -23,8 +23,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.utils.HttpDownloader; import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.api.utils.HttpDownloader; import java.net.URI; import java.net.URISyntaxException; diff --git a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java index 6c9b252f33d..12a9976207b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java @@ -26,9 +26,9 @@ import org.mockito.runners.MockitoJUnitRunner; import org.slf4j.Logger; import org.sonar.api.config.Settings; import org.sonar.api.measures.MetricFinder; -import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.MessageException; import org.sonar.batch.bootstrap.ServerClient; +import org.sonar.api.utils.HttpDownloader; import java.net.HttpURLConnection; import java.net.URI; diff --git a/sonar-core/pom.xml b/sonar-core/pom.xml index 98cc5cb0017..41ae4b71bff 100644 --- a/sonar-core/pom.xml +++ b/sonar-core/pom.xml @@ -159,6 +159,13 @@ <artifactId>bean-matchers</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.simpleframework</groupId> + <artifactId>simple</artifactId> + <version>4.1.21</version> + <scope>test</scope> + </dependency> + <!-- diff --git a/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java new file mode 100644 index 00000000000..d7a311a24c8 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/util/DefaultHttpDownloader.java @@ -0,0 +1,343 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.sonar.core.util; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.google.common.io.ByteStreams; +import com.google.common.io.CharStreams; +import com.google.common.io.Files; +import com.google.common.io.InputSupplier; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.Charsets; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.sonar.api.config.Settings; +import org.sonar.api.platform.Server; +import org.sonar.api.utils.HttpDownloader; +import org.sonar.api.utils.SonarException; +import org.sonar.api.utils.log.Loggers; + +import javax.annotation.Nullable; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.PasswordAuthentication; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.URI; +import java.nio.charset.Charset; +import java.util.List; +import java.util.Map; +import java.util.zip.GZIPInputStream; + +/** + * This component downloads HTTP files + * + * @since 2.2 + */ +public class DefaultHttpDownloader extends HttpDownloader { + public static final int TIMEOUT_MILLISECONDS = 20 * 1000; + + private final BaseHttpDownloader downloader; + private final Integer readTimeout; + + public DefaultHttpDownloader(Server server, Settings settings) { + this(server, settings, null); + } + + public DefaultHttpDownloader(Server server, Settings settings, @Nullable Integer readTimeout) { + this.readTimeout = readTimeout; + downloader = new BaseHttpDownloader(settings.getProperties(), server.getVersion()); + } + + public DefaultHttpDownloader(Settings settings) { + this(settings, null); + } + + public DefaultHttpDownloader(Settings settings, @Nullable Integer readTimeout) { + this.readTimeout = readTimeout; + downloader = new BaseHttpDownloader(settings.getProperties(), null); + } + + @Override + protected String description(URI uri) { + return String.format("%s (%s)", uri.toString(), getProxySynthesis(uri)); + } + + @Override + protected String[] getSupportedSchemes() { + return new String[] {"http", "https"}; + } + + @Override + protected byte[] readBytes(URI uri) { + return download(uri); + } + + @Override + protected String readString(URI uri, Charset charset) { + try { + return CharStreams.toString(CharStreams.newReaderSupplier(downloader.newInputSupplier(uri, this.readTimeout), charset)); + } catch (IOException e) { + throw failToDownload(uri, e); + } + } + + @Override + public String downloadPlainText(URI uri, String encoding) { + return readString(uri, Charset.forName(encoding)); + } + + @Override + public byte[] download(URI uri) { + try { + return ByteStreams.toByteArray(downloader.newInputSupplier(uri, this.readTimeout)); + } catch (IOException e) { + throw failToDownload(uri, e); + } + } + + public String getProxySynthesis(URI uri) { + return downloader.getProxySynthesis(uri); + } + + @Override + public InputStream openStream(URI uri) { + try { + return downloader.newInputSupplier(uri, this.readTimeout).getInput(); + } catch (IOException e) { + throw failToDownload(uri, e); + } + } + + @Override + public void download(URI uri, File toFile) { + try { + Files.copy(downloader.newInputSupplier(uri, this.readTimeout), toFile); + } catch (IOException e) { + FileUtils.deleteQuietly(toFile); + throw failToDownload(uri, e); + } + } + + private SonarException failToDownload(URI uri, IOException e) { + throw new SonarException(String.format("Fail to download: %s (%s)", uri, getProxySynthesis(uri)), e); + } + + public static class BaseHttpDownloader { + + private static final String GET = "GET"; + private static final String HTTP_PROXY_USER = "http.proxyUser"; + private static final String HTTP_PROXY_PASSWORD = "http.proxyPassword"; + + private static final List<String> PROXY_SETTINGS = ImmutableList.of( + "http.proxyHost", "http.proxyPort", "http.nonProxyHosts", + "http.auth.ntlm.domain", "socksProxyHost", "socksProxyPort"); + + private String userAgent; + + public BaseHttpDownloader(Map<String, String> settings, @Nullable String userAgent) { + initProxy(settings); + initUserAgent(userAgent); + } + + private void initProxy(Map<String, String> settings) { + propagateProxySystemProperties(settings); + if (requiresProxyAuthentication(settings)) { + registerProxyCredentials(settings); + } + } + + private void initUserAgent(@Nullable String sonarVersion) { + userAgent = (sonarVersion == null ? "SonarQube" : String.format("SonarQube %s", sonarVersion)); + System.setProperty("http.agent", userAgent); + } + + private String getProxySynthesis(URI uri) { + return getProxySynthesis(uri, ProxySelector.getDefault()); + } + + @VisibleForTesting + static String getProxySynthesis(URI uri, ProxySelector proxySelector) { + List<Proxy> proxies = proxySelector.select(uri); + if (proxies.size() == 1 && proxies.get(0).type().equals(Proxy.Type.DIRECT)) { + return "no proxy"; + } + + List<String> descriptions = Lists.newArrayList(); + for (Proxy proxy : proxies) { + if (proxy.type() != Proxy.Type.DIRECT) { + descriptions.add(proxy.type() + " proxy: " + proxy.address()); + } + } + + return Joiner.on(", ").join(descriptions); + } + + private void registerProxyCredentials(Map<String, String> settings) { + Authenticator.setDefault(new ProxyAuthenticator( + settings.get(HTTP_PROXY_USER), + settings.get(HTTP_PROXY_PASSWORD))); + } + + private boolean requiresProxyAuthentication(Map<String, String> settings) { + return settings.containsKey(HTTP_PROXY_USER); + } + + private void propagateProxySystemProperties(Map<String, String> settings) { + for (String key : PROXY_SETTINGS) { + if (settings.containsKey(key)) { + System.setProperty(key, settings.get(key)); + } + } + } + + public InputSupplier<InputStream> newInputSupplier(URI uri) { + return new HttpInputSupplier(uri, GET, userAgent, null, null, TIMEOUT_MILLISECONDS); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, @Nullable Integer readTimeoutMillis) { + return newInputSupplier(uri, GET, readTimeoutMillis); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, @Nullable Integer readTimeoutMillis) { + if (readTimeoutMillis != null) { + return new HttpInputSupplier(uri, requestMethod, userAgent, null, null, readTimeoutMillis); + } + return new HttpInputSupplier(uri, requestMethod, userAgent, null, null, TIMEOUT_MILLISECONDS); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password) { + return newInputSupplier(uri, GET, login, password); + } + + /** + * @since 5.0 + */ + public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, String login, String password) { + return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, TIMEOUT_MILLISECONDS); + } + + public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password, @Nullable Integer readTimeoutMillis) { + return newInputSupplier(uri, GET, login, password, readTimeoutMillis); + } + + /** + * @since 5.0 + */ + public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, String login, String password, @Nullable Integer readTimeoutMillis) { + if (readTimeoutMillis != null) { + return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, readTimeoutMillis); + } + return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, TIMEOUT_MILLISECONDS); + } + + private static class HttpInputSupplier implements InputSupplier<InputStream> { + private final String login; + private final String password; + private final URI uri; + private final String userAgent; + private final int readTimeoutMillis; + private final String requestMethod; + + HttpInputSupplier(URI uri, String requestMethod, String userAgent, String login, String password, int readTimeoutMillis) { + this.uri = uri; + this.requestMethod = requestMethod; + this.userAgent = userAgent; + this.login = login; + this.password = password; + this.readTimeoutMillis = readTimeoutMillis; + } + + @Override + public InputStream getInput() throws IOException { + Loggers.get(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri, ProxySelector.getDefault()) + ")"); + + HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); + connection.setRequestMethod(requestMethod); + HttpsTrust.INSTANCE.trust(connection); + + // allow both GZip and Deflate (ZLib) encodings + connection.setRequestProperty("Accept-Encoding", "gzip"); + if (!Strings.isNullOrEmpty(login)) { + String encoded = Base64.encodeBase64String((login + ":" + password).getBytes(Charsets.UTF_8)); + connection.setRequestProperty("Authorization", "Basic " + encoded); + } + connection.setConnectTimeout(TIMEOUT_MILLISECONDS); + connection.setReadTimeout(readTimeoutMillis); + connection.setUseCaches(true); + connection.setInstanceFollowRedirects(true); + connection.setRequestProperty("User-Agent", userAgent); + + // establish connection, get response headers + connection.connect(); + + // obtain the encoding returned by the server + String encoding = connection.getContentEncoding(); + + int responseCode = connection.getResponseCode(); + if (responseCode >= 400) { + InputStream errorResponse = null; + try { + errorResponse = connection.getErrorStream(); + if (errorResponse != null) { + String errorResponseContent = IOUtils.toString(errorResponse); + throw new HttpException(uri, responseCode, errorResponseContent); + } + throw new HttpException(uri, responseCode); + + } finally { + IOUtils.closeQuietly(errorResponse); + } + } + + InputStream resultingInputStream; + // create the appropriate stream wrapper based on the encoding type + if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { + resultingInputStream = new GZIPInputStream(connection.getInputStream()); + } else { + resultingInputStream = connection.getInputStream(); + } + return resultingInputStream; + } + } + + private static class ProxyAuthenticator extends Authenticator { + private final PasswordAuthentication auth; + + ProxyAuthenticator(String user, String password) { + auth = new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray()); + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return auth; + } + } + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpsTrust.java b/sonar-core/src/main/java/org/sonar/core/util/HttpsTrust.java index 3ca747135d1..aee97841880 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpsTrust.java +++ b/sonar-core/src/main/java/org/sonar/core/util/HttpsTrust.java @@ -17,7 +17,7 @@ * 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.sonar.api.utils; +package org.sonar.core.util; import javax.net.ssl.*; import java.net.HttpURLConnection; diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java index 091b157ecfd..06dcdedf7c1 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpDownloaderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/DefaultHttpDownloaderTest.java @@ -17,7 +17,7 @@ * 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.sonar.api.utils; +package org.sonar.core.util; import com.google.common.base.Charsets; import org.hamcrest.BaseMatcher; @@ -37,6 +37,7 @@ import org.simpleframework.http.core.Container; import org.simpleframework.transport.connect.SocketConnection; import org.sonar.api.config.Settings; import org.sonar.api.platform.Server; +import org.sonar.api.utils.SonarException; import java.io.File; import java.io.IOException; @@ -57,7 +58,7 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class HttpDownloaderTest { +public class DefaultHttpDownloaderTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -125,25 +126,25 @@ public class HttpDownloaderTest { @Test public void downloadBytes() throws URISyntaxException { - byte[] bytes = new HttpDownloader(new Settings()).readBytes(new URI(baseUrl)); + byte[] bytes = new DefaultHttpDownloader(new Settings()).readBytes(new URI(baseUrl)); assertThat(bytes.length).isGreaterThan(10); } @Test public void readString() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl), Charsets.UTF_8); + String text = new DefaultHttpDownloader(new Settings()).readString(new URI(baseUrl), Charsets.UTF_8); assertThat(text.length()).isGreaterThan(10); } @Test public void readGzipString() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/gzip/"), Charsets.UTF_8); + String text = new DefaultHttpDownloader(new Settings()).readString(new URI(baseUrl + "/gzip/"), Charsets.UTF_8); assertThat(text).isEqualTo("GZIP response"); } @Test public void readStringWithDefaultTimeout() throws URISyntaxException { - String text = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); + String text = new DefaultHttpDownloader(new Settings()).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); assertThat(text.length()).isGreaterThan(10); } @@ -159,7 +160,7 @@ public class HttpDownloaderTest { public void describeTo(Description arg0) { } }); - new HttpDownloader(new Settings(), 50).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); + new DefaultHttpDownloader(new Settings(), 50).readString(new URI(baseUrl + "/timeout/"), Charsets.UTF_8); } @Test @@ -167,7 +168,7 @@ public class HttpDownloaderTest { File toDir = temporaryFolder.newFolder(); File toFile = new File(toDir, "downloadToFile.txt"); - new HttpDownloader(new Settings()).download(new URI(baseUrl), toFile); + new DefaultHttpDownloader(new Settings()).download(new URI(baseUrl), toFile); assertThat(toFile).exists(); assertThat(toFile.length()).isGreaterThan(10l); } @@ -179,7 +180,7 @@ public class HttpDownloaderTest { try { int port = new InetSocketAddress(0).getPort(); - new HttpDownloader(new Settings()).download(new URI("http://localhost:" + port), toFile); + new DefaultHttpDownloader(new Settings()).download(new URI("http://localhost:" + port), toFile); } catch (SonarException e) { assertThat(toFile).doesNotExist(); } @@ -190,7 +191,7 @@ public class HttpDownloaderTest { Server server = mock(Server.class); when(server.getVersion()).thenReturn("2.2"); - InputStream stream = new HttpDownloader(server, new Settings()).openStream(new URI(baseUrl)); + InputStream stream = new DefaultHttpDownloader(server, new Settings()).openStream(new URI(baseUrl)); Properties props = new Properties(); props.load(stream); stream.close(); @@ -200,7 +201,7 @@ public class HttpDownloaderTest { @Test public void followRedirect() throws URISyntaxException { - String content = new HttpDownloader(new Settings()).readString(new URI(baseUrl + "/redirect/"), Charsets.UTF_8); + String content = new DefaultHttpDownloader(new Settings()).readString(new URI(baseUrl + "/redirect/"), Charsets.UTF_8); assertThat(content).contains("agent"); } @@ -208,24 +209,24 @@ public class HttpDownloaderTest { public void shouldGetDirectProxySynthesis() throws URISyntaxException { ProxySelector proxySelector = mock(ProxySelector.class); when(proxySelector.select(any(URI.class))).thenReturn(Arrays.asList(Proxy.NO_PROXY)); - assertThat(HttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("no proxy"); + assertThat(DefaultHttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("no proxy"); } @Test public void shouldGetProxySynthesis() throws URISyntaxException { ProxySelector proxySelector = mock(ProxySelector.class); when(proxySelector.select(any(URI.class))).thenReturn(Arrays.<Proxy>asList(new FakeProxy())); - assertThat(HttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("HTTP proxy: /123.45.67.89:4040"); + assertThat(DefaultHttpDownloader.BaseHttpDownloader.getProxySynthesis(new URI("http://an_url"), proxySelector)).isEqualTo("HTTP proxy: /123.45.67.89:4040"); } @Test public void supported_schemes() { - assertThat(new HttpDownloader(new Settings()).getSupportedSchemes()).contains("http"); + assertThat(new DefaultHttpDownloader(new Settings()).getSupportedSchemes()).contains("http"); } @Test public void uri_description() throws URISyntaxException { - String description = new HttpDownloader(new Settings()).description(new URI("http://sonarsource.org")); + String description = new DefaultHttpDownloader(new Settings()).description(new URI("http://sonarsource.org")); assertThat(description).matches("http://sonarsource.org \\(.*\\)"); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpsTrustTest.java b/sonar-core/src/test/java/org/sonar/core/util/HttpsTrustTest.java index 40fdffbbca3..689c5e36468 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/HttpsTrustTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/HttpsTrustTest.java @@ -17,7 +17,7 @@ * 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.sonar.api.utils; +package org.sonar.core.util; import org.junit.Test; diff --git a/sonar-plugin-api/pom.xml b/sonar-plugin-api/pom.xml index c8ad930e776..039aba7b84a 100644 --- a/sonar-plugin-api/pom.xml +++ b/sonar-plugin-api/pom.xml @@ -182,12 +182,6 @@ <artifactId>dbunit</artifactId> <scope>test</scope> </dependency> - <dependency> - <groupId>org.simpleframework</groupId> - <artifactId>simple</artifactId> - <version>4.1.21</version> - <scope>test</scope> - </dependency> </dependencies> <build> diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java index 6cff32c4d1a..1aa4bc24193 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/DefaultFormulaData.java @@ -19,12 +19,12 @@ */ package org.sonar.api.batch; -import com.google.common.collect.Lists; import org.sonar.api.measures.FormulaData; import org.sonar.api.measures.Measure; import org.sonar.api.measures.MeasuresFilter; import org.sonar.api.measures.Metric; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -61,7 +61,7 @@ public class DefaultFormulaData implements FormulaData { @Override public Collection<FormulaData> getChildren() { - List<FormulaData> result = Lists.newArrayList(); + List<FormulaData> result = new ArrayList<>(); for (DecoratorContext childContext : decoratorContext.getChildren()) { result.add(new DefaultFormulaData(childContext)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java index f7d55073bc5..98502636ad2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/ProjectClasspath.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch; -import com.google.common.collect.Lists; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.project.MavenProject; import org.sonar.api.BatchComponent; @@ -29,6 +28,7 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; import java.util.List; /** @@ -65,7 +65,7 @@ public class ProjectClasspath implements BatchComponent { protected URLClassLoader createClassLoader() { try { - List<URL> urls = Lists.newArrayList(); + List<URL> urls = new ArrayList<>(); for (File file : getElements()) { urls.add(file.toURI().toURL()); } @@ -78,7 +78,7 @@ public class ProjectClasspath implements BatchComponent { protected List<File> createElements() { try { - List<File> files = Lists.newArrayList(); + List<File> files = new ArrayList<>(); if (pom.getCompileClasspathElements() != null) { for (String classPathString : pom.getCompileClasspathElements()) { files.add(new File(classPathString)); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TimeMachineQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/TimeMachineQuery.java index 4ac061e3bed..e4d5482ad5f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/TimeMachineQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/TimeMachineQuery.java @@ -19,13 +19,13 @@ */ package org.sonar.api.batch; -import com.google.common.collect.Lists; import org.apache.commons.lang.builder.ToStringBuilder; import org.sonar.api.measures.Metric; import org.sonar.api.resources.Resource; import java.util.Arrays; import java.util.Date; +import java.util.LinkedList; import java.util.List; /** @@ -91,7 +91,7 @@ public class TimeMachineQuery { */ public TimeMachineQuery setMetrics(List<Metric> metrics) { this.metrics = metrics; - this.metricKeys = Lists.newLinkedList(); + this.metricKeys = new LinkedList<>(); for (Metric metric : this.metrics) { this.metricKeys.add(metric.getKey()); } @@ -120,7 +120,7 @@ public class TimeMachineQuery { */ public TimeMachineQuery setMetrics(Metric... metrics) { this.metrics = Arrays.asList(metrics); - this.metricKeys = Lists.newLinkedList(); + this.metricKeys = new LinkedList<>(); for (Metric metric : this.metrics) { this.metricKeys.add(metric.getKey()); } @@ -225,10 +225,10 @@ public class TimeMachineQuery { @Override public String toString() { return new ToStringBuilder(this) - .append("resource", resource) - .append("metrics", metrics) - .append("from", from) - .append("to", to) - .toString(); + .append("resource", resource) + .append("metrics", metrics) + .append("from", from) + .append("to", to) + .toString(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java index afc120524a5..9f123486313 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectDefinition.java @@ -19,7 +19,6 @@ */ package org.sonar.api.batch.bootstrap; -import com.google.common.collect.Lists; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; @@ -80,8 +79,8 @@ public class ProjectDefinition { private File baseDir, workDir, buildDir; private Map<String, String> properties = new HashMap<String, String>(); private ProjectDefinition parent = null; - private List<ProjectDefinition> subProjects = Lists.newArrayList(); - private List<Object> containerExtensions = Lists.newArrayList(); + private List<ProjectDefinition> subProjects = new ArrayList<>(); + private List<Object> containerExtensions = new ArrayList<>(); private ProjectDefinition(Properties p) { for (Entry<Object, Object> entry : p.entrySet()) { @@ -603,7 +602,7 @@ public class ProjectDefinition { } private static List<String> trim(String[] strings) { - List<String> result = Lists.newArrayList(); + List<String> result = new ArrayList<>(); for (String s : strings) { result.add(StringUtils.trim(s)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java index 5cdc2aba2b3..b755da6f5b5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Checks.java @@ -32,6 +32,8 @@ import javax.annotation.CheckForNull; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; @@ -82,8 +84,8 @@ import java.util.Map; public class Checks<C> { private final ActiveRules activeRules; private final String repository; - private final Map<RuleKey, C> checkByRule = Maps.newHashMap(); - private final Map<C, RuleKey> ruleByCheck = Maps.newIdentityHashMap(); + private final Map<RuleKey, C> checkByRule = new HashMap<>(); + private final Map<C, RuleKey> ruleByCheck = new IdentityHashMap<>(); Checks(ActiveRules activeRules, String repository) { this.activeRules = activeRules; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java index 24edaa4e763..6e23900143b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilder.java @@ -19,10 +19,10 @@ */ package org.sonar.api.batch.rule.internal; -import com.google.common.collect.Maps; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.rule.RuleKey; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -33,7 +33,7 @@ import java.util.Map; */ public class ActiveRulesBuilder { - private final Map<RuleKey, NewActiveRule> map = Maps.newLinkedHashMap(); + private final Map<RuleKey, NewActiveRule> map = new LinkedHashMap<>(); public NewActiveRule create(RuleKey ruleKey) { return new NewActiveRule(this, ruleKey); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java index d0f53d44024..8760b8f8d2e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.io.StringReader; import java.util.Calendar; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -51,7 +52,7 @@ public final class License { private Map<String, String> additionalProperties; private License(Map<String, String> properties) { - this.additionalProperties = Maps.newHashMap(properties); + this.additionalProperties = new HashMap<>(properties); product = StringUtils.defaultString(get("Product", properties), get("Plugin", properties)); organization = StringUtils.defaultString(get("Organisation", properties), get("Name", properties)); expirationDate = StringUtils.defaultString(get("Expiration", properties), get("Expires", properties)); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java index 8e9b3d5df10..c8c73bde7e5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java @@ -33,11 +33,10 @@ import org.sonar.api.resources.Qualifiers; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static com.google.common.collect.Lists.newArrayList; - /** * Declare a plugin property. Values are available at runtime through the component {@link Settings}. * <p/> @@ -127,7 +126,7 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension .propertySetKey(annotation.propertySetKey()) .fields(PropertyFieldDefinition.create(annotation.fields())) .deprecatedKey(annotation.deprecatedKey()); - List<String> qualifiers = newArrayList(); + List<String> qualifiers = new ArrayList<>(); if (annotation.project()) { qualifiers.add(Qualifiers.PROJECT); } @@ -322,14 +321,14 @@ public final class PropertyDefinition implements BatchExtension, ServerExtension * @see PropertyDefinition.Builder#subCategory(String) */ private String subCategory = ""; - private List<String> onQualifiers = newArrayList(); - private List<String> onlyOnQualifiers = newArrayList(); + private List<String> onQualifiers = new ArrayList<>(); + private List<String> onlyOnQualifiers = new ArrayList<>(); private boolean global = true; private PropertyType type = PropertyType.STRING; - private List<String> options = newArrayList(); + private List<String> options = new ArrayList<>(); private boolean multiValues = false; private String propertySetKey = ""; - private List<PropertyFieldDefinition> fields = newArrayList(); + private List<PropertyFieldDefinition> fields = new ArrayList<>(); private String deprecatedKey = ""; private boolean hidden = false; private int index = 999; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java index 02c86f06023..b44f6696d27 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinitions.java @@ -20,7 +20,6 @@ package org.sonar.api.config; import com.google.common.base.Strings; -import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; @@ -44,12 +43,12 @@ import java.util.Map; */ public final class PropertyDefinitions implements BatchComponent, ServerComponent { - private final Map<String, PropertyDefinition> definitions = Maps.newHashMap(); - private final Map<String, Category> categories = Maps.newHashMap(); - private final Map<String, SubCategory> subcategories = Maps.newHashMap(); + private final Map<String, PropertyDefinition> definitions = new HashMap<>(); + private final Map<String, Category> categories = new HashMap<>(); + private final Map<String, SubCategory> subcategories = new HashMap<>(); // deprecated key -> new key - private final Map<String, String> deprecatedKeys = Maps.newHashMap(); + private final Map<String, String> deprecatedKeys = new HashMap<>(); public PropertyDefinitions(Object... components) { if (components != null) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java index 06d856790de..93b68d49878 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyFieldDefinition.java @@ -27,10 +27,9 @@ import org.sonar.api.PropertyType; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.List; -import static com.google.common.collect.Lists.newArrayList; - /** * @since 3.3 */ @@ -52,16 +51,16 @@ public final class PropertyFieldDefinition { } static List<PropertyFieldDefinition> create(PropertyField[] fields) { - List<PropertyFieldDefinition> definitions = newArrayList(); + List<PropertyFieldDefinition> definitions = new ArrayList<>(); for (PropertyField field : fields) { definitions.add(PropertyFieldDefinition.build(field.key()) - .name(field.name()) - .description(field.description()) - .indicativeSize(field.indicativeSize()) - .type(field.type()) - .options(field.options()) - .build() - ); + .name(field.name()) + .description(field.description()) + .indicativeSize(field.indicativeSize()) + .type(field.type()) + .options(field.options()) + .build() + ); } return definitions; } @@ -112,7 +111,7 @@ public final class PropertyFieldDefinition { this.description = ""; this.indicativeSize = 20; this.type = PropertyType.STRING; - this.options = newArrayList(); + this.options = new ArrayList<>(); } public Builder name(String name) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java index fa422fa815f..662c661d195 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java @@ -23,7 +23,6 @@ import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; @@ -33,6 +32,7 @@ import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -235,7 +235,7 @@ public class Settings implements BatchComponent, ServerComponent { return ArrayUtils.EMPTY_STRING_ARRAY; } - List<String> values = Lists.newArrayList(); + List<String> values = new ArrayList<>(); for (String v : Splitter.on(",").trimResults().split(value)) { values.add(v.replace("%2C", ",")); } @@ -276,7 +276,7 @@ public class Settings implements BatchComponent, ServerComponent { } public List<String> getKeysStartingWith(String prefix) { - List<String> result = Lists.newArrayList(); + List<String> result = new ArrayList<>(); for (String key : properties.keySet()) { if (StringUtils.startsWith(key, prefix)) { result.add(key); @@ -303,7 +303,7 @@ public class Settings implements BatchComponent, ServerComponent { String text = null; if (values != null) { - List<String> escaped = Lists.newArrayList(); + List<String> escaped = new ArrayList<>(); for (String value : values) { if (null != value) { escaped.add(value.replace(",", "%2C")); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java index b13b2a4806a..149a7450994 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java @@ -22,21 +22,16 @@ package org.sonar.api.issue.action; import com.google.common.annotations.Beta; import org.sonar.api.ServerExtension; +import java.util.ArrayList; import java.util.List; -import static com.google.common.collect.Lists.newArrayList; - /** * @since 3.6 */ @Beta public class Actions implements ServerExtension { - private final List<Action> actions; - - public Actions() { - actions = newArrayList(); - } + private final List<Action> actions = new ArrayList<>(); public Action add(String actionKey) { Action action = new Action(actionKey); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java index d2827256243..db09f62cef2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/internal/DefaultIssue.java @@ -42,8 +42,6 @@ import javax.annotation.Nullable; import java.io.Serializable; import java.util.*; -import static com.google.common.collect.Lists.newArrayList; - /** * PLUGINS MUST NOT BE USED THIS CLASS, EXCEPT FOR UNIT TESTING. * @@ -506,7 +504,7 @@ public class DefaultIssue implements Issue { public DefaultIssue addChange(FieldDiffs change) { if (changes == null) { - changes = newArrayList(); + changes = new ArrayList<>(); } changes.add(change); return this; @@ -526,7 +524,7 @@ public class DefaultIssue implements Issue { public DefaultIssue addComment(DefaultIssueComment comment) { if (comments == null) { - comments = newArrayList(); + comments = new ArrayList<>(); } comments.add(comment); return this; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java index 3427c5a8461..7b3dddee46d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoreMetrics.java @@ -22,7 +22,6 @@ package org.sonar.api.measures; import com.google.common.annotations.Beta; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import org.sonar.api.resources.Scopes; import org.sonar.api.test.MutableTestPlan; import org.sonar.api.utils.SonarException; @@ -31,6 +30,7 @@ import javax.annotation.Nullable; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.LinkedList; import java.util.List; /** @@ -2226,7 +2226,7 @@ public final class CoreMetrics { private static final List<Metric> METRICS; static { - METRICS = Lists.newLinkedList(); + METRICS = new LinkedList<>(); for (Field field : CoreMetrics.class.getFields()) { if (!Modifier.isTransient(field.getModifiers()) && Metric.class.isAssignableFrom(field.getType())) { try { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java index 823155b2d0a..d4ddac91853 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/CoverageMeasuresBuilder.java @@ -19,10 +19,10 @@ */ package org.sonar.api.measures; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.sonar.api.utils.KeyValueFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -110,7 +110,7 @@ public final class CoverageMeasuresBuilder { } public Collection<Measure> createMeasures() { - Collection<Measure> measures = Lists.newArrayList(); + Collection<Measure> measures = new ArrayList<>(); if (getLinesToCover() > 0) { measures.add(new Measure(CoreMetrics.LINES_TO_COVER, (double) getLinesToCover())); measures.add(new Measure(CoreMetrics.UNCOVERED_LINES, (double) (getLinesToCover() - getCoveredLines()))); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java index 15f8bd4b4cf..a541337ca97 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/profiles/RulesProfile.java @@ -21,7 +21,6 @@ package org.sonar.api.profiles; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Transformer; import org.apache.commons.lang.StringUtils; @@ -34,7 +33,6 @@ import org.sonar.api.utils.MessageException; import javax.annotation.CheckForNull; import javax.annotation.Nullable; - import java.util.ArrayList; import java.util.List; @@ -68,7 +66,7 @@ public class RulesProfile implements Cloneable { private Boolean defaultProfile = Boolean.FALSE; private String language; private String parentName; - private List<ActiveRule> activeRules = Lists.newArrayList(); + private List<ActiveRule> activeRules = new ArrayList<>(); /** * @deprecated use the factory method create() @@ -84,7 +82,7 @@ public class RulesProfile implements Cloneable { public RulesProfile(String name, String language) { this.name = name; this.language = language; - this.activeRules = Lists.newArrayList(); + this.activeRules = new ArrayList<>(); } /** @@ -163,7 +161,7 @@ public class RulesProfile implements Cloneable { if (acceptDisabledRules) { return activeRules; } - List<ActiveRule> result = Lists.newArrayList(); + List<ActiveRule> result = new ArrayList<>(); for (ActiveRule activeRule : activeRules) { if (activeRule.isEnabled()) { result.add(activeRule); @@ -244,7 +242,7 @@ public class RulesProfile implements Cloneable { * @return the list of active rules for a given severity */ public List<ActiveRule> getActiveRules(RulePriority severity) { - List<ActiveRule> result = Lists.newArrayList(); + List<ActiveRule> result = new ArrayList<>(); for (ActiveRule activeRule : activeRules) { if (activeRule.getSeverity().equals(severity) && activeRule.isEnabled()) { result.add(activeRule); @@ -258,7 +256,7 @@ public class RulesProfile implements Cloneable { * Only enabled rules are selected. Disabled rules are excluded. */ public List<ActiveRule> getActiveRulesByRepository(String repositoryKey) { - List<ActiveRule> result = Lists.newArrayList(); + List<ActiveRule> result = new ArrayList<>(); for (ActiveRule activeRule : activeRules) { if (repositoryKey.equals(activeRule.getRepositoryKey()) && activeRule.isEnabled()) { result.add(activeRule); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java index 51a6cde057e..07457c92729 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java @@ -28,6 +28,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -47,7 +48,7 @@ public final class InputFileUtils { * @return not null list */ public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) { - List<java.io.File> files = Lists.newArrayList(); + List<java.io.File> files = new ArrayList<>(); for (InputFile inputFile : inputFiles) { files.add(inputFile.getFile()); } @@ -89,7 +90,7 @@ public final class InputFileUtils { * For internal and for testing purposes. Please use the FileSystem component to access files. */ public static List<InputFile> create(java.io.File basedir, Collection<java.io.File> files) { - List<InputFile> inputFiles = Lists.newArrayList(); + List<InputFile> inputFiles = new ArrayList<>(); for (File file : files) { InputFile inputFile = create(basedir, file); if (inputFile != null) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java index c6e4031f9e2..f6fa337b482 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java @@ -26,10 +26,10 @@ import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ListMultimap; -import com.google.common.collect.Lists; import org.sonar.api.ServerExtension; import org.sonar.api.task.TaskExtension; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -80,7 +80,7 @@ public class ResourceTypeTree implements TaskExtension, ServerExtension { } public static final class Builder { - private List<ResourceType> types = Lists.newArrayList(); + private List<ResourceType> types = new ArrayList<>(); private ListMultimap<String, String> relations = ArrayListMultimap.create(); private ResourceType root; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java index a49829e9c20..b5032895f83 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileExclusions.java @@ -19,13 +19,13 @@ */ package org.sonar.api.scan.filesystem; -import com.google.common.collect.Lists; import com.google.common.collect.ObjectArrays; import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; +import java.util.ArrayList; import java.util.List; /** @@ -52,7 +52,7 @@ public class FileExclusions implements BatchComponent { private String[] inclusions(String propertyKey) { String[] patterns = sanitize(settings.getStringArray(propertyKey)); - List<String> list = Lists.newArrayList(); + List<String> list = new ArrayList<>(); for (String pattern : patterns) { if (!"**/*".equals(pattern) && !"file:**/*".equals(pattern)) { list.add(pattern); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java index 6da18e4a049..9bd4ac7be0c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java @@ -21,13 +21,13 @@ package org.sonar.api.scan.filesystem; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; -import com.google.common.collect.Lists; import org.sonar.api.BatchComponent; import org.sonar.api.utils.PathUtils; import javax.annotation.CheckForNull; import java.io.File; +import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -50,7 +50,7 @@ public class PathResolver implements BatchComponent { } public List<File> relativeFiles(File dir, List<String> paths) { - List<File> result = Lists.newArrayList(); + List<File> result = new ArrayList<>(); for (String path : paths) { result.add(relativeFile(dir, path)); } @@ -59,7 +59,7 @@ public class PathResolver implements BatchComponent { @CheckForNull public RelativePath relativePath(Collection<File> dirs, File file) { - List<String> stack = Lists.newArrayList(); + List<String> stack = new ArrayList<>(); File cursor = file; while (cursor != null) { File parentDir = parentDir(dirs, cursor); @@ -74,7 +74,7 @@ public class PathResolver implements BatchComponent { @CheckForNull public String relativePath(File dir, File file) { - List<String> stack = Lists.newArrayList(); + List<String> stack = new ArrayList<>(); String dirPath = PathUtils.canonicalPath(dir); File cursor = file; while (cursor != null) { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java index 2cbd0ef0b4d..427350b2ea6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/Request.java @@ -28,6 +28,7 @@ import org.sonar.api.utils.SonarException; import javax.annotation.CheckForNull; import java.io.InputStream; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -187,7 +188,7 @@ public abstract class Request { return null; } Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); - List<E> result = Lists.newArrayList(); + List<E> result = new ArrayList<>(); for (String s : values) { result.add(Enum.valueOf(enumClass, s)); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java index 054ba773196..af6d5cb7a2e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/ws/internal/ValidatingRequest.java @@ -30,6 +30,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.io.InputStream; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -94,7 +95,7 @@ public abstract class ValidatingRequest extends Request { return null; } Iterable<String> values = Splitter.on(',').omitEmptyStrings().trimResults().split(value); - List<E> result = Lists.newArrayList(); + List<E> result = new ArrayList<>(); for (String s : values) { validate(s, definition); result.add(Enum.valueOf(enumClass, s)); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/FieldUtils2.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/FieldUtils2.java index 85f466672aa..2acdbe27537 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/FieldUtils2.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/FieldUtils2.java @@ -19,11 +19,11 @@ */ package org.sonar.api.utils; -import com.google.common.collect.Lists; import org.apache.commons.lang.ClassUtils; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -44,13 +44,13 @@ public final class FieldUtils2 { * <code>False</code> only matches public fields. */ public static List<Field> getFields(Class clazz, boolean forceAccess) { - List<Field> result = Lists.newArrayList(); + List<Field> result = new ArrayList<>(); Class c = clazz; while (c != null) { for (Field declaredField : c.getDeclaredFields()) { if (!Modifier.isPublic(declaredField.getModifiers())) { if (forceAccess) { - declaredField.setAccessible(true);//NOSONAR only works from sufficiently privileged code + declaredField.setAccessible(true);// NOSONAR only works from sufficiently privileged code } else { continue; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java index ee0ba26214c..2c0ae79479c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/HttpDownloader.java @@ -19,322 +19,25 @@ */ package org.sonar.api.utils; -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Joiner; -import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.google.common.io.ByteStreams; -import com.google.common.io.CharStreams; -import com.google.common.io.Files; -import com.google.common.io.InputSupplier; -import org.apache.commons.codec.binary.Base64; -import org.apache.commons.io.Charsets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; -import org.sonar.api.config.Settings; -import org.sonar.api.platform.Server; -import org.sonar.api.utils.log.Loggers; - -import javax.annotation.Nullable; import java.io.File; -import java.io.IOException; import java.io.InputStream; -import java.net.Authenticator; -import java.net.HttpURLConnection; -import java.net.PasswordAuthentication; -import java.net.Proxy; -import java.net.ProxySelector; import java.net.URI; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Map; -import java.util.zip.GZIPInputStream; /** - * This component downloads HTTP files - * - * @since 2.2 + * This component is available in IoC container, so it should be injected through + * a constructor parameter. It is available in both batch and server. */ -public class HttpDownloader extends UriReader.SchemeProcessor implements BatchComponent, ServerComponent { - public static final int TIMEOUT_MILLISECONDS = 20 * 1000; - - private final BaseHttpDownloader downloader; - private final Integer readTimeout; - - public HttpDownloader(Server server, Settings settings) { - this(server, settings, null); - } - - public HttpDownloader(Server server, Settings settings, @Nullable Integer readTimeout) { - this.readTimeout = readTimeout; - downloader = new BaseHttpDownloader(settings.getProperties(), server.getVersion()); - } - - public HttpDownloader(Settings settings) { - this(settings, null); - } - - public HttpDownloader(Settings settings, @Nullable Integer readTimeout) { - this.readTimeout = readTimeout; - downloader = new BaseHttpDownloader(settings.getProperties(), null); - } - - @Override - String description(URI uri) { - return String.format("%s (%s)", uri.toString(), getProxySynthesis(uri)); - } - - @Override - String[] getSupportedSchemes() { - return new String[] {"http", "https"}; - } - - @Override - byte[] readBytes(URI uri) { - return download(uri); - } - - @Override - String readString(URI uri, Charset charset) { - try { - return CharStreams.toString(CharStreams.newReaderSupplier(downloader.newInputSupplier(uri, this.readTimeout), charset)); - } catch (IOException e) { - throw failToDownload(uri, e); - } - } - - public String downloadPlainText(URI uri, String encoding) { - return readString(uri, Charset.forName(encoding)); - } - - public byte[] download(URI uri) { - try { - return ByteStreams.toByteArray(downloader.newInputSupplier(uri, this.readTimeout)); - } catch (IOException e) { - throw failToDownload(uri, e); - } - } - - public String getProxySynthesis(URI uri) { - return downloader.getProxySynthesis(uri); - } - - public InputStream openStream(URI uri) { - try { - return downloader.newInputSupplier(uri, this.readTimeout).getInput(); - } catch (IOException e) { - throw failToDownload(uri, e); - } - } - - public void download(URI uri, File toFile) { - try { - Files.copy(downloader.newInputSupplier(uri, this.readTimeout), toFile); - } catch (IOException e) { - FileUtils.deleteQuietly(toFile); - throw failToDownload(uri, e); - } - } - - private SonarException failToDownload(URI uri, IOException e) { - throw new SonarException(String.format("Fail to download: %s (%s)", uri, getProxySynthesis(uri)), e); - } - - public static class BaseHttpDownloader { - - private static final String GET = "GET"; - private static final String HTTP_PROXY_USER = "http.proxyUser"; - private static final String HTTP_PROXY_PASSWORD = "http.proxyPassword"; - - private static final List<String> PROXY_SETTINGS = ImmutableList.of( - "http.proxyHost", "http.proxyPort", "http.nonProxyHosts", - "http.auth.ntlm.domain", "socksProxyHost", "socksProxyPort"); - - private String userAgent; - - public BaseHttpDownloader(Map<String, String> settings, @Nullable String userAgent) { - initProxy(settings); - initUserAgent(userAgent); - } - - private void initProxy(Map<String, String> settings) { - propagateProxySystemProperties(settings); - if (requiresProxyAuthentication(settings)) { - registerProxyCredentials(settings); - } - } - - private void initUserAgent(@Nullable String sonarVersion) { - userAgent = (sonarVersion == null ? "SonarQube" : String.format("SonarQube %s", sonarVersion)); - System.setProperty("http.agent", userAgent); - } - - private String getProxySynthesis(URI uri) { - return getProxySynthesis(uri, ProxySelector.getDefault()); - } - - @VisibleForTesting - static String getProxySynthesis(URI uri, ProxySelector proxySelector) { - List<Proxy> proxies = proxySelector.select(uri); - if (proxies.size() == 1 && proxies.get(0).type().equals(Proxy.Type.DIRECT)) { - return "no proxy"; - } - - List<String> descriptions = Lists.newArrayList(); - for (Proxy proxy : proxies) { - if (proxy.type() != Proxy.Type.DIRECT) { - descriptions.add(proxy.type() + " proxy: " + proxy.address()); - } - } - - return Joiner.on(", ").join(descriptions); - } - - private void registerProxyCredentials(Map<String, String> settings) { - Authenticator.setDefault(new ProxyAuthenticator( - settings.get(HTTP_PROXY_USER), - settings.get(HTTP_PROXY_PASSWORD))); - } - - private boolean requiresProxyAuthentication(Map<String, String> settings) { - return settings.containsKey(HTTP_PROXY_USER); - } - - private void propagateProxySystemProperties(Map<String, String> settings) { - for (String key : PROXY_SETTINGS) { - if (settings.containsKey(key)) { - System.setProperty(key, settings.get(key)); - } - } - } - - public InputSupplier<InputStream> newInputSupplier(URI uri) { - return new HttpInputSupplier(uri, GET, userAgent, null, null, TIMEOUT_MILLISECONDS); - } - - public InputSupplier<InputStream> newInputSupplier(URI uri, @Nullable Integer readTimeoutMillis) { - return newInputSupplier(uri, GET, readTimeoutMillis); - } - - public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, @Nullable Integer readTimeoutMillis) { - if (readTimeoutMillis != null) { - return new HttpInputSupplier(uri, requestMethod, userAgent, null, null, readTimeoutMillis); - } - return new HttpInputSupplier(uri, requestMethod, userAgent, null, null, TIMEOUT_MILLISECONDS); - } +public abstract class HttpDownloader extends UriReader.SchemeProcessor implements BatchComponent, ServerComponent { + public abstract String downloadPlainText(URI uri, String encoding); - public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password) { - return newInputSupplier(uri, GET, login, password); - } - - /** - * @since 5.0 - */ - public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, String login, String password) { - return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, TIMEOUT_MILLISECONDS); - } - - public InputSupplier<InputStream> newInputSupplier(URI uri, String login, String password, @Nullable Integer readTimeoutMillis) { - return newInputSupplier(uri, GET, login, password, readTimeoutMillis); - } - - /** - * @since 5.0 - */ - public InputSupplier<InputStream> newInputSupplier(URI uri, String requestMethod, String login, String password, @Nullable Integer readTimeoutMillis) { - if (readTimeoutMillis != null) { - return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, readTimeoutMillis); - } - return new HttpInputSupplier(uri, requestMethod, userAgent, login, password, TIMEOUT_MILLISECONDS); - } - - private static class HttpInputSupplier implements InputSupplier<InputStream> { - private final String login; - private final String password; - private final URI uri; - private final String userAgent; - private final int readTimeoutMillis; - private final String requestMethod; - - HttpInputSupplier(URI uri, String requestMethod, String userAgent, String login, String password, int readTimeoutMillis) { - this.uri = uri; - this.requestMethod = requestMethod; - this.userAgent = userAgent; - this.login = login; - this.password = password; - this.readTimeoutMillis = readTimeoutMillis; - } - - @Override - public InputStream getInput() throws IOException { - Loggers.get(getClass()).debug("Download: " + uri + " (" + getProxySynthesis(uri, ProxySelector.getDefault()) + ")"); - - HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection(); - connection.setRequestMethod(requestMethod); - HttpsTrust.INSTANCE.trust(connection); + public abstract byte[] download(URI uri); - // allow both GZip and Deflate (ZLib) encodings - connection.setRequestProperty("Accept-Encoding", "gzip"); - if (!Strings.isNullOrEmpty(login)) { - String encoded = Base64.encodeBase64String((login + ":" + password).getBytes(Charsets.UTF_8)); - connection.setRequestProperty("Authorization", "Basic " + encoded); - } - connection.setConnectTimeout(TIMEOUT_MILLISECONDS); - connection.setReadTimeout(readTimeoutMillis); - connection.setUseCaches(true); - connection.setInstanceFollowRedirects(true); - connection.setRequestProperty("User-Agent", userAgent); + public abstract InputStream openStream(URI uri); - // establish connection, get response headers - connection.connect(); - - // obtain the encoding returned by the server - String encoding = connection.getContentEncoding(); - - int responseCode = connection.getResponseCode(); - if (responseCode >= 400) { - InputStream errorResponse = null; - try { - errorResponse = connection.getErrorStream(); - if (errorResponse != null) { - String errorResponseContent = IOUtils.toString(errorResponse); - throw new HttpException(uri, responseCode, errorResponseContent); - } - throw new HttpException(uri, responseCode); - - } finally { - IOUtils.closeQuietly(errorResponse); - } - } - - InputStream resultingInputStream; - // create the appropriate stream wrapper based on the encoding type - if (encoding != null && "gzip".equalsIgnoreCase(encoding)) { - resultingInputStream = new GZIPInputStream(connection.getInputStream()); - } else { - resultingInputStream = connection.getInputStream(); - } - return resultingInputStream; - } - } - - private static class ProxyAuthenticator extends Authenticator { - private final PasswordAuthentication auth; - - ProxyAuthenticator(String user, String password) { - auth = new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray()); - } - - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return auth; - } - } - } + public abstract void download(URI uri, File toFile); public static class HttpException extends RuntimeException { private final URI uri; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java index a5d001731aa..ad435de7b23 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/UriReader.java @@ -86,14 +86,14 @@ public class UriReader implements BatchComponent, ServerComponent { return processor; } - abstract static class SchemeProcessor { - abstract String[] getSupportedSchemes(); + public abstract static class SchemeProcessor { + protected abstract String[] getSupportedSchemes(); - abstract byte[] readBytes(URI uri); + protected abstract byte[] readBytes(URI uri); - abstract String readString(URI uri, Charset charset); + protected abstract String readString(URI uri, Charset charset); - abstract String description(URI uri); + protected abstract String description(URI uri); } @@ -108,7 +108,7 @@ public class UriReader implements BatchComponent, ServerComponent { } @Override - byte[] readBytes(URI uri) { + protected byte[] readBytes(URI uri) { try { return Files.toByteArray(new File(uri)); } catch (IOException e) { @@ -117,7 +117,7 @@ public class UriReader implements BatchComponent, ServerComponent { } @Override - String readString(URI uri, Charset charset) { + protected String readString(URI uri, Charset charset) { try { return Files.toString(new File(uri), charset); } catch (IOException e) { @@ -126,7 +126,7 @@ public class UriReader implements BatchComponent, ServerComponent { } @Override - String description(URI uri) { + protected String description(URI uri) { return new File(uri).getAbsolutePath(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java index c100a48cb54..7a14ba52e9e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java @@ -23,12 +23,12 @@ import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.System2; import java.io.File; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; @@ -38,8 +38,8 @@ import java.util.Map; */ public class Command { private final String executable; - private final List<String> arguments = Lists.newArrayList(); - private final List<String> argumentsForLogs = Lists.newArrayList(); + private final List<String> arguments = new ArrayList<>(); + private final List<String> argumentsForLogs = new ArrayList<>(); private final Map<String, String> env; private File directory; private boolean newShell = false; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/dag/DirectAcyclicGraph.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/dag/DirectAcyclicGraph.java index 113991951a4..6cd3b683af9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/dag/DirectAcyclicGraph.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/dag/DirectAcyclicGraph.java @@ -63,7 +63,7 @@ public class DirectAcyclicGraph { public List sort() { sortNodes(); - List<Object> result = Lists.newArrayList(); + List<Object> result = new ArrayList<>(); for (Node node : nodes) { result.add(node.getObject()); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java index 784ed579ef7..0325360050c 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/Filter.java @@ -19,12 +19,11 @@ */ package org.sonar.api.web; -import com.google.common.collect.Lists; +import com.google.common.base.Preconditions; +import java.util.ArrayList; import java.util.List; -import com.google.common.base.Preconditions; - /** * Definition of a filter. * @@ -44,8 +43,8 @@ public final class Filter { private Filter() { displayAs = LIST; - criteria = Lists.newArrayList(); - columns = Lists.newArrayList(); + criteria = new ArrayList<>(); + columns = new ArrayList<>(); } /** diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java index b32de7f475d..71a39897940 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/DateUtilsTest.java @@ -19,12 +19,12 @@ */ package org.sonar.api.utils; -import com.google.common.collect.Lists; import org.assertj.core.api.Assertions; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -128,7 +128,7 @@ public class DateUtilsTest { public void shouldBeThreadSafe() throws Exception { final DateUtils.ThreadSafeDateFormat format = new DateUtils.ThreadSafeDateFormat("yyyy-MM-dd'T'HH:mm:ss,S z"); final Date now = new Date(); - final List<Throwable> throwables = Lists.newArrayList(); + final List<Throwable> throwables = new ArrayList<>(); final ThreadGroup tg = new ThreadGroup("shouldBeThreadSafe") { @Override |