001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.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    
021    import java.util.Collections;
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import javax.servlet.http.Cookie;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Minhchau Dang
032     */
033    public class CookieKeys {
034    
035            public static final String COMPANY_ID = "COMPANY_ID";
036    
037            public static final String COOKIE_SUPPORT = "COOKIE_SUPPORT";
038    
039            public static final String GUEST_LANGUAGE_ID = "GUEST_LANGUAGE_ID";
040    
041            public static final String ID = "ID";
042    
043            public static final String JSESSIONID = "JSESSIONID";
044    
045            public static final String LOGIN = "LOGIN";
046    
047            public static final int MAX_AGE =(int)Time.YEAR;
048    
049            public static final String PASSWORD = "PASSWORD";
050    
051            public static final String REMEMBER_ME = "REMEMBER_ME";
052    
053            public static final String SCREEN_NAME = "SCREEN_NAME";
054    
055            public static final String USER_UUID = "USER_UUID";
056    
057            public static void addCookie(
058                    HttpServletRequest request, HttpServletResponse response,
059                    Cookie cookie) {
060    
061                    addCookie(request, response, cookie, request.isSecure());
062            }
063    
064            public static void addCookie(
065                    HttpServletRequest request, HttpServletResponse response, Cookie cookie,
066                    boolean secure) {
067    
068                    if (!_SESSION_ENABLE_PERSISTENT_COOKIES || _TCK_URL) {
069                            return;
070                    }
071    
072                    // LEP-5175
073    
074                    String name = cookie.getName();
075    
076                    String originalValue = cookie.getValue();
077                    String encodedValue = originalValue;
078    
079                    if (isEncodedCookie(name)) {
080                            encodedValue = UnicodeFormatter.bytesToHex(
081                                    originalValue.getBytes());
082    
083                            if (_log.isDebugEnabled()) {
084                                    _log.debug("Add encoded cookie " + name);
085                                    _log.debug("Original value " + originalValue);
086                                    _log.debug("Hex encoded value " + encodedValue);
087                            }
088                    }
089    
090                    cookie.setSecure(secure);
091                    cookie.setValue(encodedValue);
092                    cookie.setVersion(0);
093    
094                    // Setting a cookie will cause the TCK to lose its ability to track
095                    // sessions
096    
097                    response.addCookie(cookie);
098            }
099    
100            public static void addSupportCookie(
101                    HttpServletRequest request, HttpServletResponse response) {
102    
103                    Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");
104    
105                    cookieSupportCookie.setPath(StringPool.SLASH);
106                    cookieSupportCookie.setMaxAge(MAX_AGE);
107    
108                    addCookie(request, response, cookieSupportCookie);
109            }
110    
111            public static String getCookie(HttpServletRequest request, String name) {
112                    return getCookie(request, name, true);
113            }
114    
115            public static String getCookie(
116                    HttpServletRequest request, String name, boolean toUpperCase) {
117    
118                    String value = _get(request, name, toUpperCase);
119    
120                    if ((value != null) && isEncodedCookie(name)) {
121                            try {
122                                    String encodedValue = value;
123                                    String originalValue = new String(
124                                            UnicodeFormatter.hexToBytes(encodedValue));
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                    // See LEP-4602 and       LEP-4618.
149    
150                    if (Validator.isNotNull(_SESSION_COOKIE_DOMAIN)) {
151                            return _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                    // See LEP-4602 and LEP-4645.
162    
163                    if (host == null) {
164                            return null;
165                    }
166    
167                    // See LEP-5595.
168    
169                    if (Validator.isIPAddress(host)) {
170                            return host;
171                    }
172    
173                    int x = host.lastIndexOf(CharPool.PERIOD);
174    
175                    if (x <= 0) {
176                            return null;
177                    }
178    
179                    int y = host.lastIndexOf(CharPool.PERIOD, x - 1);
180    
181                    if (y <= 0) {
182                            return StringPool.PERIOD + host;
183                    }
184    
185                    int z = host.lastIndexOf(CharPool.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, false);
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 (_SESSION_ENABLE_PERSISTENT_COOKIES &&
225                            _SESSION_TEST_COOKIE_SUPPORT) {
226    
227                            String cookieSupport = getCookie(request, COOKIE_SUPPORT, false);
228    
229                            if (Validator.isNull(cookieSupport)) {
230                                    throw new CookieNotSupportedException();
231                            }
232                    }
233            }
234    
235            private static String _get(
236                    HttpServletRequest request, String name, boolean toUpperCase) {
237    
238                    Map<String, Cookie> cookieMap = _getCookieMap(request);
239    
240                    if (toUpperCase) {
241                            name = name.toUpperCase();
242                    }
243    
244                    Cookie cookie = cookieMap.get(name);
245    
246                    if (cookie == null) {
247                            return null;
248                    }
249                    else {
250                            return cookie.getValue();
251                    }
252            }
253    
254            private static Map<String, Cookie> _getCookieMap(
255                    HttpServletRequest request) {
256    
257                    Map<String, Cookie> cookieMap =
258                            (Map<String, Cookie>)request.getAttribute(
259                                    CookieKeys.class.getName());
260    
261                    if (cookieMap != null) {
262                            return cookieMap;
263                    }
264    
265                    Cookie[] cookies = request.getCookies();
266    
267                    if (cookies == null) {
268                            cookieMap = Collections.emptyMap();
269                    }
270                    else {
271                            cookieMap = new HashMap<String, Cookie>(cookies.length * 4 / 3);
272    
273                            for (Cookie cookie : cookies) {
274                                    String cookieName = GetterUtil.getString(cookie.getName());
275    
276                                    cookieName = cookieName.toUpperCase();
277    
278                                    cookieMap.put(cookieName, cookie);
279                            }
280                    }
281    
282                    request.setAttribute(CookieKeys.class.getName(), cookieMap);
283    
284                    return cookieMap;
285            }
286    
287            private static final String _SESSION_COOKIE_DOMAIN = PropsUtil.get(
288                    PropsKeys.SESSION_COOKIE_DOMAIN);
289    
290            private static final boolean _SESSION_ENABLE_PERSISTENT_COOKIES =
291                    GetterUtil.getBoolean(
292                            PropsUtil.get(PropsKeys.SESSION_ENABLE_PERSISTENT_COOKIES));
293    
294            private static final boolean _SESSION_TEST_COOKIE_SUPPORT =
295                    GetterUtil.getBoolean(
296                            PropsUtil.get(PropsKeys.SESSION_TEST_COOKIE_SUPPORT));
297    
298            private static final boolean _TCK_URL = GetterUtil.getBoolean(
299                    PropsUtil.get(PropsKeys.TCK_URL));
300    
301            private static Log _log = LogFactoryUtil.getLog(CookieKeys.class);
302    
303    }