This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository pollen. See https://gitlab.nuiton.org/chorem/pollen.git commit 20819529eae5c2a4e05370d859b2277b7ecf40ed Author: Cécilia Bossard <bossard@codelutin.com> Date: Fri Jul 17 16:18:33 2020 +0200 Cache management for resources --- .../rest/api/PollenRestApiRequestFilter.java | 7 ++-- .../pollen/rest/api/v1/PollenResourceApi.java | 45 ++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiRequestFilter.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiRequestFilter.java index ce6e30b6..c6d8506e 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiRequestFilter.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/PollenRestApiRequestFilter.java @@ -185,9 +185,10 @@ public class PollenRestApiRequestFilter implements ContainerRequestFilter, Conta } } else { - - addTokenToResponse(containerResponseContext); - + if(!"image".equals(containerResponseContext.getMediaType().getType())) { + // add cookies + addTokenToResponse(containerResponseContext); + } } String origin = containerRequestContext.getHeaderString("Origin"); diff --git a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java index 918beb69..dbea0302 100644 --- a/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java +++ b/pollen-rest-api/src/main/java/org/chorem/pollen/rest/api/v1/PollenResourceApi.java @@ -40,8 +40,11 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; +import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Request; import javax.ws.rs.core.Response; import java.io.IOException; @@ -58,11 +61,36 @@ public class PollenResourceApi { @GET @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response getResource(@Context PollenResourceService pollenResourceService, - @PathParam("resourceId") PollenEntityId<PollenResource> resourceId) { + @PathParam("resourceId") PollenEntityId<PollenResource> resourceId, + @Context Request req) { ResourceStreamBean resource = pollenResourceService.getResource(resourceId.getEntityId()); - return Response.ok(resource.getResourceContent(), resource.getContentType()).build(); + if (resource == null) { + return Response.noContent().build(); + } + + // Cache policy (cache of 1 year as this ressource should never change) + CacheControl cc = new CacheControl(); + cc.setMaxAge(31536000); + + //Calculate the ETag on resource ID + EntityTag etag = new EntityTag(resource.getEntityId()); + //Verify if it matched with etag available in http request + Response.ResponseBuilder rb = req.evaluatePreconditions(etag); + + //If ETag matches the rb will be non-null; + //Use the rb to return the response without any further processing + if (rb != null) + { + return rb.cacheControl(cc).tag(etag).build(); + } + + //If rb is null then either it is first time request; or resource is modified + //Get the updated representation and return with Etag attached to it + rb = Response.ok(resource.getResourceContent(), resource.getContentType()).cacheControl(cc).tag(etag); + + return rb.build(); } @Path("/resources/{resourceId}/download") @@ -84,7 +112,18 @@ public class PollenResourceApi { @QueryParam("maxDimension") boolean maxDimension) throws IOException { ResourceStreamBean resource = pollenResourceService.getResourcePreview(resourceId.getEntityId(), maxDimension); - return Response.ok(resource.getResourceContent(), resource.getContentType()).build(); + + if (resource == null) { + return Response.noContent().build(); + } + + // Cache policy (cache of 1 year as this ressource should never change) + CacheControl cc = new CacheControl(); + cc.setMaxAge(31536000); + + Response.ResponseBuilder builder = Response.ok(resource.getResourceContent(), resource.getContentType()); + + return builder.cacheControl(cc).build(); } @Path("/resources/{resourceId}/meta") -- To stop receiving notification emails like this one, please contact chorem.org SCM administrator <admin+scm@chorem.org>.