CookieUtil.java
4.47 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.Cookie
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* org.apache.commons.codec.net.URLCodec
* org.apache.commons.httpclient.Cookie
* org.apache.commons.httpclient.Header
* org.apache.commons.httpclient.HttpMethod
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.commerce.common;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CookieUtil {
private static final Logger log = LoggerFactory.getLogger(CookieUtil.class);
private static final URLCodec urlCodec = new URLCodec();
private static final String HTTP_HEADER_SET_COOKIE = "Set-Cookie";
private static final String URL_HOST_REGEX = "https?://([\\w\\d_\\.\\-]*)(:\\d+)?(/.*)?";
public static final String SESSION_COOKIE = "JSESSIONID";
public static final boolean HTTP_ONLY = true;
public static void setCookie(HttpServletRequest request, HttpServletResponse response, String name, String value, int maxAge, boolean httpOnly) {
StringBuilder header = new StringBuilder();
header.append(name).append("=").append(value);
String contextPath = request.getContextPath();
String cookiePath = contextPath == null || contextPath.length() == 0 ? "/" : contextPath;
header.append("; Path=").append(cookiePath);
if (maxAge >= 0) {
header.append("; Max-Age=").append(maxAge);
}
if (httpOnly) {
header.append("; HttpOnly");
}
if (request.isSecure()) {
header.append("; Secure");
}
response.addHeader("Set-Cookie", header.toString());
}
public static String hostFromUrl(String url) {
Matcher matcher = Pattern.compile("https?://([\\w\\d_\\.\\-]*)(:\\d+)?(/.*)?").matcher(url);
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
@Deprecated
public static List<Cookie> getSessionCookies(HttpServletRequest request, String prefix, String remoteHost) {
javax.servlet.http.Cookie[] requestCookies = request.getCookies();
ArrayList<Cookie> cookies = new ArrayList<Cookie>();
try {
if (requestCookies != null) {
for (javax.servlet.http.Cookie c : requestCookies) {
if (!c.getName().startsWith(prefix)) continue;
String[] values = urlCodec.decode(c.getValue()).split(";");
cookies.add(new Cookie(remoteHost, "JSESSIONID", values[0], values[1], c.getMaxAge(), c.getSecure()));
}
}
}
catch (Exception e) {
log.error("Could not get jcrSession cookies from request: ", (Throwable)e);
return new ArrayList<Cookie>();
}
return cookies;
}
@Deprecated
public static void setSessionCookies(HttpServletResponse response, String prefix, List<Cookie> cookies) {
try {
for (Cookie c : cookies) {
String value = urlCodec.encode(c.getValue() + ";" + c.getPath());
javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie(prefix + c.getPath().substring(1), value);
cookie.setPath("/");
response.addCookie(cookie);
}
}
catch (Exception e) {
log.error("Could not set jcrSession cookies on response: ", (Throwable)e);
}
}
@Deprecated
public static boolean hasUpdatedSessionCookies(HttpMethod method) {
if (method.hasBeenUsed()) {
Header[] headers = method.getResponseHeaders("Set-Cookie");
for (int i = 0; i < headers.length; ++i) {
if (!headers[i].getValue().contains("JSESSIONID")) continue;
return true;
}
}
return false;
}
@Deprecated
public static boolean hasSessionCookie(List<Cookie> cookies) {
for (Cookie c : cookies) {
if (!c.getName().equals("JSESSIONID")) continue;
return true;
}
return false;
}
}