001: /*
002: * $Id: RequestMap.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.dispatcher;
022:
023: import java.io.Serializable;
024: import java.util.AbstractMap;
025: import java.util.Enumeration;
026: import java.util.HashSet;
027: import java.util.Set;
028:
029: import javax.servlet.http.HttpServletRequest;
030:
031: /**
032: * A simple implementation of the {@link java.util.Map} interface to handle a collection of request attributes.
033: */
034: public class RequestMap extends AbstractMap implements Serializable {
035:
036: private static final long serialVersionUID = -7675640869293787926L;
037:
038: private Set<Object> entries;
039: private HttpServletRequest request;
040:
041: /**
042: * Saves the request to use as the backing for getting and setting values
043: *
044: * @param request the http servlet request.
045: */
046: public RequestMap(final HttpServletRequest request) {
047: this .request = request;
048: }
049:
050: /**
051: * Removes all attributes from the request as well as clears entries in this map.
052: */
053: public void clear() {
054: entries = null;
055: Enumeration keys = request.getAttributeNames();
056:
057: while (keys.hasMoreElements()) {
058: String key = (String) keys.nextElement();
059: request.removeAttribute(key);
060: }
061: }
062:
063: /**
064: * Returns a Set of attributes from the http request.
065: *
066: * @return a Set of attributes from the http request.
067: */
068: public Set entrySet() {
069: if (entries == null) {
070: entries = new HashSet<Object>();
071:
072: Enumeration enumeration = request.getAttributeNames();
073:
074: while (enumeration.hasMoreElements()) {
075: final String key = enumeration.nextElement().toString();
076: final Object value = request.getAttribute(key);
077: entries.add(new Entry() {
078: public boolean equals(Object obj) {
079: Entry entry = (Entry) obj;
080:
081: return ((key == null) ? (entry.getKey() == null)
082: : key.equals(entry.getKey()))
083: && ((value == null) ? (entry.getValue() == null)
084: : value
085: .equals(entry
086: .getValue()));
087: }
088:
089: public int hashCode() {
090: return ((key == null) ? 0 : key.hashCode())
091: ^ ((value == null) ? 0 : value
092: .hashCode());
093: }
094:
095: public Object getKey() {
096: return key;
097: }
098:
099: public Object getValue() {
100: return value;
101: }
102:
103: public Object setValue(Object obj) {
104: request.setAttribute(key.toString(), obj);
105:
106: return value;
107: }
108: });
109: }
110: }
111:
112: return entries;
113: }
114:
115: /**
116: * Returns the request attribute associated with the given key or <tt>null</tt> if it doesn't exist.
117: *
118: * @param key the name of the request attribute.
119: * @return the request attribute or <tt>null</tt> if it doesn't exist.
120: */
121: public Object get(Object key) {
122: return request.getAttribute(key.toString());
123: }
124:
125: /**
126: * Saves an attribute in the request.
127: *
128: * @param key the name of the request attribute.
129: * @param value the value to set.
130: * @return the object that was just set.
131: */
132: public Object put(Object key, Object value) {
133: entries = null;
134: request.setAttribute(key.toString(), value);
135:
136: return get(key);
137: }
138:
139: /**
140: * Removes the specified request attribute.
141: *
142: * @param key the name of the attribute to remove.
143: * @return the value that was removed or <tt>null</tt> if the value was not found (and hence, not removed).
144: */
145: public Object remove(Object key) {
146: entries = null;
147:
148: Object value = get(key);
149: request.removeAttribute(key.toString());
150:
151: return value;
152: }
153: }
|