aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-06-25 15:53:51 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-06-25 18:01:35 +0200
commitd4feed19cf6c8c8adf7fcdb037436871c962175a (patch)
tree527965c695694bff91a8c5cc70ce78f344103c04 /sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
parent8d8bc9bf3d4964f71e9c0e0d719ad1416ba8c16a (diff)
downloadsonar-scanner-cli-d4feed19cf6c8c8adf7fcdb037436871c962175a.tar.gz
sonar-scanner-cli-d4feed19cf6c8c8adf7fcdb037436871c962175a.zip
SONARUNNER-141 Remove forked mode
Diffstat (limited to 'sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java')
-rw-r--r--sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java163
1 files changed, 0 insertions, 163 deletions
diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
deleted file mode 100644
index 915fba8..0000000
--- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * SonarQube Runner - Implementation
- * Copyright (C) 2011 SonarSource
- * dev@sonar.codehaus.org
- *
- * 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 02
- */
-package org.sonar.runner.impl;
-
-import org.sonar.home.cache.PersistentCache;
-import com.github.kevinsawicki.http.HttpRequest;
-import org.apache.commons.io.FileUtils;
-
-import java.io.File;
-import java.io.IOException;
-import java.net.ConnectException;
-import java.net.URL;
-import java.net.UnknownHostException;
-import java.text.MessageFormat;
-import java.util.Properties;
-import java.util.concurrent.Callable;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-class ServerConnection {
-
- private static final String SONAR_SERVER_CAN_NOT_BE_REACHED = "Sonar server ''{0}'' can not be reached";
- private static final String STATUS_RETURNED_BY_URL_IS_INVALID = "Status returned by url : ''{0}'' is invalid : {1}";
- static final int CONNECT_TIMEOUT_MILLISECONDS = 30000;
- static final int READ_TIMEOUT_MILLISECONDS = 60000;
- private static final Pattern CHARSET_PATTERN = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
-
- private final String serverUrl;
- private final String userAgent;
-
- private final PersistentCache wsCache;
- private final boolean isModePreview;
-
- private ServerConnection(String serverUrl, String app, String appVersion, boolean preview, PersistentCache cache) {
- this.serverUrl = removeEndSlash(serverUrl);
- this.userAgent = app + "/" + appVersion;
- this.wsCache = cache;
- this.isModePreview = preview;
- }
-
- private static String removeEndSlash(String url) {
- if (url == null) {
- return null;
- }
- return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
- }
-
- static ServerConnection create(Properties properties, PersistentCache cache) {
- String serverUrl = properties.getProperty("sonar.host.url");
- String app = properties.getProperty(InternalProperties.RUNNER_APP);
- String appVersion = properties.getProperty(InternalProperties.RUNNER_APP_VERSION);
- String analysisMode = properties.getProperty("sonar.analysis.mode");
- boolean preview = "preview".equalsIgnoreCase(analysisMode);
-
- return new ServerConnection(serverUrl, app, appVersion, preview, cache);
- }
-
- private class StringDownloader implements Callable<String> {
- private String url;
-
- StringDownloader(String url) {
- this.url = url;
- }
-
- @Override
- public String call() throws Exception {
- HttpRequest httpRequest = null;
- try {
- httpRequest = newHttpRequest(new URL(url));
- String charset = getCharsetFromContentType(httpRequest.contentType());
- if (charset == null || "".equals(charset)) {
- charset = "UTF-8";
- }
- if (!httpRequest.ok()) {
- throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, url, httpRequest.code()));
- }
- return httpRequest.body(charset);
- } finally {
- if (httpRequest != null) {
- httpRequest.disconnect();
- }
- }
- }
- }
-
- void download(String path, File toFile) {
- String fullUrl = serverUrl + path;
- try {
- Logs.debug("Download " + fullUrl + " to " + toFile.getAbsolutePath());
- HttpRequest httpRequest = newHttpRequest(new URL(fullUrl));
- if (!httpRequest.ok()) {
- throw new IOException(MessageFormat.format(STATUS_RETURNED_BY_URL_IS_INVALID, fullUrl, httpRequest.code()));
- }
- httpRequest.receive(toFile);
-
- } catch (Exception e) {
- if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
- Logs.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
- }
- FileUtils.deleteQuietly(toFile);
- throw new IllegalStateException("Fail to download: " + fullUrl, e);
- }
- }
-
- String downloadStringCache(String path) throws Exception {
- String fullUrl = serverUrl + path;
- try {
- if (isModePreview) {
- return wsCache.getString(serverUrl, new StringDownloader(fullUrl));
- } else {
- return new StringDownloader(fullUrl).call();
- }
- } catch (HttpRequest.HttpRequestException e) {
- if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) {
- Logs.error(MessageFormat.format(SONAR_SERVER_CAN_NOT_BE_REACHED, serverUrl));
- }
- throw e;
- }
- }
-
- private HttpRequest newHttpRequest(URL url) {
- HttpRequest request = HttpRequest.get(url);
- request.trustAllCerts().trustAllHosts();
- request.acceptGzipEncoding().uncompress(true);
- request.connectTimeout(CONNECT_TIMEOUT_MILLISECONDS).readTimeout(READ_TIMEOUT_MILLISECONDS);
- request.userAgent(userAgent);
- return request;
- }
-
- /**
- * Parse out a charset from a content type header.
- *
- * @param contentType e.g. "text/html; charset=EUC-JP"
- * @return "EUC-JP", or null if not found. Charset is trimmed and upper-cased.
- */
- static String getCharsetFromContentType(String contentType) {
- if (contentType == null) {
- return null;
- }
- Matcher m = CHARSET_PATTERN.matcher(contentType);
- if (m.find()) {
- return m.group(1).trim().toUpperCase();
- }
- return null;
- }
-}