KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > CookieSourceImpl


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.services.impl;
16
17 import javax.servlet.http.Cookie JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.apache.tapestry.services.CookieSource;
22
23 /**
24  * Implementation of the {@link org.apache.tapestry.services.CookieSource}service interface.
25  *
26  * @author Howard Lewis Ship
27  * @since 4.0
28  */

29 public class CookieSourceImpl implements CookieSource
30 {
31     private HttpServletRequest JavaDoc _request;
32
33     private HttpServletResponse JavaDoc _response;
34
35     public String JavaDoc readCookieValue(String JavaDoc name)
36     {
37         Cookie JavaDoc[] cookies = _request.getCookies();
38
39         if (cookies == null)
40             return null;
41
42         for (int i = 0; i < cookies.length; i++)
43         {
44             if (cookies[i].getName().equals(name))
45                 return cookies[i].getValue();
46         }
47
48         return null;
49     }
50
51     public void writeCookieValue(String JavaDoc name, String JavaDoc value)
52     {
53         Cookie JavaDoc cookie = new Cookie JavaDoc(name, value);
54         cookie.setPath(_request.getContextPath());
55
56         _response.addCookie(cookie);
57     }
58
59     public void removeCookieValue(String JavaDoc name)
60     {
61         Cookie JavaDoc cookie = new Cookie JavaDoc(name, null);
62         cookie.setPath(_request.getContextPath());
63         cookie.setMaxAge(0);
64
65         _response.addCookie(cookie);
66     }
67
68     public void setRequest(HttpServletRequest JavaDoc request)
69     {
70         _request = request;
71     }
72
73     public void setResponse(HttpServletResponse JavaDoc response)
74     {
75         _response = response;
76     }
77 }
Popular Tags