001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import javax.servlet.http.HttpServletRequest;
008: import java.io.Serializable;
009: import java.util.AbstractMap;
010: import java.util.Enumeration;
011: import java.util.HashSet;
012: import java.util.Set;
013:
014: /**
015: * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
016: *
017: * @author Patrick Lightbody
018: * @author Bill Lynch (docs)
019: */
020: public class RequestMap extends AbstractMap implements Serializable {
021:
022: Set entries;
023: private HttpServletRequest request;
024:
025: /**
026: * Saves the request to use as the backing for getting and setting values
027: *
028: * @param request the http servlet request.
029: */
030: public RequestMap(final HttpServletRequest request) {
031: this .request = request;
032: }
033:
034: /**
035: * Removes all attributes from the request as well as clears entries in this map.
036: */
037: public void clear() {
038: entries = null;
039: Enumeration keys = request.getAttributeNames();
040:
041: while (keys.hasMoreElements()) {
042: String key = (String) keys.nextElement();
043: request.removeAttribute(key);
044: }
045: }
046:
047: /**
048: * Returns a Set of attributes from the http request.
049: *
050: * @return a Set of attributes from the http request.
051: */
052: public Set entrySet() {
053: if (entries == null) {
054: entries = new HashSet();
055:
056: Enumeration enumeration = request.getAttributeNames();
057:
058: while (enumeration.hasMoreElements()) {
059: final String key = enumeration.nextElement().toString();
060: final Object value = request.getAttribute(key);
061: entries.add(new Entry() {
062: public boolean equals(Object obj) {
063: Entry entry = (Entry) obj;
064:
065: return ((key == null) ? (entry.getKey() == null)
066: : key.equals(entry.getKey()))
067: && ((value == null) ? (entry.getValue() == null)
068: : value
069: .equals(entry
070: .getValue()));
071: }
072:
073: public int hashCode() {
074: return ((key == null) ? 0 : key.hashCode())
075: ^ ((value == null) ? 0 : value
076: .hashCode());
077: }
078:
079: public Object getKey() {
080: return key;
081: }
082:
083: public Object getValue() {
084: return value;
085: }
086:
087: public Object setValue(Object obj) {
088: request.setAttribute(key.toString(), obj);
089:
090: return value;
091: }
092: });
093: }
094: }
095:
096: return entries;
097: }
098:
099: /**
100: * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
101: *
102: * @param key the name of the request attribute.
103: * @return the request attribute or <tt>null</tt> if it doesn't exist.
104: */
105: public Object get(Object key) {
106: return request.getAttribute(key.toString());
107: }
108:
109: /**
110: * Saves an attribute in the request.
111: *
112: * @param key the name of the request attribute.
113: * @param value the value to set.
114: * @return the object that was just set.
115: */
116: public Object put(Object key, Object value) {
117: entries = null;
118: request.setAttribute(key.toString(), value);
119:
120: return get(key);
121: }
122:
123: /**
124: * Removes the specified request attribute.
125: *
126: * @param key the name of the attribute to remove.
127: * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
128: */
129: public Object remove(Object key) {
130: entries = null;
131:
132: Object value = get(key);
133: request.removeAttribute(key.toString());
134:
135: return value;
136: }
137: }
|