1   /**
2    * Copyright (c) 2000-2007 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.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  
28  import javax.servlet.http.Cookie;
29  
30  /**
31   * <a href="CookieUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class CookieUtil {
37  
38      public static String get(Cookie[] cookies, String name) {
39          if ((cookies != null) && (cookies.length > 0)) {
40              for (int i = 0; i < cookies.length; i++) {
41                  String cookieName = GetterUtil.getString(cookies[i].getName());
42  
43                  if (cookieName.equalsIgnoreCase(name)) {
44                      return cookies[i].getValue();
45                  }
46              }
47          }
48  
49          return null;
50      }
51  
52      public static String get(String cookie, String tag) {
53          if (cookie == null) {
54              return "";
55          }
56  
57          tag = tag + "=";
58  
59          if (cookie.startsWith(tag)) {
60              int y = cookie.indexOf(';');
61  
62              return cookie.substring(tag.length(), y);
63          }
64  
65          tag = ";" + tag;
66  
67          int x = cookie.indexOf(tag);
68  
69          if (x != -1) {
70              int y = cookie.indexOf(';', x + 1);
71  
72              return cookie.substring(x + tag.length(), y);
73          }
74  
75          return "";
76      }
77  
78      public static String set(String cookie, String tag, String sub) {
79          if (cookie == null) {
80              return "";
81          }
82  
83          tag = tag + "=";
84  
85          if (cookie.startsWith(tag)) {
86              int y = cookie.indexOf(';');
87  
88              StringMaker sm = new StringMaker();
89  
90              sm.append(tag).append(sub).append(";");
91              sm.append(cookie.substring(y + 1, cookie.length()));
92  
93              return sm.toString();
94          }
95  
96          tag = ";" + tag;
97  
98          int x = cookie.indexOf(tag);
99  
100         if (x != -1) {
101             int y = cookie.indexOf(';', x + 1);
102 
103             StringMaker sm = new StringMaker();
104 
105             sm.append(cookie.substring(0, x + tag.length()));
106             sm.append(sub);
107             sm.append(cookie.substring(y, cookie.length()));
108 
109             return sm.toString();
110         }
111 
112         return cookie + tag.substring(1, tag.length()) + sub + ";";
113     }
114 
115 }