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/02 15:58:12 UTC

svn commit: r991937 - in /wicket/trunk/wicket/src/main: disabled/org/apache/wicket/markup/html/ java/org/apache/wicket/ajax/ java/org/apache/wicket/markup/html/ java/org/apache/wicket/protocol/http/ java/org/apache/wicket/protocol/http/servlet/ java/or...

Author: pete
Date: Thu Sep  2 13:58:12 2010
New Revision: 991937

URL: http://svn.apache.org/viewvc?rev=991937&view=rev
Log:
Unify deactivation of HTTP caching: set directives for HTTP/1.0 (Pragma) and HTTP/1.1 (Cache). Do not use 'Cache:must-revalidate' as it implies the resource can basically be cached. Do not use 'Cache:no-store' as it's superseded by 'Cache:no-cache'. Disable caching for server-side redirects (http status = 302 temporary moved) otherwise there is a chance that redirects get cached in proxies when using very aggressive caching. Set rfc-recommended 'Date' header and 'Expires'.

Modified:
    wicket/trunk/wicket/src/main/disabled/org/apache/wicket/markup/html/WebResource.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/src/main/disabled/org/apache/wicket/markup/html/WebResource.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/disabled/org/apache/wicket/markup/html/WebResource.java?rev=991937&r1=991936&r2=991937&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/disabled/org/apache/wicket/markup/html/WebResource.java (original)
+++ wicket/trunk/wicket/src/main/disabled/org/apache/wicket/markup/html/WebResource.java Thu Sep  2 13:58:12 2010
@@ -19,6 +19,7 @@ package org.apache.wicket.markup.html;
 import org.apache.wicket.Application;
 import org.apache.wicket.Resource;
 import org.apache.wicket.Response;
+import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.protocol.http.WebResponse;
 
 /**
@@ -77,7 +78,7 @@ public abstract class WebResource extend
 		}
 		else
 		{
-			response.setHeader("Cache-Control", "no-cache, must-revalidate");
+			RequestUtils.disableCaching(response);
 		}
 	}
 

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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -38,6 +38,7 @@ import org.apache.wicket.markup.html.int
 import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
 import org.apache.wicket.markup.parser.filter.HtmlHeaderSectionHandler;
 import org.apache.wicket.markup.repeater.AbstractRepeater;
+import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.request.IRequestCycle;
 import org.apache.wicket.request.IRequestHandler;
 import org.apache.wicket.request.Response;
@@ -567,9 +568,7 @@ public class AjaxRequestTarget implement
 		response.setContentType("text/xml; charset=" + encoding);
 
 		// Make sure it is not cached by a client
-		response.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
-		response.setHeader("Cache-Control", "no-cache, must-revalidate");
-		response.setHeader("Pragma", "no-cache");
+		RequestUtils.disableCaching(response);
 
 		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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -25,6 +25,7 @@ import org.apache.wicket.markup.html.int
 import org.apache.wicket.markup.html.link.BookmarkablePageLink;
 import org.apache.wicket.markup.parser.filter.HtmlHeaderSectionHandler;
 import org.apache.wicket.model.IModel;
+import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.request.IRequestHandler;
 import org.apache.wicket.request.Response;
 import org.apache.wicket.request.handler.IPageRequestHandler;
@@ -51,7 +52,7 @@ import org.slf4j.LoggerFactory;
  * a no-arg constructor or with a constructor that accepts a PageParameters argument (which wraps
  * any query string parameters for a request). In case the page has both constructors, the
  * constructor with PageParameters will be used.
- * 
+ *
  * @author Jonathan Locke
  * @author Eelco Hillenius
  * @author Juergen Donnerstag
@@ -79,7 +80,7 @@ public class WebPage extends Page implem
 
 		/**
 		 * Construct.
-		 * 
+		 *
 		 * @param webPage
 		 */
 		PageMapChecker(WebPage webPage)
@@ -129,10 +130,10 @@ public class WebPage extends Page implem
 	 * Note that nothing is done with the page parameters argument. This constructor is provided so
 	 * that tools such as IDEs will include it their list of suggested constructors for derived
 	 * classes.
