2020import com .fasterxml .jackson .annotation .JsonProperty ;
2121import com .fasterxml .jackson .core .type .TypeReference ;
2222import 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 ;
2833
2934import java .io .IOException ;
3035import java .io .PrintStream ;
36+ import java .net .URISyntaxException ;
3137import java .net .URLEncoder ;
3238import java .text .MessageFormat ;
3339import java .util .List ;
3440
3541import 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 ;
3743
3844@ SuppressWarnings ("WeakerAccess" )
3945public class SonarMasterCoverageRepository implements MasterCoverageRepository {
@@ -52,9 +58,17 @@ public SonarMasterCoverageRepository(String sonarUrl, String login, String passw
5258 this .sonarUrl = sonarUrl ;
5359 this .login = login ;
5460 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 ();
5872 }
5973 }
6074
@@ -80,10 +94,9 @@ public float get(final String gitHubRepoUrl) {
8094 * @throws SonarProjectRetrievalException if no project could be found or an error occurred during retrieval
8195 */
8296 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 >>() {
87100 });
88101
89102 if (sonarProjects .isEmpty ()) {
@@ -108,25 +121,23 @@ private SonarProject getSonarProject(final String repoName) throws SonarProjectR
108121 */
109122 private float getCoverageMeasure (SonarProject project ) throws SonarCoverageMeasureRetrievalException {
110123 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" );
114126 return Float .parseFloat (value ) / 100 ;
115127 } catch (Exception e ) {
116128 throw new SonarCoverageMeasureRetrievalException (String .format ("failed to get coverage measure for sonar project %s - %s" , project .getKey (), e .getMessage ()), e );
117129 }
118130 }
119131
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 ();
126137 if (status >= SC_BAD_REQUEST ) {
127- throw new HttpClientException (uri , status , method . getResponseBodyAsString ( ));
138+ throw new HttpClientException (uri , status , EntityUtils . toString ( response . getEntity () ));
128139 }
129- return method ;
140+ return response ;
130141 }
131142
132143 private void log (String format , Object ... arguments ) {
0 commit comments