incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1141618 - in /sling/trunk/bundles/api: pom.xml src/main/java/org/apache/sling/api/request/RequestUtil.java src/test/java/org/apache/sling/api/request/RequestUtilTest.java
Date Thu, 30 Jun 2011 16:53:37 GMT
Author: justin
Date: Thu Jun 30 16:53:36 2011
New Revision: 1141618

URL: http://svn.apache.org/viewvc?rev=1141618&view=rev
Log:
SLING-2112 - adding RequestUtil.handleIfModifiedSince method (thanks to Antonio Sanso for
the patch!)

Added:
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/request/RequestUtilTest.java
Modified:
    sling/trunk/bundles/api/pom.xml
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java

Modified: sling/trunk/bundles/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/pom.xml?rev=1141618&r1=1141617&r2=1141618&view=diff
==============================================================================
--- sling/trunk/bundles/api/pom.xml (original)
+++ sling/trunk/bundles/api/pom.xml Thu Jun 30 16:53:36 2011
@@ -73,6 +73,12 @@
             <groupId>org.jmock</groupId>
             <artifactId>jmock-junit4</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.testing</artifactId>
+            <version>2.0.6</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java?rev=1141618&r1=1141617&r2=1141618&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/RequestUtil.java Thu
Jun 30 16:53:36 2011
@@ -24,6 +24,9 @@ import java.util.Map;
 
 import javax.servlet.Servlet;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.sling.api.servlets.HttpConstants;
+import org.apache.sling.api.SlingHttpServletRequest;
 
 /**
  * @since 2.1
@@ -160,4 +163,28 @@ public class RequestUtil {
         }
         return oldValue;
     }
+
+    /**
+     * Checks if the request contains a if-last-modified-since header and if the the
+	 * request's underlying resource has a jcr:lastModified property. if the properties were
modified
+     * before the header a 304 is sent otherwise the response last modified header is set.
+     * @param req the request
+     * @param resp the response
+     * @return <code>true</code> if the response was set
+     */
+    public static boolean handleIfModifiedSince(SlingHttpServletRequest req, HttpServletResponse
resp){
+        boolean responseSet=false;
+        long lastModified=req.getResource().getResourceMetadata().getModificationTime();
+        if (lastModified!=-1){
+            long modifiedTime = lastModified/1000; //seconds
+            long ims = req.getDateHeader(HttpConstants.HEADER_IF_MODIFIED_SINCE)/1000; //seconds
+            if (modifiedTime <= ims) {
+                resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+                responseSet=true;
+            }
+            resp.setDateHeader(HttpConstants.HEADER_LAST_MODIFIED, lastModified);
+        }
+        return responseSet;
+    }
+
 }

Added: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/request/RequestUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/request/RequestUtilTest.java?rev=1141618&view=auto
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/request/RequestUtilTest.java
(added)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/request/RequestUtilTest.java
Thu Jun 30 16:53:36 2011
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.api.request;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Locale;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.testing.sling.MockResource;
+import org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest;
+
+import junit.framework.TestCase;
+
+public class RequestUtilTest extends TestCase {
+
+      
+    public void testHandleIfModifiedSince(){
+        assertTrue(RequestUtil.handleIfModifiedSince(getMockRequest(1309268989938L,1309269042730L),getMockResponse()));
+        
+        assertFalse(RequestUtil.handleIfModifiedSince(getMockRequest(1309269042730L,1309268989938L),getMockResponse()));
+        assertFalse(RequestUtil.handleIfModifiedSince(getMockRequest(-1,1309268989938L),getMockResponse()));
+    }
+
+    protected SlingHttpServletRequest getMockRequest(final long modificationTime, final long
ifModifiedSince) {
+        final String resourcePath = "foo";
+        final MockSlingHttpServletRequest r = new MockSlingHttpServletRequest(resourcePath,
null, null, null, null) {
+            @Override
+            public long getDateHeader(String name) {
+                return ifModifiedSince;
+            }
+            
+        };
+        final String path = "/foo/node";
+        final MockResource mr = new MockResource(null, path, null) {};
+        mr.getResourceMetadata().setModificationTime(modificationTime);
+        r.setResource(mr);
+        return r;
+    }
+
+    public void testParserAcceptHeader(){
+        assertEquals(RequestUtil.parserAcceptHeader("compress;q=0.5, gzip;q=1.0").get("compress"),
0.5);
+        assertEquals(RequestUtil.parserAcceptHeader("compress,gzip").get("compress"),1.0);
+        assertEquals(RequestUtil.parserAcceptHeader("compress").get("compress"),1.0);
+        assertEquals(RequestUtil.parserAcceptHeader("compress;q=string,gzip;q=1.0").get("compress"),
1.0);
+    
+        assertNull(RequestUtil.parserAcceptHeader("compress;q=0.5, gzip;q=1.0").get("compres"));
+    }
+
+    
+    protected HttpServletResponse getMockResponse() {
+
+        return new HttpServletResponse() {
+
+            public void setLocale(Locale loc) {}
+
+            public void setContentType(String type) {}
+
+            public void setContentLength(int len) {}
+
+            public void setCharacterEncoding(String charset) {}
+
+            public void setBufferSize(int size) {}
+
+            public void resetBuffer() {}
+
+            public void reset() {}
+
+            public boolean isCommitted() { 
+                return false;
+            }
+
+            public PrintWriter getWriter() throws IOException {
+                return null;
+            }
+
+            public ServletOutputStream getOutputStream() throws IOException {
+                return null;
+            }
+
+            public Locale getLocale() { 
+                return null;
+            }
+
+            public String getContentType() { 
+                return null;
+            }
+
+            public String getCharacterEncoding() { 
+                return null;
+            }
+
+            public int getBufferSize() {
+                return 0;
+            }
+
+            public void flushBuffer() throws IOException {}
+
+            public void setStatus(int sc, String sm) {}
+
+            public void setStatus(int sc) {}
+
+            public void setIntHeader(String name, int value) {}
+
+            public void setHeader(String name, String value) {}
+
+            public void setDateHeader(String name, long date) {}
+
+            public void sendRedirect(String location) throws IOException {}
+
+            public void sendError(int sc, String msg) throws IOException {}
+
+            public void sendError(int sc) throws IOException {}
+
+            public String encodeUrl(String url) {
+                return null;
+            }
+
+            public String encodeURL(String url) {
+                return null;
+            }
+
+            public String encodeRedirectUrl(String url) {
+                return null;
+            }
+
+            public String encodeRedirectURL(String url) {
+                return null;
+            }
+
+            public boolean containsHeader(String name) {
+                return false;
+            }
+
+            public void addIntHeader(String name, int value) {}
+
+            public void addHeader(String name, String value) {}
+
+            public void addDateHeader(String name, long date) {}
+
+            public void addCookie(Cookie cookie) {}
+        };
+
+    }
+
+
+}



Mime
View raw message