Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Mierzwa <dev.maciej.mierzwa@gmail.com>
  • Loading branch information
MaciejMierzwa committed Dec 22, 2023
1 parent 9b9d215 commit 06f375f
Showing 1 changed file with 11 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public HTTPMetadataResolver2(final HttpClient client, final String metadataURL)
}

public HTTPMetadataResolver2(final Timer backgroundTaskTimer, final HttpClient client, final String metadataURL)
throws ResolverException {
throws ResolverException {
super(backgroundTaskTimer);

if (client == null) {
Expand Down Expand Up @@ -85,43 +85,33 @@ protected String getMetadataIdentifier() {
protected byte[] fetchMetadata() throws ResolverException {
final HttpGet httpGet = buildHttpGet();
final HttpClientContext context = HttpClientContext.create();
ClassicHttpResponse response = null;

try {
log.debug("{} Attempting to fetch metadata document from '{}'", getLogPrefix(), metadataURI);
response = httpClient.execute(httpGet, context, response1 -> {
if (response1.getCode() == HttpStatus.SC_NOT_MODIFIED) {
return httpClient.execute(httpGet, context, response1 -> {
final int httpStatusCode = response1.getCode();
if (httpStatusCode == HttpStatus.SC_NOT_MODIFIED) {
log.debug("{} Metadata document from '{}' has not changed since last retrieval", getLogPrefix(), getMetadataURI());
return null;
}
final int httpStatusCode = response1.getCode();
if (httpStatusCode != HttpStatus.SC_OK) {
final String errMsg = "Non-ok status code " + httpStatusCode + " returned from remote metadata source " + metadataURI;
log.error("{} " + errMsg, getLogPrefix());
throw new HttpException(errMsg);
}

processConditionalRetrievalHeaders(response1);

return response1;
try {
return getMetadataBytesFromResponse(response1);
} catch (ResolverException e) {
final String errMsg = "Error retrieving metadata from " + metadataURI;
throw new HttpException(errMsg, e);
}
});

final byte[] rawMetadata = getMetadataBytesFromResponse(response);
log.debug("{} Successfully fetched {} bytes of metadata from {}", getLogPrefix(), rawMetadata.length, getMetadataURI());

return rawMetadata;
} catch (final IOException e) {
final String errMsg = "Error retrieving metadata from " + metadataURI;
log.error("{} {}: {}", getLogPrefix(), errMsg, e.getMessage());
throw new ResolverException(errMsg, e);
} finally {
try {
if (response != null) {
response.close();
}
} catch (final IOException e) {
log.error("{} Error closing HTTP response from {}", metadataURI, getLogPrefix(), e);
}
}
}

Expand Down Expand Up @@ -150,7 +140,7 @@ protected void processConditionalRetrievalHeaders(final ClassicHttpResponse resp
}
}

protected byte[] getMetadataBytesFromResponse(final ClassicHttpResponse response) throws ResolverException, IOException {
protected byte[] getMetadataBytesFromResponse(final ClassicHttpResponse response) throws ResolverException {
log.debug("{} Attempting to extract metadata from response to request for metadata from '{}'", getLogPrefix(), getMetadataURI());
try {
final InputStream ins = response.getEntity().getContent();
Expand Down

0 comments on commit 06f375f

Please sign in to comment.