You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pe...@apache.org on 2011/07/01 17:28:25 UTC
svn commit: r1141965 - in /wicket/trunk:
wicket-request/src/main/java/org/apache/wicket/request/
wicket-request/src/test/java/org/apache/wicket/request/
wicket-util/src/main/java/org/apache/wicket/util/date/
Author: pete
Date: Fri Jul 1 15:28:24 2011
New Revision: 1141965
URL: http://svn.apache.org/viewvc?rev=1141965&view=rev
Log:
added RFC1123 date formatter class for thread-safe(!) formatting of dates contained in http response headers
Added:
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/
wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/RFC1123DateFormatter.java
Modified:
wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/HttpHeaderCollection.java
wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/HttpHeaderCollectionTest.java
Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/HttpHeaderCollection.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/HttpHeaderCollection.java?rev=1141965&r1=1141964&r2=1141965&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/HttpHeaderCollection.java (original)
+++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/HttpHeaderCollection.java Fri Jul 1 15:28:24 2011
@@ -16,11 +16,8 @@
*/
package org.apache.wicket.request;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
-import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -28,8 +25,8 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
-import java.util.TimeZone;
+import org.apache.wicket.util.date.RFC1123DateFormatter;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.time.Time;
@@ -45,24 +42,9 @@ public class HttpHeaderCollection
{
private final Map<HeaderKey, List<Object>> headers;
- /** Greenwich Mean Time (GMT) timezone */
- private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
-
- /** rfc 1123 compliant time stamp for headers */
- private static final String RFC_1123_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
-
- /** date format for date headers */
- public static final DateFormat DATE_FORMAT;
-
/** returned in case no header values were found */
private static final String[] NO_VALUES = new String[0];
- static
- {
- DATE_FORMAT = new SimpleDateFormat(RFC_1123_DATE_FORMAT, Locale.US);
- DATE_FORMAT.setTimeZone(GMT);
- }
-
public HttpHeaderCollection()
{
headers = new HashMap<HeaderKey, List<Object>>();
@@ -179,10 +161,7 @@ public class HttpHeaderCollection
{
if (value instanceof Time)
{
- synchronized(DATE_FORMAT)
- {
- return DATE_FORMAT.format(new Date(((Time)value).getMilliseconds()));
- }
+ return RFC1123DateFormatter.formatDate((Time)value);
}
else
{
Modified: wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/HttpHeaderCollectionTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/HttpHeaderCollectionTest.java?rev=1141965&r1=1141964&r2=1141965&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/HttpHeaderCollectionTest.java (original)
+++ wicket/trunk/wicket-request/src/test/java/org/apache/wicket/request/HttpHeaderCollectionTest.java Fri Jul 1 15:28:24 2011
@@ -18,6 +18,7 @@ package org.apache.wicket.request;
import java.util.Locale;
import java.util.Set;
+import java.util.TimeZone;
import org.apache.wicket.util.time.Time;
import org.junit.Test;
@@ -81,17 +82,26 @@ public class HttpHeaderCollectionTest
assertEquals(time1, headers.getDateHeader("date"));
assertEquals("Thu, 01 Jan 1970 00:16:40 GMT", headers.getHeader("date"));
- // a change of the locale must not affect the date format
+ // a change of the locale or timezone must not affect the date format
final Locale defaultLocale = Locale.getDefault();
+ final TimeZone defaultLocaleefaultTimezone = TimeZone.getDefault();
try
{
+ final String expected = "Thu, 01 Jan 1970 00:16:40 GMT";
+
Locale.setDefault(Locale.CHINESE);
- assertEquals("Thu, 01 Jan 1970 00:16:40 GMT", headers.getHeader("date"));
+ TimeZone.setDefault(TimeZone.getTimeZone("CET"));
+ assertEquals(expected, headers.getHeader("date"));
+
+ Locale.setDefault(Locale.US);
+ TimeZone.setDefault(TimeZone.getTimeZone("EST"));
+ assertEquals(expected, headers.getHeader("date"));
}
finally
{
Locale.setDefault(defaultLocale);
+ TimeZone.setDefault(defaultLocaleefaultTimezone);
}
assertArrayEquals(new String[]{"Thu, 01 Jan 1970 00:16:40 GMT", "Thu, 01 Jan 1970 00:33:20 GMT", "not-a-date"},
Added: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/RFC1123DateFormatter.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/RFC1123DateFormatter.java?rev=1141965&view=auto
==============================================================================
--- wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/RFC1123DateFormatter.java (added)
+++ wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/date/RFC1123DateFormatter.java Fri Jul 1 15:28:24 2011
@@ -0,0 +1,93 @@
+/*
+ * 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.wicket.util.date;
+
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.TimeZone;
+
+import org.apache.wicket.util.time.Time;
+
+/**
+ * print date in RFC1123 format
+ * <p/>
+ * Contrary to {@link java.text.SimpleDateFormat} this is thread-safe.
+ * <p/>
+ * taken from the source code of jetty 7.3.0, thanks for Greg Wilkins!
+ *
+ * @author Peter Ertl
+ *
+ */
+public final class RFC1123DateFormatter
+{
+ private static final String[] DAYS =
+ {"Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
+ private static final String[] MONTHS =
+ {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"};
+
+ public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
+
+ private RFC1123DateFormatter()
+ {
+ }
+
+ public static String formatDate(Time time)
+ {
+ final Calendar cal = GregorianCalendar.getInstance(GMT);
+ final StringBuilder buf = new StringBuilder(32);
+
+ cal.setTimeInMillis(time.getMilliseconds());
+
+ int day_of_week = cal.get(Calendar.DAY_OF_WEEK);
+ int day_of_month = cal.get(Calendar.DAY_OF_MONTH);
+ int month = cal.get(Calendar.MONTH);
+ int year = cal.get(Calendar.YEAR);
+ int century = year / 100;
+ year = year % 100;
+
+ int hours = cal.get(Calendar.HOUR_OF_DAY);
+ int minutes = cal.get(Calendar.MINUTE);
+ int seconds = cal.get(Calendar.SECOND);
+
+ buf.append(DAYS[day_of_week]);
+ buf.append(',');
+ buf.append(' ');
+ appendTwoDigits(buf, day_of_month);
+
+ buf.append(' ');
+ buf.append(MONTHS[month]);
+ buf.append(' ');
+ appendTwoDigits(buf, century);
+ appendTwoDigits(buf, year);
+
+ buf.append(' ');
+ appendTwoDigits(buf, hours);
+ buf.append(':');
+ appendTwoDigits(buf, minutes);
+ buf.append(':');
+ appendTwoDigits(buf, seconds);
+ buf.append(" GMT");
+
+ return buf.toString();
+ }
+
+ private static void appendTwoDigits(StringBuilder str, int number)
+ {
+ str.append((char)(number / 10 + '0'));
+ str.append((char)(number % 10 + '0'));
+ }
+}