View Javadoc

1   /*
2    * $Id: RequestMap.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher;
19  
20  import java.io.Serializable;
21  import java.util.AbstractMap;
22  import java.util.Enumeration;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  
29  /***
30   * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
31   */
32  public class RequestMap extends AbstractMap implements Serializable {
33  
34  	private static final long serialVersionUID = -7675640869293787926L;
35  	
36  	private Set<Object> entries;
37      private HttpServletRequest request;
38  
39  
40      /***
41       * Saves the request to use as the backing for getting and setting values
42       *
43       * @param request the http servlet request.
44       */
45      public RequestMap(final HttpServletRequest request) {
46          this.request = request;
47      }
48  
49  
50      /***
51       * Removes all attributes from the request as well as clears entries in this map.
52       */
53      public void clear() {
54          entries = null;
55          Enumeration keys = request.getAttributeNames();
56  
57          while (keys.hasMoreElements()) {
58              String key = (String) keys.nextElement();
59              request.removeAttribute(key);
60          }
61      }
62  
63      /***
64       * Returns a Set of attributes from the http request.
65       *
66       * @return a Set of attributes from the http request.
67       */
68      public Set entrySet() {
69          if (entries == null) {
70              entries = new HashSet<Object>();
71  
72              Enumeration enumeration = request.getAttributeNames();
73  
74              while (enumeration.hasMoreElements()) {
75                  final String key = enumeration.nextElement().toString();
76                  final Object value = request.getAttribute(key);
77                  entries.add(new Entry() {
78                      public boolean equals(Object obj) {
79                          Entry entry = (Entry) obj;
80  
81                          return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
82                      }
83  
84                      public int hashCode() {
85                          return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
86                      }
87  
88                      public Object getKey() {
89                          return key;
90                      }
91  
92                      public Object getValue() {
93                          return value;
94                      }
95  
96                      public Object setValue(Object obj) {
97                          request.setAttribute(key.toString(), obj);
98  
99                          return value;
100                     }
101                 });
102             }
103         }
104 
105         return entries;
106     }
107 
108     /***
109      * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
110      *
111      * @param key the name of the request attribute.
112      * @return the request attribute or <tt>null</tt> if it doesn't exist.
113      */
114     public Object get(Object key) {
115         return request.getAttribute(key.toString());
116     }
117 
118     /***
119      * Saves an attribute in the request.
120      *
121      * @param key   the name of the request attribute.
122      * @param value the value to set.
123      * @return the object that was just set.
124      */
125     public Object put(Object key, Object value) {
126         entries = null;
127         request.setAttribute(key.toString(), value);
128 
129         return get(key);
130     }
131 
132     /***
133      * Removes the specified request attribute.
134      *
135      * @param key the name of the attribute to remove.
136      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
137      */
138     public Object remove(Object key) {
139         entries = null;
140 
141         Object value = get(key);
142         request.removeAttribute(key.toString());
143 
144         return value;
145     }
146 }