Search in sources :

Example 1 with BlockBlobURL

use of com.microsoft.azure.storage.blob.BlockBlobURL in project geowebcache by GeoWebCache.

the class AzureClient method putProperties.

public void putProperties(String resourceKey, Properties properties) throws StorageException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        properties.store(out, "");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    try {
        BlockBlobURL blob = getBlockBlobURL(resourceKey);
        byte[] bytes = out.toByteArray();
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        BlobHTTPHeaders headers = new BlobHTTPHeaders();
        headers.withBlobContentType("text/plain");
        int status = blob.upload(Flowable.just(buffer), bytes.length, headers, null, null, null).blockingGet().statusCode();
        if (!HttpStatus.valueOf(status).is2xxSuccessful()) {
            throw new StorageException("Upload request failed with status " + status + " on resource " + resourceKey);
        }
    } catch (RestException e) {
        throw new StorageException("Failed to update e property file at " + resourceKey, e);
    }
}
Also used : BlockBlobURL(com.microsoft.azure.storage.blob.BlockBlobURL) BlobHTTPHeaders(com.microsoft.azure.storage.blob.models.BlobHTTPHeaders) RestException(com.microsoft.rest.v2.RestException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) StorageException(org.geowebcache.storage.StorageException)

Example 2 with BlockBlobURL

use of com.microsoft.azure.storage.blob.BlockBlobURL in project geowebcache by GeoWebCache.

the class AzureClient method getBytes.

@Nullable
public byte[] getBytes(String key) throws StorageException {
    BlockBlobURL blob = getBlockBlobURL(key);
    try {
        DownloadResponse response = blob.download().blockingGet();
        ByteBuffer buffer = FlowableUtil.collectBytesInBuffer(response.body(null)).blockingGet();
        byte[] result = new byte[buffer.remaining()];
        buffer.get(result);
        return result;
    } catch (RestException e) {
        if (e.response().statusCode() == 404) {
            return null;
        }
        throw new StorageException("Failed to retreive bytes for " + key, e);
    }
}
Also used : BlockBlobURL(com.microsoft.azure.storage.blob.BlockBlobURL) RestException(com.microsoft.rest.v2.RestException) DownloadResponse(com.microsoft.azure.storage.blob.DownloadResponse) ByteBuffer(java.nio.ByteBuffer) StorageException(org.geowebcache.storage.StorageException) Nullable(javax.annotation.Nullable)

Example 3 with BlockBlobURL

use of com.microsoft.azure.storage.blob.BlockBlobURL in project geowebcache by GeoWebCache.

the class AzureBlobStore method put.

@Override
public void put(TileObject obj) throws StorageException {
    final Resource blob = obj.getBlob();
    checkNotNull(blob);
    checkNotNull(obj.getBlobFormat());
    final String key = keyBuilder.forTile(obj);
    BlockBlobURL blobURL = client.getBlockBlobURL(key);
    // if there are listeners, gather first the old size with a "head" request
    Long oldSize = null;
    boolean existed = false;
    if (!listeners.isEmpty()) {
        try {
            BlobGetPropertiesResponse properties = blobURL.getProperties().blockingGet();
            oldSize = properties.headers().contentLength();
            existed = true;
        } catch (RestException e) {
            if (e.response().statusCode() != HttpStatus.NOT_FOUND.value()) {
                throw new StorageException("Failed to check if the container exists", e);
            }
        }
    }
    // then upload
    try (InputStream is = blob.getInputStream()) {
        byte[] bytes = IOUtils.toByteArray(is);
        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        String mimeType = MimeType.createFromFormat(obj.getBlobFormat()).getMimeType();
        BlobHTTPHeaders headers = new BlobHTTPHeaders().withBlobContentType(mimeType);
        int status = blobURL.upload(Flowable.just(buffer), bytes.length, headers, null, null, null).blockingGet().statusCode();
        if (!HttpStatus.valueOf(status).is2xxSuccessful()) {
            throw new StorageException("Failed to upload tile to Azure on container " + client.getContainerName() + " and key " + key + " got HTTP  status " + status);
        }
    } catch (RestException | IOException | MimeException e) {
        throw new StorageException("Failed to upload tile to Azure on container " + client.getContainerName() + " and key " + key, e);
    }
    // along with the metadata
    putParametersMetadata(obj.getLayerName(), obj.getParametersId(), obj.getParameters());
    // This is important because listeners may be tracking tile existence
    if (!listeners.isEmpty()) {
        if (existed) {
            listeners.sendTileUpdated(obj, oldSize);
        } else {
            listeners.sendTileStored(obj);
        }
    }
}
Also used : BlockBlobURL(com.microsoft.azure.storage.blob.BlockBlobURL) InputStream(java.io.InputStream) Resource(org.geowebcache.io.Resource) ByteArrayResource(org.geowebcache.io.ByteArrayResource) RestException(com.microsoft.rest.v2.RestException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) BlobHTTPHeaders(com.microsoft.azure.storage.blob.models.BlobHTTPHeaders) MimeException(org.geowebcache.mime.MimeException) BlobGetPropertiesResponse(com.microsoft.azure.storage.blob.models.BlobGetPropertiesResponse) StorageException(org.geowebcache.storage.StorageException)

