Skip to content

Commit 93af275

Browse files
authored
BI-550 - HTTP requests should return CloseableHttpResponse (#424)
1 parent a2e28a5 commit 93af275

File tree

18 files changed

+322
-641
lines changed

18 files changed

+322
-641
lines changed

build-info-client/src/main/java/org/jfrog/build/client/ArtifactoryHttpClient.java

+34-44
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
import org.apache.commons.io.IOUtils;
2727
import org.apache.commons.lang.StringUtils;
2828
import org.apache.http.HttpEntity;
29-
import org.apache.http.HttpResponse;
3029
import org.apache.http.HttpStatus;
31-
import org.apache.http.StatusLine;
30+
import org.apache.http.client.methods.CloseableHttpResponse;
3231
import org.apache.http.client.methods.HttpGet;
3332
import org.apache.http.client.methods.HttpPut;
3433
import org.apache.http.util.EntityUtils;
@@ -37,6 +36,7 @@
3736

3837
import java.io.IOException;
3938
import java.io.InputStream;
39+
import java.nio.charset.StandardCharsets;
4040

4141
import static org.apache.commons.codec.binary.StringUtils.getBytesUtf8;
4242
import static org.apache.commons.codec.binary.StringUtils.newStringUsAscii;
@@ -182,49 +182,43 @@ public PreemptiveHttpClient getHttpClient(int connectionTimeout) {
182182

183183
public ArtifactoryVersion getVersion() throws IOException {
184184
String versionUrl = artifactoryUrl + VERSION_INFO_URL;
185-
HttpResponse response = executeGetRequest(versionUrl);
186-
int statusCode = response.getStatusLine().getStatusCode();
187-
if (statusCode == HttpStatus.SC_NOT_FOUND) {
188-
consumeEntity(response);
189-
return ArtifactoryVersion.NOT_FOUND;
190-
}
191-
if (statusCode != HttpStatus.SC_OK) {
192-
throw new IOException(getMessageFromEntity(response.getEntity()));
193-
}
194-
HttpEntity httpEntity = response.getEntity();
195-
if (httpEntity != null) {
185+
try (CloseableHttpResponse response = executeGetRequest(versionUrl)) {
186+
HttpEntity httpEntity = response.getEntity();
187+
int statusCode = response.getStatusLine().getStatusCode();
188+
if (statusCode == HttpStatus.SC_NOT_FOUND) {
189+
EntityUtils.consumeQuietly(httpEntity);
190+
return ArtifactoryVersion.NOT_FOUND;
191+
}
192+
if (statusCode != HttpStatus.SC_OK) {
193+
String message = getMessageFromEntity(httpEntity);
194+
EntityUtils.consumeQuietly(httpEntity);
195+
throw new IOException(message);
196+
}
197+
if (httpEntity == null) {
198+
return ArtifactoryVersion.NOT_FOUND;
199+
}
196200
try (InputStream content = httpEntity.getContent()) {
197201
JsonNode result = getJsonNode(content);
198202
log.debug("Version result: " + result);
199203
String version = result.get("version").asText();
200204
JsonNode addonsNode = result.get("addons");
201205
boolean hasAddons = (addonsNode != null) && addonsNode.iterator().hasNext();
202206
return new ArtifactoryVersion(version, hasAddons);
203-
} finally {
204-
EntityUtils.consume(httpEntity);
205207
}
206208
}
207-
return ArtifactoryVersion.NOT_FOUND;
208209
}
209210

210211
public JsonNode getJsonNode(InputStream content) throws IOException {
211212
JsonParser parser = createJsonParser(content);
212213
return parser.readValueAsTree();
213214
}
214215

215-
private HttpResponse executeGetRequest(String lastModifiedUrl) throws IOException {
216+
private CloseableHttpResponse executeGetRequest(String lastModifiedUrl) throws IOException {
216217
PreemptiveHttpClient client = getHttpClient();
217218
HttpGet httpGet = new HttpGet(lastModifiedUrl);
218219
return client.execute(httpGet);
219220
}
220221

221-
private void consumeEntity(HttpResponse response) throws IOException {
222-
HttpEntity httpEntity = response.getEntity();
223-
if (httpEntity != null) {
224-
EntityUtils.consume(httpEntity);
225-
}
226-
}
227-
228222
public JsonParser createJsonParser(InputStream in) throws IOException {
229223
JsonFactory jsonFactory = createJsonFactory();
230224
return jsonFactory.createParser(in);
@@ -250,45 +244,41 @@ public ArtifactoryUploadResponse upload(HttpPut httpPut, HttpEntity fileEntity)
250244
}
251245

252246
public ArtifactoryUploadResponse execute(HttpPut httpPut) throws IOException {
253-
HttpResponse response = getHttpClient().execute(httpPut);
254-
255-
ArtifactoryUploadResponse artifactoryResponse = null;
256-
if (response.getEntity() != null && response.getEntity().getContent() != null) {
257-
InputStream in = response.getEntity().getContent();
258-
String content = IOUtils.toString(in, "UTF-8");
259-
if (StringUtils.isNotEmpty(content)) {
247+
ArtifactoryUploadResponse artifactoryResponse = new ArtifactoryUploadResponse();
248+
try (CloseableHttpResponse response = getHttpClient().execute(httpPut)) {
249+
artifactoryResponse.setStatusLine(response.getStatusLine());
250+
if (response.getEntity() == null || response.getEntity().getContent() == null) {
251+
return artifactoryResponse;
252+
}
253+
try (InputStream in = response.getEntity().getContent()) {
254+
String content = IOUtils.toString(in, StandardCharsets.UTF_8);
255+
if (StringUtils.isEmpty(content)) {
256+
return artifactoryResponse;
257+
}
260258
try {
261259
JsonParser parser = createJsonParser(content);
262260
artifactoryResponse = parser.readValueAs(ArtifactoryUploadResponse.class);
261+
artifactoryResponse.setStatusLine(response.getStatusLine());
263262
} catch (Exception e) {
264263
// Displays the response received from the client and the stacktrace in case an Exception caught.
265264
log.info("Response received: \n\n" + content + "\n\n");
266265
log.error("Failed while reading the response from: " + httpPut, e);
267-
} finally {
268-
in.close();
269266
}
270267
}
268+
return artifactoryResponse;
271269
}
272-
273-
if (artifactoryResponse == null) {
274-
artifactoryResponse = new ArtifactoryUploadResponse();
275-
}
276-
StatusLine statusLine = response.getStatusLine();
277-
artifactoryResponse.setStatusLine(statusLine);
278-
return artifactoryResponse;
279270
}
280271

281272
/**
282-
* @param entity the entity to retrive the message from.
273+
* @param entity the entity to retrieve the message from.
283274
* @return response entity content.
284-
* @throws IOException
275+
* @throws IOException if entity couldn't serialize
285276
*/
286277

287278
public String getMessageFromEntity(HttpEntity entity) throws IOException {
288279
String responseMessage = "";
289280
if (entity != null) {
290281
responseMessage = getResponseEntityContent(entity);
291-
EntityUtils.consume(entity);
292282
if (StringUtils.isNotBlank(responseMessage)) {
293283
responseMessage = " Response message: " + responseMessage;
294284
}
@@ -306,7 +296,7 @@ public String getMessageFromEntity(HttpEntity entity) throws IOException {
306296
private String getResponseEntityContent(HttpEntity responseEntity) throws IOException {
307297
InputStream in = responseEntity.getContent();
308298
if (in != null) {
309-
return IOUtils.toString(in, "UTF-8");
299+
return IOUtils.toString(in, StandardCharsets.UTF_8);
310300
}
311301
return "";
312302
}

build-info-client/src/main/java/org/jfrog/build/client/PreemptiveHttpClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public PreemptiveHttpClient(PoolingHttpClientConnectionManager connectionManager
7575
this.httpClient = clientBuilder.build();
7676
}
7777

78-
public HttpResponse execute(HttpUriRequest request) throws IOException {
78+
public CloseableHttpResponse execute(HttpUriRequest request) throws IOException {
7979
HttpClientContext clientContext = HttpClientContext.create();
8080
if (StringUtils.isNotEmpty(accessToken)) {
8181
clientContext.setUserToken(accessToken);

build-info-client/src/main/java/org/jfrog/build/client/bintrayResponse/BintrayFailure.java

-62
This file was deleted.

build-info-client/src/main/java/org/jfrog/build/client/bintrayResponse/BintrayResponse.java

-10
This file was deleted.

build-info-client/src/main/java/org/jfrog/build/client/bintrayResponse/BintrayResponseFactory.java

-47
This file was deleted.

build-info-client/src/main/java/org/jfrog/build/client/bintrayResponse/BintraySuccess.java

-32
This file was deleted.

build-info-client/src/main/java/org/jfrog/build/client/bintrayResponse/EmptyBintrayResponse.java

-27
This file was deleted.

build-info-extractor-docker/src/main/java/org/jfrog/build/extractor/docker/DockerUtils.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.commons.io.IOUtils;
99
import org.apache.commons.lang3.StringUtils;
1010
import org.apache.http.HttpEntity;
11-
import org.apache.http.HttpResponse;
11+
import org.apache.http.client.methods.CloseableHttpResponse;
1212
import org.apache.http.util.EntityUtils;
1313
import org.jfrog.build.extractor.clientConfiguration.client.ArtifactoryDependenciesClient;
1414

@@ -276,20 +276,21 @@ public static Boolean isImageVersioned(String imageTag) {
276276
* As a result, the marker layer will transform to a regular docker layer (required to collect build info such as sha1, etc.).
277277
*
278278
* @param repo - Repository from which to download the layer
279-
* @param imageName- Image name to download
280-
* @param imageDigests - image diegest to download
279+
* @param imageName - Image name to download
280+
* @param imageDigests - image digest to download
281281
* @param dependenciesClient - Dependencies client
282282
*/
283283
public static void downloadMarkerLayer(String repo, String imageName, String imageDigests, ArtifactoryDependenciesClient dependenciesClient) throws IOException {
284284
String url = dependenciesClient.getArtifactoryUrl() + "/api/docker/" + repo + "/v2/" + imageName + "/blobs/" + imageDigests;
285-
HttpResponse response = dependenciesClient.getArtifactMetadata(url);
286-
EntityUtils.consume(response.getEntity());
285+
try (CloseableHttpResponse response = dependenciesClient.getArtifactMetadata(url)) {
286+
EntityUtils.consume(response.getEntity());
287+
}
287288
}
288289

289290
/**
290291
* @param imagePath - path to an image in artifactory without proxy e.g. image/image-tag
291-
* @param repo - target repo to search
292-
* @param cmd - docker push cmd/ docker pull cmd
292+
* @param repo - target repo to search
293+
* @param cmd - docker push cmd/ docker pull cmd
293294
* @return All possible paths in Artifactory in order to find image manifest using proxy.
294295
*/
295296
public static List<String> getArtManifestPath(String imagePath, String repo, CommandType cmd) {

0 commit comments

Comments
 (0)