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   * name-value.</p>
35   *
36   * @author Craig R. McClanahan
37   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
38   */
39  
40  final class ServletHeaderMap implements Map {
41  
42  
43      public ServletHeaderMap(HttpServletRequest request) {
44          this.request = request;
45      }
46  
47  
48      private HttpServletRequest request = null;
49  
50  
51      public void clear() {
52          throw new UnsupportedOperationException();
53      }
54  
55  
56      public boolean containsKey(Object key) {
57          return (request.getHeader(key(key)) != null);
58      }
59  
60  
61      public boolean containsValue(Object value) {
62          Iterator values = values().iterator();
63          while (values.hasNext()) {
64              if (value.equals(values.next())) {
65                  return (true);
66              }
67          }
68          return (false);
69      }
70  
71  
72      public Set entrySet() {
73          Set set = new HashSet();
74          Enumeration keys = request.getHeaderNames();
75          String key;
76          while (keys.hasMoreElements()) {
77              key = (String) keys.nextElement();
78              set.add(new MapEntry(key, request.getHeader(key), false));
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          return (request.getHeader(key(key)));
91      }
92  
93  
94      public int hashCode() {
95          return (request.hashCode());
96      }
97  
98  
99      public boolean isEmpty() {
100         return (size() < 1);
101     }
102 
103 
104     public Set keySet() {
105         Set set = new HashSet();
106         Enumeration keys = request.getHeaderNames();
107         while (keys.hasMoreElements()) {
108             set.add(keys.nextElement());
109         }
110         return (set);
111     }
112 
113 
114     public Object put(Object key, Object value) {
115         throw new UnsupportedOperationException();
116     }
117 
118 
119     public void putAll(Map map) {
120         throw new UnsupportedOperationException();
121     }
122 
123 
124     public Object remove(Object key) {
125         throw new UnsupportedOperationException();
126     }
127 
128 
129     public int size() {
130         int n = 0;
131         Enumeration keys = request.getHeaderNames();
132         while (keys.hasMoreElements()) {
133             keys.nextElement();
134             n++;
135         }
136         return (n);
137     }
138 
139 
140     public Collection values() {
141         List list = new ArrayList();
142         Enumeration keys = request.getHeaderNames();
143         while (keys.hasMoreElements()) {
144             list.add(request.getHeader((String) keys.nextElement()));
145         }
146         return (list);
147     }
148 
149 
150     private String key(Object key) {
151         if (key == null) {
152             throw new IllegalArgumentException();
153         } else if (key instanceof String) {
154             return ((String) key);
155         } else {
156             return (key.toString());
157         }
158     }
159 
160 
161 }