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 | 0 | 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 | 0 | Cookie[] cookies = _request.getCookies(); |
40 | |
|
41 | 0 | if (cookies == null) |
42 | 0 | return null; |
43 | |
|
44 | 0 | for (int i = 0; i < cookies.length; i++) |
45 | |
{ |
46 | 0 | if (cookies[i].getName().equals(name)) |
47 | 0 | return cookies[i].getValue(); |
48 | |
} |
49 | |
|
50 | 0 | return null; |
51 | |
} |
52 | |
|
53 | |
public void writeCookieValue(String name, String value) |
54 | |
{ |
55 | 0 | writeCookieValue(name, value, _defaultMaxAge); |
56 | 0 | } |
57 | |
|
58 | |
public void writeCookieValue(String name, String value, int maxAge) |
59 | |
{ |
60 | 0 | Cookie cookie = new Cookie(name, value); |
61 | 0 | cookie.setPath(_request.getContextPath() + "/"); |
62 | 0 | cookie.setMaxAge(maxAge); |
63 | |
|
64 | 0 | _response.addCookie(cookie); |
65 | 0 | } |
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 | 0 | Cookie cookie = new Cookie(name, value); |
85 | 0 | cookie.setPath(_request.getContextPath() + "/"); |
86 | 0 | cookie.setDomain(domain); |
87 | 0 | cookie.setMaxAge(maxAge); |
88 | |
|
89 | 0 | _response.addCookie(cookie); |
90 | 0 | } |
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 | 0 | Cookie cookie = new Cookie(name, null); |
103 | 0 | cookie.setPath(_request.getContextPath() + "/"); |
104 | 0 | cookie.setMaxAge(0); |
105 | |
|
106 | 0 | _response.addCookie(cookie); |
107 | 0 | } |
108 | |
|
109 | |
public void setRequest(HttpServletRequest request) |
110 | |
{ |
111 | 0 | _request = request; |
112 | 0 | } |
113 | |
|
114 | |
public void setResponse(HttpServletResponse response) |
115 | |
{ |
116 | 0 | _response = response; |
117 | 0 | } |
118 | |
|
119 | |
public void setDefaultMaxAge(int defaultMaxAge) |
120 | |
{ |
121 | 0 | _defaultMaxAge = defaultMaxAge; |
122 | 0 | } |
123 | |
} |