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 19:35:59 UTC

svn commit: r996183 - in /wicket/trunk: wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/ wicket-request/src/main/java/org/apache/wicket/request/http/ wicket/src/main/java/org/apache/wicket/ajax/ wicket/src/mai...

Author: pete
Date: Sat Sep 11 17:35:59 2010
New Revision: 996183

URL: http://svn.apache.org/viewvc?rev=996183&view=rev
Log:
move caching functionality into WebResponse, improved javadocs a little

Modified:
    wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteBehavior.java
    wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/http/WebResponse.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/RequestUtils.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebResponse.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/request/handler/EmptyAjaxRequestHandler.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/request/resource/AbstractResource.java

Modified: wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteBehavior.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteBehavior.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteBehavior.java (original)
+++ wicket/trunk/wicket-extensions/src/main/java/org/apache/wicket/extensions/ajax/markup/html/autocomplete/AutoCompleteBehavior.java Sat Sep 11 17:35:59 2010
@@ -110,7 +110,7 @@ public abstract class AutoCompleteBehavi
 					.getResponseRequestEncoding();
 				r.setContentType("text/xml; charset=" + encoding);
 
-				RequestUtils.disableCaching(r);
+				r.disableCaching();
 
 				Iterator<T> comps = getChoices(val);
 				renderer.renderHeader(r);

Modified: wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/http/WebResponse.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/http/WebResponse.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/http/WebResponse.java (original)
+++ wicket/trunk/wicket-request/src/main/java/org/apache/wicket/request/http/WebResponse.java Sat Sep 11 17:35:59 2010
@@ -21,8 +21,9 @@ import java.io.IOException;
 import javax.servlet.http.Cookie;
 
 import org.apache.wicket.request.Response;
+import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.string.Strings;
-
+import org.apache.wicket.util.time.Duration;
 
 /**
  * Base class for web-related responses.
@@ -31,6 +32,9 @@ import org.apache.wicket.util.string.Str
  */
 public abstract class WebResponse extends Response
 {
+	// one year, maximum recommended cache duration in RFC-2616
+	public static final Duration MAX_CACHE_DURATION = Duration.days(365);
+
 	/**
 	 * Add a cookie to the web response
 	 * 
@@ -157,11 +161,82 @@ public abstract class WebResponse extend
 	public abstract void flush();
 
 	/**
-	 * scope for cache entries when caching is enabled
+	 * Make this response non-cacheable
 	 */
-	public enum CacheScope
+	public void disableCaching()
 	{
-		PUBLIC("public"),
+		setDateHeader("Date", System.currentTimeMillis());
+		setDateHeader("Expires", 0);
+		setHeader("Pragma", "no-cache");
+		setHeader("Cache-Control", "no-cache, no-store");
+	}
+
+	/**
+	 * Make this response cacheable
+	 *
+	 * @param duration
+	 *            maximum duration before the response must be invalidated by any caches.
+	 *            It should not exceed one year, based on
+	 *            <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC-2616</a>.
+	 * @param scope
+	 *            controls which caches are allowed to cache the response
+	 *
+	 * @see WebResponse#MAX_CACHE_DURATION
+	 */
+	public void enableCaching(Duration duration, WebResponse.CacheScope scope)
+	{
+		Args.notNull(duration, "duration");
+		Args.notNull(scope, "scope");
+
+		// do not exceed the maximum recommended value from RFC-2616
+		if(duration.compareTo(MAX_CACHE_DURATION) > 0)
+			duration = MAX_CACHE_DURATION;
+
+		// Get current time
+		long now = System.currentTimeMillis();
+
+		// Time of message generation
+		setDateHeader("Date", now);
+
+		// Time for cache expiry = now + duration
+		setDateHeader("Expires", now + duration.getMilliseconds());
+
+		// Enable caching and set max age
+		setHeader("Cache-Control", scope.cacheControl + ", max-age=" + duration.getMilliseconds());
+
+		// Let caches distinguish between compressed and uncompressed
+		// versions of the resource so they can serve them properly
+		setHeader("Vary", "Accept-Encoding");
+	}
+
+	/**
+	 * scope for caching web responses
+	 * <p/>
+	 * Unless the response is confidential or session-specific the general advice is
+	 * to prefer value <code>PUBLIC</code> for best network performance.
+	 * <p/>
+	 * This value will basically affect the header [Cache-Control]. Details can be found
+	 *  <a href="http://palisade.plynt.com/issues/2008Jul/cache-control-attributes">here</a>
+	 * or in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC-2616</a>.
+	 */
+	public static enum CacheScope
+	{
+		/**
+		 * all caches are allowed to cache the response
+		 * <p/>
+		 * Use this value for caching is your response is not confidential or session-specific. In that
+		 * case public proxies and caches are allowed to cache the response. In some versions of Firefox
+		 * it will enable caching of resources over SSL (details can be found
+		 * <a href="http://blog.pluron.com/2008/07/why-you-should.html">here</a>).
+		 */
+		 PUBLIC("public"),
+		 /**
+		 *	only the client may cache the response
+		  * <p/>
+		  * Use this setting if the response is session-specific or confidential and you don't
+		  * want it to be cached on public caches or proxies. On some versions of Firefox this
+		  * will disable caching of any resources in over SSL connections.
+		 */
 		PRIVATE("private");
 
 		// value for Cache-Control header
@@ -171,10 +246,5 @@ public abstract class WebResponse extend
 		{
 			this.cacheControl = cacheControl;
 		}
-
-		public String getCacheControl()
-		{
-			return cacheControl;
-		}
 	}
 }

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/AjaxRequestTarget.java Sat Sep 11 17:35:59 2010
@@ -575,7 +575,7 @@ public class AjaxRequestTarget implement
 			response.setContentType("text/xml; charset=" + encoding);
 
 			// Make sure it is not cached by a client
