You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jc...@apache.org on 2007/09/06 17:12:09 UTC

svn commit: r573292 - in /wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket: markup/html/form/persistence/CookieValuePersister.java protocol/http/WebRequest.java

Author: jcompagner
Date: Thu Sep  6 08:12:09 2007
New Revision: 573292

URL: http://svn.apache.org/viewvc?rev=573292&view=rev
Log:
extra helper method for cookies in WebRequest, its quite annoying that you have to do iterate it everytime on every place..

Modified:
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/persistence/CookieValuePersister.java
    wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/WebRequest.java

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/persistence/CookieValuePersister.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/persistence/CookieValuePersister.java?rev=573292&r1=573291&r2=573292&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/persistence/CookieValuePersister.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/markup/html/form/persistence/CookieValuePersister.java Thu Sep  6 08:12:09 2007
@@ -36,7 +36,7 @@
 public class CookieValuePersister implements IValuePersister
 {
 	private static final long serialVersionUID = 1L;
-	
+
 	/** Logging */
 	private final static Logger log = LoggerFactory.getLogger(CookieValuePersister.class);
 
@@ -50,9 +50,9 @@
 		{
 			clear(cookie);
 			if (log.isDebugEnabled())
-            {
+			{
 				log.debug("Cookie for " + component + " removed");
-            }
+			}
 		}
 	}
 
@@ -82,22 +82,23 @@
 		final String value = component.getValue();
 
 		Cookie cookie = getCookie(component);
-		if (cookie == null) 
+		if (cookie == null)
 		{
 			cookie = new Cookie(name, value == null ? "" : value);
 		}
-		else 
+		else
 		{
 			cookie.setValue(value == null ? "" : value);
 		}
 		cookie.setSecure(false);
 		cookie.setMaxAge(getSettings().getMaxAge());
-		
+
 		save(cookie);
 	}
-	
+
 	/**
-	 * @param component Component to get name for
+	 * @param component
+	 *            Component to get name for
 	 * @return The name of the component.
 	 */
 	protected String getName(final FormComponent component)
