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.webapp.filter.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  /**
28   * HttpServletRequest Cookies as Map.
29   * <p>
30   * NOTE: This class was copied from myfaces impl 
31   * org.apache.myfaces.context.servlet and it is
32   * used by TomahawkFacesContextWrapper. By that reason, it could change
33   * in the future.
34   * </p>
35   * 
36   * @since 1.1.7
37   * @author Dimitry D'hondt
38   * @author Anton Koinov
39   * @version $Revision: 691871 $ $Date: 2008-09-03 23:32:08 -0500 (Wed, 03 Sep 2008) $
40   */
41  public class CookieMap extends AbstractAttributeMap
42  {
43      private static final Cookie[] EMPTY_ARRAY = new Cookie[0];
44  
45      final HttpServletRequest _httpServletRequest;
46  
47      CookieMap(HttpServletRequest httpServletRequest)
48      {
49          _httpServletRequest = httpServletRequest;
50      }
51  
52      public void clear()
53      {
54          throw new UnsupportedOperationException(
55              "Cannot clear HttpRequest Cookies");
56      }
57  
58      public boolean containsKey(Object key)
59      {
60          Cookie[] cookies = _httpServletRequest.getCookies();
61          if (cookies == null) return false;
62          for (int i = 0, len = cookies.length; i < len; i++)
63          {
64              if (cookies[i].getName().equals(key))
65              {
66                  return true;
67              }
68          }
69  
70          return false;
71      }
72  
73      public boolean containsValue(Object findValue)
74      {
75          if (findValue == null)
76          {
77              return false;
78          }
79  
80          Cookie[] cookies = _httpServletRequest.getCookies();
81          if (cookies == null) return false;
82          for (int i = 0, len = cookies.length; i < len; i++)
83          {
84              if (findValue.equals(cookies[i]))
85              {
86                  return true;
87              }
88          }
89  
90          return false;
91      }
92  
93      public boolean isEmpty()
94      {
95          Cookie[] cookies = _httpServletRequest.getCookies();
96          return cookies == null || cookies.length == 0;
97      }
98  
99      public int size()
100     {
101         Cookie[] cookies = _httpServletRequest.getCookies();
102         return cookies == null ? 0 : cookies.length;
103     }
104 
105     public void putAll(Map t)
106     {
107         throw new UnsupportedOperationException();
108     }
109 
110 
111     protected Object getAttribute(String key)
112     {
113         Cookie[] cookies = _httpServletRequest.getCookies();
114         if (cookies == null) return null;
115         for (int i = 0, len = cookies.length; i < len; i++)
116         {
117             if (cookies[i].getName().equals(key))
118             {
119                 return cookies[i];
120             }
121         }
122 
123         return null;
124     }
125 
126     protected void setAttribute(String key, Object value)
127     {
128         throw new UnsupportedOperationException(
129             "Cannot set HttpRequest Cookies");
130     }
131 
132     protected void removeAttribute(String key)
133     {
134         throw new UnsupportedOperationException(
135             "Cannot remove HttpRequest Cookies");
136     }
137 
138     protected Enumeration getAttributeNames()
139     {
140         Cookie[] cookies = _httpServletRequest.getCookies();
141         if (cookies == null)
142         {
143             return new CookieNameEnumeration(EMPTY_ARRAY);
144         }
145         else
146         {
147             return new CookieNameEnumeration(cookies);
148         }
149     }
150 
151     private static class CookieNameEnumeration implements Enumeration
152     {
153         private final Cookie[] _cookies;
154         private final int _length;
155         private int _index;
156 
157         public CookieNameEnumeration(Cookie[] cookies)
158         {
159             _cookies = cookies;
160             _length = cookies.length;
161         }
162 
163         public boolean hasMoreElements()
164         {
165             return _index < _length;
166         }
167 
168         public Object nextElement()
169         {
170             return _cookies[_index++].getName();
171         }
172     }
173 }