-			RequestUtils.disableCaching(response);
+			response.disableCaching();
 
 			response.write("<?xml version=\"1.0\" encoding=\"");
 			response.write(encoding);

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebPage.java Sat Sep 11 17:35:59 2010
@@ -211,7 +211,7 @@ public class WebPage extends Page implem
 	 */
 	protected void setHeaders(WebResponse response)
 	{
-		RequestUtils.disableCaching(response);
+		response.disableCaching();
 	}
 
 	/**

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=996183&r1=996182&r2=996183&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 17:35:59 2010
@@ -26,9 +26,7 @@ import javax.servlet.http.HttpServletReq
 import org.apache.wicket.Application;
 import org.apache.wicket.request.UrlDecoder;
 import org.apache.wicket.request.cycle.RequestCycle;
-import org.apache.wicket.request.http.WebResponse;
 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;
 
@@ -37,9 +35,6 @@ import org.apache.wicket.util.time.Durat
  */
 public final class RequestUtils
 {
-	 // one year, maximum recommended cache duration in RFC-2616
-	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
 	 * value map.
@@ -249,92 +244,4 @@ public final class RequestUtils
 		}
 		return Charset.forName(charsetName);
 	}
-
-	/**
-	 * set all required headers to disable caching
-	 * <p/>
-	 * the following headers are set:
-	 * <ul>
-	 * <li>"Pragma" is set for older browsers only supporting HTTP 1.0.</li>
-	 * <li>"Cache-Control" is set for modern browsers that support HTTP 1.1.</li>
-	 * <li>"Expires" additionally sets the content expiry in the past which effectively prohibits caching</li>
-	 * <li>"Date" is recommended in general</li>
-	 * </ul>
-	 *
-	 * @param response web response
-	 */
-	public static void disableCaching(WebResponse response)
-	{
-		Args.notNull(response, "response");
-		response.setDateHeader("Date", System.currentTimeMillis());
-		response.setDateHeader("Expires", 0);
-		response.setHeader("Pragma", "no-cache");
-		response.setHeader("Cache-Control", "no-cache, no-store");
-	}
-
-	/**
-	 * enable caching for the given response
-	 * <p/>
-	 * The [duration] is the maximum time in seconds until the response is invalidated from the cache. The
-	 * maximum duration should not exceed one year, based on
-	 * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC-2616</a>.
-	 * <p/>
-	 * The [cachePublic] flag will let you control if the response may be cached
-	 * by public caches or just by the client itself. This sets the http response header
-	 *
-	 * <ul>
-	 * <li><code>[Cache-Control: public]</code> if <code>cachePublic = true</code></li>
-	 * <li><code>[Cache-Control: private]</code> if <code>cachePublic = false</code></li>
-	 * </ul>
-	 * <p/>
-	 * Details on <code>Cache-Control</code> header can be found
-	 *  <a href="http://palisade.plynt.com/issues/2008Jul/cache-control-attributes">here</a>
-	 * or in <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">RFC-2616</a>.
-	 * <p/>
-	 * Choose <code>cachePublic = false</code> wisely since setting <code>Cache-Control: private</code>
-	 * may cause trouble with some versions of Firefox which will not cache SSL content at all. More details
-	 * on this Firefox issue can be found <a href="http://blog.pluron.com/2008/07/why-you-should.html">here</a>.
-	 * <p/>
-	 * Never set <code>cachePublic=true</code> when the response is confidential or client-specific. You
-	 * don't want to see your sensitive private data on some public proxy.
-	 * <p/>
-	 * Unless the response really is confidential / top-secret or client-specific the general advice is
-	 * to always prefer <code>cachePublic=true</code> for best network performance.
-	 *
-	 * @param response
-	 *            response that should be cacheable
-	 * @param duration
-	 *            duration in seconds that the response may be cached
-	 *            (Integer.MAX_VALUE will select maximum duration based on RFC-2616)
-	 * @param cachePublic
-	 *            If <code>true</code> all caches are allowed to cache the response.
-	 *            If <code>false</code> only the client may cache the response (if at all).
-	 *
-	 * @see RequestUtils#MAX_CACHE_DURATION
-	 */
-	public static void enableCaching(WebResponse response, Duration duration, WebResponse.CacheScope scope)
-	{
-		Args.notNull(duration, "duration");
-		Args.notNull(response, "response");
-
-		// do not exceed the maximum recommended value from RFC-2616
-		if(duration.compareTo(MAX_CACHE_DURATION) > 0)
-			duration = MAX_CACHE_DURATION;
-
-		// Get current time
-		long now = System.currentTimeMillis();
-
-		// Time of message generation
-		response.setDateHeader("Date", now);
-
-		// Time for cache expiry = now + duration
-		response.setDateHeader("Expires", now + duration.getMilliseconds());
-
-		// Enable caching and set max age
-		response.setHeader("Cache-Control", scope.getCacheControl() + ", max-age=" + duration.getMilliseconds());
-
-		// Let caches distinguish between compressed and uncompressed
-		// versions of the resource so they can serve them properly
-		response.setHeader("Vary", "Accept-Encoding");
-	}
 }

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebResponse.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebResponse.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebResponse.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/servlet/ServletWebResponse.java Sat Sep 11 17:35:59 2010
@@ -239,7 +239,7 @@ public class ServletWebResponse extends 
 			// usually highly dynamic and can not be statically mapped
 			// to a request url in general
 			if (cacheable == false)
