View Javadoc

1   /*
2    * Copyright 1999-2004 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.Enumeration;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import javax.servlet.http.HttpServletRequest;
28  import org.apache.commons.chain.web.MapEntry;
29  
30  
31  /***
32   * <p>Private implementation of <code>Map</code> for servlet request
33   * name-values[].</p>
34   *
35   * @author Craig R. McClanahan
36   * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
37   */
38  
39  final class ServletHeaderValuesMap implements Map {
40  
41  
42      public ServletHeaderValuesMap(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 (request.getHeader(key(key)) != null);
57      }
58  
59  
60      public boolean containsValue(Object value) {
61          if (!(value instanceof String[])) {
62              return (false);
63          }
64          String[] test = (String[]) value;
65          Iterator values = values().iterator();
66          while (values.hasNext()) {
67              String[] actual = (String[]) values.next();
68              if (test.length == actual.length) {
69                  boolean matched = true;
70                  for (int i = 0; i < test.length; i++) {
71                      if (!test[i].equals(actual[i])) {
72                          matched = false;
73                          break;
74                      }
75                  }
76                  if (matched) {
77                      return (true);
78                  }
79              }
80          }
81          return (false);
82      }
83  
84  
85      public Set entrySet() {
86          Set set = new HashSet();
87          Enumeration keys = request.getHeaderNames();
88          String key;
89          while (keys.hasMoreElements()) {
90              key = (String) keys.nextElement();
91              set.add(new MapEntry(key, request.getHeaders(key), false));
92          }
93          return (set);
94      }
95  
96  
97      public boolean equals(Object o) {
98          return (request.equals(o));
99      }
100 
101 
102     public Object get(Object key) {
103         List list = new ArrayList();
104         Enumeration values = request.getHeaders(key(key));
105         while (values.hasMoreElements()) {
106             list.add((String) values.nextElement());
107         }
108         return (((String[]) list.toArray(new String[list.size()])));
109     }
110 
111 
112     public int hashCode() {
113         return (request.hashCode());
114     }
115 
116 
117     public boolean isEmpty() {
118         return (size() < 1);
119     }
120 
121 
122     public Set keySet() {
123         Set set = new HashSet();
124         Enumeration keys = request.getHeaderNames();
125         while (keys.hasMoreElements()) {
126             set.add(keys.nextElement());
127         }
128         return (set);
129     }
130 
131 
132     public Object put(Object key, Object value) {
133         throw new UnsupportedOperationException();
134     }
135 
136 
137     public void putAll(Map map) {
138         throw new UnsupportedOperationException();
139     }
140 
141 
142     public Object remove(Object key) {
143         throw new UnsupportedOperationException();
144     }
145 
146 
147     public int size() {
148         int n = 0;
149         Enumeration keys = request.getHeaderNames();
150         while (keys.hasMoreElements()) {
151             keys.nextElement();
152             n++;
153         }
154         return (n);
155     }
156 
157 
158     public Collection values() {
159         List list = new ArrayList();
160         Enumeration keys = request.getHeaderNames();
161         while (keys.hasMoreElements()) {
162             String key = (String) keys.nextElement();
163             List list1 = new ArrayList();
164             Enumeration values = request.getHeaders(key);
165             while (values.hasMoreElements()) {
166                 list1.add((String) values.nextElement());
167             }
168             list.add(((String[]) list1.toArray(new String[list1.size()])));
169         }
170         return (list);
171     }
172 
173 
174     private String key(Object key) {
175         if (key == null) {
176             throw new IllegalArgumentException();
177         } else if (key instanceof String) {
178             return ((String) key);
179         } else {
180             return (key.toString());
181         }
182     }
183 
184 
185 }