Add a storage client method for checking wheter a user has a stored manifest.

This commit is contained in:
Jon Chambers
2021-03-11 15:10:40 -05:00
committed by Jon Chambers
parent 1e5d7582da
commit 8b6012f8a8
2 changed files with 70 additions and 0 deletions

View File

@@ -30,14 +30,19 @@ public class SecureStorageClient {
private final ExternalServiceCredentialGenerator storageServiceCredentialGenerator;
private final URI deleteUri;
private final URI getManifestUri;
private final FaultTolerantHttpClient httpClient;
@VisibleForTesting
static final String DELETE_PATH = "/v1/storage";
@VisibleForTesting
static final String GET_MANIFEST_PATH = "/v1/storage/manifest";
public SecureStorageClient(final ExternalServiceCredentialGenerator storageServiceCredentialGenerator, final Executor executor, final SecureStorageServiceConfiguration configuration) throws CertificateException {
this.storageServiceCredentialGenerator = storageServiceCredentialGenerator;
this.deleteUri = URI.create(configuration.getUri()).resolve(DELETE_PATH);
this.getManifestUri = URI.create(configuration.getUri()).resolve(GET_MANIFEST_PATH);
this.httpClient = FaultTolerantHttpClient.newBuilder()
.withCircuitBreaker(configuration.getCircuitBreakerConfiguration())
.withRetry(configuration.getRetryConfiguration())
@@ -68,4 +73,24 @@ public class SecureStorageClient {
throw new RuntimeException("Failed to delete storage service data: " + response.statusCode());
});
}
public CompletableFuture<Boolean> hasStoredData(final UUID accountUuid) {
final ExternalServiceCredentials credentials = storageServiceCredentialGenerator.generateFor(accountUuid.toString());
final HttpRequest request = HttpRequest.newBuilder()
.uri(getManifestUri)
.GET()
.header("Authorization", "Basic " + Base64.encodeBytes((credentials.getUsername() + ":" + credentials.getPassword()).getBytes(StandardCharsets.UTF_8)))
.build();
return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(response -> {
if (response.statusCode() >= 200 && response.statusCode() < 300) {
return true;
} else if (response.statusCode() == 404) {
return false;
}
throw new RuntimeException("Failed to check for presence of manifest: " + response.statusCode());
});
}
}