Skip to content

Commit

Permalink
SONARJNKNS-251 Use sonar-ws to perform HTTP calls
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed May 26, 2016
1 parent 04ac3f2 commit e9f1fbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<gitRepositoryName>jenkins-sonar-plugin</gitRepositoryName>
<version.artifactory.plugin>2.4.1</version.artifactory.plugin>
<jenkins.version>1.580.3</jenkins.version>
<java.level>6</java.level>
<java.level>7</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
</properties>

Expand All @@ -124,6 +124,11 @@
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-ws</artifactId>
<version>5.6-RC1</version>
</dependency>
<dependency>
<!-- needed for SonarPublisher -->
<groupId>org.jenkins-ci.main</groupId>
Expand Down
37 changes: 13 additions & 24 deletions src/main/java/hudson/plugins/sonar/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit e9f1fbb

Please sign in to comment.