[exo-jcr-commits] exo-jcr SVN: r450 - in jcr/trunk/exo.jcr.component.webdav/src: main/java/org/exoplatform/services/jcr/webdav/command and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Fri Oct 30 06:57:51 EDT 2009


Author: dkatayev
Date: 2009-10-30 06:57:51 -0400 (Fri, 30 Oct 2009)
New Revision: 450

Added:
   jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/CacheControlType.java
Modified:
   jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/WebDavServiceImpl.java
   jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/command/GetCommand.java
   jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/TestCashing.java
Log:
EXOJCR-178 Implemented mechanizm to manage caching using regexps

Modified: jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/WebDavServiceImpl.java
===================================================================
--- jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/WebDavServiceImpl.java	2009-10-30 10:56:26 UTC (rev 449)
+++ jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/WebDavServiceImpl.java	2009-10-30 10:57:51 UTC (rev 450)
@@ -131,11 +131,11 @@
     * Initialization "auto-version"-parameter value.
     */
    public static final String INIT_PARAM_AUTO_VERSION = "auto-version";
-   
+
    public static final String INIT_PARAM_CACHE_CONTROL = "cache-control";
-   
-   public static HashMap<String, String> cacheControlMap = new HashMap<String, String>();
 
+   private HashMap<MediaType, String> cacheControlMap = new HashMap<MediaType, String>();
+
    /**
     * Logger.
     */
@@ -260,19 +260,27 @@
       ValueParam pCacheControl = params.getValueParam(INIT_PARAM_CACHE_CONTROL);
       if (pCacheControl != null)
       {
-         String cacheControlValue = pCacheControl.getValue();
-         for (String element : cacheControlValue.split(";"))
+         String cacheControlConfigValue = pCacheControl.getValue();
+
+         try
          {
-            String contentTypes = element.split(":")[0];
-            String value = element.split(":")[1];
-            String[] types = contentTypes.split(",");
-            for (String type : types)
+            String[] elements = cacheControlConfigValue.split(";");
+            for (String element : elements)
             {
-               cacheControlMap.put(type, value);
+               String cacheValue = element.split(":")[1];
+               String keys = element.split(":")[0];
+               for (String key : keys.split(","))
+               {
+                  MediaType mediaType = new MediaType(key.split("/")[0], key.split("/")[1]);
+                  cacheControlMap.put(mediaType, cacheValue);
+               }
             }
-            
          }
-         log.info(INIT_PARAM_CACHE_CONTROL + " = " + pCacheControl);
+         catch (Exception e)
+         {
+            log.warn("Invalid " + INIT_PARAM_CACHE_CONTROL + " parameter");
+         }
+
       }
 
    }

Modified: jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/command/GetCommand.java
===================================================================
--- jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/command/GetCommand.java	2009-10-30 10:56:26 UTC (rev 449)
+++ jcr/trunk/exo.jcr.component.webdav/src/main/java/org/exoplatform/services/jcr/webdav/command/GetCommand.java	2009-10-30 10:57:51 UTC (rev 450)
@@ -18,11 +18,25 @@
  */
 package org.exoplatform.services.jcr.webdav.command;
 
+import java.io.InputStream;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.xml.transform.stream.StreamSource;
+
 import org.exoplatform.common.http.HTTPStatus;
 import org.exoplatform.common.util.HierarchicalProperty;
 import org.exoplatform.services.jcr.webdav.Range;
-import org.exoplatform.services.jcr.webdav.WebDavServiceImpl;
-import org.exoplatform.services.jcr.webdav.WebDavConst.CacheConstants;
 import org.exoplatform.services.jcr.webdav.resource.CollectionResource;
 import org.exoplatform.services.jcr.webdav.resource.FileResource;
 import org.exoplatform.services.jcr.webdav.resource.Resource;
@@ -38,21 +52,8 @@
 import org.exoplatform.services.log.Log;
 import org.exoplatform.services.rest.ExtHttpHeaders;
 import org.exoplatform.services.rest.ext.provider.XSLTStreamingOutput;
+import org.exoplatform.services.rest.impl.header.MediaTypeHelper;
 
-import java.io.InputStream;
-import java.net.URI;
-import java.util.HashMap;
-import java.util.List;
-
-import javax.jcr.Node;
-import javax.jcr.PathNotFoundException;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.xml.transform.stream.StreamSource;
-
 /**
  * Created by The eXo Platform SAS Author : <a
  * href="gavrikvetal at gmail.com">Vitaly Guly</a>.
@@ -80,7 +81,7 @@
     * @param ranges ranges
     * @return the instance of javax.ws.rs.core.Response
     */
