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

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