001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet;
006:
007: import java.util.AbstractMap;
008: import java.util.Enumeration;
009: import java.util.HashSet;
010: import java.util.Iterator;
011: import java.util.Set;
012:
013: import javax.portlet.PortletRequest;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: /**
019: * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
020: *
021: * @author Nils-Helge Garli
022: */
023: public class PortletRequestMap extends AbstractMap {
024:
025: private static final Log LOG = LogFactory
026: .getLog(PortletRequestMap.class);
027:
028: private Set entries = null;
029:
030: private PortletRequest request = null;
031:
032: /**
033: * Saves the request to use as the backing for getting and setting values
034: *
035: * @param request the portlet request.
036: */
037: public PortletRequestMap(PortletRequest request) {
038: this .request = request;
039: if (LOG.isDebugEnabled()) {
040: LOG.debug("Dumping request parameters: ");
041: Iterator params = request.getParameterMap().keySet()
042: .iterator();
043: while (params.hasNext()) {
044: String key = (String) params.next();
045: String val = request.getParameter(key);
046: LOG.debug(key + " = " + val);
047: }
048: }
049: }
050:
051: /**
052: * Removes all attributes from the request as well as clears entries in this
053: * map.
054: */
055: public void clear() {
056: entries = null;
057: Enumeration keys = request.getAttributeNames();
058:
059: while (keys.hasMoreElements()) {
060: String key = (String) keys.nextElement();
061: request.removeAttribute(key);
062: }
063: }
064:
065: /**
066: * Returns a Set of attributes from the portlet request.
067: *
068: * @return a Set of attributes from the portlet request.
069: */
070: public Set entrySet() {
071: if (entries == null) {
072: entries = new HashSet();
073:
074: Enumeration enumeration = request.getAttributeNames();
075:
076: while (enumeration.hasMoreElements()) {
077: final String key = enumeration.nextElement().toString();
078: final Object value = request.getAttribute(key);
079: entries.add(new Entry() {
080: public boolean equals(Object obj) {
081: Entry entry = (Entry) obj;
082:
083: return ((key == null) ? (entry.getKey() == null)
084: : key.equals(entry.getKey()))
085: && ((value == null) ? (entry.getValue() == null)
086: : value
087: .equals(entry
088: .getValue()));
089: }
090:
091: public int hashCode() {
092: return ((key == null) ? 0 : key.hashCode())
093: ^ ((value == null) ? 0 : value
094: .hashCode());
095: }
096:
097: public Object getKey() {
098: return key;
099: }
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: }
|