diff options
author | SimonBrandhof <simon.brandhof@gmail.com> | 2013-04-08 14:57:13 +0200 |
---|---|---|
committer | SimonBrandhof <simon.brandhof@gmail.com> | 2013-04-08 14:57:13 +0200 |
commit | 1ed751ab62fc2cb0cac57a409ebef0e0425b6525 (patch) | |
tree | d418672ac0a1f2815d2a2b962f7f63c00dc5f11f /sonar-runner-impl/src/main/java/org/sonar/runner/impl | |
parent | 3df1496b29c383b09ff4a2882620f6b922a7196c (diff) | |
download | sonar-scanner-cli-1ed751ab62fc2cb0cac57a409ebef0e0425b6525.tar.gz sonar-scanner-cli-1ed751ab62fc2cb0cac57a409ebef0e0425b6525.zip |
SONARPLUGINS-2709 Send HTTP credentials
Diffstat (limited to 'sonar-runner-impl/src/main/java/org/sonar/runner/impl')
-rw-r--r-- | sonar-runner-impl/src/main/java/org/sonar/runner/impl/ServerConnection.java | 71 |
1 files changed, 27 insertions, 44 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 index 3985bff..3ea0f1b 100644 --- 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 @@ -19,17 +19,12 @@ */ package org.sonar.runner.impl; +import com.github.kevinsawicki.http.HttpRequest; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; import java.net.ConnectException; -import java.net.HttpURLConnection; import java.net.URL; import java.net.UnknownHostException; import java.util.Properties; @@ -58,70 +53,58 @@ class ServerConnection { } void download(String path, File toFile) { - InputStream input = null; - FileOutputStream output = null; String fullUrl = serverUrl + path; try { Logs.debug("Download " + fullUrl + " to " + toFile.getAbsolutePath()); - HttpURLConnection connection = newHttpConnection(new URL(fullUrl)); - int statusCode = connection.getResponseCode(); - if (statusCode != HttpURLConnection.HTTP_OK) { - throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); + HttpRequest httpRequest = newHttpRequest(new URL(fullUrl)); + if (!httpRequest.ok()) { + throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code()); } - output = new FileOutputStream(toFile, false); - input = connection.getInputStream(); - IOUtils.copyLarge(input, output); + httpRequest.receive(toFile); } catch (Exception e) { - if (e instanceof ConnectException || e instanceof UnknownHostException) { + if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) { Logs.error("Sonar server '" + serverUrl + "' can not be reached"); } - IOUtils.closeQuietly(output); FileUtils.deleteQuietly(toFile); throw new IllegalStateException("Fail to download: " + fullUrl, e); - } finally { - IOUtils.closeQuietly(input); - IOUtils.closeQuietly(output); } } String downloadString(String path) throws IOException { String fullUrl = serverUrl + path; - HttpURLConnection conn = newHttpConnection(new URL(fullUrl)); - String charset = getCharsetFromContentType(conn.getContentType()); - if (charset == null || "".equals(charset)) { - charset = "UTF-8"; - } - Reader reader = null; + HttpRequest httpRequest = newHttpRequest(new URL(fullUrl)); try { - int statusCode = conn.getResponseCode(); - if (statusCode != HttpURLConnection.HTTP_OK) { - throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + statusCode); + String charset = getCharsetFromContentType(httpRequest.contentType()); + if (charset == null || "".equals(charset)) { + charset = "UTF-8"; + } + if (!httpRequest.ok()) { + throw new IOException("Status returned by url : '" + fullUrl + "' is invalid : " + httpRequest.code()); } - reader = new InputStreamReader(conn.getInputStream(), charset); - return IOUtils.toString(reader); - } catch (IOException e) { - if (e instanceof ConnectException || e instanceof UnknownHostException) { + return httpRequest.body(charset); + + } catch (HttpRequest.HttpRequestException e) { + if (e.getCause() instanceof ConnectException || e.getCause() instanceof UnknownHostException) { Logs.error("Sonar server '" + serverUrl + "' can not be reached"); } throw e; } finally { - IOUtils.closeQuietly(reader); - conn.disconnect(); + httpRequest.disconnect(); } } - private HttpURLConnection newHttpConnection(URL url) throws IOException { - //TODO send credentials - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setConnectTimeout(CONNECT_TIMEOUT_MILLISECONDS); - connection.setReadTimeout(READ_TIMEOUT_MILLISECONDS); - connection.setInstanceFollowRedirects(true); - connection.setRequestMethod("GET"); - connection.setRequestProperty("User-Agent", userAgent); - return connection; + 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); + + // TODO send credentials + return request; } /** |