-	 * 
+	 *
 	 * Please call this constructor (or the one with the pagemap) if you want to remember the
 	 * pageparameters {@link #getPageParameters()}. So that they are reused for stateless links.
-	 * 
+	 *
 	 * @param parameters
 	 *            Wrapped query string parameters.
 	 */
@@ -147,7 +148,7 @@ public class WebPage extends Page implem
 	 * markup language, such as VXML, would require the creation of a different Page subclass in an
 	 * appropriate package under org.apache.wicket.markup. To support VXML (voice markup), one might
 	 * create the package org.apache.wicket.markup.vxml and a subclass of Page called VoicePage.
-	 * 
+	 *
 	 * @return Markup type for HTML
 	 */
 	@Override
@@ -192,28 +193,29 @@ public class WebPage extends Page implem
 	}
 
 	/**
-	 * Subclasses can override this to set there headers when the Page is being served. By default 2
-	 * headers will be set
-	 * 
+	 * Subclasses can override this to set there headers when the Page is being served.
+	 * By default these headers are set:
+	 *
 	 * <pre>
+	 * response.setHeader(&quot;Date&quot;, &quot;[now]&quot;);
+	 * response.setHeader(&quot;Expires&quot;, &quot;[0]&quot;);
 	 * response.setHeader(&quot;Pragma&quot;, &quot;no-cache&quot;);
-	 * response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache, max-age=0, must-revalidate&quot;);
+	 * response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache&quot;);
 	 * </pre>
-	 * 
+	 *
 	 * So if a Page wants to control this or doesn't want to set this info it should override this
 	 * method and don't call super.
-	 * 
+	 *
 	 * @param response
 	 *            The WebResponse where set(Date)Header can be called on.
 	 */
 	protected void setHeaders(WebResponse response)
 	{
-		response.setHeader("Pragma", "no-cache");
-		response.setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate"); // no-store
+		RequestUtils.disableCaching(response);
 	}
 
 	/**
-	 * 
+	 *
 	 * @see org.apache.wicket.Component#onAfterRender()
 	 */
 	@Override
@@ -317,7 +319,7 @@ public class WebPage extends Page implem
 
 	/**
 	 * Creates and returns a bookmarkable link to this application's home page.
-	 * 
+	 *
 	 * @param id
 	 *            Name of link
 	 * @return Link to home page for this application

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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -26,6 +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.string.Strings;
 
@@ -46,20 +47,18 @@ public final class RequestUtils
 	 */
 	public static void decodeParameters(String queryString, PageParameters params)
 	{
-		final String[] paramTuples = queryString.split("&");
-		for (int t = 0; t < paramTuples.length; t++)
+		for (String paramTuple : Strings.split(queryString, '&'))
 		{
-			final String[] bits = paramTuples[t].split("=");
+			final String[] bits = Strings.split(paramTuple, '=');
+
 			if (bits.length == 2)
 			{
-				params.addNamedParameter(UrlDecoder.QUERY_INSTANCE.decode(bits[0],
-					getCurrentCharset()), UrlDecoder.QUERY_INSTANCE.decode(bits[1],
-					getCurrentCharset()));
+				params.addNamedParameter(UrlDecoder.QUERY_INSTANCE.decode(bits[0], getCurrentCharset()),
+				                         UrlDecoder.QUERY_INSTANCE.decode(bits[1], getCurrentCharset()));
 			}
 			else
 			{
-				params.addNamedParameter(UrlDecoder.QUERY_INSTANCE.decode(bits[0],
-					getCurrentCharset()), "");
+				params.addNamedParameter(UrlDecoder.QUERY_INSTANCE.decode(bits[0], getCurrentCharset()), "");
 			}
 		}
 	}
@@ -134,7 +133,7 @@ public final class RequestUtils
 				}
 			}
 		}
