This project has retired. For details please refer to its Attic page.
ServletHeaderValuesMap xref
View Javadoc

1   /*
2    * $Id: ServletHeaderValuesMap.java 652862 2008-05-02 18:22:56Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.servlet.context;
22  
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Enumeration;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  
33  import javax.servlet.http.HttpServletRequest;
34  
35  import org.apache.tiles.context.MapEntry;
36  
37  
38  /***
39   * <p>Private implementation of <code>Map</code> for servlet request
40   * name-values[].</p>
41   *
42   * @version $Rev: 652862 $ $Date: 2008-05-02 20:22:56 +0200 (ven, 02 mag 2008) $
43   */
44  
45  final class ServletHeaderValuesMap implements Map<String, String[]> {
46  
47  
48      /***
49       * Constructor.
50       *
51       * @param request The request object to use.
52       */
53      public ServletHeaderValuesMap(HttpServletRequest request) {
54          this.request = request;
55      }
56  
57  
58      /***
59       * The request object to use.
60       */
61      private HttpServletRequest request = null;
62  
63  
64      /*** {@inheritDoc} */
65      public void clear() {
66          throw new UnsupportedOperationException();
67      }
68  
69  
70      /*** {@inheritDoc} */
71      public boolean containsKey(Object key) {
72          return (request.getHeader(key(key)) != null);
73      }
74  
75  
76      /*** {@inheritDoc} */
77      public boolean containsValue(Object value) {
78          if (!(value instanceof String[])) {
79              return (false);
80          }
81          String[] test = (String[]) value;
82          Iterator<String[]> values = values().iterator();
83          while (values.hasNext()) {
84              String[] actual = values.next();
85              if (test.length == actual.length) {
86                  boolean matched = true;
87                  for (int i = 0; i < test.length; i++) {
88                      if (!test[i].equals(actual[i])) {
89                          matched = false;
90                          break;
91                      }
92                  }
93                  if (matched) {
94                      return (true);
95                  }
96              }
97          }
98          return (false);
99      }
100 
101 
102     /*** {@inheritDoc} */
103     @SuppressWarnings("unchecked")
104     public Set<Map.Entry<String, String[]>> entrySet() {
105         Set<Map.Entry<String, String[]>> set = new HashSet<Map.Entry<String, String[]>>();
106         Enumeration<String> keys = request.getHeaderNames();
107         String key;
108         while (keys.hasMoreElements()) {
109             key = keys.nextElement();
110             Enumeration<String> headerEnum = request.getHeaders(key);
111             set.add(new MapEntry<String, String[]>(key,
112                     enumeration2array(headerEnum), false));
113         }
114         return (set);
115     }
116 
117 
118     /*** {@inheritDoc} */
119     @SuppressWarnings("unchecked")
120     public boolean equals(Object o) {
121         HttpServletRequest otherRequest = ((ServletHeaderValuesMap) o).request;
122         boolean retValue = true;
123         synchronized (request) {
124             for (Enumeration<String> attribs = request.getHeaderNames(); attribs
125                     .hasMoreElements()
126                     && retValue;) {
127                 String parameterName = attribs.nextElement();
128                 retValue = request.getHeaders(parameterName).equals(
129                         otherRequest.getHeaders(parameterName));
130             }
131         }
132 
133         return retValue;
134     }
135 
136 
137     /*** {@inheritDoc} */
138     @SuppressWarnings("unchecked")
139     public String[] get(Object key) {
140         List<String> list = new ArrayList<String>();
141         Enumeration<String> values = request.getHeaders(key(key));
142         while (values.hasMoreElements()) {
143             list.add(values.nextElement());
144         }
145         return ((list.toArray(new String[list.size()])));
146     }
147 
148 
149     /*** {@inheritDoc} */
150     public int hashCode() {
151         return (request.hashCode());
152     }
153 
154 
155     /*** {@inheritDoc} */
156     public boolean isEmpty() {
157         return (size() < 1);
158     }
159 
160 
161     /*** {@inheritDoc} */
162     @SuppressWarnings("unchecked")
163     public Set<String> keySet() {
164         Set<String> set = new HashSet<String>();
165         Enumeration<String> keys = request.getHeaderNames();
166         while (keys.hasMoreElements()) {
167             set.add(keys.nextElement());
168         }
169         return (set);
170     }
171 
172 
173     /*** {@inheritDoc} */
174     public String[] put(String key, String[] value) {
175         throw new UnsupportedOperationException();
176     }
177 
178 
179     /*** {@inheritDoc} */
180     public void putAll(Map<? extends String, ? extends String[]> map) {
181         throw new UnsupportedOperationException();
182     }
183 
184 
185     /*** {@inheritDoc} */
186     public String[] remove(Object key) {
187         throw new UnsupportedOperationException();
188     }
189 
190 
191 
192     /*** {@inheritDoc} */
193     @SuppressWarnings("unchecked")
194     public int size() {
195         int n = 0;
196         Enumeration<String> keys = request.getHeaderNames();
197         while (keys.hasMoreElements()) {
198             keys.nextElement();
199             n++;
200         }
201         return (n);
202     }
203 
204 
205     /*** {@inheritDoc} */
206     @SuppressWarnings("unchecked")
207     public Collection<String[]> values() {
208         List<String[]> list = new ArrayList<String[]>();
209         Enumeration<String> keys = request.getHeaderNames();
210         while (keys.hasMoreElements()) {
211             String key = keys.nextElement();
212             Enumeration<String> values = request.getHeaders(key);
213             list.add(enumeration2array(values));
214         }
215         return (list);
216     }
217 
218 
219     /***
220      * Returns the string representation of the key.
221      *
222      * @param key The key.
223      * @return The string representation of the key.
224      * @throws IllegalArgumentException If the key is <code>null</code>.
225      */
226     private String key(Object key) {
227         if (key == null) {
228             throw new IllegalArgumentException();
229         } else if (key instanceof String) {
230             return ((String) key);
231         } else {
232             return (key.toString());
233         }
234     }
235 
236     /***
237      * Converts the content of a string enumeration to an array of strings.
238      *
239      * @param enumeration The enumeration to convert.
240      * @return The corresponding array.
241      */
242     private String[] enumeration2array(Enumeration<String> enumeration) {
243         List<String> list1 = new ArrayList<String>();
244         while (enumeration.hasMoreElements()) {
245             list1.add(enumeration.nextElement());
246         }
247 
248         return list1.toArray(new String[list1.size()]);
249     }
250 }