1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
32
33
34
35 public final class UserCookieUtil {
36
37
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
52
53
54
55
56
57
58
59
60 private UserCookieUtil() {
61 super();
62 }
63
64
65
66
67
68
69
70
71
72 public static String getUserLoginCookie(final HttpServletRequest request) {
73 return getCookieValue(request, USER_LOGIN_COOKIE_KEY);
74 }
75
76
77
78
79
80
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
92
93
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
105
106
107
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
122
123
124
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
136
137
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
160
161
162
163
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
180
181 }