-				RequestUtils.disableCaching(this);
+				this.disableCaching();
 
 			httpServletResponse.sendRedirect(url);
 		}

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/request/handler/EmptyAjaxRequestHandler.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/handler/EmptyAjaxRequestHandler.java?rev=996183&r1=996182&r2=996183&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/request/handler/EmptyAjaxRequestHandler.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/request/handler/EmptyAjaxRequestHandler.java Sat Sep 11 17:35:59 2010
@@ -66,7 +66,7 @@ public final class EmptyAjaxRequestHandl
 		response.setContentType("text/xml; charset=" + encoding);
 
 		// Make sure it is not cached by a client
-		RequestUtils.disableCaching(response);
+		response.disableCaching();
 
 		response.write("<?xml version=\"1.0\" encoding=\"");
 		response.write(encoding);

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=996183&r1=996182&r2=996183&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 17:35:59 2010
@@ -86,8 +86,8 @@ public abstract class AbstractResource i
 		{
 			cacheDuration = Application.get().getResourceSettings().getDefaultCacheDuration();
 
-			// set caching on public caches to false. this behavior is similar to wicket 1.4
-			// setting it to [true] seems to be sexy but could potentially cache confidential
+			// disallow caching for public caches. this behavior is similar to wicket 1.4:
+			// setting it to [PUBLIC] seems to be sexy but could potentially cache confidential
 			// data on public proxies for users migrating to 1.5
 			cacheScope = WebResponse.CacheScope.PRIVATE;
 		}
