All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bazaarvoice.emodb.blob.client.BlobStoreAuthenticatorProxy Maven / Gradle / Ivy

There is a newer version: 6.5.190
Show newest version
package com.bazaarvoice.emodb.blob.client;

import com.bazaarvoice.emodb.auth.InvalidCredentialException;
import com.bazaarvoice.emodb.auth.util.CredentialEncrypter;
import com.bazaarvoice.emodb.blob.api.AuthBlobStore;
import com.bazaarvoice.emodb.blob.api.Blob;
import com.bazaarvoice.emodb.blob.api.BlobMetadata;
import com.bazaarvoice.emodb.blob.api.BlobNotFoundException;
import com.bazaarvoice.emodb.blob.api.BlobStore;
import com.bazaarvoice.emodb.blob.api.RangeNotSatisfiableException;
import com.bazaarvoice.emodb.blob.api.RangeSpecification;
import com.bazaarvoice.emodb.blob.api.Table;
import com.bazaarvoice.emodb.sor.api.Audit;
import com.bazaarvoice.emodb.sor.api.TableExistsException;
import com.bazaarvoice.emodb.sor.api.TableOptions;
import com.bazaarvoice.emodb.sor.api.UnknownTableException;

import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

/**
 * BlobStore instance that takes an {@link AuthBlobStore} and API key and proxies all calls using the API key.
 */
class BlobStoreAuthenticatorProxy implements BlobStore {

    private final AuthBlobStore _authBlobStore;
    private final String _apiKey;

    BlobStoreAuthenticatorProxy(AuthBlobStore authBlobStore, String apiKey) {
        requireNonNull(authBlobStore, "AuthBlobstore is required");
        _authBlobStore = authBlobStore;
        _apiKey = validateApiKey(apiKey);
    }

    private static String validateApiKey(String apiKey) throws InvalidCredentialException {
        requireNonNull(apiKey, "API key is required");
        if (apiKey.isEmpty()) {
            throw new InvalidCredentialException("API key cannot be empty");
        }
        if (CredentialEncrypter.isPotentiallyEncryptedString(apiKey)) {
            throw new InvalidCredentialException("API Key is encrypted, please decrypt it");
        }
        return apiKey;
    }

    @Override
    public Iterator listTables(@Nullable String fromTableExclusive, long limit) {
        return _authBlobStore.listTables(_apiKey, fromTableExclusive, limit);
    }

    @Override
    public Iterator scanMetadata(String table, @Nullable String fromBlobIdExclusive, long limit) {
        return _authBlobStore.scanMetadata(_apiKey, table, fromBlobIdExclusive, limit);
    }

    @Override
    public TableOptions getTableOptions(String table) throws UnknownTableException {
        return _authBlobStore.getTableOptions(_apiKey, table);
    }

    @Override
    public Blob get(String table, String blobId) throws BlobNotFoundException {
        return _authBlobStore.get(_apiKey, table, blobId);
    }

    @Override
    public boolean getTableExists(String table) {
        return _authBlobStore.getTableExists(_apiKey, table);
    }

    @Override
    public Table getTableMetadata(String table) {
        return _authBlobStore.getTableMetadata(_apiKey, table);
    }

    @Override
    public BlobMetadata getMetadata(String table, String blobId)
            throws BlobNotFoundException {
        return _authBlobStore.getMetadata(_apiKey, table, blobId);
    }

    @Override
    public void dropTable(String table, Audit audit) throws UnknownTableException {
        _authBlobStore.dropTable(_apiKey, table, audit);
    }

    @Override
    public Collection getTablePlacements() {
        return _authBlobStore.getTablePlacements(_apiKey);
    }

    @Override
    public void put(String table, String blobId, Supplier in, Map attributes)
            throws IOException {
        _authBlobStore.put(_apiKey, table, blobId, in, attributes);
    }

    @Override
    public void delete(String table, String blobId) {
        _authBlobStore.delete(_apiKey, table, blobId);
    }

    @Override
    public long getTableApproximateSize(String table) throws UnknownTableException {
        return _authBlobStore.getTableApproximateSize(_apiKey, table);
    }

    @Override
    public void purgeTableUnsafe(String table, Audit audit) throws UnknownTableException {
        _authBlobStore.purgeTableUnsafe(_apiKey, table, audit);
    }

    @Override
    public Blob get(String table, String blobId, @Nullable RangeSpecification rangeSpec)
            throws BlobNotFoundException, RangeNotSatisfiableException {
        return _authBlobStore.get(_apiKey, table, blobId, rangeSpec);
    }

    @Override
    public boolean isTableAvailable(String table) {
        return _authBlobStore.isTableAvailable(_apiKey, table);
    }

    @Override
    public Map getTableAttributes(String table)
            throws UnknownTableException {
        return _authBlobStore.getTableAttributes(_apiKey, table);
    }

    @Override
    public void createTable(String table, TableOptions options, Map attributes, Audit audit)
            throws TableExistsException {
        _authBlobStore.createTable(_apiKey, table, options, attributes, audit);
    }

    @Override
    public void setTableAttributes(String table, Map attributes, Audit audit)
            throws UnknownTableException {
        _authBlobStore.setTableAttributes(_apiKey, table, attributes, audit);
    }

    AuthBlobStore getProxiedInstance() {
        return _authBlobStore;
    }
}