View Javadoc

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