@@ -308,7 +308,7 @@ public abstract class AbstractResource i
 		 */
 		public void setCacheDurationToMaximum()
 		{
-			cacheDuration = RequestUtils.MAX_CACHE_DURATION;
+			cacheDuration = WebResponse.MAX_CACHE_DURATION;
 		}
 
 		/**
@@ -333,14 +333,15 @@ public abstract class AbstractResource i
 		}
 
 		/**
-		 * returns if the resource may be cached by public caches or not
+		 * returns what kind of caches are allowed to cache the resource response
 		 * <p/>
-		 * resources are only cached at all if the cache duration for the response is > 0.
+		 * resources are only cached at all if caching is enabled by setting a cache duration.
 		 *
-		 * @return <code>true</code> if public caches are allowed to cache the resource
+		 * @return cache scope
 		 *
 		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
-		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, org.apache.wicket.util.time.Duration, org.apache.wicket.request.http.WebResponse.CacheScope)
+		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#setCacheDuration(org.apache.wicket.util.time.Duration)
+		 * @see org.apache.wicket.request.http.WebResponse.CacheScope
 		 */
 		public WebResponse.CacheScope getCacheScope()
 		{
@@ -348,19 +349,21 @@ public abstract class AbstractResource i
 		}
 
 		/**
-		 * controls if the resource may be cached by public caches
+		 * controls what kind of caches are allowed to cache the response
 		 * <p/>
-		 * resources are only cached at all if the cache duration for the response is > 0.
+		 * resources are only cached at all if caching is enabled by setting a cache duration.
 		 *
-		 * @param cacheScope
-		 *             if <code>true</code> public caches are allowed to cache the resource
+		 * @param scope
+		 *            scope for caching
 		 *
 		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#getCacheDuration()
-		 * @see org.apache.wicket.protocol.http.RequestUtils#enableCaching(org.apache.wicket.request.http.WebResponse, org.apache.wicket.util.time.Duration, org.apache.wicket.request.http.WebResponse.CacheScope)
+		 * @see org.apache.wicket.request.resource.AbstractResource.ResourceResponse#setCacheDuration(org.apache.wicket.util.time.Duration)
+		 * @see org.apache.wicket.request.http.WebResponse.CacheScope
 		 */
-		public void setCacheScope(WebResponse.CacheScope cacheScope)
+		public void setCacheScope(WebResponse.CacheScope scope)
 		{
-			this.cacheScope = cacheScope;
+			Args.notNull(scope, "scope");
+			this.cacheScope = scope;
 		}
 
 		/**
@@ -408,11 +411,11 @@ public abstract class AbstractResource i
 
 		if(duration.compareTo(Duration.NONE) > 0)
 		{
-			RequestUtils.enableCaching(response, duration, data.getCacheScope());
+			response.enableCaching(duration, data.getCacheScope());
 		}
 		else
 		{
-			RequestUtils.disableCaching(response);
+			response.disableCaching();
 		}
 	}