You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/05/13 00:49:42 UTC

svn commit: r655680 - in /incubator/shindig/trunk: ./ java/gadgets/ java/gadgets/src/main/java/org/apache/shindig/gadgets/http/ java/gadgets/src/test/java/org/apache/shindig/gadgets/ java/gadgets/src/test/java/org/apache/shindig/gadgets/http/

Author: etnu
Date: Mon May 12 15:49:41 2008
New Revision: 655680

URL: http://svn.apache.org/viewvc?rev=655680&view=rev
Log:
Applying updated SHINDIG-257 patch, also from Brian Eaton. This version includes usage of joda instead of SimpleDateFormat, and it adds several new test cases.

We should still move some of this stuff out of HttpUtil entirely, but that is left for a future change.


Added:
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpUtilTest.java
Modified:
    incubator/shindig/trunk/java/gadgets/pom.xml
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpUtil.java
    incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicContentCacheTest.java
    incubator/shindig/trunk/pom.xml

Modified: incubator/shindig/trunk/java/gadgets/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/pom.xml?rev=655680&r1=655679&r2=655680&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/pom.xml (original)
+++ incubator/shindig/trunk/java/gadgets/pom.xml Mon May 12 15:49:41 2008
@@ -240,5 +240,9 @@
       <groupId>commons-codec</groupId>
       <artifactId>commons-codec</artifactId>
     </dependency>
+    <dependency>
+      <groupId>joda-time</groupId>
+      <artifactId>joda-time</artifactId>
+    </dependency>
   </dependencies>
 </project>

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpUtil.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpUtil.java?rev=655680&r1=655679&r2=655680&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpUtil.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/HttpUtil.java Mon May 12 15:49:41 2008
@@ -24,11 +24,13 @@
 import org.apache.shindig.gadgets.GadgetContext;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.View;
+import org.joda.time.DateTimeZone;
+import org.joda.time.format.DateTimeFormat;
+import org.joda.time.format.DateTimeFormatter;
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
-import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashMap;
@@ -46,24 +48,11 @@
   public static final long START_TIME = System.currentTimeMillis();
   // 1 year.
   private static final int DEFAULT_TTL = 60 * 60 * 24 * 365;
-
-  /**
-   * Format to use for date headers (see section 3.3.1 of RFC 2616).
-   * 
-   * Note that the SimpleDateFormat parsing rules are generous and can read
-   * all three of the formats specified by RFC 2616.
-   */
-  public static final String HTTP_DATE_HEADER_FORMAT =
-      "EEE, dd MMM yyyy HH:mm:ss zzz";
   
-  // SimpleDateFormat: slow to init, but not thread-safe.
-  private static ThreadLocal<SimpleDateFormat> httpDateFormatter = 
-      new ThreadLocal<SimpleDateFormat>() {
-        @Override
-        protected SimpleDateFormat initialValue() {
-          return new SimpleDateFormat(HTTP_DATE_HEADER_FORMAT, Locale.US);
-        }
-      };
+  private static DateTimeFormatter httpDateFormatter = DateTimeFormat
+      .forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'")
+      .withLocale(Locale.US)
+      .withZone(DateTimeZone.UTC);
 
   /**
    * Sets default caching Headers (Expires, Cache-Control, Last-Modified)
@@ -108,7 +97,7 @@
    */
   public static Date parseDate(String dateStr) {
     try {
-      return httpDateFormatter.get().parse(dateStr);
+      return httpDateFormatter.parseDateTime(dateStr).toDate();
     } catch (Exception e) {
       // Don't care.
       return null;
@@ -122,7 +111,7 @@
    * @return HTTP date string.
    */
   public static String formatDate(Date date) {
-    return httpDateFormatter.get().format(date);
+    return httpDateFormatter.print(date.getTime());
   }
 
 

Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicContentCacheTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicContentCacheTest.java?rev=655680&r1=655679&r2=655680&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicContentCacheTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/BasicContentCacheTest.java Mon May 12 15:49:41 2008
@@ -28,6 +28,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.TimeZone;
 
 /**
  * Tests for basic content cache
@@ -37,7 +38,10 @@
    * Used to parse Expires: header.
    */
   private final static DateFormat dateFormat
-      = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
+      = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
+  static {
+    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+  }
 
   private ContentCache cache;
 

Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpUtilTest.java?rev=655680&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpUtilTest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/HttpUtilTest.java Mon May 12 15:49:41 2008
@@ -0,0 +1,72 @@
+/*
+ * 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.shindig.gadgets.http;
+
+import junit.framework.TestCase;
+
+import java.util.Date;
+import java.util.Locale;
+
+public class HttpUtilTest extends TestCase {
+  
+  public void testFormatInWrongLocale() {
+    Locale orig = Locale.getDefault();
+    try {
+      Locale.setDefault(Locale.ITALY);
+      testFormatDate();
+    } finally {
+      Locale.setDefault(orig);
+    }
+  }
+  
+  public void testParseDate_rfc1123() {
+    String expires = "Sun, 06 Nov 1994 08:49:37 GMT";
+    Date date = HttpUtil.parseDate(expires);
+    assertEquals(784111777000L, date.getTime());
+    
+    date = HttpUtil.parseDate("Mon, 12 May 2008 17:00:18 GMT");
+    assertEquals(1210611618000L, date.getTime());
+  }
+
+  public void testParseDate_wrongTimeZone() {
+    String expires = "Mon, 12 May 2008 09:23:29 PDT";
+    assertNull(HttpUtil.parseDate(expires));
+  }
+  
+  public void testParseDate_rfc1036() {
+    // We don't support this, though RFC 2616 suggests we should
+    String expires = "Sunday, 06-Nov-94 08:49:37 GMT";
+    assertNull(HttpUtil.parseDate(expires));
+  }
+  
+  public void testParseDate_asctime() {
+    // We don't support this, though RFC 2616 suggests we should
+    String expires = "Sun Nov  6 08:49:37 1994";
+    assertNull(HttpUtil.parseDate(expires));
+  }
+
+  public void testFormatDate() {
+    Date date = new Date(784111777000L);
+    assertEquals("Sun, 06 Nov 1994 08:49:37 GMT", HttpUtil.formatDate(date));
+    
+    date = new Date(1210611618000L);
+    assertEquals("Mon, 12 May 2008 17:00:18 GMT", HttpUtil.formatDate(date));
+  }
+
+}

Modified: incubator/shindig/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/pom.xml?rev=655680&r1=655679&r2=655680&view=diff
==============================================================================
--- incubator/shindig/trunk/pom.xml (original)
+++ incubator/shindig/trunk/pom.xml Mon May 12 15:49:41 2008
@@ -755,6 +755,11 @@
         <artifactId>abdera-extensions-html</artifactId>
         <version>0.5.0-incubating-SNAPSHOT</version>
       </dependency>
+      <dependency>
+        <groupId>joda-time</groupId>
+        <artifactId>joda-time</artifactId>
+        <version>1.5.2</version>
+      </dependency>
     </dependencies>
   </dependencyManagement>
 </project>