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);
}
}
}
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);
}
}
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());
}
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);
}
}
}
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;
}
Aggregations