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