001: /*
002: * $Id: PortletRequestMap.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet;
022:
023: import java.util.AbstractMap;
024: import java.util.Enumeration;
025: import java.util.HashSet;
026: import java.util.Iterator;
027: import java.util.Set;
028:
029: import javax.portlet.PortletRequest;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
036: *
037: */
038: public class PortletRequestMap extends AbstractMap {
039:
040: private static final Log LOG = LogFactory
041: .getLog(PortletRequestMap.class);
042:
043: private Set<Object> entries = null;
044: private PortletRequest request = null;
045:
046: /**
047: * Saves the request to use as the backing for getting and setting values
048: *
049: * @param request the portlet request.
050: */
051: public PortletRequestMap(PortletRequest request) {
052: this .request = request;
053: if (LOG.isDebugEnabled()) {
054: LOG.debug("Dumping request parameters: ");
055: Iterator params = request.getParameterMap().keySet()
056: .iterator();
057: while (params.hasNext()) {
058: String key = (String) params.next();
059: String val = request.getParameter(key);
060: LOG.debug(key + " = " + val);
061: }
062: }
063: }
064:
065: /**
066: * Removes all attributes from the request as well as clears entries in this
067: * map.
068: */
069: public void clear() {
070: entries = null;
071: Enumeration keys = request.getAttributeNames();
072:
073: while (keys.hasMoreElements()) {
074: String key = (String) keys.nextElement();
075: request.removeAttribute(key);
076: }
077: }
078:
079: /**
080: * Returns a Set of attributes from the portlet request.
081: *
082: * @return a Set of attributes from the portlet request.
083: */
084: public Set entrySet() {
085: if (entries == null) {
086: entries = new HashSet<Object>();
087:
088: Enumeration enumeration = request.getAttributeNames();
089:
090: while (enumeration.hasMoreElements()) {
091: final String key = enumeration.nextElement().toString();
092: final Object value = request.getAttribute(key);
093: entries.add(new Entry() {
094: public boolean equals(Object obj) {
095: Entry entry = (Entry) obj;
096:
097: return ((key == null) ? (entry.getKey() == null)
098: : key.equals(entry.getKey()))
099: && ((value == null) ? (entry.getValue() == null)
100: : value
101: .equals(entry
102: .getValue()));
103: }
104:
105: public int hashCode() {
106: return ((key == null) ? 0 : key.hashCode())
107: ^ ((value == null) ? 0 : value
108: .hashCode());
109: }
110:
111: public Object getKey() {
112: return key;
113: }
114:
115: public Object getValue() {
116: return value;
117: }
118:
119: public Object setValue(Object obj) {
120: request.setAttribute(key, obj);
121:
122: return value;
123: }
124: });
125: }
126: }
127:
128: return entries;
129: }
130:
131: /**
132: * Returns the request attribute associated with the given key or
133: * <tt>null</tt> if it doesn't exist.
134: *
135: * @param key the name of the request attribute.
136: * @return the request attribute or <tt>null</tt> if it doesn't exist.
137: */
138: public Object get(Object key) {
139: return request.getAttribute(key.toString());
140: }
141:
142: /**
143: * Saves an attribute in the request.
144: *
145: * @param key the name of the request attribute.
146: * @param value the value to set.
147: * @return the object that was just set.
148: */
149: public Object put(Object key, Object value) {
150: entries = null;
151: request.setAttribute(key.toString(), value);
152:
153: return get(key);
154: }
155:
156: /**
157: * Removes the specified request attribute.
158: *
159: * @param key the name of the attribute to remove.
160: * @return the value that was removed or <tt>null</tt> if the value was
161: * not found (and hence, not removed).
162: */
163: public Object remove(Object key) {
164: entries = null;
165:
166: Object value = get(key);
167: request.removeAttribute(key.toString());
168:
169: return value;
170: }
171:
172: }
|