@@ -106,8 +107,8 @@
 	}
 
 	/**
-	 * Convenience method for deleting a cookie by name. Delete the cookie by
-	 * setting its maximum age to zero.
+	 * Convenience method for deleting a cookie by name. Delete the cookie by setting its maximum
+	 * age to zero.
 	 * 
 	 * @param cookie
 	 *            The cookie to delete
@@ -133,18 +134,18 @@
 	 */
 	private String cookieToDebugString(final Cookie cookie)
 	{
-		return "[Cookie " + " name = " + cookie.getName() + ", value = " + cookie.getValue()
-				+ ", domain = " + cookie.getDomain() + ", path = " + cookie.getPath()
-				+ ", maxAge = " + Time.valueOf(cookie.getMaxAge()).toDateString() + "("
-				+ cookie.getMaxAge() + ")" + "]";
+		return "[Cookie " + " name = " + cookie.getName() + ", value = " + cookie.getValue() +
+				", domain = " + cookie.getDomain() + ", path = " + cookie.getPath() +
+				", maxAge = " + Time.valueOf(cookie.getMaxAge()).toDateString() + "(" +
+				cookie.getMaxAge() + ")" + "]";
 	}
 
 	/**
-	 * Gets the cookie for a given persistent form component. The name of the
-	 * cookie will be the component's page relative path (@see
-	 * org.apache.wicket.markup.html.form.FormComponent#getPageRelativePath()). Be reminded
-	 * that only if the cookie data have been provided by the client (browser),
-	 * they'll be accessible by the server.
+	 * Gets the cookie for a given persistent form component. The name of the cookie will be the
+	 * component's page relative path (@see
+	 * org.apache.wicket.markup.html.form.FormComponent#getPageRelativePath()). Be reminded that
+	 * only if the cookie data have been provided by the client (browser), they'll be accessible by
+	 * the server.
 	 * 
 	 * @param component
 	 *            The form component
@@ -155,59 +156,51 @@
 		// Gets the cookie's name
 		final String name = getName(component);
 
-		// Get all cookies attached to the Request by the client browser
-		Cookie[] cookies = getCookies();
-		if (cookies != null)
+		// Get the cookie attached to the Request by the client browser
+		Cookie cookie = getCookie(name);
+		if (cookie != null)
 		{
-			for (int i = 0; i < cookies.length; i++)
+			// cookie with no value do me no good!
+			if (cookie.getValue() != null && cookie.getValue().length() > 0)
 			{
-				Cookie cookie = cookies[i];
-
-				// Names must match and Value must not be empty
-				if (cookie.getName().equals(name))
+				if (log.isDebugEnabled())
 				{
-					// cookies with no value do me no good!
-					if (cookie.getValue() != null && cookie.getValue().length() > 0)
-					{
-						if (log.isDebugEnabled())
-						{
-							log.debug("Got cookie: " + cookieToDebugString(cookie));
-						}
-						return cookie;
-					}
-					else
-					{
-						if (log.isDebugEnabled())
-						{
-							log.debug("Got cookie " + name
-									+ ", but it had no value; returning null");
-						}
-					}
+					log.debug("Got cookie: " + cookieToDebugString(cookie));
+				}
+				return cookie;
+			}
+			else
+			{
+				if (log.isDebugEnabled())
+				{
+					log.debug("Got cookie " + name + ", but it had no value; returning null");
 				}
 			}
 		}
-
 		return null;
 	}
-    
-    /**
-     * Gets any cookies for request.
-     * 
-     * @return Any cookies for this request
-     */
-    private Cookie[] getCookies()
-    {
-        try
-        {
-            return getWebRequest().getCookies();
-        }
-        catch (NullPointerException ex)
-        {
-            // Ignore any app server problem here
-        }
 
-        return new Cookie[0];
-    }
+	/**
+	 * Gets any cookies for request.
+	 * 
+	 * @param name
+	 *            The name of the cookie to be looked up
+	 * 
+	 * @return Any cookies for this request
+	 */
+	private Cookie getCookie(String name)
+	{
+		try
+		{
+			return getWebRequest().getCookie(name);
+		}
+		catch (NullPointerException ex)
+		{
+			// Ignore any app server problem here
+		}
+
+		return null;
+	}
 
 	/**
 	 * Persister defaults are maintained centrally by the Application.
@@ -216,7 +209,8 @@
 	 */
 	private CookieValuePersisterSettings getSettings()
 	{
-		return RequestCycle.get().getApplication().getSecuritySettings().getCookieValuePersisterSettings();
+		return RequestCycle.get().getApplication().getSecuritySettings()
+				.getCookieValuePersisterSettings();
 	}
 
 	/**
@@ -252,34 +246,34 @@
 		{
 			return null;
 		}
-        else
-        {
-            final String comment = getSettings().getComment();
-    		if (comment != null)
-    		{
-    			cookie.setComment(comment);
-    		}
-    
-            final String domain = getSettings().getDomain();
-    		if (domain != null)
-    		{
-    			cookie.setDomain(domain);
-    		}
-    
-    		cookie.setPath("/");
-			//cookie.setPath(getWebRequest().getContextPath());
-
-    		cookie.setVersion(getSettings().getVersion());
-    		cookie.setSecure(getSettings().getSecure());
-
-    		getWebResponse().addCookie(cookie);
-    
-    		if (log.isDebugEnabled())
-    		{
-    			log.debug("saved: " + cookieToDebugString(cookie));
-    		}
-    
-    		return cookie;
-        }
+		else
+		{
+			final String comment = getSettings().getComment();
+			if (comment != null)
+			{
+				cookie.setComment(comment);
+			}
+
+			final String domain = getSettings().getDomain();
+			if (domain != null)
+			{
+				cookie.setDomain(domain);
+			}
+
+			cookie.setPath("/");
+			// cookie.setPath(getWebRequest().getContextPath());
+
+			cookie.setVersion(getSettings().getVersion());
+			cookie.setSecure(getSettings().getSecure());
+
+			getWebResponse().addCookie(cookie);
+
+			if (log.isDebugEnabled())
+			{
+				log.debug("saved: " + cookieToDebugString(cookie));
+			}
+
+			return cookie;
+		}
 	}
 }

Modified: wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/WebRequest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/WebRequest.java?rev=573292&r1=573291&r2=573292&view=diff
==============================================================================
--- wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/WebRequest.java (original)
+++ wicket/trunk/jdk-1.4/wicket/src/main/java/org/apache/wicket/protocol/http/WebRequest.java Thu Sep  6 08:12:09 2007
@@ -27,10 +27,10 @@
 
 
 /**
- * Subclass of Request for HTTP protocol requests which holds an underlying
- * HttpServletRequest object. A variety of convenience methods are available
- * that operate on the HttpServletRequest object. These methods do things such
- * as providing access to parameters, cookies, URLs and path information.
+ * Subclass of Request for HTTP protocol requests which holds an underlying HttpServletRequest
+ * object. A variety of convenience methods are available that operate on the HttpServletRequest
+ * object. These methods do things such as providing access to parameters, cookies, URLs and path
+ * information.
  * 
  * @author Jonathan Locke
  */