-		String newpath = Strings.join("/", newcomponents.toArray(new String[0]));
+		String newpath = Strings.join("/", newcomponents.toArray(new String[newcomponents.size()]));
 		if (path.endsWith("/"))
 		{
 			return newpath + "/";
@@ -159,7 +158,7 @@ public final class RequestUtils
 	 *            path, relative to requestPath
 	 * @return absolute path for given url
 	 */
-	public final static String toAbsolutePath(final String requestPath, String relativePagePath)
+	public static String toAbsolutePath(final String requestPath, String relativePagePath)
 	{
 		final StringBuffer result;
 		if (requestPath.endsWith("/"))
@@ -245,4 +244,22 @@ public final class RequestUtils
 		}
 		return Charset.forName(charsetName);
 	}
+
+	/**
+	 * set all required headers to disable caching
+	 *
+	 * "Pragma" is required for older browsers only supporting HTTP 1.0.
+	 * "Cache" is recommended for HTTP 1.1.
+	 * "Expires" additionally sets the content expiry in the past which effectively prohibits caching.
+	 * "Date" is recommended in general
+	 *
+	 * @param response web response
+	 */
+	public static void disableCaching(WebResponse response)
+	{
+		response.setDateHeader("Date", System.currentTimeMillis());
+		response.setDateHeader("Expires", 0);
+		response.setHeader("Pragma", "no-cache");
+		response.setHeader("Cache-Control", "no-cache");
+	}
 }

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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -223,12 +223,24 @@ public class ServletWebResponse extends 
 	@Override
 	public void sendRedirect(String url)
 	{
+		sendRedirect(url, false);
+	}
+
+	private void sendRedirect(String url, boolean cacheable)
+	{
 		redirect = true;
 		url = getAbsoluteURL(url);
 		url = httpServletResponse.encodeRedirectURL(url);
 
 		try
 		{
+			// proxies eventually cache '302 temporary redirect' responses:
+			// for most wicket use cases this is fatal since redirects are
+			// usually highly dynamic and can not be statically mapped
+			// to a request url in general
+			if (cacheable == false)
+				RequestUtils.disableCaching(this);
+
 			httpServletResponse.sendRedirect(url);
 		}
 		catch (IOException e)

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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -17,6 +17,7 @@
 package org.apache.wicket.request.handler;
 
 import org.apache.wicket.Application;
+import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.request.IRequestCycle;
 import org.apache.wicket.request.IRequestHandler;
 import org.apache.wicket.request.http.WebResponse;
@@ -65,9 +66,7 @@ public final class EmptyAjaxRequestHandl
 		response.setContentType("text/xml; charset=" + encoding);
 
 		// Make sure it is not cached by a client
-		response.setDateHeader("Expires", System.currentTimeMillis());
-		response.setHeader("Cache-Control", "no-cache, must-revalidate");
-		response.setHeader("Pragma", "no-cache");
+		RequestUtils.disableCaching(response);
 
 		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=991937&r1=991936&r2=991937&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 Thu Sep  2 13:58:12 2010
@@ -25,6 +25,7 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.wicket.Application;
 import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.protocol.http.RequestUtils;
 import org.apache.wicket.request.Response;
 import org.apache.wicket.request.http.WebRequest;
 import org.apache.wicket.request.http.WebResponse;
@@ -352,13 +353,13 @@ public abstract class AbstractResource i
 	protected void configureCache(final WebRequest request, final WebResponse response,
 		final ResourceResponse data, final Attributes attributes)
 	{
-		long now = System.currentTimeMillis();
-
-		// Time of message generation
-		response.setDateHeader("Date", now);
-
 		if (data.isCacheable())
 		{
+			long now = System.currentTimeMillis();
+
+			// Time of message generation
+			response.setDateHeader("Date", now);
+
 			// Time for cache expiry
 			response.setDateHeader("Expires", now + (data.getCacheDuration() * 1000L));
 
@@ -371,7 +372,7 @@ public abstract class AbstractResource i
 		}
 		else
 		{
-			response.setHeader(CACHE_CONTROL, "no-cache, must-revalidate");
+			RequestUtils.disableCaching(response);
 		}
 	}