1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.portal.CookieNotSupportedException;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.util.CookieUtil;
31  
32  import javax.servlet.http.Cookie;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.commons.codec.binary.Hex;
37  
38  /**
39   * <a href="CookieKeys.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Minhchau Dang
43   *
44   */
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          addCookie(request, response, cookie, request.isSecure());
74      }
75  
76      public static void addCookie(
77          HttpServletRequest request, HttpServletResponse response,
78          Cookie cookie, boolean secure) {
79  
80          if (!PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES ||
81              PropsValues.TCK_URL) {
82  
83              return;
84          }
85  
86          // LEP-5175
87  
88          String name = cookie.getName();
89  
90          String originalValue = cookie.getValue();
91          String encodedValue = originalValue;
92  
93          if (isEncodedCookie(name)) {
94              encodedValue = new String(Hex.encodeHex(originalValue.getBytes()));
95  
96              if (_log.isDebugEnabled()) {
97                  _log.debug("Add encoded cookie " + name);
98                  _log.debug("Original value " + originalValue);
99                  _log.debug("Hex encoded value " + encodedValue);
100             }
101         }
102 
103         cookie.setSecure(secure);
104         cookie.setValue(encodedValue);
105         cookie.setVersion(VERSION);
106 
107         // Setting a cookie will cause the TCK to lose its ability to track
108         // sessions
109 
110         response.addCookie(cookie);
111     }
112 
113     public static void addSupportCookie(
114         HttpServletRequest request, HttpServletResponse response) {
115 
116         Cookie cookieSupportCookie = new Cookie(COOKIE_SUPPORT, "true");
117 
118         cookieSupportCookie.setPath(StringPool.SLASH);
119         cookieSupportCookie.setMaxAge(MAX_AGE);
120 
121         addCookie(request, response, cookieSupportCookie);
122     }
123 
124     public static String getCookie(HttpServletRequest request, String name) {
125         String value = CookieUtil.get(request, name);
126 
127         if ((value != null) && isEncodedCookie(name)) {
128             try {
129                 String encodedValue = value;
130                 String originalValue = new String(
131                     Hex.decodeHex(encodedValue.toCharArray()));
132 
133                 if (_log.isDebugEnabled()) {
134                     _log.debug("Get encoded cookie " + name);
135                     _log.debug("Hex encoded value " + encodedValue);
136                     _log.debug("Original value " + originalValue);
137                 }
138 
139                 return originalValue;
140             }
141             catch (Exception e) {
142                 if (_log.isWarnEnabled()) {
143                     _log.warn(e.getMessage());
144                 }
145 
146                 return value;
147             }
148         }
149 
150         return value;
151     }
152 
153     public static String getDomain(HttpServletRequest request) {
154 
155         // See LEP-4602 and LEP-4618.
156 
157         if (Validator.isNotNull(PropsValues.SESSION_COOKIE_DOMAIN)) {
158             return PropsValues.SESSION_COOKIE_DOMAIN;
159         }
160 
161         String host = request.getServerName();
162 
163         return getDomain(host);
164     }
165 
166     public static String getDomain(String host) {
167 
168         // See LEP-4602 and LEP-4645.
169 
170         if (host == null) {
171             return null;
172         }
173 
174         // See LEP-5595.
175 
176         if (Validator.isIPAddress(host)) {
177             return host;
178         }
179 
180         int x = host.lastIndexOf(StringPool.PERIOD);
181 
182         if (x <= 0) {
183             return null;
184         }
185 
186         int y = host.lastIndexOf(StringPool.PERIOD, x - 1);
187 
188         if (y <= 0) {
189             return StringPool.PERIOD + host;
190         }
191 
192         int z = host.lastIndexOf(StringPool.PERIOD, y - 1);
193 
194         String domain = null;
195 
196         if (z <= 0) {
197             domain = host.substring(y);
198         }
199         else {
200             domain = host.substring(z);
201         }
202 
203         return domain;
204     }
205 
206     public static boolean hasSessionId(HttpServletRequest request) {
207         String jsessionid = getCookie(request, JSESSIONID);
208 
209         if (jsessionid != null) {
210             return true;
211         }
212         else {
213             return false;
214         }
215     }
216 
217     public static boolean isEncodedCookie(String name) {
218         if (name.equals(ID) || name.equals(LOGIN) || name.equals(PASSWORD) ||
219             name.equals(SCREEN_NAME)) {
220 
221             return true;
222         }
223         else {
224             return false;
225         }
226     }
227 
228     public static void validateSupportCookie(HttpServletRequest request)
229         throws CookieNotSupportedException {
230 
231         if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES &&
232             PropsValues.SESSION_TEST_COOKIE_SUPPORT) {
233 
234             String cookieSupport = getCookie(request, COOKIE_SUPPORT);
235 
236             if (Validator.isNull(cookieSupport)) {
237                 throw new CookieNotSupportedException();
238             }
239         }
240     }
241 
242     private static Log _log = LogFactoryUtil.getLog(CookieKeys.class);
243 
244 }