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

1   /*
2    * $Id: ServletHeaderValuesMap.java 581987 2007-10-04 19:03:29Z 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 org.apache.tiles.context.MapEntry;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import java.util.*;
28  
29  
30  /***
31   * <p>Private implementation of <code>Map</code> for servlet request
32   * name-values[].</p>
33   *
34   * @version $Rev: 581987 $ $Date: 2007-10-04 21:03:29 +0200 (Thu, 04 Oct 2007) $
35   */
36  
37  final class ServletHeaderValuesMap implements Map<String, String[]> {
38  
39  
40      /***
41       * Constructor.
42       *
43       * @param request The request object to use.
44       */
45      public ServletHeaderValuesMap(HttpServletRequest request) {
46          this.request = request;
47      }
48  
49  
50      /***
51       * The request object to use.
52       */
53      private HttpServletRequest request = null;
54  
55  
56      /*** {@inheritDoc} */
57      public void clear() {
58          throw new UnsupportedOperationException();
59      }
60  
61  
62      /*** {@inheritDoc} */
63      public boolean containsKey(Object key) {
64          return (request.getHeader(key(key)) != null);
65      }
66  
67  
68      /*** {@inheritDoc} */
69      public boolean containsValue(Object value) {
70          if (!(value instanceof String[])) {
71              return (false);
72          }
73          String[] test = (String[]) value;
74          Iterator<String[]> values = values().iterator();
75          while (values.hasNext()) {
76              String[] actual = values.next();
77              if (test.length == actual.length) {
78                  boolean matched = true;
79                  for (int i = 0; i < test.length; i++) {
80                      if (!test[i].equals(actual[i])) {
81                          matched = false;
82                          break;
83                      }
84                  }
85                  if (matched) {
86                      return (true);
87                  }
88              }
89          }
90          return (false);
91      }
92  
93  
94      /*** {@inheritDoc} */
95      @SuppressWarnings("unchecked")
96      public Set<Map.Entry<String, String[]>> entrySet() {
97          Set<Map.Entry<String, String[]>> set = new HashSet<Map.Entry<String, String[]>>();
98          Enumeration<String> keys = request.getHeaderNames();
99          String key;
100         while (keys.hasMoreElements()) {
101             key = keys.nextElement();
102             Enumeration<String> headerEnum = request.getHeaders(key);
103             set.add(new MapEntry<String, String[]>(key,
104                     enumeration2array(headerEnum), false));
105         }
106         return (set);
107     }
108 
109 
110     /*** {@inheritDoc} */
111     @SuppressWarnings("unchecked")
112 	public boolean equals(Object o) {
113         HttpServletRequest otherRequest = ((ServletHeaderValuesMap) o).request;
114         boolean retValue = true;
115         synchronized (request) {
116             for (Enumeration<String> attribs = request.getHeaderNames(); attribs
117                     .hasMoreElements()
118                     && retValue;) {
119                 String parameterName = attribs.nextElement();
120                 retValue = request.getHeaders(parameterName).equals(
121                         otherRequest.getHeaders(parameterName));
122             }
123         }
124 
125         return retValue;
126     }
127 
128 
129     /*** {@inheritDoc} */
130     @SuppressWarnings("unchecked")
131 	public String[] get(Object key) {
132         List<String> list = new ArrayList<String>();
133         Enumeration<String> values = request.getHeaders(key(key));
134         while (values.hasMoreElements()) {
135             list.add(values.nextElement());
136         }
137         return ((list.toArray(new String[list.size()])));
138     }
139 
140 
141     /*** {@inheritDoc} */
142     public int hashCode() {
143         return (request.hashCode());
144     }
145 
146 
147     /*** {@inheritDoc} */
148     public boolean isEmpty() {
149         return (size() < 1);
150     }
151 
152 
153     /*** {@inheritDoc} */
154     @SuppressWarnings("unchecked")
155     public Set<String> keySet() {
156         Set<String> set = new HashSet<String>();
157         Enumeration<String> keys = request.getHeaderNames();
158         while (keys.hasMoreElements()) {
159             set.add(keys.nextElement());
160         }
161         return (set);
162     }
163 
164 
165     /*** {@inheritDoc} */
166     public String[] put(String key, String[] value) {
167         throw new UnsupportedOperationException();
168     }
169 
170 
171     /*** {@inheritDoc} */
172     public void putAll(Map<? extends String, ? extends String[]> map) {
173         throw new UnsupportedOperationException();
174     }
175 
176 
177     /*** {@inheritDoc} */
178     public String[] remove(Object key) {
179         throw new UnsupportedOperationException();
180     }
181 
182 
183 
184     /*** {@inheritDoc} */
185     @SuppressWarnings("unchecked")
186     public int size() {
187         int n = 0;
188         Enumeration<String> keys = request.getHeaderNames();
189         while (keys.hasMoreElements()) {
190             keys.nextElement();
191             n++;
192         }
193         return (n);
194     }
195 
196 
197     /*** {@inheritDoc} */
198     @SuppressWarnings("unchecked")
199 	public Collection<String[]> values() {
200         List<String[]> list = new ArrayList<String[]>();
201         Enumeration<String> keys = request.getHeaderNames();
202         while (keys.hasMoreElements()) {
203             String key = keys.nextElement();
204             Enumeration<String> values = request.getHeaders(key);
205             list.add(enumeration2array(values));
206         }
207         return (list);
208     }
209 
210 
211     /***
212      * Returns the string representation of the key.
213      *
214      * @param key The key.
215      * @return The string representation of the key.
216      * @throws IllegalArgumentException If the key is <code>null</code>.
217      */
218     private String key(Object key) {
219         if (key == null) {
220             throw new IllegalArgumentException();
221         } else if (key instanceof String) {
222             return ((String) key);
223         } else {
224             return (key.toString());
225         }
226     }
227 
228     /***
229      * Converts the content of a string enumeration to an array of strings.
230      *
231      * @param enumeration The enumeration to convert.
232      * @return The corresponding array.
233      */
234     private String[] enumeration2array(Enumeration<String> enumeration) {
235         List<String> list1 = new ArrayList<String>();
236         while (enumeration.hasMoreElements()) {
237             list1.add(enumeration.nextElement());
238         }
239         
240         return list1.toArray(new String[list1.size()]);
241     }
242 }