View Javadoc

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