View Javadoc

1   /*
2    * Copyright (2005-2009) Schibsted ASA
3    * This file is part of SESAT.
4    *
5    *   SESAT is free software: you can redistribute it and/or modify
6    *   it under the terms of the GNU Affero General Public License as published by
7    *   the Free Software Foundation, either version 3 of the License, or
8    *   (at your option) any later version.
9    *
10   *   SESAT is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *   GNU Affero General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Affero General Public License
16   *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  package no.sesat.search.user.service;
19  
20  import no.sesat.search.user.UserCookie;
21  import org.apache.log4j.Logger;
22  
23  import javax.servlet.http.Cookie;
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  import java.text.DateFormat;
27  import java.text.ParseException;
28  import java.util.Date;
29  
30  /**
31   * Utility class for user cookie handling.
32   *
33   * @version $Id: UserCookieUtil.java 23 2009-06-23 16:17:24Z ssmiweve $
34   */
35  public final class UserCookieUtil {
36  
37      // Constants -----------------------------------------------------
38  
39      private static final Logger LOG = Logger.getLogger(UserCookie.class);
40  
41      public static final String USER_COOKIE_PATH = "/";
42  
43      public static final String USER_LOGIN_COOKIE_KEY = "SesamUser";
44  
45      public static final String USER_UPDATE_COOKIE_KEY = "SesamUserUpdate";
46  
47      private static final String LOGIN_KEY_DEFAULT_VALUE = "0";
48  
49      public static final DateFormat TIMESTAMP_FORMATTER = DateFormat.getDateTimeInstance();
50  
51      // Attributes ----------------------------------------------------
52  
53      // Static --------------------------------------------------------
54  
55      // Constructors --------------------------------------------------
56  
57      /**
58       * Utility class should not be instantiated.
59       */
60      private UserCookieUtil() {
61          super();
62      }
63  
64      // Public --------------------------------------------------------
65  
66      /**
67       * Look for and return the user login cookie value. Can return null.
68       *
69       * @param request the http servlet request
70       * @return the cookie value that is found, null if no cookie
71       */
72      public static String getUserLoginCookie(final HttpServletRequest request) {
73          return getCookieValue(request, USER_LOGIN_COOKIE_KEY);
74      }
75  
76      /**
77       * Set the user login cookie value.
78       *
79       * @param response the http servlet response
80       * @param content the content of the user cookie
81       */
82      public static void setUserLoginCookie(final HttpServletResponse response, final String content) {
83          if (null != response) {
84              response.addCookie(createUserLoginCookie(content));
85          } else {
86              LOG.warn("No response when trying to set the user login cookie.");
87          }
88      }
89  
90      /**
91       * Set the default user login cookie, signals no login.
92       *
93       * @param response the http servlet response
94       */
95      public static void setUserLoginCookieDefault(final HttpServletResponse response) {
96          if (null != response) {
97              response.addCookie(createUserLoginCookie(LOGIN_KEY_DEFAULT_VALUE));
98          } else {
99              LOG.warn("No response when trying to set the user login cookie.");
100         }
101     }
102 
103     /**
104      * Look for and return the user update cookie timestamp. Can return null.
105      *
106      * @param request the http servlet request
107      * @return the timestamp that is found, null if no cookie
108      */
109     public static Date getUserUpdateCookie(final HttpServletRequest request) {
110         final String cookieValue = getCookieValue(request, USER_UPDATE_COOKIE_KEY);
111 
112         try {
113             return (cookieValue != null ? TIMESTAMP_FORMATTER.parse(cookieValue) : null);
114         } catch (final ParseException e) {
115             LOG.warn("Illegal user update cookie value: " + cookieValue);
116             return null;
117         }
118     }
119 
120     /**
121      * Set the user update cookie value.
122      *
123      * @param response the http servlet response
124      * @param timestamp the update timestamp
125      */
126     public static void setUserUpdateCookie(final HttpServletResponse response, final Date timestamp) {
127         if (null != response) {
128             response.addCookie(createUserUpdateCookie(timestamp));
129         } else {
130             LOG.warn("No response when trying to set the user update cookie.");
131         }
132     }
133 
134     /**
135      * Return the default value for login key, meaning that the user is not logged in.
136      *
137      * @return the default value for login key
138      */
139     public static String getLoginKeyDefaultValue() {
140         return LOGIN_KEY_DEFAULT_VALUE;
141     }
142 
143     public static String getCookieValue(final HttpServletRequest request, final String cookieName) {
144         if (null != request) {
145             if (null != request.getCookies()) {
146                 for (final Cookie c : request.getCookies()) {
147                     if (c.getName().equals(cookieName)) {
148                         return c.getValue();
149                     }
150                 }
151             }
152         } else {
153             LOG.warn("No request when trying to get the user login cookie.");
154         }
155 
156         return null;
157     }
158 
159     // Package protected ---------------------------------------------
160 
161     // Protected -----------------------------------------------------
162 
163     // Private -------------------------------------------------------
164 
165     private static Cookie createUserLoginCookie(final String content) {
166         final Cookie cookie = new Cookie(USER_LOGIN_COOKIE_KEY, content);
167         cookie.setPath(USER_COOKIE_PATH);
168         cookie.setMaxAge(Integer.MAX_VALUE);
169         return cookie;
170     }
171 
172     private static Cookie createUserUpdateCookie(final Date timestamp) {
173         final Cookie cookie = new Cookie(USER_UPDATE_COOKIE_KEY, TIMESTAMP_FORMATTER.format(timestamp));
174         cookie.setPath(USER_COOKIE_PATH);
175         cookie.setMaxAge(Integer.MAX_VALUE);
176         return cookie;
177     }
178 
179     // Inner classes -------------------------------------------------
180 
181 }