001 /*
002 * Databinder: a simple bridge from Wicket to Hibernate
003 * Copyright (C) 2008 Nathan Hamblen nathan@technically.us
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either
008 * version 2.1 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 * Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public
016 * License along with this library; if not, write to the Free Software
017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
018 */
019 package net.databinder;
020
021 import java.util.HashMap;
022 import java.util.Map;
023
024 import javax.servlet.http.Cookie;
025
026 import org.apache.wicket.Response;
027 import org.apache.wicket.protocol.http.WebApplication;
028 import org.apache.wicket.protocol.http.WebRequest;
029
030 /**
031 * Request cycle with cookie convenience methods that reflects removal immediately.
032 */
033 public class CookieRequestCycle extends ExceptionFilteringRequestCycle {
034 /** cache of cookies from request */
035 private Map<String, Cookie> cookies;
036
037 public CookieRequestCycle(WebApplication application, WebRequest request, Response response) {
038 super(application, request, response);
039 }
040
041 /**
042 * Return or build cache of cookies cookies from request.
043 */
044 protected Map<String, Cookie> getCookies() {
045 if (cookies == null) {
046 Cookie ary[] = ((WebRequest)getRequest()).getCookies();
047 cookies = new HashMap<String, Cookie>(ary == null ? 0 : ary.length);
048 if (ary != null)
049 for (Cookie c : ary)
050 cookies.put(c.getName(), c);
051 }
052 return cookies;
053 }
054
055 /**
056 * Retrieve cookie from request, so long as it hasn't been cleared. Cookies cleared by
057 * clearCookie() are still contained in the current request's cookie array, but this method
058 * will not return them.
059 * @param name cookie name
060 * @return cookie requested, or null if unavailable
061 */
062 public Cookie getCookie(String name) {
063 return getCookies().get(name);
064 }
065
066 /**
067 * Applies scope to cookies set by this application. Base implementation
068 * sets the path to / . Override if limiting scope to a path, or expanding
069 * it to a broader domain.
070 * @param cookie to have its scope set
071 */
072 public void applyScope(Cookie cookie) {
073 cookie.setPath("/");
074 }
075
076 /**
077 * Sets a new a cookie with an expiration time of zero to an clear an old one from the
078 * browser, and removes any copy from this request's cookie cache. Subsequent calls to
079 * <tt>getCookie(String name)</tt> during this request will not return a cookie of that name.
080 * @param name cookie name
081 */
082 public void clearCookie(String name) {
083 getCookies().remove(name);
084 Cookie empty = new Cookie(name, "");
085 applyScope(empty);
086 getWebResponse().clearCookie(empty);
087 }
088
089 }