Skip to content

Commit e9f1fbb

Browse files
committed
SONARJNKNS-251 Use sonar-ws to perform HTTP calls
1 parent 04ac3f2 commit e9f1fbb

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

pom.xml

+6-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<gitRepositoryName>jenkins-sonar-plugin</gitRepositoryName>
107107
<version.artifactory.plugin>2.4.1</version.artifactory.plugin>
108108
<jenkins.version>1.580.3</jenkins.version>
109-
<java.level>6</java.level>
109+
<java.level>7</java.level>
110110
<findbugs.failOnError>false</findbugs.failOnError>
111111
</properties>
112112

@@ -124,6 +124,11 @@
124124
</pluginRepositories>
125125

126126
<dependencies>
127+
<dependency>
128+
<groupId>org.sonarsource.sonarqube</groupId>
129+
<artifactId>sonar-ws</artifactId>
130+
<version>5.6-RC1</version>
131+
</dependency>
127132
<dependency>
128133
<!-- needed for SonarPublisher -->
129134
<groupId>org.jenkins-ci.main</groupId>

src/main/java/hudson/plugins/sonar/client/HttpClient.java

+13-24
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,22 @@
1818
*/
1919
package hudson.plugins.sonar.client;
2020

21-
import org.apache.commons.io.IOUtils;
2221
import org.apache.commons.lang.StringUtils;
23-
24-
import com.google.common.base.Charsets;
25-
26-
import java.io.InputStream;
27-
import java.net.HttpURLConnection;
28-
import java.net.URL;
29-
import java.nio.charset.StandardCharsets;
22+
import org.sonarqube.ws.client.GetRequest;
23+
import org.sonarqube.ws.client.HttpConnector;
24+
import org.sonarqube.ws.client.WsResponse;
3025

3126
public class HttpClient {
32-
public String getHttp(String urlToRead, String username, String password) throws Exception {
33-
URL url = new URL(urlToRead);
34-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
35-
36-
if (!StringUtils.isEmpty(username)) {
37-
// to support authentication tokens
38-
String userpass = username + ":";
39-
if (!StringUtils.isEmpty(password)) {
40-
userpass = userpass + password;
41-
}
42-
String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes(Charsets.UTF_8));
43-
conn.setRequestProperty("Authorization", basicAuth);
44-
}
27+
public String getHttp(String url, String usernameOrToken, String password) throws Exception {
28+
String baseUrl = StringUtils.substringBeforeLast(url, "/");
29+
String path = StringUtils.substringAfterLast(url, "/");
30+
HttpConnector httpConnector = HttpConnector.newBuilder()
31+
.userAgent("Scanner for Jenkins")
32+
.url(baseUrl)
33+
.credentials(usernameOrToken, password)
34+
.build();
35+
WsResponse response = httpConnector.call(new GetRequest(path));
36+
return response.content();
4537

46-
conn.setRequestMethod("GET");
47-
InputStream is = conn.getInputStream();
48-
return IOUtils.toString(is, Charsets.UTF_8.name());
4938
}
5039
}

0 commit comments

Comments
 (0)