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.Enumeration;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  import javax.servlet.http.HttpServletRequest;
29  import org.apache.commons.chain.web.MapEntry;
30  
31  
32  /**
33   * <p>Private implementation of <code>Map</code> for servlet request
34   * attributes.</p>
35   *
36   * @author Craig R. McClanahan
37   * @version $Revision: 661007 $ $Date: 2008-05-28 17:28:49 +0100 (Wed, 28 May 2008) $
38   */
39  
40  final class ServletRequestScopeMap implements Map {
41  
42  
43      public ServletRequestScopeMap(HttpServletRequest request) {
44          this.request = request;
45      }
46  
47  
48      private HttpServletRequest request = null;
49  
50  
51      public void clear() {
52          Iterator keys = keySet().iterator();
53          while (keys.hasNext()) {
54              request.removeAttribute((String) keys.next());
55          }
56      }
57  
58  
59      public boolean containsKey(Object key) {
60          return (request.getAttribute(key(key)) != null);
61      }
62  
63  
64      public boolean containsValue(Object value) {
65          if (value == null) {
66              return (false);
67          }
68          Enumeration keys = request.getAttributeNames();
69          while (keys.hasMoreElements()) {
70              Object next = request.getAttribute((String) keys.nextElement());
71              if (value.equals(next)) {
72                  return (true);
73              }
74          }
75          return (false);
76      }
77  
78  
79      public Set entrySet() {
80          Set set = new HashSet();
81          Enumeration keys = request.getAttributeNames();
82          String key;
83          while (keys.hasMoreElements()) {
84              key = (String) keys.nextElement();
85              set.add(new MapEntry(key, request.getAttribute(key), true));
86          }
87          return (set);
88      }
89  
90  
91      public boolean equals(Object o) {
92          return (request.equals(o));
93      }
94  
95  
96      public Object get(Object key) {
97          return (request.getAttribute(key(key)));
98      }
99  
100 
101     public int hashCode() {
102         return (request.hashCode());
103     }
104 
105 
106     public boolean isEmpty() {
107         return (size() < 1);
108     }
109 
110 
111     public Set keySet() {
112         Set set = new HashSet();
113         Enumeration keys = request.getAttributeNames();
114         while (keys.hasMoreElements()) {
115             set.add(keys.nextElement());
116         }
117         return (set);
118     }
119 
120 
121     public Object put(Object key, Object value) {
122         if (value == null) {
123             return (remove(key));
124         }
125         String skey = key(key);
126         Object previous = request.getAttribute(skey);
127         request.setAttribute(skey, value);
128         return (previous);
129     }
130 
131 
132     public void putAll(Map map) {
133         Iterator entries = map.entrySet().iterator();
134         while (entries.hasNext()) {
135             Map.Entry entry = (Map.Entry)entries.next();
136             put(entry.getKey(), entry.getValue());
137         }
138     }
139 
140 
141     public Object remove(Object key) {
142         String skey = key(key);
143         Object previous = request.getAttribute(skey);
144         request.removeAttribute(skey);
145         return (previous);
146     }
147 
148 
149     public int size() {
150         int n = 0;
151         Enumeration keys = request.getAttributeNames();
152         while (keys.hasMoreElements()) {
153             keys.nextElement();
154             n++;
155         }
156         return (n);
157     }
158 
159 
160     public Collection values() {
161         List list = new ArrayList();
162         Enumeration keys = request.getAttributeNames();
163         while (keys.hasMoreElements()) {
164             list.add(request.getAttribute((String) keys.nextElement()));
165         }
166         return (list);
167     }
168 
169 
170     private String key(Object key) {
171         if (key == null) {
172             throw new IllegalArgumentException();
173         } else if (key instanceof String) {
174             return ((String) key);
175         } else {
176             return (key.toString());
177         }
178     }
179 
180 
181 }