View Javadoc

1   /*
2    * $Id: RequestMap.java 651946 2008-04-27 13:41:38Z 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  
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                          Entry entry = (Entry) obj;
84  
85                          return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue()));
86                      }
87  
88                      public int hashCode() {
89                          return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode());
90                      }
91  
92                      public Object getKey() {
93                          return key;
94                      }
95  
96                      public Object getValue() {
97                          return value;
98                      }
99  
100                     public Object setValue(Object obj) {
101                         request.setAttribute(key, obj);
102 
103                         return value;
104                     }
105                 });
106             }
107         }
108 
109         return entries;
110     }
111 
112     /***
113      * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
114      *
115      * @param key the name of the request attribute.
116      * @return the request attribute or <tt>null</tt> if it doesn't exist.
117      */
118     public Object get(Object key) {
119         return request.getAttribute(key.toString());
120     }
121 
122     /***
123      * Saves an attribute in the request.
124      *
125      * @param key   the name of the request attribute.
126      * @param value the value to set.
127      * @return the object that was just set.
128      */
129     public Object put(Object key, Object value) {
130         entries = null;
131         request.setAttribute(key.toString(), value);
132 
133         return get(key);
134     }
135 
136     /***
137      * Removes the specified request attribute.
138      *
139      * @param key the name of the attribute to remove.
140      * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
141      */
142     public Object remove(Object key) {
143         entries = null;
144 
145         Object value = get(key);
146         request.removeAttribute(key.toString());
147 
148         return value;
149     }
150 }