You are viewing a plain text version of this content. The canonical link for it is here.
Posted to awf-commits@incubator.apache.org by el...@apache.org on 2011/07/27 07:31:52 UTC

svn commit: r1151365 - in /incubator/deft/sandbox/src/main/java/org/deftserver: util/DateUtil.java web/handler/StaticContentHandler.java

Author: elecharny
Date: Wed Jul 27 07:31:51 2011
New Revision: 1151365

URL: http://svn.apache.org/viewvc?rev=1151365&view=rev
Log:
Applied patch fro DEFT-96

Modified:
    incubator/deft/sandbox/src/main/java/org/deftserver/util/DateUtil.java
    incubator/deft/sandbox/src/main/java/org/deftserver/web/handler/StaticContentHandler.java

Modified: incubator/deft/sandbox/src/main/java/org/deftserver/util/DateUtil.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/deftserver/util/DateUtil.java?rev=1151365&r1=1151364&r2=1151365&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/deftserver/util/DateUtil.java (original)
+++ incubator/deft/sandbox/src/main/java/org/deftserver/util/DateUtil.java Wed Jul 27 07:31:51 2011
@@ -20,10 +20,13 @@
 package org.deftserver.util;
 
 import java.text.DateFormat;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.regex.Pattern;
 
 public class DateUtil {
 
@@ -31,15 +34,70 @@ public class DateUtil {
 	private final static TimeZone GMT_ZONE;
 	private final static String RFC_1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss zzz";
 	private final static DateFormat RFC_1123_FORMAT;
-	
+
+	/** Pattern to find digits only. */
+	private final static Pattern DIGIT_PATTERN = Pattern.compile("^\\d+$");
+
 	static {
-		RFC_1123_FORMAT = new SimpleDateFormat(RFC_1123_PATTERN, LOCALE);
+		RFC_1123_FORMAT = new SimpleDateFormat(DateUtil.RFC_1123_PATTERN, DateUtil.LOCALE);
 		GMT_ZONE = TimeZone.getTimeZone("GMT");
-		RFC_1123_FORMAT.setTimeZone(GMT_ZONE);
+		DateUtil.RFC_1123_FORMAT.setTimeZone(DateUtil.GMT_ZONE);
 	}
-		
+
 	public static String getCurrentAsString() {
-		return RFC_1123_FORMAT.format(new Date());
+		return DateUtil.RFC_1123_FORMAT.format(new Date());
+	}
+
+	/**
+	 * Translate a given date <code>String</code> in the <em>RFC 1123</em> format to a <code>long</code> representing the number of milliseconds since
+	 * epoch.
+	 * 
+	 * @param dateString
+	 *            a date <code>String</code> in the <em>RFC 1123</em> format.
+	 * @return the parsed <code>Date</code> in milliseconds.
+	 */
+	private static long parseDateStringToMilliseconds(final String dateString) {
+
+		try {
+			return DateUtil.RFC_1123_FORMAT.parse(dateString).getTime();
+		} catch (final ParseException e) {
+			return 0;
+		}
+	}
+
+	/**
+	 * Parse a given date <code>String</code> to a <code>long</code> representation of the time. Where the provided value is all digits the value is
+	 * returned as a <code>long</code>, otherwise attempt is made to parse the <code>String</code> as a <em>RFC 1123</em> date.
+	 * 
+	 * @param dateValue
+	 *            the value to parse.
+	 * @return the <code>long</code> value following parse, or zero where not successful.
+	 */
+	public static long parseToMilliseconds(final String dateValue) {
+
+		long ms = 0;
+
+		if (DateUtil.DIGIT_PATTERN.matcher(dateValue).matches()) {
+			ms = Long.parseLong(dateValue);
+		} else {
+			ms = parseDateStringToMilliseconds(dateValue);
+		}
+
+		return ms;
+	}
+
+	/**
+	 * Converts a millisecond representation of a date to a <code>RFC 1123</code> formatted <code>String</code>.
+	 * 
+	 * @param dateValue
+	 *            the <code>Date</code> represented as milliseconds.
+	 * @return a <code>String</code> representation of the date.
+	 */
+	public static String parseToRFC1123(final long dateValue) {
+
+		final Calendar calendar = Calendar.getInstance();
+		calendar.setTimeInMillis(dateValue);
+
+		return DateUtil.RFC_1123_FORMAT.format(calendar.getTime());
 	}
-	
 }
\ No newline at end of file

Modified: incubator/deft/sandbox/src/main/java/org/deftserver/web/handler/StaticContentHandler.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/deftserver/web/handler/StaticContentHandler.java?rev=1151365&r1=1151364&r2=1151365&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/deftserver/web/handler/StaticContentHandler.java (original)
+++ incubator/deft/sandbox/src/main/java/org/deftserver/web/handler/StaticContentHandler.java Wed Jul 27 07:31:51 2011
@@ -29,6 +29,7 @@ import java.io.File;
 import javax.activation.FileTypeMap;
 import javax.activation.MimetypesFileTypeMap;
 
+import org.deftserver.util.DateUtil;
 import org.deftserver.web.http.HttpException;
 import org.deftserver.web.http.HttpRequest;
 import org.deftserver.web.http.HttpResponse;
@@ -82,7 +83,7 @@ public class StaticContentHandler extend
 		}
 
 		final long lastModified = file.lastModified();
-		response.setHeader("Last-Modified", String.valueOf(lastModified));
+		response.setHeader("Last-Modified", DateUtil.parseToRFC1123(lastModified));
 		response.setHeader("Cache-Control", "public");
 		String mimeType = mimeTypeMap.getContentType(file);
 		if ("text/plain".equals(mimeType)) {
@@ -91,7 +92,7 @@ public class StaticContentHandler extend
 		response.setHeader("Content-Type", mimeType);
 		final String ifModifiedSince = request.getHeader("If-Modified-Since");
 		if (ifModifiedSince != null) {
-			long ims = Long.parseLong(ifModifiedSince);
+			final long ims = DateUtil.parseToMilliseconds(ifModifiedSince);
 			if (lastModified <= ims) {
 				response.setStatusCode(304);	//Not Modified
 				logger.debug("not modified");