View Javadoc

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