KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > context > servlet > CookieMap


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

16 package org.apache.myfaces.context.servlet;
17
18 import java.util.Enumeration JavaDoc;
19
20 import javax.servlet.http.Cookie JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 /**
24  * HttpServletRequest Cookies as Map.
25  *
26  * @author Dimitry D'hondt
27  * @author Anton Koinov
28  * @version $Revision: 1.10 $ $Date: 2004/11/24 13:40:11 $
29  * $Log: CookieMap.java,v $
30  * Revision 1.10 2004/11/24 13:40:11 manolito
31  * additional null cookies fixes
32  *
33  * Revision 1.9 2004/11/24 13:36:11 manolito
34  * SF-Bug #1072442 fixed
35  *
36  * Revision 1.8 2004/11/23 12:26:45 manolito
37  * SF-Bug #1071640 fixed
38  *
39  */

40 public class CookieMap extends AbstractAttributeMap
41 {
42     private static final Cookie JavaDoc[] EMPTY_ARRAY = new Cookie JavaDoc[0];
43
44     final HttpServletRequest JavaDoc _httpServletRequest;
45
46     CookieMap(HttpServletRequest JavaDoc httpServletRequest)
47     {
48         _httpServletRequest = httpServletRequest;
49     }
50
51     public void clear()
52     {
53         throw new UnsupportedOperationException JavaDoc(
54             "Cannot clear HttpRequest Cookies");
55     }
56
57     public boolean containsKey(Object JavaDoc key)
58     {
59         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
60         if (cookies == null) return false;
61         for (int i = 0, len = cookies.length; i < len; i++)
62         {
63             if (cookies[i].getName().equals(key))
64             {
65                 return true;
66             }
67         }
68
69         return false;
70     }
71
72     public boolean containsValue(Object JavaDoc findValue)
73     {
74         if (findValue == null)
75         {
76             return false;
77         }
78
79         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
80         if (cookies == null) return false;
81         for (int i = 0, len = cookies.length; i < len; i++)
82         {
83             if (findValue.equals(cookies[i].getValue()))
84             {
85                 return true;
86             }
87         }
88
89         return false;
90     }
91
92     public boolean isEmpty()
93     {
94         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
95         return cookies == null || cookies.length == 0;
96     }
97
98     public int size()
99     {
100         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
101         return cookies == null ? 0 : cookies.length;
102     }
103
104     protected Object JavaDoc getAttribute(String JavaDoc key)
105     {
106         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
107         if (cookies == null) return null;
108         for (int i = 0, len = cookies.length; i < len; i++)
109         {
110             if (cookies[i].getName().equals(key))
111             {
112                 return cookies[i];
113             }
114         }
115
116         return null;
117     }
118
119     protected void setAttribute(String JavaDoc key, Object JavaDoc value)
120     {
121         throw new UnsupportedOperationException JavaDoc(
122             "Cannot set HttpRequest Cookies");
123     }
124
125     protected void removeAttribute(String JavaDoc key)
126     {
127         throw new UnsupportedOperationException JavaDoc(
128             "Cannot remove HttpRequest Cookies");
129     }
130
131     protected Enumeration JavaDoc getAttributeNames()
132     {
133         Cookie JavaDoc[] cookies = _httpServletRequest.getCookies();
134         if (cookies == null)
135         {
136             return new CookieNameEnumeration(EMPTY_ARRAY);
137         }
138         else
139         {
140             return new CookieNameEnumeration(cookies);
141         }
142     }
143     
144     private static class CookieNameEnumeration implements Enumeration JavaDoc
145     {
146         private final Cookie JavaDoc[] _cookies;
147         private final int _length;
148         private int _index;
149         
150         public CookieNameEnumeration(Cookie JavaDoc[] cookies)
151         {
152             _cookies = cookies;
153             _length = cookies.length;
154         }
155
156         public boolean hasMoreElements()
157         {
158             return _index < _length;
159         }
160
161         public Object JavaDoc nextElement()
162         {
163             return _cookies[_index++].getName();
164         }
165     }
166 }
167
Popular Tags