Refactored instantiation of helper classes: @Service instead of manual
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / UserCookieGenerator.java
1 package de.juplo.yourshouter;
2
3
4 import javax.servlet.http.Cookie;
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.http.HttpServletResponse;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import org.springframework.web.util.CookieGenerator;
11 import org.thymeleaf.util.StringUtils;
12
13
14 /**
15  * Utility class for managing the cookie that remembers the user.
16  *
17  * @author Kai Moritz
18  */
19 final class UserCookieGenerator
20 {
21   private final static Logger LOG =
22       LoggerFactory.getLogger(UserCookieGenerator.class);
23
24   public final static UserCookieGenerator INSTANCE = new UserCookieGenerator();
25
26
27   private final CookieGenerator generator = new CookieGenerator();
28
29
30   /**
31    * Constructs an instance of this class, using <code>user</code> as the
32    * cookie-name.
33    */
34   private UserCookieGenerator()
35   {
36     generator.setCookieName("user");
37   }
38
39
40   /**
41    * Creates a cookie with the name <code>user</code>, that stores the ID of
42    * the user for subsequent calls.
43    *
44    * @param user
45    *     The ID of the current user
46    * @param response
47    *     The {@link HttpServletResponse} to store the cookie in.
48    */
49   public void addCookie(String user, HttpServletResponse response)
50   {
51     LOG.debug("adding cookie {}={}", generator.getCookieName(), user);
52     generator.addCookie(response, user);
53   }
54
55   /**
56    * Removes the cookie with the name <code>user</code> by storing an empty
57    * string as its value.
58    *
59    * @param response
60    *     The {@link HttpServletResponse} to remove the cookie from.
61    */
62   public void removeCookie(HttpServletResponse response)
63   {
64     LOG.debug("removing cookie {}", generator.getCookieName());
65     generator.addCookie(response, "");
66   }
67
68   /**
69    * Reads the current value of the cookie with the name <code>user</code>.
70    *
71    * @param request
72    *   The {@link HttpServletRequest} to read the cookie-value from.
73    * @return
74    *   The value of the cookie with the name <code>user</code>, or
75    *   <code>null</code>, if no cookie by that name can be found or the value
76    *   of the cookie is an empty string.
77    */
78   public String readCookieValue(HttpServletRequest request)
79   {
80     String name = generator.getCookieName();
81     Cookie[] cookies = request.getCookies();
82     if (cookies != null)
83     {
84       for (Cookie cookie : cookies)
85       {
86         if (cookie.getName().equals(name))
87         {
88           String value = cookie.getValue();
89           if (!StringUtils.isEmptyOrWhitespace(value))
90           {
91             LOG.debug("found cookie {}={}", name, value);
92             return value;
93           }
94         }
95       }
96     }
97     LOG.debug("cookie \"{}\" not found!", name);
98     return null;
99   }
100 }