@@ -47,13 +47,36 @@
 	}
 
 	/**
+	 * Get the requests' cookie by name
+	 * 
+	 * @param name
+	 *            The name of the cookie to be looked up
+	 * 
+	 * @return A cookie, null if not found.
+	 */
+	public Cookie getCookie(String name)
+	{
+		Cookie[] cookies = getCookies();
+		if (cookies != null && cookies.length > 0)
+		{
+			for (int i = 0; i < cookies.length; i++)
+			{
+				if (cookies[i].getName().equals(name))
+				{
+					return cookies[i];
+				}
+			}
+		}
+		return null;
+	}
+
+	/**
 	 * Gets the wrapped http servlet request object.
 	 * <p>
-	 * WARNING: it is usually a bad idea to depend on the http servlet request
-	 * directly. Please use the classes and methods that are exposed by Wicket
-	 * (such as {@link org.apache.wicket.Session} instead. Send an email to the mailing
-	 * list in case it is not clear how to do things or you think you miss
-	 * functionality which causes you to depend on this directly.
+	 * WARNING: it is usually a bad idea to depend on the http servlet request directly. Please use
+	 * the classes and methods that are exposed by Wicket (such as {@link org.apache.wicket.Session}
+	 * instead. Send an email to the mailing list in case it is not clear how to do things or you
+	 * think you miss functionality which causes you to depend on this directly.
 	 * </p>
 	 * 
 	 * @return the wrapped http serlvet request object.
@@ -61,10 +84,9 @@
 	public abstract HttpServletRequest getHttpServletRequest();
 
 	/**
-	 * Returns the preferred <code>Locale</code> that the client will accept
-	 * content in, based on the Accept-Language header. If the client request
-	 * doesn't provide an Accept-Language header, this method returns the
-	 * default locale for the server.
+	 * Returns the preferred <code>Locale</code> that the client will accept content in, based on
+	 * the Accept-Language header. If the client request doesn't provide an Accept-Language header,
+	 * this method returns the default locale for the server.
 	 * 
 	 * @return the preferred <code>Locale</code> for the client
 	 */
@@ -103,15 +125,15 @@
 	public abstract String getServletPath();
 
 	/**
-	 * Create a runtime context type specific (e.g. Servlet or Portlet)
-	 * MultipartWebRequest wrapper for handling multipart content uploads.
+	 * Create a runtime context type specific (e.g. Servlet or Portlet) MultipartWebRequest wrapper
+	 * for handling multipart content uploads.
 	 * 
 	 * @param maxSize
 	 *            the maximum size this request may be
 	 * @return new WebRequest wrapper implementing MultipartWebRequest
 	 */
 	public abstract WebRequest newMultipartWebRequest(Bytes maxSize);
-	
+
 	/**
 	 * Is the request an ajax request?
 	 *