|
7 | 7 | import java.nio.charset.StandardCharsets; |
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.Base64; |
| 10 | +import java.util.HashMap; |
10 | 11 | import java.util.List; |
| 12 | +import java.util.Map; |
11 | 13 | import org.apache.commons.lang.StringUtils; |
12 | 14 | import org.apache.hc.client5.http.auth.AuthScope; |
13 | 15 | import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; |
|
20 | 22 | import org.apache.hc.client5.http.protocol.HttpClientContext; |
21 | 23 | import org.apache.hc.core5.http.HttpHeaders; |
22 | 24 | import org.apache.hc.core5.http.NameValuePair; |
23 | | -import org.apache.hc.core5.http.ParseException; |
24 | 25 | import org.apache.hc.core5.http.io.entity.EntityUtils; |
25 | 26 | import org.apache.hc.core5.http.message.BasicNameValuePair; |
26 | 27 | import org.quality.gates.jenkins.plugin.JobConfigData; |
@@ -66,7 +67,9 @@ public void setLogged(boolean logged) { |
66 | 67 | private void loginApi(SonarInstance sonarInstance) { |
67 | 68 | httpClientContext = HttpClientContext.create(); |
68 | 69 |
|
69 | | - if (StringUtils.isNotEmpty(sonarInstance.getToken().getPlainText())) { |
| 70 | + // For some reason the Token Secret can be null even if empty in the Global Configuration UI |
| 71 | + if (sonarInstance.getToken() != null |
| 72 | + && StringUtils.isNotEmpty(sonarInstance.getToken().getPlainText())) { |
70 | 73 | token = sonarInstance.getToken().getPlainText(); |
71 | 74 | httpClient = HttpClientBuilder.create().build(); |
72 | 75 | } else { |
@@ -121,19 +124,23 @@ private String executeGetRequest(CloseableHttpClient client, HttpGet request) th |
121 | 124 | request.addHeader(HttpHeaders.AUTHORIZATION, authHeader); |
122 | 125 | } |
123 | 126 |
|
124 | | - try (var response = client.execute(request, httpClientContext, classicHttpResponse -> classicHttpResponse)) { |
125 | | - var statusCode = response.getCode(); |
126 | | - var entity = response.getEntity(); |
127 | | - var returnResponse = EntityUtils.toString(entity); |
| 127 | + try { |
| 128 | + var responseMap = client.execute(request, httpClientContext, classicHttpResponse -> { |
| 129 | + Map<String, Object> retVal = new HashMap<>(); |
| 130 | + retVal.put("statusCode", classicHttpResponse.getCode()); |
| 131 | + retVal.put("content", EntityUtils.toString(classicHttpResponse.getEntity())); |
| 132 | + return retVal; |
| 133 | + }); |
128 | 134 |
|
129 | | - EntityUtils.consume(entity); |
| 135 | + var statusCode = (Integer) responseMap.get("statusCode"); |
| 136 | + var returnResponse = responseMap.get("content").toString(); |
130 | 137 |
|
131 | 138 | if (statusCode != 200) { |
132 | 139 | throw new QGException("Expected status 200, got: " + statusCode + ". Response: " + returnResponse); |
133 | 140 | } |
134 | 141 |
|
135 | 142 | return returnResponse; |
136 | | - } catch (IOException | ParseException e) { |
| 143 | + } catch (IOException e) { |
137 | 144 | throw new QGException("GET execution error", e); |
138 | 145 | } |
139 | 146 | } |
|
0 commit comments