-   public Response get(Session session, String path, String version, String baseURI, List<Range> ranges, String ifModifiedSince, HashMap<String, String> cahceControls)
+   public Response get(Session session, String path, String version, String baseURI, List<Range> ranges, String ifModifiedSince, HashMap<MediaType, String> cahceControls)
    {
 
       if (version == null)
@@ -267,18 +268,24 @@
     * @param contentType content type
     * @return Cache-Control value
     */
-   private String generateCacheControl(HashMap<String, String> controls, String contentType)
+   private String generateCacheControl(HashMap<MediaType, String> cacheControlMap, String contentType)
    {
       
+      ArrayList<MediaType> mediaTypesList = new ArrayList<MediaType>(cacheControlMap.keySet());
+      Collections.sort(mediaTypesList, MediaTypeHelper.MEDIA_TYPE_COMPARATOR);
+      String cacheControlValue = "";
       
-      if (controls.containsKey(contentType))
+      for (MediaType mediaType : mediaTypesList)
       {
-         return controls.get(contentType);
+         if(contentType.equals(MediaType.WILDCARD)){
+            cacheControlValue = cacheControlMap.get(MediaType.WILDCARD_TYPE);
+            break;
+         } else if (mediaType.isCompatible(new MediaType(contentType.split("/")[0], contentType.split("/")[1]) )) {
+            cacheControlValue = cacheControlMap.get(mediaType);
+            break;
+         }
       }
-      else
-      {
-         return CacheConstants.NO_CACHE;
-      }
+      return cacheControlValue;    
    }
 
 }

Added: jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/CacheControlType.java
===================================================================
--- jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/CacheControlType.java	                        (rev 0)
+++ jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/CacheControlType.java	2009-10-30 10:57:51 UTC (rev 450)
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.jcr.webdav.ext;
+
+/**
+ * @author <a href="mailto:dmitry.kataev at exoplatform.com">Dmytro Katayev</a>
+ * @version $Id: $
+ *
+ */
+public class CacheControlType
+{
+   private String contentType;
+   private String cacheValue;
+   
+   /**
+    * @return the cacheValue
+    */
+   public String getCacheValue()
+   {
+      return cacheValue;
+   }
+
+   /**
+    * @return the contentType
+    */
+   public String getContentType()
+   {
+      return contentType;
+   }
+   
+   public CacheControlType(String contentType, String cacheValue)
+   {
+      this.contentType = contentType;
+      this.cacheValue = cacheValue;
+   }
+   
+
+}


Property changes on: jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/CacheControlType.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/TestCashing.java
===================================================================
--- jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/TestCashing.java	2009-10-30 10:56:26 UTC (rev 449)
+++ jcr/trunk/exo.jcr.component.webdav/src/test/java/org/exoplatform/services/jcr/webdav/ext/TestCashing.java	2009-10-30 10:57:51 UTC (rev 450)
@@ -21,6 +21,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Locale;
 import java.util.TimeZone;
 
@@ -43,11 +44,10 @@
  */
 public class TestCashing extends BaseStandaloneTest
 {
-
    private String path = TestUtils.getFileName();
 
    private String fileContent = TestUtils.getFileContent();
-   
+
    private Node node;
 
    @Override
@@ -58,8 +58,8 @@
       node = TestUtils.addContent(session, path, inputStream, defaultFileNodeType, "");
    }
 
-   
-   public void testIfModifiedSince() throws Exception{
+   public void testIfModifiedSince() throws Exception
+   {
       Node contentNode = node.getNode("jcr:content");
       Property lastModifiedProperty = contentNode.getProperty("jcr:lastModified");
       String formatPattern = "EEE, dd MMM yyyy HH:mm:ss z";
@@ -71,20 +71,28 @@
       ContainerResponse response = service(WebDAVMethods.GET, getPathWS() + path, "", headers, null);
       assertEquals(HTTPStatus.NOT_MODIFIED, response.getStatus());
    }
-   
-   public void testCacheConf() throws Exception {
+
+   public void testCacheConf() throws Exception
+   {
+      ArrayList<CacheControlType> testValues = new ArrayList<CacheControlType>();
+      testValues.add(new CacheControlType("text/xml", "max-age=1800"));
+      testValues.add(new CacheControlType("text/pdf", "max-age=777"));
+      testValues.add(new CacheControlType("image/jpg", "max-age=3600"));
+      testValues.add(new CacheControlType("image/gif", "max-age=555"));
+      testValues.add(new CacheControlType("test/test", "no-cache"));
+      testValues.add(new CacheControlType("*/*", "no-cache"));
+
       Node contentNode = node.getNode("jcr:content");
-      contentNode.setProperty("jcr:mimeType", "text/xml");
-      contentNode.getSession().save();
-      ContainerResponse response = service(WebDAVMethods.GET, getPathWS() + path, "", null, null);
-      String cacheControlHeader = response.getHttpHeaders().get(HttpHeaders.CACHE_CONTROL).toString();
-      assertEquals(cacheControlHeader, "[max-age=1800]");
-      
-      contentNode.setProperty("jcr:mimeType", "image/jpeg");
-      contentNode.getSession().save();
-      response = service(WebDAVMethods.GET, getPathWS() + path, "", null, null);
-      cacheControlHeader = response.getHttpHeaders().get(HttpHeaders.CACHE_CONTROL).toString();
-      assertEquals(cacheControlHeader, "[max-age=3600]");
+
+      for (CacheControlType cacheControlType : testValues)
+      {
+         contentNode.setProperty("jcr:mimeType", cacheControlType.getContentType());
+         contentNode.getSession().save();
+         ContainerResponse response = service(WebDAVMethods.GET, getPathWS() + path, "", null, null);
+         String cacheControlHeader = response.getHttpHeaders().get(HttpHeaders.CACHE_CONTROL).toString();
+         cacheControlHeader = cacheControlHeader.substring(1, cacheControlHeader.length() - 1);
+         assertEquals(cacheControlHeader, cacheControlType.getCacheValue());
+      }
    }
 
    @Override



More information about the exo-jcr-commits mailing list