Search in sources :

Example 1 with Resource

use of org.geowebcache.io.Resource in project geowebcache by GeoWebCache.

the class S3BlobStore 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);
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(blob.getSize());
    String blobFormat = obj.getBlobFormat();
    String mimeType;
    try {
        mimeType = MimeType.createFromFormat(blobFormat).getMimeType();
    } catch (MimeException me) {
        throw new RuntimeException(me);
    }
    objectMetadata.setContentType(mimeType);
    // don't bother for the extra call if there are no listeners
    final boolean existed;
    ObjectMetadata oldObj;
    if (listeners.isEmpty()) {
        existed = false;
        oldObj = null;
    } else {
        oldObj = s3Ops.getObjectMetadata(key);
        existed = oldObj != null;
    }
    final ByteArrayInputStream input = toByteArray(blob);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata).withCannedAcl(acl);
    log.trace(log.isTraceEnabled() ? ("Storing " + key) : "");
    s3Ops.putObject(putObjectRequest);
    putParametersMetadata(obj.getLayerName(), obj.getParametersId(), obj.getParameters());
    /*
         * This is important because listeners may be tracking tile existence
         */
    if (!listeners.isEmpty()) {
        if (existed) {
            long oldSize = oldObj.getContentLength();
            listeners.sendTileUpdated(obj, oldSize);
        } else {
            listeners.sendTileStored(obj);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Resource(org.geowebcache.io.Resource) ByteArrayResource(org.geowebcache.io.ByteArrayResource) MimeException(org.geowebcache.mime.MimeException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 2 with Resource

use of org.geowebcache.io.Resource in project geowebcache by GeoWebCache.

the class HazelcastCacheProviderTest method testTilePut.

@Test
public void testTilePut() throws Exception {
    // Clearing cache
    cache1.clear();
    // Ensure that both caches are cleared
    assertEquals(0, cache1.getStatistics().getActualSize());
    assertEquals(0, cache2.getStatistics().getActualSize());
    // Put a TileObject
    Resource bytes = new ByteArrayResource("1 2 3 4 5 6 test".getBytes());
    long[] xyz = { 1L, 2L, 3L };
    Map<String, String> parameters = new HashMap<>();
    parameters.put("a", "x");
    parameters.put("b", "ΓΈ");
    TileObject to = TileObject.createCompleteTileObject("test:123123 112", xyz, "EPSG:4326", "image/jpeg", parameters, bytes);
    mem1.put(to);
    // Try to get the same TileObject
    TileObject to2 = TileObject.createQueryTileObject("test:123123 112", xyz, "EPSG:4326", "image/jpeg", parameters);
    assertTrue(mem2.get(to2));
    // Checks on the format
    assertEquals(to.getBlobFormat(), to2.getBlobFormat());
    // Checks if the resources are equals
    try (InputStream is = to.getBlob().getInputStream();
        InputStream is2 = to2.getBlob().getInputStream()) {
        checkInputStreams(is, is2);
    }
    // Ensure Caches contain the result
    TileObject to3 = cache1.getTileObj(to);
    assertNotNull(to3);
    // Checks if the resources are equals
    try (InputStream is = to.getBlob().getInputStream();
        InputStream is3 = to3.getBlob().getInputStream()) {
        checkInputStreams(is, is3);
    }
    TileObject to4 = cache2.getTileObj(to);
    assertNotNull(to4);
    // Checks if the resources are equals
    try (InputStream is = to.getBlob().getInputStream();
        InputStream is4 = to4.getBlob().getInputStream()) {
        checkInputStreams(is, is4);
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) Resource(org.geowebcache.io.Resource) ByteArrayResource(org.geowebcache.io.ByteArrayResource) TileObject(org.geowebcache.storage.TileObject) ByteArrayResource(org.geowebcache.io.ByteArrayResource) Test(org.junit.Test)

Example 3 with Resource

use of org.geowebcache.io.Resource in project geowebcache by GeoWebCache.

the class AbstractAzureBlobStoreIntegrationTest method testPutGetBlobIsNotByteArrayResource.

@Test
public void testPutGetBlobIsNotByteArrayResource() throws MimeException, IOException {
    File tileFile = File.createTempFile("tile", ".png");
    Files.write(new byte[1024], tileFile);
    Resource blob = new FileResource(tileFile);
    TileObject tile = queryTile(20, 30, 12);
    tile.setBlob(blob);
    blobStore.put(tile);
    TileObject queryTile = queryTile(20, 30, 12);
    boolean found = blobStore.get(queryTile);
    assertTrue(found);
    Resource resource = queryTile.getBlob();
    assertNotNull(resource);
    assertEquals(1024, resource.getSize());
}
Also used : Resource(org.geowebcache.io.Resource) FileResource(org.geowebcache.io.FileResource) ByteArrayResource(org.geowebcache.io.ByteArrayResource) FileResource(org.geowebcache.io.FileResource) TileObject(org.geowebcache.storage.TileObject) File(java.io.File) Test(org.junit.Test)

Example 4 with Resource

use of org.geowebcache.io.Resource 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 5 with Resource

use of org.geowebcache.io.Resource in project geowebcache by GeoWebCache.

the class ArcGISCacheLayer method setLayerBlankTile.

private boolean setLayerBlankTile(ConveyorTile tile) {
    // TODO cache result
    String layerPath = getLayerPath().append(File.separatorChar).toString();
    File png = new File(layerPath + "blank.png");
    Resource blank = null;
    try {
        if (png.exists()) {
            blank = readFile(png);
            tile.setBlob(blank);
            tile.setMimeType(MimeType.createFromFormat("image/png"));
        } else {
            File jpeg = new File(layerPath + "missing.jpg");
            if (jpeg.exists()) {
                blank = readFile(jpeg);
                tile.setBlob(blank);
                tile.setMimeType(MimeType.createFromFormat("image/jpeg"));
            }
        }
    } catch (Exception e) {
        return false;
    }
    return blank != null;
}
Also used : Resource(org.geowebcache.io.Resource) FileResource(org.geowebcache.io.FileResource) File(java.io.File) OutsideCoverageException(org.geowebcache.grid.OutsideCoverageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MimeException(org.geowebcache.mime.MimeException) GeoWebCacheException(org.geowebcache.GeoWebCacheException)

Aggregations

Resource (org.geowebcache.io.Resource)50 ByteArrayResource (org.geowebcache.io.ByteArrayResource)36 Test (org.junit.Test)28 TileObject (org.geowebcache.storage.TileObject)16 HashMap (java.util.HashMap)12 File (java.io.File)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 FileResource (org.geowebcache.io.FileResource)7 GeoWebCacheException (org.geowebcache.GeoWebCacheException)6 GridSubset (org.geowebcache.grid.GridSubset)6 MimeType (org.geowebcache.mime.MimeType)6 TileLayer (org.geowebcache.layer.TileLayer)5 BoundingBox (org.geowebcache.grid.BoundingBox)4 MimeException (org.geowebcache.mime.MimeException)4 StorageBrokerTest (org.geowebcache.storage.StorageBrokerTest)4 StorageException (org.geowebcache.storage.StorageException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 GridSet (org.geowebcache.grid.GridSet)3