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