View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.context.servlet;
20  
21  import java.util.Enumeration;
22  import java.util.Map;
23  
24  import javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.myfaces.util.AbstractAttributeMap;
28  
29  /**
30   * HttpServletRequest Cookies as Map.
31   *
32   * @author Dimitry D'hondt
33   * @author Anton Koinov
34   * @version $Revision: 1000660 $ $Date: 2010-09-23 18:02:43 -0500 (Thu, 23 Sep 2010) $
35   */
36  public class CookieMap extends AbstractAttributeMap
37  {
38      private static final Cookie[] EMPTY_ARRAY = new Cookie[0];
39  
40      final HttpServletRequest _httpServletRequest;
41  
42      CookieMap(HttpServletRequest httpServletRequest)
43      {
44          _httpServletRequest = httpServletRequest;
45      }
46  
47      public void clear()
48      {
49          throw new UnsupportedOperationException(
50              "Cannot clear HttpRequest Cookies");
51      }
52  
53      public boolean containsKey(Object key)
54      {
55          Cookie[] cookies = _httpServletRequest.getCookies();
56          if (cookies == null) return false;
57          for (int i = 0, len = cookies.length; i < len; i++)
58          {
59              if (cookies[i].getName().equals(key))
60              {
61                  return true;
62              }
63          }
64  
65          return false;
66      }
67  
68      public boolean containsValue(Object findValue)
69      {
70          if (findValue == null)
71          {
72              return false;
73          }
74  
75          Cookie[] cookies = _httpServletRequest.getCookies();
76          if (cookies == null) return false;
77          for (int i = 0, len = cookies.length; i < len; i++)
78          {
79              if (findValue.equals(cookies[i]))
80              {
81                  return true;
82              }
83          }
84  
85          return false;
86      }
87  
88      public boolean isEmpty()
89      {
90          Cookie[] cookies = _httpServletRequest.getCookies();
91          return cookies == null || cookies.length == 0;
92      }
93  
94      public int size()
95      {
96          Cookie[] cookies = _httpServletRequest.getCookies();
97          return cookies == null ? 0 : cookies.length;
98      }
99  
100     public void putAll(Map t)
101     {
102         throw new UnsupportedOperationException();
103     }
104 
105 
106     protected Object getAttribute(String key)
107     {
108         Cookie[] cookies = _httpServletRequest.getCookies();
109         if (cookies == null) return null;
110         for (int i = 0, len = cookies.length; i < len; i++)
111         {
112             if (cookies[i].getName().equals(key))
113             {
114                 return cookies[i];
115             }
116         }
117 
118         return null;
119     }
120 
121     protected void setAttribute(String key, Object value)
122     {
123         throw new UnsupportedOperationException(
124             "Cannot set HttpRequest Cookies");
125     }
126 
127     protected void removeAttribute(String key)
128     {
129         throw new UnsupportedOperationException(
130             "Cannot remove HttpRequest Cookies");
131     }
132 
133     protected Enumeration getAttributeNames()
134     {
135         Cookie[] cookies = _httpServletRequest.getCookies();
136         if (cookies == null)
137         {
138             return new CookieNameEnumeration(EMPTY_ARRAY);
139         }
140         else
141         {
142             return new CookieNameEnumeration(cookies);
143         }
144     }
145 
146     private static class CookieNameEnumeration implements Enumeration
147     {
148         private final Cookie[] _cookies;
149         private final int _length;
150         private int _index;
151 
152         public CookieNameEnumeration(Cookie[] cookies)
153         {
154             _cookies = cookies;
155             _length = cookies.length;
156         }
157 
158         public boolean hasMoreElements()
159         {
160             return _index < _length;
161         }
162 
163         public Object nextElement()
164         {
165             return _cookies[_index++].getName();
166         }
167     }
168 }