20
20
import com .fasterxml .jackson .annotation .JsonProperty ;
21
21
import com .fasterxml .jackson .core .type .TypeReference ;
22
22
import com .fasterxml .jackson .databind .ObjectMapper ;
23
- import org .apache .commons .httpclient .HttpClient ;
24
- import org .apache .commons .httpclient .auth .BasicScheme ;
25
- import org .apache .commons .httpclient .methods .GetMethod ;
26
- import org .apache .commons .httpclient .UsernamePasswordCredentials ;
27
- import org .apache .commons .httpclient .auth .AuthScope ;
23
+ import org .apache .hc .client5 .http .auth .AuthScope ;
24
+ import org .apache .hc .client5 .http .auth .UsernamePasswordCredentials ;
25
+ import org .apache .hc .client5 .http .classic .HttpClient ;
26
+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
27
+ import org .apache .hc .client5 .http .impl .auth .BasicCredentialsProvider ;
28
+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
29
+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
30
+ import org .apache .hc .core5 .http .HttpHost ;
31
+ import org .apache .hc .core5 .http .ParseException ;
32
+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
28
33
29
34
import java .io .IOException ;
30
35
import java .io .PrintStream ;
36
+ import java .net .URISyntaxException ;
31
37
import java .net .URLEncoder ;
32
38
import java .text .MessageFormat ;
33
39
import java .util .List ;
34
40
35
41
import static com .fasterxml .jackson .databind .DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES ;
36
- import static org .apache .commons . httpclient .HttpStatus .SC_BAD_REQUEST ;
42
+ import static org .apache .hc . core5 . http .HttpStatus .SC_BAD_REQUEST ;
37
43
38
44
@ SuppressWarnings ("WeakerAccess" )
39
45
public class SonarMasterCoverageRepository implements MasterCoverageRepository {
@@ -52,9 +58,17 @@ public SonarMasterCoverageRepository(String sonarUrl, String login, String passw
52
58
this .sonarUrl = sonarUrl ;
53
59
this .login = login ;
54
60
this .buildLog = buildLog ;
55
- httpClient = new HttpClient ();
56
- if (login != null ) {
57
- httpClient .getState ().setCredentials (AuthScope .ANY , new UsernamePasswordCredentials (login , password ));
61
+ if (this .login != null ) {
62
+ try {
63
+ BasicCredentialsProvider provider = new BasicCredentialsProvider ();
64
+ AuthScope scope = new AuthScope (HttpHost .create (sonarUrl ));
65
+ provider .setCredentials (scope , new UsernamePasswordCredentials (login , password .toCharArray ()));
66
+ this .httpClient = HttpClients .custom ().setDefaultCredentialsProvider (provider ).build ();
67
+ } catch (URISyntaxException e ) {
68
+ throw new RuntimeException (e );
69
+ }
70
+ } else {
71
+ this .httpClient = HttpClients .createDefault ();
58
72
}
59
73
}
60
74
@@ -80,10 +94,9 @@ public float get(final String gitHubRepoUrl) {
80
94
* @throws SonarProjectRetrievalException if no project could be found or an error occurred during retrieval
81
95
*/
82
96
private SonarProject getSonarProject (final String repoName ) throws SonarProjectRetrievalException {
83
- try {
84
- final String searchUri = sonarUrl + SONAR_SEARCH_PROJECTS_API_PATH + "?search=" + repoName ;
85
- final GetMethod method = executeGetRequest (searchUri );
86
- final List <SonarProject > sonarProjects = objectMapper .readValue (method .getResponseBodyAsStream (), new TypeReference <List <SonarProject >>() {
97
+ final String searchUri = sonarUrl + SONAR_SEARCH_PROJECTS_API_PATH + "?search=" + repoName ;
98
+ try (final CloseableHttpResponse response = executeGetRequest (searchUri )) {
99
+ final List <SonarProject > sonarProjects = objectMapper .readValue (EntityUtils .toString (response .getEntity ()), new TypeReference <List <SonarProject >>() {
87
100
});
88
101
89
102
if (sonarProjects .isEmpty ()) {
@@ -108,25 +121,23 @@ private SonarProject getSonarProject(final String repoName) throws SonarProjectR
108
121
*/
109
122
private float getCoverageMeasure (SonarProject project ) throws SonarCoverageMeasureRetrievalException {
110
123
final String uri = MessageFormat .format ("{0}{1}?componentKey={2}&metricKeys={3}" , sonarUrl , SONAR_COMPONENT_MEASURE_API_PATH , URLEncoder .encode (project .getKey ()), SONAR_OVERALL_LINE_COVERAGE_METRIC_NAME );
111
- try {
112
- final GetMethod method = executeGetRequest (uri );
113
- String value = JsonUtils .findInJson (method .getResponseBodyAsString (), "component.measures[0].value" );
124
+ try (final CloseableHttpResponse response = executeGetRequest (uri )) {
125
+ String value = JsonUtils .findInJson (EntityUtils .toString (response .getEntity ()), "component.measures[0].value" );
114
126
return Float .parseFloat (value ) / 100 ;
115
127
} catch (Exception e ) {
116
128
throw new SonarCoverageMeasureRetrievalException (String .format ("failed to get coverage measure for sonar project %s - %s" , project .getKey (), e .getMessage ()), e );
117
129
}
118
130
}
119
131
120
- private GetMethod executeGetRequest (String uri ) throws IOException , HttpClientException {
121
- final GetMethod method = new GetMethod (uri );
122
- if (login != null ) {
123
- method .getHostAuthState ().setAuthScheme (new BasicScheme ());
124
- }
125
- int status = httpClient .executeMethod (method );
132
+ private CloseableHttpResponse executeGetRequest (String uri ) throws IOException , HttpClientException , ParseException {
133
+ final HttpGet method = new HttpGet (uri );
134
+
135
+ CloseableHttpResponse response = (CloseableHttpResponse ) httpClient .execute (method );
136
+ int status = response .getCode ();
126
137
if (status >= SC_BAD_REQUEST ) {
127
- throw new HttpClientException (uri , status , method . getResponseBodyAsString ( ));
138
+ throw new HttpClientException (uri , status , EntityUtils . toString ( response . getEntity () ));
128
139
}
129
- return method ;
140
+ return response ;
130
141
}
131
142
132
143
private void log (String format , Object ... arguments ) {
0 commit comments