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

1   /*
2    * $Id: ServletParamValuesMap.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  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  
34  import org.apache.tiles.context.MapEntry;
35  
36  
37  /***
38   * <p>Private implementation of <code>Map</code> for servlet parameter
39   * name-values[].</p>
40   *
41   * @version $Rev: 652862 $ $Date: 2008-05-02 20:22:56 +0200 (ven, 02 mag 2008) $
42   */
43  
44  final class ServletParamValuesMap implements Map<String, String[]> {
45  
46  
47      /***
48       * Constructor.
49       *
50       * @param request The request object to use.
51       */
52      public ServletParamValuesMap(ServletRequest request) {
53          this.request = request;
54      }
55  
56  
57      /***
58       * The request object to use.
59       */
60      private ServletRequest 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          if (!(value instanceof String[])) {
78              return (false);
79          }
80          String[] test = (String[]) value;
81          Iterator<String[]> values = values().iterator();
82          while (values.hasNext()) {
83              String[] actual = values.next();
84              if (test.length == actual.length) {
85                  boolean matched = true;
86                  for (int i = 0; i < test.length; i++) {
87                      if (!test[i].equals(actual[i])) {
88                          matched = false;
89                          break;
90                      }
91                  }
92                  if (matched) {
93                      return (true);
94                  }
95              }
96          }
97          return (false);
98      }
99  
100 
101     /*** {@inheritDoc} */
102     @SuppressWarnings("unchecked")
103     public Set<Map.Entry<String, String[]>> entrySet() {
104         Set<Map.Entry<String, String[]>> set = new HashSet<Map.Entry<String, String[]>>();
105         Enumeration<String> keys = request.getParameterNames();
106         String key;
107         while (keys.hasMoreElements()) {
108             key = keys.nextElement();
109             set.add(new MapEntry<String, String[]>(key, request
110                     .getParameterValues(key), false));
111         }
112         return (set);
113     }
114 
115 
116     /*** {@inheritDoc} */
117     @SuppressWarnings("unchecked")
118     public boolean equals(Object o) {
119         ServletRequest otherRequest = ((ServletParamValuesMap) o).request;
120         boolean retValue = true;
121         synchronized (request) {
122             for (Enumeration<String> attribs = request.getParameterNames(); attribs
123                     .hasMoreElements()
124                     && retValue;) {
125                 String parameterName = attribs.nextElement();
126                 retValue = request.getParameterValues(parameterName).equals(
127                         otherRequest.getParameterValues(parameterName));
128             }
129         }
130 
131         return retValue;
132     }
133 
134 
135     /*** {@inheritDoc} */
136     public String[] get(Object key) {
137         return (request.getParameterValues(key(key)));
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.getParameterNames();
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     /*** {@inheritDoc} */
184     @SuppressWarnings("unchecked")
185     public int size() {
186         int n = 0;
187         Enumeration<String> keys = request.getParameterNames();
188         while (keys.hasMoreElements()) {
189             keys.nextElement();
190             n++;
191         }
192         return (n);
193     }
194 
195 
196     /*** {@inheritDoc} */
197     @SuppressWarnings("unchecked")
198     public Collection<String[]> values() {
199         List<String[]> list = new ArrayList<String[]>();
200         Enumeration<String> keys = request.getParameterNames();
201         while (keys.hasMoreElements()) {
202             list.add(request.getParameterValues(keys.nextElement()));
203         }
204         return (list);
205     }
206 
207 
208     /***
209      * Returns the string representation of the key.
210      *
211      * @param key The key.
212      * @return The string representation of the key.
213      * @throws IllegalArgumentException If the key is <code>null</code>.
214      */
215     private String key(Object key) {
216         if (key == null) {
217             throw new IllegalArgumentException();
218         } else if (key instanceof String) {
219             return ((String) key);
220         } else {
221             return (key.toString());
222         }
223     }
224 
225 
226 }