From: remillet Date: Tue, 28 Feb 2017 19:44:18 +0000 (-0800) Subject: DRYD-84: Adding CacheMaxAge service binding option for including in HTTP response... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=57357f84564a0fa7237fef70426a608f1d12c4d6;p=tmp%2Fjakarta-migration.git DRYD-84: Adding CacheMaxAge service binding option for including in HTTP response headers. --- diff --git a/services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java b/services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java index 0a4f59b64..cc86d029c 100644 --- a/services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java +++ b/services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java @@ -38,7 +38,6 @@ import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.publicitem.PublicItemUtil; import org.collectionspace.services.nuxeo.client.java.CommonList; import org.collectionspace.services.common.CSWebApplicationException; - import org.jboss.resteasy.plugins.providers.multipart.InputPart; import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; @@ -67,6 +66,8 @@ import java.util.Map; @Consumes("application/xml") @Produces("application/xml") public class BlobResource extends NuxeoBasedResource { + + private static final int DEFAULT_MAX_CACHE_AGE = 86400; // 1 day of seconds. @Override public String getServiceName(){ @@ -240,6 +241,17 @@ public class BlobResource extends NuxeoBasedResource { return response; } + @Override + protected int getCacheMaxAge(ServiceContext ctx) { + int result = super.getCacheMaxAge(ctx); + + if (result <= 0) { + result = this.DEFAULT_MAX_CACHE_AGE; + } + + return result; + } + @GET @Path("{csid}/content") public Response getBlobContent( @PathParam("csid") String csid) { @@ -253,13 +265,14 @@ public class BlobResource extends NuxeoBasedResource { InputStream contentStream = getBlobContent(ctx, csid, null /*derivative term*/, mimeType /*will get set*/); Response.ResponseBuilder responseBuilder = Response.ok(contentStream, mimeType.toString()); + setCacheControl(ctx, responseBuilder); responseBuilder = responseBuilder.header("Content-Disposition","inline;filename=\"" + blobsCommon.getName() +"\""); result = responseBuilder.build(); } catch (Exception e) { throw bigReThrow(e, ServiceMessages.CREATE_FAILED); } - + return result; } @@ -338,6 +351,7 @@ public class BlobResource extends NuxeoBasedResource { StringBuffer mimeType = new StringBuffer(); InputStream contentStream = getBlobContent(ctx, csid, derivativeTerm, mimeType); Response.ResponseBuilder responseBuilder = Response.ok(contentStream, mimeType.toString()); + setCacheControl(ctx, responseBuilder); responseBuilder = responseBuilder.header("Content-Disposition","inline;filename=\"" + blobsCommon.getName() +"\""); result = responseBuilder.build(); diff --git a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto-unified.xml b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto-unified.xml index f599cafca..d3511febc 100644 --- a/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto-unified.xml +++ b/services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto-unified.xml @@ -155,6 +155,7 @@ + 86400 name diff --git a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java index 21240018f..74284e5a5 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java @@ -23,6 +23,7 @@ */ package org.collectionspace.services.common; +import java.math.BigInteger; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,6 +31,7 @@ import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.core.CacheControl; import javax.ws.rs.core.Context; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; @@ -38,8 +40,8 @@ import javax.ws.rs.core.UriInfo; import org.collectionspace.services.client.CollectionSpaceClient; import org.collectionspace.services.common.CSWebApplicationException; import org.collectionspace.services.common.api.Tools; +import org.collectionspace.services.common.config.ServiceConfigUtils; import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl; -import org.collectionspace.services.common.context.MultipartServiceContext; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.context.ServiceContextProperties; import org.collectionspace.services.common.document.BadRequestException; @@ -53,7 +55,9 @@ import org.collectionspace.services.common.security.UnauthorizedException; import org.collectionspace.services.common.storage.StorageClient; import org.collectionspace.services.common.storage.jpa.JpaStorageClientImpl; import org.collectionspace.services.config.service.ServiceBindingType; +import org.collectionspace.services.config.service.DocHandlerParams.Params; import org.collectionspace.services.description.ServiceDescription; + import org.jboss.resteasy.spi.HttpRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -418,7 +422,7 @@ public abstract class AbstractCollectionSpaceResourceImpl public ServiceDescription getDescription(@Context UriInfo uriInfo) { ServiceDescription result = null; - ServiceContext ctx = null; + ServiceContext ctx = null; try { ctx = createServiceContext(uriInfo); result = getDescription(ctx); @@ -437,7 +441,7 @@ public abstract class AbstractCollectionSpaceResourceImpl * @param ctx * @return */ - public ServiceDescription getDescription(ServiceContext ctx) { + public ServiceDescription getDescription(ServiceContext ctx) { ServiceDescription result = new ServiceDescription(); result.setDocumentType(getDocType(ctx.getTenantId())); @@ -644,5 +648,39 @@ public abstract class AbstractCollectionSpaceResourceImpl public TenantBindingConfigReaderImpl getTenantBindingsReader() { return ServiceMain.getInstance().getTenantBindingConfigReader(); } - + + /** + * Get max cache age for HTTP responses from the tenant's service bindings. + * + * @param ctx + * @return + */ + protected int getCacheMaxAge(ServiceContext ctx) { + BigInteger result = null; + + try { + Params docHandlerParams = ServiceConfigUtils.getDocHandlerParams(ctx.getTenantId(), ctx.getServiceName()); + if (docHandlerParams.getCacheMaxAge() != null) { + result = docHandlerParams.getCacheMaxAge(); + } + } catch (DocumentException e) { + logger.debug("Failed to retrieve cache-age-max from service bindings.", e); + } + + return result != null ? result.intValue() : 0; + } + + protected Response.ResponseBuilder setCacheControl(ServiceContext ctx, Response.ResponseBuilder responseBuilder) { + int cacheMaxAge = getCacheMaxAge(ctx); + + if (cacheMaxAge > 0) { + CacheControl cacheControl = new CacheControl(); + cacheControl.setMaxAge(getCacheMaxAge(ctx)); + responseBuilder.cacheControl(cacheControl); + logger.debug(String.format("Cache-max-age for service '%s' is set to '%d' in the service bindings for tenant ID='%s'.", + ctx.getServiceName(), cacheMaxAge, ctx.getTenantId())); + } + + return responseBuilder; + } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/config/ServiceConfigUtils.java b/services/common/src/main/java/org/collectionspace/services/common/config/ServiceConfigUtils.java index 53f57b468..978d2ba54 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/config/ServiceConfigUtils.java +++ b/services/common/src/main/java/org/collectionspace/services/common/config/ServiceConfigUtils.java @@ -26,6 +26,7 @@ package org.collectionspace.services.common.config; import java.util.ArrayList; import java.util.List; +import org.collectionspace.services.common.ServiceMain; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.common.document.DocumentException; import org.collectionspace.services.common.document.DocumentHandler; @@ -47,6 +48,26 @@ public class ServiceConfigUtils { final static Logger logger = LoggerFactory.getLogger(ServiceConfigUtils.class); + public static DocHandlerParams.Params getDocHandlerParams(String tenantId, String serviceName) throws DocumentException { + TenantBindingConfigReaderImpl tReader = + ServiceMain.getInstance().getTenantBindingConfigReader(); + TenantBindingType tenantBinding = tReader.getTenantBinding(tenantId); + if (tenantBinding == null) { + String msg = "No tenant binding found for tenantId=" + tenantId + + " while processing request for service= " + serviceName; + logger.error(msg); + throw new IllegalStateException(msg); + } + ServiceBindingType serviceBinding = tReader.getServiceBinding(tenantId, serviceName); + DocHandlerParams dhb = serviceBinding.getDocHandlerParams(); + if (dhb != null && dhb.getParams() != null) { + return dhb.getParams(); + } + + throw new DocumentException("No DocHandlerParams configured for: " + + serviceBinding.getName()); + } + /* * Returns the document handler parameters that were loaded at startup from the * tenant bindings config file. diff --git a/services/config/src/main/resources/service.xsd b/services/config/src/main/resources/service.xsd index 0295f92e0..ea6682df2 100644 --- a/services/config/src/main/resources/service.xsd +++ b/services/config/src/main/resources/service.xsd @@ -242,6 +242,7 @@ +