1
22
23 package com.liferay.portal.util;
24
25 import com.liferay.portal.CookieNotSupportedException;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.util.CookieUtil;
29
30 import javax.servlet.http.Cookie;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.apache.commons.codec.binary.Hex;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
45 public class CookieKeys {
46
47 public static final String COOKIE_SUPPORT = "COOKIE_SUPPORT";
48
49 public static final String COMPANY_ID = "COMPANY_ID";
50
51 public static final String GUEST_LANGUAGE_ID = "GUEST_LANGUAGE_ID";
52
53 public static final String ID = "ID";
54
55 public static final String JSESSIONID = "jsessionid";
56
57 public static final String LOGIN = "LOGIN";
58
59 public static final String PASSWORD = "PASSWORD";
60
61 public static final String REMEMBER_ME = "REMEMBER_ME";
62
63 public static final String SCREEN_NAME = "SCREEN_NAME";
64
65 public static final int MAX_AGE = 31536000;
66
67 public static final int VERSION = 0;
68
69 public static void addCookie(
70 HttpServletRequest request, HttpServletResponse response,
71 Cookie cookie) {
72
73 if (!PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES ||
74 PropsValues.TCK_URL) {
75
76 return;
77 }
78
79
81 String name = cookie.getName();
82
83 String originalValue = cookie.getValue();
84 String encodedValue = originalValue;
85
86 if (isEncodedCookie(name)) {
87 encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));
88
89 if (_log.isDebugEnabled()) {
90 _log.debug("Add encoded cookie " + name);
91 _log.debug("Original value " + originalValue);
92 _log.debug("Hex encoded value " + encodedValue);
93 }
94 }
95
96 cookie.setSecure(request.isSecure());
97 cookie.setValue(encodedValue);
98 cookie.setVersion(VERSION);
99
100
103 response.addCookie(cookie);
104 }
105
106 public static void addSupportCookie(
107 HttpServletRequest request, HttpServletResponse response) {
108
109 Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");
110
111 cookieSupportCookie.setPath(StringPool.SLASH);
112 cookieSupportCookie.setMaxAge(MAX_AGE);
113
114 addCookie(request, response, cookieSupportCookie);
115 }
116
117 public static String getCookie(HttpServletRequest request, String name) {
118 String value = CookieUtil.get(request, name);
119
120 if ((value != null) && isEncodedCookie(name)) {
121 try {
122 String encodedValue = value;
123 String originalValue = new String(
124 Hex.decodeHex(encodedValue.toCharArray()));
125
126 if (_log.isDebugEnabled()) {
127 _log.debug("Get encoded cookie " + name);
128 _log.debug("Hex encoded value " + encodedValue);
129 _log.debug("Original value " + originalValue);
130 }
131
132 return originalValue;
133 }
134 catch (Exception e) {
135 if (_log.isWarnEnabled()) {
136 _log.warn(e.getMessage());
137 }
138
139 return value;
140 }
141 }
142
143 return value;
144 }
145
146 public static String getDomain(HttpServletRequest request) {
147
148
150 if (Validator.isNotNull(PropsValues.SESSION_COOKIE_DOMAIN)) {
151 return PropsValues.SESSION_COOKIE_DOMAIN;
152 }
153
154 String host = request.getServerName();
155
156 return getDomain(host);
157 }
158
159 public static String getDomain(String host) {
160
161
163 if (host == null) {
164 return null;
165 }
166
167
169 if (Validator.isIPAddress(host)) {
170 return host;
171 }
172
173 int x = host.lastIndexOf(StringPool.PERIOD);
174
175 if (x <= 0) {
176 return null;
177 }
178
179 int y = host.lastIndexOf(StringPool.PERIOD, x - 1);
180
181 if (y <= 0) {
182 return StringPool.PERIOD + host;
183 }
184
185 int z = host.lastIndexOf(StringPool.PERIOD, y - 1);
186
187 String domain = null;
188
189 if (z <= 0) {
190 domain = host.substring(y);
191 }
192 else {
193 domain = host.substring(z);
194 }
195
196 return domain;
197 }
198
199 public static boolean hasSessionId(HttpServletRequest request) {
200 String jsessionid = getCookie(request, JSESSIONID);
201
202 if (jsessionid != null) {
203 return true;
204 }
205 else {
206 return false;
207 }
208 }
209
210 public static boolean isEncodedCookie(String name) {
211 if (name.equals(ID) || name.equals(LOGIN) || name.equals(PASSWORD) ||
212 name.equals(SCREEN_NAME)) {
213
214 return true;
215 }
216 else {
217 return false;
218 }
219 }
220
221 public static void validateSupportCookie(HttpServletRequest request)
222 throws CookieNotSupportedException {
223
224 if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES &&
225 PropsValues.SESSION_TEST_COOKIE_SUPPORT) {
226
227 String cookieSupport = getCookie(request, COOKIE_SUPPORT);
228
229 if (Validator.isNull(cookieSupport)) {
230 throw new CookieNotSupportedException();
231 }
232 }
233 }
234
235 private static Log _log = LogFactory.getLog(CookieKeys.class);
236
237 }