View Javadoc

1   /*
2    * Copyright 2005-2006 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.commons.chain.web.servlet;
17  
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.Cookie;
27  import org.apache.commons.chain.web.MapEntry;
28  
29  
30  /***
31   * <p>Private implementation of <code>Map</code> for servlet cookies</p>
32   *
33   * @author Niall Pemberton
34   * @version $Revision: 411948 $ $Date: 2006-06-06 00:29:02 +0100 (Tue, 06 Jun 2006) $
35   * @since Chain 1.1
36   */
37  
38  final class ServletCookieMap implements Map {
39  
40  
41      public ServletCookieMap(HttpServletRequest request) {
42          this.request = request;
43      }
44  
45  
46      private HttpServletRequest request = null;
47  
48  
49      public void clear() {
50          throw new UnsupportedOperationException();
51      }
52  
53  
54      public boolean containsKey(Object key) {
55          return (get(key) != null);
56      }
57  
58  
59      public boolean containsValue(Object value) {
60          Cookie[] cookies = request.getCookies();
61          if (cookies != null) {
62              for (int i = 0; i < cookies.length; i++) {
63                  if (cookies[i].equals(value)) {
64                      return true;
65                  }
66              }
67          }
68          return (false);
69      }
70  
71  
72      public Set entrySet() {
73          Set set = new HashSet();
74          Cookie[] cookies = request.getCookies();
75          if (cookies != null) {
76              for (int i = 0; i < cookies.length; i++) {
77                  set.add(new MapEntry(cookies[i].getName(), cookies[i], false));
78              }
79          }
80          return (set);
81      }
82  
83  
84      public boolean equals(Object o) {
85          return (request.equals(o));
86      }
87  
88  
89      public Object get(Object key) {
90          Cookie[] cookies = request.getCookies();
91          if (cookies != null) {
92              for (int i = 0; i < cookies.length; i++) {
93                  if (cookies[i].getName().equals(key(key))) {
94                      return cookies[i];
95                  }
96              }
97          }
98          return null;
99      }
100 
101 
102     public int hashCode() {
103         return (request.hashCode());
104     }
105 
106 
107     public boolean isEmpty() {
108         return (size() < 1);
109     }
110 
111 
112     public Set keySet() {
113         Set set = new HashSet();
114         Cookie[] cookies = request.getCookies();
115         if (cookies != null) {
116             for (int i = 0; i < cookies.length; i++) {
117                 set.add(cookies[i].getName());
118             }
119         }
120         return (set);
121     }
122 
123 
124     public Object put(Object key, Object value) {
125         throw new UnsupportedOperationException();
126     }
127 
128 
129     public void putAll(Map map) {
130         throw new UnsupportedOperationException();
131     }
132 
133 
134     public Object remove(Object key) {
135         throw new UnsupportedOperationException();
136     }
137 
138 
139     public int size() {
140         Cookie[] cookies = request.getCookies();
141         return (cookies == null ?  0 : cookies.length);
142     }
143 
144 
145     public Collection values() {
146         List list = new ArrayList(size());
147         Cookie[] cookies = request.getCookies();
148         if (cookies != null) {
149             for (int i = 0; i < cookies.length; i++) {
150                 list.add(cookies[i]);
151             }
152         }
153         return (list);
154     }
155 
156 
157     private String key(Object key) {
158         if (key == null) {
159             throw new IllegalArgumentException();
160         } else if (key instanceof String) {
161             return ((String) key);
162         } else {
163             return (key.toString());
164         }
165     }
166 
167 
168 }