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 2010/09/11 18:35:07 UTC

svn commit: r996165 - in /wicket/trunk/wicket/src/main/java/org/apache/wicket: protocol/http/RequestUtils.java request/resource/AbstractResource.java settings/IResourceSettings.java settings/Settings.java

Author: pete
Date: Sat Sep 11 16:35:06 2010
New Revision: 996165

URL: http://svn.apache.org/viewvc?rev=996165&view=rev
Log:
use class Duration for cache duration

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java?rev=996165&r1=996164&r2=996165&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java Sat Sep 11 16:35:06 2010
@@ -30,6 +30,7 @@ import org.apache.wicket.request.http.We
 import org.apache.wicket.request.mapper.parameter.PageParameters;
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.string.Strings;
+import org.apache.wicket.util.time.Duration;
 
 /**
  * Wicket Http specific utilities class.
@@ -37,7 +38,7 @@ import org.apache.wicket.util.string.Str
 public final class RequestUtils
 {
 	 // one year, maximum recommended cache duration in RFC-2616
-	public static final int MAX_CACHE_DURATION = 60 * 60 * 24 * 365;
+	public static final Duration MAX_CACHE_DURATION = Duration.days(365);
 
 	/**
 	 * Decode the provided queryString as a series of key/ value pairs and set them in the provided
@@ -311,15 +312,13 @@ public final class RequestUtils
 	 *
 	 * @see RequestUtils#MAX_CACHE_DURATION
 	 */
