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