Skip to content

Commit 7c17743

Browse files
committed
BI 473 - Move the getItemLastModified API to ArtifactoryBuildInfoClient
1 parent 7526468 commit 7c17743

File tree

2 files changed

+20
-47
lines changed

2 files changed

+20
-47
lines changed

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class ArtifactoryHttpClient {
5454
new ArtifactoryVersion("5.2.1");
5555
public static final ArtifactoryVersion MINIMAL_ARTIFACTORY_VERSION = new ArtifactoryVersion("2.2.3");
5656
public static final String VERSION_INFO_URL = "/api/system/version";
57-
public static final String ITEM_LAST_MODIFIED = "/api/storage/";
5857
private static final int DEFAULT_CONNECTION_TIMEOUT_SECS = 300; // 5 Minutes in seconds
5958
public static final int DEFAULT_CONNECTION_RETRY = 3;
6059
private final Log log;
@@ -174,26 +173,7 @@ public ArtifactoryVersion getVersion() throws IOException {
174173
return ArtifactoryVersion.NOT_FOUND;
175174
}
176175

177-
public ItemLastModified getItemLastModified(String path) throws IOException {
178-
String lastModifiedUrl = artifactoryUrl + ITEM_LAST_MODIFIED + path + "?lastModified";
179-
HttpResponse response = executeGetRequest(lastModifiedUrl);
180-
int statusCode = response.getStatusLine().getStatusCode();
181-
if (statusCode != HttpStatus.SC_OK) {
182-
throw new IOException("The path " + path + " returned " + response.getStatusLine().getStatusCode() + ":" + getMessageFromEntity(response.getEntity()));
183-
}
184-
HttpEntity httpEntity = response.getEntity();
185-
if (httpEntity != null) {
186-
try (InputStream content = httpEntity.getContent()) {
187-
JsonNode result = getJsonNode(httpEntity, content);
188-
String version = result.get("lastModified").asText();
189-
String uri = result.get("uri").asText();
190-
return new ItemLastModified(uri, version);
191-
}
192-
}
193-
throw new IOException("The path " + path + " returned empty entity");
194-
}
195-
196-
private JsonNode getJsonNode(HttpEntity httpEntity, InputStream content) throws IOException {
176+
public JsonNode getJsonNode(HttpEntity httpEntity, InputStream content) throws IOException {
197177
JsonParser parser = createJsonParser(content);
198178
EntityUtils.consume(httpEntity);
199179
return parser.readValueAsTree();

build-info-extractor/src/main/java/org/jfrog/build/extractor/clientConfiguration/client/ArtifactoryBuildInfoClient.java

+19-26
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
import org.jfrog.build.api.release.Promotion;
4545
import org.jfrog.build.api.util.FileChecksumCalculator;
4646
import org.jfrog.build.api.util.Log;
47-
import org.jfrog.build.client.ArtifactoryHttpClient;
48-
import org.jfrog.build.client.ArtifactoryUploadResponse;
49-
import org.jfrog.build.client.ArtifactoryVersion;
50-
import org.jfrog.build.client.PreemptiveHttpClient;
47+
import org.jfrog.build.client.*;
5148
import org.jfrog.build.client.bintrayResponse.BintrayResponse;
5249
import org.jfrog.build.client.bintrayResponse.BintrayResponseFactory;
5350
import org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails;
@@ -83,6 +80,7 @@ public class ArtifactoryBuildInfoClient extends ArtifactoryBaseClient implements
8380
public static final String BUILD_BROWSE_URL = "/webapp/builds";
8481
public static final String APPLICATION_VND_ORG_JFROG_ARTIFACTORY_JSON = "application/vnd.org.jfrog.artifactory+json";
8582
public static final String APPLICATION_JSON = "application/json";
83+
public static final String ITEM_LAST_MODIFIED = "/api/storage/";
8684

8785
/**
8886
* Creates a new client for the given Artifactory url.
@@ -307,33 +305,28 @@ private HttpResponse sendHttpRequest(HttpUriRequest request, int ... httpStatuse
307305
}
308306
}
309307

310-
public String getItemLastModified(String path) throws IOException, ParseException {
311-
String url = artifactoryUrl + "/api/storage/" + path + "?lastModified&deep=1";
308+
public ItemLastModified getItemLastModified(String path) throws IOException {
309+
String url = artifactoryUrl + ITEM_LAST_MODIFIED + path + "?lastModified";
312310
HttpGet get = new HttpGet(url);
313311
HttpResponse response = httpClient.getHttpClient().execute(get);
314-
315-
StatusLine statusLine = response.getStatusLine();
316-
if (statusLine.getStatusCode() != HttpStatus.SC_OK) {
317-
HttpEntity entity = response.getEntity();
318-
throw new IOException("Failed to obtain item info. Status code: " + statusLine.getStatusCode() + httpClient.getMessageFromEntity(entity));
319-
} else {
320-
HttpEntity entity = response.getEntity();
321-
if (entity != null) {
322-
InputStream content = entity.getContent();
323-
JsonParser parser;
324-
try {
325-
parser = httpClient.createJsonParser(content);
326-
JsonNode result = parser.readValueAsTree();
327-
return result.get("lastModified").asText();
328-
} finally {
329-
if (content != null) {
330-
content.close();
331-
}
332-
EntityUtils.consume(entity);
312+
int statusCode = response.getStatusLine().getStatusCode();
313+
if (statusCode != HttpStatus.SC_OK) {
314+
throw new IOException("While requesting item info for path " + path + " received " + response.getStatusLine().getStatusCode() + ":" + httpClient.getMessageFromEntity(response.getEntity()));
315+
}
316+
HttpEntity httpEntity = response.getEntity();
317+
if (httpEntity != null) {
318+
try (InputStream content = httpEntity.getContent()) {
319+
JsonNode result = httpClient.getJsonNode(httpEntity, content);
320+
JsonNode lastModified = result.get("lastModified");
321+
JsonNode uri = result.get("uri");
322+
if (lastModified == null || uri == null) {
323+
throw new IOException("Unexpected JSON response when requesting info for path " + path + httpClient.getMessageFromEntity(response.getEntity()));
333324
}
325+
326+
return new ItemLastModified(uri.asText(), lastModified.asText());
334327
}
335328
}
336-
return null;
329+
throw new IOException("The path " + path + " returned empty entity");
337330
}
338331

339332
/**

0 commit comments

Comments
 (0)