|
18 | 18 | */
|
19 | 19 | package hudson.plugins.sonar.client;
|
20 | 20 |
|
21 |
| -import org.apache.commons.io.IOUtils; |
22 | 21 | 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; |
30 | 25 |
|
31 | 26 | 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(); |
45 | 37 |
|
46 |
| - conn.setRequestMethod("GET"); |
47 |
| - InputStream is = conn.getInputStream(); |
48 |
| - return IOUtils.toString(is, Charsets.UTF_8.name()); |
49 | 38 | }
|
50 | 39 | }
|
0 commit comments