1
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
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 }