001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.CookieNotSupportedException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.CharPool;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.util.CookieUtil;
024
025 import javax.servlet.http.Cookie;
026 import javax.servlet.http.HttpServletRequest;
027 import javax.servlet.http.HttpServletResponse;
028
029 import org.apache.commons.codec.binary.Hex;
030
031
035 public class CookieKeys implements com.liferay.portal.kernel.util.CookieKeys {
036
037 public static void addCookie(
038 HttpServletRequest request, HttpServletResponse response,
039 Cookie cookie) {
040
041 addCookie(request, response, cookie, request.isSecure());
042 }
043
044 public static void addCookie(
045 HttpServletRequest request, HttpServletResponse response,
046 Cookie cookie, boolean secure) {
047
048 if (!PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES ||
049 PropsValues.TCK_URL) {
050
051 return;
052 }
053
054
055
056 String name = cookie.getName();
057
058 String originalValue = cookie.getValue();
059 String encodedValue = originalValue;
060
061 if (isEncodedCookie(name)) {
062 encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));
063
064 if (_log.isDebugEnabled()) {
065 _log.debug("Add encoded cookie " + name);
066 _log.debug("Original value " + originalValue);
067 _log.debug("Hex encoded value " + encodedValue);
068 }
069 }
070
071 cookie.setSecure(secure);
072 cookie.setValue(encodedValue);
073 cookie.setVersion(VERSION);
074
075
076
077
078 response.addCookie(cookie);
079 }
080
081 public static void addSupportCookie(
082 HttpServletRequest request, HttpServletResponse response) {
083
084 Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");
085
086 cookieSupportCookie.setPath(StringPool.SLASH);
087 cookieSupportCookie.setMaxAge(MAX_AGE);
088
089 addCookie(request, response, cookieSupportCookie);
090 }
091
092 public static String getCookie(HttpServletRequest request, String name) {
093 return getCookie(request, name, true);
094 }
095
096 public static String getCookie(
097 HttpServletRequest request, String name, boolean toUpperCase) {
098
099 String value = CookieUtil.get(request, name, toUpperCase);
100
101 if ((value != null) && isEncodedCookie(name)) {
102 try {
103 String encodedValue = value;
104 String originalValue = new String(
105 Hex.decodeHex(encodedValue.toCharArray()));
106
107 if (_log.isDebugEnabled()) {
108 _log.debug("Get encoded cookie " + name);
109 _log.debug("Hex encoded value " + encodedValue);
110 _log.debug("Original value " + originalValue);
111 }
112
113 return originalValue;
114 }
115 catch (Exception e) {
116 if (_log.isWarnEnabled()) {
117 _log.warn(e.getMessage());
118 }
119
120 return value;
121 }
122 }
123
124 return value;
125 }
126
127 public static String getDomain(HttpServletRequest request) {
128
129
130
131 if (Validator.isNotNull(PropsValues.SESSION_COOKIE_DOMAIN)) {
132 return PropsValues.SESSION_COOKIE_DOMAIN;
133 }
134
135 String host = request.getServerName();
136
137 return getDomain(host);
138 }
139
140 public static String getDomain(String host) {
141
142
143
144 if (host == null) {
145 return null;
146 }
147
148
149
150 if (Validator.isIPAddress(host)) {
151 return host;
152 }
153
154 int x = host.lastIndexOf(CharPool.PERIOD);
155
156 if (x <= 0) {
157 return null;
158 }
159
160 int y = host.lastIndexOf(CharPool.PERIOD, x - 1);
161
162 if (y <= 0) {
163 return StringPool.PERIOD + host;
164 }
165
166 int z = host.lastIndexOf(CharPool.PERIOD, y - 1);
167
168 String domain = null;
169
170 if (z <= 0) {
171 domain = host.substring(y);
172 }
173 else {
174 domain = host.substring(z);
175 }
176
177 return domain;
178 }
179
180 public static boolean hasSessionId(HttpServletRequest request) {
181 String jsessionid = getCookie(request, JSESSIONID, false);
182
183 if (jsessionid != null) {
184 return true;
185 }
186 else {
187 return false;
188 }
189 }
190
191 public static boolean isEncodedCookie(String name) {
192 if (name.equals(ID) || name.equals(LOGIN) || name.equals(PASSWORD) ||
193 name.equals(SCREEN_NAME)) {
194
195 return true;
196 }
197 else {
198 return false;
199 }
200 }
201
202 public static void validateSupportCookie(HttpServletRequest request)
203 throws CookieNotSupportedException {
204
205 if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES &&
206 PropsValues.SESSION_TEST_COOKIE_SUPPORT) {
207
208 String cookieSupport = getCookie(request, COOKIE_SUPPORT, false);
209
210 if (Validator.isNull(cookieSupport)) {
211 throw new CookieNotSupportedException();
212 }
213 }
214 }
215
216 public static final int MAX_AGE = 31536000;
217
218 public static final int VERSION = 0;
219
220 private static Log _log = LogFactoryUtil.getLog(CookieKeys.class);
221
222 }