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

1   /*
2    * $Id: ServletHeaderMap.java 769961 2009-04-29 22:07:34Z 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  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.tiles.context.MapEntry;
36  
37  /***
38   * <p>Private implementation of <code>Map</code> for servlet request
39   * name-value.</p>
40   *
41   * @version $Rev: 769961 $ $Date: 2009-04-30 00:07:34 +0200 (gio, 30 apr 2009) $
42   */
43  
44  final class ServletHeaderMap implements Map<String, String> {
45  
46  
47      /***
48       * Constructor.
49       *
50       * @param request The request object to use.
51       * @deprecated Use {@link #ServletHeaderMap(HttpServletRequest,HttpServletResponse)} instead
52       */
53      public ServletHeaderMap(HttpServletRequest request) {
54          this(request, null);
55      }
56  
57  
58      /***
59       * Constructor.
60       *
61       * @param request The request object to use.
62       * @param response The response object to use.
63       * @since 2.2.0
64       */
65      public ServletHeaderMap(HttpServletRequest request, HttpServletResponse response) {
66          this.request = request;
67          this.response = response;
68      }
69  
70  
71      /***
72       * The request object to use.
73       */
74      private HttpServletRequest request = null;
75  
76      /***
77       * The request object to use.
78       */
79      private HttpServletResponse response = null;
80  
81  
82      /*** {@inheritDoc} */
83      public void clear() {
84          throw new UnsupportedOperationException();
85      }
86  
87  
88      /*** {@inheritDoc} */
89      public boolean containsKey(Object key) {
90          return (request.getHeader(key(key)) != null);
91      }
92  
93  
94      /*** {@inheritDoc} */
95      public boolean containsValue(Object value) {
96          Iterator<String> values = values().iterator();
97          while (values.hasNext()) {
98              if (value.equals(values.next())) {
99                  return (true);
100             }
101         }
102         return (false);
103     }
104 
105 
106     /*** {@inheritDoc} */
107     @SuppressWarnings("unchecked")
108     public Set<Map.Entry<String, String>> entrySet() {
109         Set<Map.Entry<String, String>> set = new HashSet<Map.Entry<String, String>>();
110         Enumeration<String> keys = request.getHeaderNames();
111         String key;
112         while (keys.hasMoreElements()) {
113             key = keys.nextElement();
114             set.add(new MapEntry<String, String>(key, request.getHeader(key),
115                     false));
116         }
117         return (set);
118     }
119 
120 
121     /*** {@inheritDoc} */
122     @SuppressWarnings("unchecked")
123     public boolean equals(Object o) {
124         HttpServletRequest otherRequest = ((ServletHeaderMap) o).request;
125         boolean retValue = true;
126         synchronized (request) {
127             for (Enumeration<String> attribs = request.getHeaderNames(); attribs
128                     .hasMoreElements()
129                     && retValue;) {
130                 String parameterName = attribs.nextElement();
131                 retValue = request.getHeader(parameterName).equals(
132                         otherRequest.getHeader(parameterName));
133             }
134         }
135 
136         return retValue;
137     }
138 
139 
140     /*** {@inheritDoc} */
141     public String get(Object key) {
142         return (request.getHeader(key(key)));
143     }
144 
145 
146     /*** {@inheritDoc} */
147     public int hashCode() {
148         return (request.hashCode());
149     }
150 
151 
152     /*** {@inheritDoc} */
153     public boolean isEmpty() {
154         return (size() < 1);
155     }
156 
157 
158     /*** {@inheritDoc} */
159     @SuppressWarnings("unchecked")
160     public Set<String> keySet() {
161         Set<String> set = new HashSet<String>();
162         Enumeration<String> keys = request.getHeaderNames();
163         while (keys.hasMoreElements()) {
164             set.add(keys.nextElement());
165         }
166         return (set);
167     }
168 
169 
170     /*** {@inheritDoc} */
171     public String put(String key, String value) {
172         response.setHeader(key, value);
173         return value;
174     }
175 
176 
177     /*** {@inheritDoc} */
178     public void putAll(Map<? extends String, ? extends String> map) {
179         for (Map.Entry<? extends String, ? extends String> entry : map
180                 .entrySet()) {
181             response.setHeader(entry.getKey(), entry.getValue());
182         }
183     }
184 
185 
186     /*** {@inheritDoc} */
187     public String remove(Object key) {
188         throw new UnsupportedOperationException();
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             list.add(request.getHeader(keys.nextElement()));
212         }
213         return (list);
214     }
215 
216 
217     /***
218      * Returns the string representation of the key.
219      *
220      * @param key The key.
221      * @return The string representation of the key.
222      * @throws IllegalArgumentException If the key is <code>null</code>.
223      */
224     private String key(Object key) {
225         if (key == null) {
226             throw new IllegalArgumentException();
227         } else if (key instanceof String) {
228             return ((String) key);
229         } else {
230             return (key.toString());
231         }
232     }
233 
234 
235 }