]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-84: Adding CacheMaxAge service binding option for including in HTTP response...
authorremillet <remillet@yahoo.com>
Tue, 28 Feb 2017 19:44:18 +0000 (11:44 -0800)
committerremillet <remillet@yahoo.com>
Tue, 28 Feb 2017 19:44:18 +0000 (11:44 -0800)
services/blob/service/src/main/java/org/collectionspace/services/blob/BlobResource.java
services/common/src/main/cspace/config/services/tenants/tenant-bindings-proto-unified.xml
services/common/src/main/java/org/collectionspace/services/common/AbstractCollectionSpaceResourceImpl.java
services/common/src/main/java/org/collectionspace/services/common/config/ServiceConfigUtils.java
services/config/src/main/resources/service.xsd

index 0a4f59b64e44b5d81345d3ff5347e2140fac3364..cc86d029c16238c1e5cfa80bcddc458ace1fe834 100644 (file)
@@ -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();
index f599cafcab6f29561b46b5a83c05ba3043f09b60..d3511febc03e8ce496a9edbf69f4574f8ca6745f 100644 (file)
                        </service:documentHandler>
                        <service:DocHandlerParams xmlns:service="http://collectionspace.org/services/config/service">
                                <service:params>
+                                   <service:CacheMaxAge>86400</service:CacheMaxAge> <!-- By default, cache blobs for 1 full day -->
                                        <service:ListResultsFields>
                                                <service:ListResultField>
                                                        <service:element>name</service:element>
index 21240018fa7c010e202bbde0bf9c909607fe3fd0..74284e5a597e4144bcdab6ec85c146a70ad4cd81 100644 (file)
@@ -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<IT, OT>
     public ServiceDescription getDescription(@Context UriInfo uriInfo) {
        ServiceDescription result = null;
 
-       ServiceContext  ctx = null;
+       ServiceContext<IT, OT>  ctx = null;
         try {
             ctx = createServiceContext(uriInfo);
             result = getDescription(ctx);
@@ -437,7 +441,7 @@ public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
      * @param ctx
      * @return
      */
-    public ServiceDescription getDescription(ServiceContext ctx) {
+    public ServiceDescription getDescription(ServiceContext<IT, OT> ctx) {
        ServiceDescription result = new ServiceDescription();
        
        result.setDocumentType(getDocType(ctx.getTenantId()));
@@ -644,5 +648,39 @@ public abstract class AbstractCollectionSpaceResourceImpl<IT, OT>
     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<IT, OT> 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<IT, OT> 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;
+    }  
 }
index 53f57b46805561e333451a6d232ed30038a3022c..978d2ba544c7be8c4cfa305887700ecadae688f2 100644 (file)
@@ -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.
index 0295f92e0fbd8aeeae107b2b7563b8be3196e6e4..ea6682df231ed073d0d053a576fe200522f191ff 100644 (file)
             <xs:element name="params">
                 <xs:complexType>
                     <xs:sequence>
+                        <xs:element name="CacheMaxAge" type="xs:integer" minOccurs="0" maxOccurs="1" default="86400"/>  <!-- default of 1 day -->
                         <xs:element name="SchemaName" type="xs:string" minOccurs="0" maxOccurs="1"/>
                         <xs:element name="RefnameDisplayNameField" type="ListResultField" minOccurs="0" maxOccurs="1"/>  <!-- Should rename 'ListResultField' to a more generic name -->
                         <xs:element name="SupportsHierarchy" type="xs:boolean" minOccurs="0" maxOccurs="1" default="false"/>