1   /**
2    * Copyright (c) 2000-2008 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.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  /**
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(HttpServletResponse res, Cookie cookie) {
70          if (PropsValues.SESSION_ENABLE_PERSISTENT_COOKIES) {
71              if (!PropsValues.TCK_URL) {
72  
73                  // LEP-5175
74  
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                  // Setting a cookie will cause the TCK to lose its ability
94                  // to track sessions
95  
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         // See LEP-4602 and LEP-4618.
142 
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         // See LEP-4602 and LEP-4645.
155 
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 }