Example 4 with BlockBlobURL

use of com.microsoft.azure.storage.blob.BlockBlobURL in project geowebcache by GeoWebCache.

the class AzureBlobStore method delete.

@Override
public boolean delete(TileObject obj) throws StorageException {
    final String key = keyBuilder.forTile(obj);
    BlockBlobURL blob = client.getBlockBlobURL(key);
    // don't bother for the extra call if there are no listeners
    if (listeners.isEmpty()) {
        try {
            int statusCode = blob.delete().blockingGet().statusCode();
            return HttpStatus.valueOf(statusCode).is2xxSuccessful();
        } catch (RestException e) {
            return false;
        }
    }
    try {
        // if there are listeners, gather extra information
        BlobGetPropertiesResponse properties = blob.getProperties().blockingGet();
        Long oldSize = properties.headers().contentLength();
        int statusCode = blob.delete().blockingGet().statusCode();
        if (!HttpStatus.valueOf(statusCode).is2xxSuccessful()) {
            return false;
        }
        if (oldSize != null) {
            obj.setBlobSize(oldSize.intValue());
        }
    } catch (RestException e) {
        if (e.response().statusCode() != 404) {
            throw new StorageException("Failed to delete tile ", e);
        }
        return false;
    }
    listeners.sendTileDeleted(obj);
    return true;
}
Also used : BlockBlobURL(com.microsoft.azure.storage.blob.BlockBlobURL) RestException(com.microsoft.rest.v2.RestException) BlobGetPropertiesResponse(com.microsoft.azure.storage.blob.models.BlobGetPropertiesResponse) StorageException(org.geowebcache.storage.StorageException)

Example 5 with BlockBlobURL

use of com.microsoft.azure.storage.blob.BlockBlobURL in project geowebcache by GeoWebCache.

the class AzureBlobStore method get.

@Override
public boolean get(TileObject obj) throws StorageException {
    final String key = keyBuilder.forTile(obj);
    final BlockBlobURL blob = client.getBlockBlobURL(key);
    try {
        DownloadResponse response = blob.download().blockingGet();
        ByteBuffer buffer = FlowableUtil.collectBytesInBuffer(response.body(null)).blockingGet();
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        obj.setBlobSize(bytes.length);
        obj.setBlob(new ByteArrayResource(bytes));
        obj.setCreated(response.headers().lastModified().toEpochSecond() * 1000l);
    } catch (RestException e) {
        if (e.response().statusCode() == 404) {
            return false;
        }
        throw new StorageException("Error getting " + key, e);
    }
    return true;
}
Also used : BlockBlobURL(com.microsoft.azure.storage.blob.BlockBlobURL) RestException(com.microsoft.rest.v2.RestException) DownloadResponse(com.microsoft.azure.storage.blob.DownloadResponse) ByteArrayResource(org.geowebcache.io.ByteArrayResource) ByteBuffer(java.nio.ByteBuffer) StorageException(org.geowebcache.storage.StorageException)

Aggregations

BlockBlobURL (com.microsoft.azure.storage.blob.BlockBlobURL)7 RestException (com.microsoft.rest.v2.RestException)6 StorageException (org.geowebcache.storage.StorageException)5 ByteBuffer (java.nio.ByteBuffer)4 DownloadResponse (com.microsoft.azure.storage.blob.DownloadResponse)2 BlobGetPropertiesResponse (com.microsoft.azure.storage.blob.models.BlobGetPropertiesResponse)2 BlobHTTPHeaders (com.microsoft.azure.storage.blob.models.BlobHTTPHeaders)2 IOException (java.io.IOException)2 ByteArrayResource (org.geowebcache.io.ByteArrayResource)2 BlobItem (com.microsoft.azure.storage.blob.models.BlobItem)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Nullable (javax.annotation.Nullable)1 Resource (org.geowebcache.io.Resource)1 MimeException (org.geowebcache.mime.MimeException)1