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

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