1   package com.atlassian.seraph.cookie;
2   
3   import com.atlassian.seraph.config.SecurityConfigFactory;
4   import org.apache.log4j.Logger;
5   
6   import javax.servlet.http.Cookie;
7   import javax.servlet.http.HttpServletRequest;
8   import javax.servlet.http.HttpServletResponse;
9   
10  /**
11   *
12   * @deprecated replaced by the {@link com.atlassian.seraph.service.rememberme.RememberMeService} code
13   */
14  public class DefaultCookieHandler implements CookieHandler
15  {
16      private static final Logger log = Logger.getLogger(DefaultCookieHandler.class);
17  
18      public void invalidateCookie(HttpServletRequest request, HttpServletResponse response, String cookieName,
19              String path)
20      {
21          if (log.isDebugEnabled())
22          {
23              log.debug("invalidateCookie " + cookieName + " for path " + path);
24          }
25          setCookie(request, response, cookieName, null, 0, path);
26      }
27  
28      public Cookie getCookie(HttpServletRequest request, String name)
29      {
30          final boolean dbg = log.isDebugEnabled();
31          if (dbg) {
32              log.debug("Looking for a cookie named : '" + name + "'");
33          }
34          final Cookie cookies[] = request.getCookies();
35          if (cookies == null || name == null || name.length() == 0)
36          {
37              if (dbg)
38              {
39                  if (cookies == null) {
40                      log.debug("The Cookies array in the HTTP request is null");
41                  }
42              }
43              return null;
44          }
45          //Otherwise, we have to do a linear scan for the cookie.
46          for (int i = 0; i < cookies.length; i++)
47          {
48              if (cookies[i].getName().equals(name))
49              {
50                  return cookies[i];
51              }
52          }
53          if (dbg)
54          {
55              log.debug("No cookie was found with name :" + name);
56          }
57          return null;
58      }
59  
60      public Cookie setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
61              int maxAge, String path)
62      {
63          final boolean isInSecureCookie = SecurityConfigFactory.getInstance().isInsecureCookie();
64          if (log.isDebugEnabled())
65          {
66              log.debug("setCookie " + name + ":" + value + " path : " + path + " maxAge : " + maxAge + (isInSecureCookie ? " insecure" : " secure"));
67          }
68          Cookie cookie = new Cookie(name, value);
69          cookie.setMaxAge(maxAge);
70          cookie.setPath(path);
71          if (!isInSecureCookie)
72          {
73              cookie.setSecure(request.isSecure());
74          }
75  
76          response.addCookie(cookie);
77  
78          return cookie;
79      }
80  
81      public String getCookieValue(HttpServletRequest request, String name)
82      {
83          Cookie cookie = getCookie(request, name);
84          if (cookie != null)
85          {
86              return cookie.getValue();
87          }
88          return null;
89      }
90  }