View Javadoc

1   /*
2    * $Id: RequestMap.java 471756 2006-11-06 15:01:43Z husted $
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.struts2.dispatcher;
22  
23  import java.io.Serializable;
24  import java.util.AbstractMap;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.Set;
28  
29  import javax.servlet.http.HttpServletRequest;
30  
31  
32  /***
33   * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
34   */
35  public class RequestMap extends AbstractMap implements Serializable {
36  
37      private static final long serialVersionUID = -7675640869293787926L;
38  
39      private Set<Object> entries;
40      private HttpServletRequest request;
41  
42  
43      /***
44       * Saves the request to use as the backing for getting and setting values
45       *
46       * @param request the http servlet request.
47       */
48      public RequestMap(final HttpServletRequest request) {
49          this.request = request;
50      }
51  
52  
53      /***
54       * Removes all attributes from the request as well as clears entries in this map.
55       */
56      public void clear() {
57          entries = null;
58          Enumeration keys = request.getAttributeNames();
59  
60          while (keys.hasMoreElements()) {
61              String key = (String) keys.nextElement();
62              request.removeAttribute(key);
63          }
64      }
65  
66      /***
67       * Returns a Set of attributes from the http request.
68       *
69       * @return a Set of attributes from the http request.
70       */
71      public Set entrySet() {
72          if (entries == null) {
73              entries = new HashSet<Object>();
74  
75              Enumeration enumeration = request.getAttributeNames();
76  
77              while (enumeration.hasMoreElements()) {
78                  final String key = enumeration.nextElement().toString();
79                  final Object value = request.getAttribute(key);
80                  entries.add(new Entry() {
81                      public boolean equals(Object obj) {
82                          Entry entry = (Entry) obj;
83  
84                          return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
85                      }
86  
87                      public int hashCode() {
88                          return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
89                      }
90  
91                      public Object getKey() {
92                          return key;
93                      }
94  
95                      public Object getValue() {
96                          return value;
97                      }
98  
99                      public Object setValue(Object obj) {
100                         request.setAttribute(key.toString(), obj);
101 
102                         return value;
103                     }
104                 });
105             }
106         }
107 
108         return entries;
109     }
110 
111     /***
112      * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
113      *
114      * @param key the name of the request attribute.
115      * @return the request attribute or <tt>null</tt> if it doesn't exist.
116      */
117     public Object get(Object key) {
118         return request.getAttribute(key.toString());
119     }
120 
121     /***
122      * Saves an attribute in the request.
123      *
124      * @param key   the name of the request attribute.
125      * @param value the value to set.
126      * @return the object that was just set.
127      */
128     public Object put(Object key, Object value) {
129         entries = null;
130         request.setAttribute(key.toString(), value);
131 
132         return get(key);
133     }
134 
135     /***
136      * Removes the specified request attribute.
137      *
138      * @param key the name of the attribute to remove.
139      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
140      */
141     public Object remove(Object key) {
142         entries = null;
143 
144         Object value = get(key);
145         request.removeAttribute(key.toString());
146 
147         return value;
148     }
149 }