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(HttpServletResponse res, Cookie cookie) {
70 if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES) {
71 if (!PropsValues.TCK_URL) {
72
73
75 String name = cookie.getName();
76
77 String originalValue = cookie.getValue();
78 String encodedValue = originalValue;
79
80 if (isEncodedCookie(name)) {
81 encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));
82
83 if (_log.isDebugEnabled()) {
84 _log.debug("Add encoded cookie " + name);
85 _log.debug("Original value " + originalValue);
86 _log.debug("Hex encoded value " + encodedValue);
87 }
88 }
89
90 cookie.setValue(encodedValue);
91 cookie.setVersion(VERSION);
92
93
96 res.addCookie(cookie);
97 }
98 }
99 }
100
101 public static void addSupportCookie(HttpServletResponse res) {
102 Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");
103
104 cookieSupportCookie.setPath(StringPool.SLASH);
105 cookieSupportCookie.setMaxAge(MAX_AGE);
106
107 addCookie(res, cookieSupportCookie);
108 }
109
110 public static String getCookie(HttpServletRequest req, String name) {
111 String value = CookieUtil.get(req, name);
112
113 if ((value != null) && isEncodedCookie(name)) {
114 try {
115 String encodedValue = value;
116 String originalValue = new String(
117 Hex.decodeHex(encodedValue.toCharArray()));
118
119 if (_log.isDebugEnabled()) {
120 _log.debug("Get encoded cookie " + name);
121 _log.debug("Hex encoded value " + encodedValue);
122 _log.debug("Original value " + originalValue);
123 }
124
125 return originalValue;
126 }
127 catch (Exception e) {
128 if (_log.isWarnEnabled()) {
129 _log.warn(e.getMessage());
130 }
131
132 return value;
133 }
134 }
135
136 return value;
137 }
138
139 public static String getDomain(HttpServletRequest req) {
140
141
143 if (Validator.isNotNull(PropsValues.SESSION_COOKIE_DOMAIN)) {
144 return PropsValues.SESSION_COOKIE_DOMAIN;
145 }
146
147 String host = req.getServerName();
148
149 return getDomain(host);
150 }
151
152 public static String getDomain(String host) {
153
154
156 if (host == null) {
157 return null;
158 }
159
160 int x = host.lastIndexOf(StringPool.PERIOD);
161
162 if (x <= 0) {
163 return null;
164 }
165
166 int y = host.lastIndexOf(StringPool.PERIOD, x - 1);
167
168 if (y <= 0) {
169 return StringPool.PERIOD + host;
170 }
171
172 int z = host.lastIndexOf(StringPool.PERIOD, y - 1);
173
174 String domain = null;
175
176 if (z <= 0) {
177 domain = host.substring(y);
178 }
179 else {
180 domain = host.substring(z);
181 }
182
183 return domain;
184 }
185
186 public static boolean hasSessionId(HttpServletRequest req) {
187 String jsessionid = getCookie(req, JSESSIONID);
188
189 if (jsessionid != null) {
190 return true;
191 }
192 else {
193 return false;
194 }
195 }
196
197 public static boolean isEncodedCookie(String name) {
198 if (name.equals(ID) || name.equals(LOGIN) || name.equals(PASSWORD) ||
199 name.equals(SCREEN_NAME)) {
200
201 return true;
202 }
203 else {
204 return false;
205 }
206 }
207
208 public static void validateSupportCookie(HttpServletRequest req)
209 throws CookieNotSupportedException {
210
211 if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES &&
212 PropsValues.SESSION_TEST_COOKIE_SUPPORT) {
213
214 String cookieSupport = getCookie(req, COOKIE_SUPPORT);
215
216 if (Validator.isNull(cookieSupport)) {
217 throw new CookieNotSupportedException();
218 }
219 }
220 }
221
222 private static Log _log = LogFactory.getLog(CookieKeys.class);
223
224 }