View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.servlet;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.Cookie;
28  import org.apache.commons.chain.web.MapEntry;
29  
30  
31  /**
32   * <p>Private implementation of <code>Map</code> for servlet cookies</p>
33   *
34   * @author Niall Pemberton
35   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
36   * @since Chain 1.1
37   */
38  
39  final class ServletCookieMap implements Map {
40  
41  
42      public ServletCookieMap(HttpServletRequest request) {
43          this.request = request;
44      }
45  
46  
47      private HttpServletRequest request = null;
48  
49  
50      public void clear() {
51          throw new UnsupportedOperationException();
52      }
53  
54  
55      public boolean containsKey(Object key) {
56          return (get(key) != null);
57      }
58  
59  
60      public boolean containsValue(Object value) {
61          Cookie[] cookies = request.getCookies();
62          if (cookies != null) {
63              for (int i = 0; i < cookies.length; i++) {
64                  if (cookies[i].equals(value)) {
65                      return true;
66                  }
67              }
68          }
69          return (false);
70      }
71  
72  
73      public Set entrySet() {
74          Set set = new HashSet();
75          Cookie[] cookies = request.getCookies();
76          if (cookies != null) {
77              for (int i = 0; i < cookies.length; i++) {
78                  set.add(new MapEntry(cookies[i].getName(), cookies[i], false));
79              }
80          }
81          return (set);
82      }
83  
84  
85      public boolean equals(Object o) {
86          return (request.equals(o));
87      }
88  
89  
90      public Object get(Object key) {
91          Cookie[] cookies = request.getCookies();
92          if (cookies != null) {
93              for (int i = 0; i < cookies.length; i++) {
94                  if (cookies[i].getName().equals(key(key))) {
95                      return cookies[i];
96                  }
97              }
98          }
99          return null;
100     }
101 
102 
103     public int hashCode() {
104         return (request.hashCode());
105     }
106 
107 
108     public boolean isEmpty() {
109         return (size() < 1);
110     }
111 
112 
113     public Set keySet() {
114         Set set = new HashSet();
115         Cookie[] cookies = request.getCookies();
116         if (cookies != null) {
117             for (int i = 0; i < cookies.length; i++) {
118                 set.add(cookies[i].getName());
119             }
120         }
121         return (set);
122     }
123 
124 
125     public Object put(Object key, Object value) {
126         throw new UnsupportedOperationException();
127     }
128 
129 
130     public void putAll(Map map) {
131         throw new UnsupportedOperationException();
132     }
133 
134 
135     public Object remove(Object key) {
136         throw new UnsupportedOperationException();
137     }
138 
139 
140     public int size() {
141         Cookie[] cookies = request.getCookies();
142         return (cookies == null ?  0 : cookies.length);
143     }
144 
145 
146     public Collection values() {
147         List list = new ArrayList(size());
148         Cookie[] cookies = request.getCookies();
149         if (cookies != null) {
150             for (int i = 0; i < cookies.length; i++) {
151                 list.add(cookies[i]);
152             }
153         }
154         return (list);
155     }
156 
157 
158     private String key(Object key) {
159         if (key == null) {
160             throw new IllegalArgumentException();
161         } else if (key instanceof String) {
162             return ((String) key);
163         } else {
164             return (key.toString());
165         }
166     }
167 
168 
169 }