1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import org.apache.tapestry.services.CookieSource; |
18 |
|
|
19 |
|
import javax.servlet.http.Cookie; |
20 |
|
import javax.servlet.http.HttpServletRequest; |
21 |
|
import javax.servlet.http.HttpServletResponse; |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
7 |
public class CookieSourceImpl implements CookieSource |
30 |
|
{ |
31 |
|
private HttpServletRequest _request; |
32 |
|
|
33 |
|
private HttpServletResponse _response; |
34 |
|
|
35 |
|
private int _defaultMaxAge; |
36 |
|
|
37 |
|
public String readCookieValue(String name) |
38 |
|
{ |
39 |
3 |
Cookie[] cookies = _request.getCookies(); |
40 |
|
|
41 |
3 |
if (cookies == null) |
42 |
1 |
return null; |
43 |
|
|
44 |
4 |
for (int i = 0; i < cookies.length; i++) |
45 |
|
{ |
46 |
3 |
if (cookies[i].getName().equals(name)) |
47 |
1 |
return cookies[i].getValue(); |
48 |
|
} |
49 |
|
|
50 |
1 |
return null; |
51 |
|
} |
52 |
|
|
53 |
|
public void writeCookieValue(String name, String value) |
54 |
|
{ |
55 |
1 |
writeCookieValue(name, value, _defaultMaxAge); |
56 |
1 |
} |
57 |
|
|
58 |
|
public void writeCookieValue(String name, String value, int maxAge) |
59 |
|
{ |
60 |
2 |
Cookie cookie = new Cookie(name, value); |
61 |
2 |
cookie.setPath(_request.getContextPath() + "/"); |
62 |
2 |
cookie.setMaxAge(maxAge); |
63 |
|
|
64 |
2 |
_response.addCookie(cookie); |
65 |
2 |
} |
66 |
|
|
67 |
|
public void writeCookieValue(String name, String value, String path) |
68 |
|
{ |
69 |
0 |
Cookie cookie = new Cookie(name, value); |
70 |
0 |
cookie.setPath(path); |
71 |
0 |
_response.addCookie(cookie); |
72 |
0 |
} |
73 |
|
|
74 |
|
public void writeDomainCookieValue(String name, String value, String domain) |
75 |
|
{ |
76 |
0 |
Cookie cookie = new Cookie(name, value); |
77 |
0 |
cookie.setPath(_request.getContextPath() + "/"); |
78 |
0 |
cookie.setDomain(domain); |
79 |
0 |
_response.addCookie(cookie); |
80 |
0 |
} |
81 |
|
|
82 |
|
public void writeDomainCookieValue(String name, String value, String domain, int maxAge) |
83 |
|
{ |
84 |
1 |
Cookie cookie = new Cookie(name, value); |
85 |
1 |
cookie.setPath(_request.getContextPath() + "/"); |
86 |
1 |
cookie.setDomain(domain); |
87 |
1 |
cookie.setMaxAge(maxAge); |
88 |
|
|
89 |
1 |
_response.addCookie(cookie); |
90 |
1 |
} |
91 |
|
|
92 |
|
public void writeCookieValue(String name, String value, String path, String domain) |
93 |
|
{ |
94 |
0 |
Cookie cookie = new Cookie(name, value); |
95 |
0 |
cookie.setPath(path); |
96 |
0 |
cookie.setDomain(domain); |
97 |
0 |
_response.addCookie(cookie); |
98 |
0 |
} |
99 |
|
|
100 |
|
public void removeCookieValue(String name) |
101 |
|
{ |
102 |
1 |
Cookie cookie = new Cookie(name, null); |
103 |
1 |
cookie.setPath(_request.getContextPath() + "/"); |
104 |
1 |
cookie.setMaxAge(0); |
105 |
|
|
106 |
1 |
_response.addCookie(cookie); |
107 |
1 |
} |
108 |
|
|
109 |
|
public void setRequest(HttpServletRequest request) |
110 |
|
{ |
111 |
7 |
_request = request; |
112 |
7 |
} |
113 |
|
|
114 |
|
public void setResponse(HttpServletResponse response) |
115 |
|
{ |
116 |
4 |
_response = response; |
117 |
4 |
} |
118 |
|
|
119 |
|
public void setDefaultMaxAge(int defaultMaxAge) |
120 |
|
{ |
121 |
3 |
_defaultMaxAge = defaultMaxAge; |
122 |
3 |
} |
123 |
|
} |