CookieUtil.java
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.qgutech.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
public class CookieUtil {
private static final int MINUTE_SECONDS = 60;
public CookieUtil() {
}
public static void setCookie(HttpServletResponse response, String name, String value) {
setCookie(response, name, value, "/", (String)null);
}
public static void setCookie(HttpServletResponse response, String name, String value, String path) {
setCookie(response, name, value, path, (String)null);
}
public static void setCookie(HttpServletResponse response, String name, String value, String path, String age) {
setCookie(response, name, value, path, age, (String)null);
}
public static void setCookie(HttpServletResponse response, String name, String value, String path, String age, String domain) {
Cookie cookie = new Cookie(name, value);
cookie.setSecure(false);
cookie.setPath(path);
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
int ageValue = getAgeIntValue(age);
if (ageValue >= 0) {
cookie.setMaxAge(ageValue);
}
response.addCookie(cookie);
}
public static void setHttpsCookie(HttpServletResponse response, String name, String value) {
setHttpsCookie(response, name, value, "/", (String)null);
}
public static void setHttpsCookie(HttpServletResponse response, String name, String value, String path) {
setHttpsCookie(response, name, value, path, (String)null);
}
public static void setHttpsCookie(HttpServletResponse response, String name, String value, String path, String age) {
setHttpsCookie(response, name, value, path, age, (String)null);
}
public static void setHttpsCookie(HttpServletResponse response, String name, String value, String path, String age, String domain) {
Cookie cookie = new Cookie(name, value);
cookie.setSecure(true);
cookie.setPath(path);
if (StringUtils.isNotEmpty(domain)) {
cookie.setDomain(domain);
}
int ageValue = getAgeIntValue(age);
if (ageValue >= 0) {
cookie.setMaxAge(ageValue);
}
response.addCookie(cookie);
}
private static int getAgeIntValue(String age) {
try {
return Integer.valueOf(age) * 60;
} catch (Exception var2) {
return -1;
}
}
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
Cookie returnCookie = null;
if (cookies == null) {
return returnCookie;
} else {
for(int i = 0; i < cookies.length; ++i) {
Cookie thisCookie = cookies[i];
if (thisCookie.getName().equals(name)) {
returnCookie = thisCookie;
break;
}
}
return returnCookie;
}
}
public static String getCookieValue(HttpServletRequest request, String name) {
Cookie cookie = getCookie(request, name);
return cookie == null ? null : cookie.getValue();
}
public static String getAllCookieNameAndValue(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
StringBuffer cs = new StringBuffer();
for(int i = 0; i < cookies.length; ++i) {
cs.append(cookies[i].getName()).append("=").append(cookies[i].getValue()).append(";");
}
return cs.toString();
}
public static void deleteCookie(HttpServletResponse response, Cookie cookie, String path) {
if (cookie != null) {
cookie.setMaxAge(0);
cookie.setPath(path);
response.addCookie(cookie);
}
}
public static void deleteCookie(HttpServletResponse response, Cookie cookie) {
deleteCookie(response, cookie, "/");
}
public static String getAppURL(HttpServletRequest request) {
StringBuffer url = new StringBuffer();
int port = request.getServerPort();
if (port < 0) {
port = 80;
}
String scheme = request.getScheme();
url.append(scheme);
url.append("://");
url.append(request.getServerName());
if (scheme.equals("http") && port != 80 || scheme.equals("https") && port != 443) {
url.append(':');
url.append(port);
}
url.append(request.getContextPath());
return url.toString();
}
public static void addCookie(HttpServletResponse response, String name, String value, String path, String age, String domain, boolean isSecure, boolean isHttpOnly) {
int maxAge = getAgeIntValue(age);
addCookieByHeader(response, name, value, maxAge, path, domain, isSecure, isHttpOnly);
}
private static void addCookieByHeader(HttpServletResponse response, String name, String value, int maxAge, String path, String domain, boolean isSecure, boolean isHttpOnly) {
StringBuilder buffer = new StringBuilder();
buffer.append(name).append("=").append(value).append(";");
if (maxAge == 0) {
buffer.append("Expires=Thu Jan 01 08:00:00 CST 1970;");
} else if (maxAge > 0) {
buffer.append("Max-Age=").append(maxAge).append(";");
}
if (domain != null) {
buffer.append("domain=").append(domain).append(";");
}
if (path != null) {
buffer.append("path=").append(path).append(";");
}
if (isSecure) {
buffer.append("secure;");
}
if (isHttpOnly) {
buffer.append("HTTPOnly;");
}
response.addHeader("Set-Cookie", buffer.toString());
}
public static String getCookieDomain(HttpServletRequest request) {
return request.getServerName();
}
}