01: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.container.servlet;
07:
08: import java.util.Enumeration;
09: import java.util.HashSet;
10: import java.util.Hashtable;
11: import java.util.Map;
12: import java.util.Set;
13:
14: import javax.servlet.http.HttpServletRequest;
15: import javax.servlet.http.HttpServletRequestWrapper;
16:
17: import org.apache.commons.collections.iterators.IteratorEnumeration;
18:
19: /**
20: * HttpServletRequest wrapper that tracks a set of request attributes
21: * that are local to this request.
22: *
23: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
24: * @version $Id: PortletAttributeRequestWrapper.java 35889 2005-06-07 22:34:00Z edalquist $
25: */
26: public class PortletAttributeRequestWrapper extends
27: HttpServletRequestWrapper {
28: private final Map scopedAttributes = new Hashtable();
29:
30: public PortletAttributeRequestWrapper(HttpServletRequest request) {
31: super (request);
32: }
33:
34: /**
35: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
36: */
37: public Object getAttribute(String name) {
38: if (name == null)
39: throw new IllegalArgumentException(
40: "Attribute name cannot be null");
41:
42: final Object value = this .scopedAttributes.get(name);
43:
44: if (value != null)
45: return value;
46: else
47: return super .getAttribute(name);
48: }
49:
50: /**
51: * @see javax.servlet.ServletRequest#getAttributeNames()
52: */
53: public Enumeration getAttributeNames() {
54: final Set namesSet = new HashSet();
55:
56: for (final Enumeration parentNames = super .getAttributeNames(); parentNames
57: .hasMoreElements();) {
58: namesSet.add(parentNames.nextElement());
59: }
60:
61: namesSet.addAll(this .scopedAttributes.keySet());
62:
63: return new IteratorEnumeration(namesSet.iterator());
64: }
65:
66: /**
67: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
68: */
69: public void removeAttribute(String name) {
70: if (name == null)
71: throw new IllegalArgumentException(
72: "Attribute name cannot be null");
73:
74: this .scopedAttributes.remove(name);
75: super .removeAttribute(name);
76: }
77:
78: /**
79: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
80: */
81: public void setAttribute(String name, Object value) {
82: if (name == null)
83: throw new IllegalArgumentException(
84: "Attribute name cannot be null");
85:
86: if (value == null)
87: this.scopedAttributes.remove(name);
88: else
89: this.scopedAttributes.put(name, value);
90: }
91: }
|