-	public static void enableCaching(WebResponse response, int duration, boolean cachePublic)
+	public static void enableCaching(WebResponse response, Duration duration, boolean cachePublic)
 	{
+		Args.notNull(duration, "duration");
 		Args.notNull(response, "response");
 
-		if(duration < 0)
-			throw new IllegalArgumentException("duration must be a positive value");
-
 		// do not exceed the maximum recommended value from RFC-2616
-		if(duration > MAX_CACHE_DURATION)
+		if(duration.compareTo(MAX_CACHE_DURATION) > 0)
 			duration = MAX_CACHE_DURATION;
 
 		// Get current time
@@ -329,13 +328,13 @@ public final class RequestUtils
 		response.setDateHeader("Date", now);
 
 		// Time for cache expiry = now + duration
-		response.setDateHeader("Expires", now + (duration * 1000L));
+		response.setDateHeader("Expires", now + duration.getMilliseconds());
 
 		// Set caching scope
 		String scope = cachePublic ? "public" : "private";
 
 		// Enable caching and set max age
-		response.setHeader("Cache-Control", scope + ", max-age=" + duration);
+		response.setHeader("Cache-Control", scope + ", max-age=" + duration.getMilliseconds());
 
 		// Let caches distinguish between compressed and uncompressed
 		// versions of the resource so they can serve them properly

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java?rev=996165&r1=996164&r2=996165&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java Sat Sep 11 16:35:06 2010
@@ -32,6 +32,7 @@ import org.apache.wicket.request.http.We
 import org.apache.wicket.settings.IResourceSettings;
 import org.apache.wicket.util.io.Streams;
 import org.apache.wicket.util.lang.Args;
+import org.apache.wicket.util.time.Duration;
 
 /**
  * Convenience resource implementation. The subclass must implement
@@ -75,7 +76,7 @@ public abstract class AbstractResource i
 		private long contentLength = -1;
 		private Date lastModified = null;
 		private WriteCallback writeCallback;
-		private int cacheDuration;
+		private Duration cacheDuration;
 		private boolean cachePublic;
 
 		/**
@@ -299,7 +300,7 @@ public abstract class AbstractResource i
 		 */
 		public void disableCaching()
 		{
-			setCacheDuration(0);
+			setCacheDuration(Duration.NONE);
 		}
 
 		/**
@@ -312,20 +313,21 @@ public abstract class AbstractResource i
 
 		/**
 		 * Sets the duration for which this resource should be cached on client (in seconds). #see
-		 * {@link IResourceSettings#setDefaultCacheDuration(int)}
+		 * {@link IResourceSettings#setDefaultCacheDuration(org.apache.wicket.util.time.Duration)}
 		 * 
-		 * @param cacheDuration
+		 * @param duration
 		 *            caching duration in seconds
 		 */
-		public void setCacheDuration(int cacheDuration)
+		public void setCacheDuration(Duration duration)
 		{
-			this.cacheDuration = cacheDuration;
+			Args.notNull(duration, "duration");
+			this.cacheDuration = duration;
 		}
 
 		/**
 		 * @return duration for which the resource shoudl be cached on client (in seconds)
 		 */
-		public int getCacheDuration()
+		public Duration getCacheDuration()
 		{
 			return cacheDuration;
 		}
@@ -338,7 +340,7 @@ public abstract class AbstractResource i
 		 * @return <code>true</code> if public caches are allowed to cache the resource
 		 *
 		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
-		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, int, boolean)
+		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, org.apache.wicket.util.time.Duration, boolean)
 		 */
 		public boolean isCachePublic()
 		{
@@ -354,7 +356,7 @@ public abstract class AbstractResource i
 		 *             if <code>true</code> public caches are allowed to cache the resource
 		 *
 		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
-		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, int, boolean)
+		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, org.apache.wicket.util.time.Duration, boolean)
 		 */
 		public void setCachePublic(boolean cachePublic)
 		{
@@ -402,9 +404,9 @@ public abstract class AbstractResource i
 	protected void configureCache(final WebRequest request, final WebResponse response,
 		final ResourceResponse data, final Attributes attributes)
 	{
-		final int duration = data.getCacheDuration();
+	    Duration duration = data.getCacheDuration();
 
-		if(duration > 0)
+		if(duration.compareTo(Duration.NONE) > 0)
 		{
 			RequestUtils.enableCaching(response, duration, data.isCachePublic());
 		}

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java?rev=996165&r1=996164&r2=996165&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/IResourceSettings.java Sat Sep 11 16:35:06 2010
@@ -118,11 +118,14 @@ public interface IResourceSettings
 	void addStringResourceLoader(final int index, final IStringResourceLoader loader);
 
 	/**
-	 * Get the the default cache duration (3600 secs == 1hr) for WebResource.
-	 * 
-	 * @return cache duration
+	 * Get the the default cache duration for resources.
+	 * <p/>
+	 *
+	 * @return cache duration (Duration.NONE will be returned if caching is disabled)
+	 *
+	 * @see org.apache.wicket.util.time.Duration#NONE
 	 */
-	int getDefaultCacheDuration();
+	Duration getDefaultCacheDuration();
 
 	/**
 	 * Whether to disable gzip compression for resources. You need this on SAP, which gzips things
@@ -213,16 +216,17 @@ public interface IResourceSettings
 	boolean getUseDefaultOnMissingResource();
 
 	/**
-	 * Set the the default cache duration for WebResource.
+	 * Set the the default cache duration for resources.
 	 * <p/>
-	 * Based on RFC-2616 this should not exceed one year.
+	 * Based on RFC-2616 this should not exceed one year. If you set Duration.NONE caching will be disabled.
 	 *
 	 * @param defaultDuration
 	 *            default cache duration in seconds
 	 *
+	 * @see org.apache.wicket.util.time.Duration#NONE
 	 * @see org.apache.wicket.protocol.http.RequestUtils#MAX_CACHE_DURATION
 	 */
-	void setDefaultCacheDuration(int defaultDuration);
+	void setDefaultCacheDuration(Duration defaultDuration);
 
 	/**
 	 * Sets whether to disable gzip compression for resources. You need to set this on some SAP

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java?rev=996165&r1=996164&r2=996165&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/settings/Settings.java Sat Sep 11 16:35:06 2010
@@ -57,6 +57,7 @@ import org.apache.wicket.util.crypt.KeyI
 import org.apache.wicket.util.file.IResourceFinder;
 import org.apache.wicket.util.file.IResourcePath;
 import org.apache.wicket.util.file.Path;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.lang.Bytes;
 import org.apache.wicket.util.resource.locator.IResourceStreamLocator;
 import org.apache.wicket.util.resource.locator.ResourceStreamLocator;
@@ -318,7 +319,7 @@ public final class Settings
 	private boolean useTimestampOnResourcesName = true;
 
 	/** Default cache duration */
-	private int defaultCacheDuration = 3600;
+	private Duration defaultCacheDuration = Duration.hours(1);
 
 	private MarkupFactory markupFactory;
 
@@ -1379,21 +1380,18 @@ public final class Settings
 	/**
 	 * @see org.apache.wicket.settings.IResourceSettings#getDefaultCacheDuration()
 	 */
-	public final int getDefaultCacheDuration()
+	public final Duration getDefaultCacheDuration()
 	{
 		return defaultCacheDuration;
 	}
 
 	/**
-	 * @see org.apache.wicket.settings.IResourceSettings#setDefaultCacheDuration(long)
+	 * @see org.apache.wicket.settings.IResourceSettings#setDefaultCacheDuration(org.apache.wicket.util.time.Duration)
 	 */
-	public final void setDefaultCacheDuration(int defaultDuration)
+	public final void setDefaultCacheDuration(Duration duration)
 	{
-		if (defaultDuration < 0)
-		{
-			throw new IllegalArgumentException("Parameter 'defaultDuration' must not be < 0");
-		}
-		defaultCacheDuration = defaultDuration;
+		Args.notNull(duration, "duration");
+		defaultCacheDuration = duration;
 	}
 
 	/**