View Javadoc

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