From e9f1fbb9a1adbd3313fd9ffc409688c246f5434c Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 26 May 2016 18:00:47 +0200 Subject: [PATCH] SONARJNKNS-251 Use sonar-ws to perform HTTP calls --- pom.xml | 7 +++- .../plugins/sonar/client/HttpClient.java | 37 +++++++------------ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index a6772d2c..7c97ec5d 100644 --- a/pom.xml +++ b/pom.xml @@ -106,7 +106,7 @@ jenkins-sonar-plugin 2.4.1 1.580.3 - 6 + 7 false @@ -124,6 +124,11 @@ + + org.sonarsource.sonarqube + sonar-ws + 5.6-RC1 + org.jenkins-ci.main diff --git a/src/main/java/hudson/plugins/sonar/client/HttpClient.java b/src/main/java/hudson/plugins/sonar/client/HttpClient.java index af0b6f3f..2ca5aee6 100644 --- a/src/main/java/hudson/plugins/sonar/client/HttpClient.java +++ b/src/main/java/hudson/plugins/sonar/client/HttpClient.java @@ -18,33 +18,22 @@ */ package hudson.plugins.sonar.client; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; - -import com.google.common.base.Charsets; - -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.StandardCharsets; +import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.HttpConnector; +import org.sonarqube.ws.client.WsResponse; public class HttpClient { - public String getHttp(String urlToRead, String username, String password) throws Exception { - URL url = new URL(urlToRead); - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - - if (!StringUtils.isEmpty(username)) { - // to support authentication tokens - String userpass = username + ":"; - if (!StringUtils.isEmpty(password)) { - userpass = userpass + password; - } - String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes(Charsets.UTF_8)); - conn.setRequestProperty("Authorization", basicAuth); - } + public String getHttp(String url, String usernameOrToken, String password) throws Exception { + String baseUrl = StringUtils.substringBeforeLast(url, "/"); + String path = StringUtils.substringAfterLast(url, "/"); + HttpConnector httpConnector = HttpConnector.newBuilder() + .userAgent("Scanner for Jenkins") + .url(baseUrl) + .credentials(usernameOrToken, password) + .build(); + WsResponse response = httpConnector.call(new GetRequest(path)); + return response.content(); - conn.setRequestMethod("GET"); - InputStream is = conn.getInputStream(); - return IOUtils.toString(is, Charsets.UTF_8.name()); } }