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

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