001: /* Copyright 2007 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.servlet;
007:
008: import java.io.BufferedReader;
009: import java.io.IOException;
010: import java.io.UnsupportedEncodingException;
011: import java.security.Principal;
012: import java.util.Collections;
013: import java.util.Enumeration;
014: import java.util.HashSet;
015: import java.util.Hashtable;
016: import java.util.Locale;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.ServletInputStream;
022: import javax.servlet.http.Cookie;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpSession;
025:
026: import org.apache.commons.collections.iterators.IteratorEnumeration;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: /**
031: * HttpServletRequest wrapper that tracks a set of request attributes that are local to
032: * this request wrapper.
033: *
034: * @author Eric Dalquist
035: * @version $Id: AttributeScopingRequestWrapper.java,v 1.1 2006/04/25 22:15:26 dalquist Exp $
036: */
037: public class AttributeScopingRequestWrapper implements
038: HttpServletRequest {
039: private static final Log LOG = LogFactory
040: .getLog(AttributeScopingRequestWrapper.class);
041:
042: private final Map scopedAttributes = new Hashtable();
043: private final HttpServletRequest delegate;
044:
045: public AttributeScopingRequestWrapper(HttpServletRequest request) {
046: if (request == null) {
047: throw new IllegalArgumentException(
048: "Wrapped request can no be null.");
049: }
050:
051: this .delegate = request;
052: }
053:
054: /**
055: * Provides access to the attributes stored in this wrapper.
056: * @return A READ-ONLY Map of the attributes stored in this wrapper.
057: */
058: public Map getScopedAttributeMap() {
059: return Collections.unmodifiableMap(this .scopedAttributes);
060: }
061:
062: //*********************************************
063: // Over-ridden methods to scope attributes.
064: //*********************************************
065:
066: /**
067: * If the attribute isn't in the Map for this wrapper the wrapped request is
068: * consulted.
069: *
070: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
071: */
072: public Object getAttribute(String name) {
073: if (name == null) {
074: throw new IllegalArgumentException(
075: "Attribute name cannot be null");
076: }
077:
078: Object value = this .scopedAttributes.get(name);
079: if (value != null) {
080: if (LOG.isDebugEnabled()) {
081: LOG.debug("getAttribute(" + name
082: + ") - from scoped Map with value='" + value
083: + "'");
084: }
085: } else {
086: value = this .delegate.getAttribute(name);
087:
088: if (LOG.isDebugEnabled()) {
089: LOG.debug("getAttribute(" + name
090: + ") - from parent request with value='"
091: + value + "'");
092: }
093: }
094:
095: return value;
096: }
097:
098: /**
099: * The set of attribute names is merged from the attributes stored in this request and the
100: * attributes from the parent requests.
101: *
102: * @see javax.servlet.ServletRequest#getAttributeNames()
103: */
104: public Enumeration getAttributeNames() {
105: final Set namesSet = new HashSet();
106:
107: for (final Enumeration parentNames = this .delegate
108: .getAttributeNames(); parentNames.hasMoreElements();) {
109: namesSet.add(parentNames.nextElement());
110: }
111:
112: if (LOG.isDebugEnabled()) {
113: LOG
114: .debug("getAttributeNames() - parent request attribute names="
115: + namesSet + "");
116: }
117:
118: namesSet.addAll(this .scopedAttributes.keySet());
119:
120: if (LOG.isDebugEnabled()) {
121: LOG.debug("getAttributeNames() - all attribute names="
122: + namesSet + "");
123: }
124:
125: return new IteratorEnumeration(namesSet.iterator());
126: }
127:
128: /**
129: * Removes the attribute from this request and the wrapped request.
130: *
131: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
132: */
133: public void removeAttribute(String name) {
134: if (name == null) {
135: throw new IllegalArgumentException(
136: "Attribute name cannot be null");
137: }
138:
139: if (LOG.isDebugEnabled()) {
140: LOG.debug("removeAttribute(" + name + ")");
141: }
142:
143: this .scopedAttributes.remove(name);
144: this .delegate.removeAttribute(name);
145: }
146:
147: /**
148: * Stores the attribute in the request wrapper.
149: *
150: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
151: */
152: public void setAttribute(String name, Object value) {
153: if (name == null) {
154: throw new IllegalArgumentException(
155: "Attribute name cannot be null");
156: }
157:
158: if (LOG.isDebugEnabled()) {
159: LOG.debug("setAttribute(" + name + ", " + value + ")");
160: }
161:
162: if (value == null) {
163: this .scopedAttributes.remove(name);
164: } else {
165: this .scopedAttributes.put(name, value);
166: }
167: }
168:
169: //*********************************************
170: // Delegating methods
171: //*********************************************
172:
173: /**
174: * @see javax.servlet.http.HttpServletRequest#getAuthType()
175: */
176: public String getAuthType() {
177: return this .delegate.getAuthType();
178: }
179:
180: /**
181: * @see javax.servlet.ServletRequest#getCharacterEncoding()
182: */
183: public String getCharacterEncoding() {
184: return this .delegate.getCharacterEncoding();
185: }
186:
187: /**
188: * @see javax.servlet.ServletRequest#getContentLength()
189: */
190: public int getContentLength() {
191: return this .delegate.getContentLength();
192: }
193:
194: /**
195: * @see javax.servlet.ServletRequest#getContentType()
196: */
197: public String getContentType() {
198: return this .delegate.getContentType();
199: }
200:
201: /**
202: * @see javax.servlet.http.HttpServletRequest#getContextPath()
203: */
204: public String getContextPath() {
205: return this .delegate.getContextPath();
206: }
207:
208: /**
209: * @see javax.servlet.http.HttpServletRequest#getCookies()
210: */
211: public Cookie[] getCookies() {
212: return this .delegate.getCookies();
213: }
214:
215: /**
216: * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
217: */
218: public long getDateHeader(String arg0) {
219: return this .delegate.getDateHeader(arg0);
220: }
221:
222: /**
223: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
224: */
225: public String getHeader(String arg0) {
226: return this .delegate.getHeader(arg0);
227: }
228:
229: /**
230: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
231: */
232: public Enumeration getHeaderNames() {
233: return this .delegate.getHeaderNames();
234: }
235:
236: /**
237: * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
238: */
239: public Enumeration getHeaders(String arg0) {
240: return this .delegate.getHeaders(arg0);
241: }
242:
243: /**
244: * @see javax.servlet.ServletRequest#getInputStream()
245: */
246: public ServletInputStream getInputStream() throws IOException {
247: return this .delegate.getInputStream();
248: }
249:
250: /**
251: * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
252: */
253: public int getIntHeader(String arg0) {
254: return this .delegate.getIntHeader(arg0);
255: }
256:
257: /**
258: * @see javax.servlet.ServletRequest#getLocale()
259: */
260: public Locale getLocale() {
261: return this .delegate.getLocale();
262: }
263:
264: /**
265: * @see javax.servlet.ServletRequest#getLocales()
266: */
267: public Enumeration getLocales() {
268: return this .delegate.getLocales();
269: }
270:
271: /**
272: * @see javax.servlet.http.HttpServletRequest#getMethod()
273: */
274: public String getMethod() {
275: return this .delegate.getMethod();
276: }
277:
278: /**
279: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
280: */
281: public String getParameter(String arg0) {
282: return this .delegate.getParameter(arg0);
283: }
284:
285: /**
286: * @see javax.servlet.ServletRequest#getParameterMap()
287: */
288: public Map getParameterMap() {
289: return this .delegate.getParameterMap();
290: }
291:
292: /**
293: * @see javax.servlet.ServletRequest#getParameterNames()
294: */
295: public Enumeration getParameterNames() {
296: return this .delegate.getParameterNames();
297: }
298:
299: /**
300: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
301: */
302: public String[] getParameterValues(String arg0) {
303: return this .delegate.getParameterValues(arg0);
304: }
305:
306: /**
307: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
308: */
309: public String getPathInfo() {
310: return this .delegate.getPathInfo();
311: }
312:
313: /**
314: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
315: */
316: public String getPathTranslated() {
317: return this .delegate.getPathTranslated();
318: }
319:
320: /**
321: * @see javax.servlet.ServletRequest#getProtocol()
322: */
323: public String getProtocol() {
324: return this .delegate.getProtocol();
325: }
326:
327: /**
328: * @see javax.servlet.http.HttpServletRequest#getQueryString()
329: */
330: public String getQueryString() {
331: return this .delegate.getQueryString();
332: }
333:
334: /**
335: * @see javax.servlet.ServletRequest#getReader()
336: */
337: public BufferedReader getReader() throws IOException {
338: return this .delegate.getReader();
339: }
340:
341: /**
342: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
343: * @deprecated
344: */
345: public String getRealPath(String arg0) {
346: return this .delegate.getRealPath(arg0);
347: }
348:
349: /**
350: * @see javax.servlet.ServletRequest#getRemoteAddr()
351: */
352: public String getRemoteAddr() {
353: return this .delegate.getRemoteAddr();
354: }
355:
356: /**
357: * @see javax.servlet.ServletRequest#getRemoteHost()
358: */
359: public String getRemoteHost() {
360: return this .delegate.getRemoteHost();
361: }
362:
363: /**
364: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
365: */
366: public String getRemoteUser() {
367: return this .delegate.getRemoteUser();
368: }
369:
370: /**
371: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
372: */
373: public RequestDispatcher getRequestDispatcher(String arg0) {
374: return this .delegate.getRequestDispatcher(arg0);
375: }
376:
377: /**
378: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
379: */
380: public String getRequestedSessionId() {
381: return this .delegate.getRequestedSessionId();
382: }
383:
384: /**
385: * @see javax.servlet.http.HttpServletRequest#getRequestURI()
386: */
387: public String getRequestURI() {
388: return this .delegate.getRequestURI();
389: }
390:
391: /**
392: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
393: */
394: public StringBuffer getRequestURL() {
395: return this .delegate.getRequestURL();
396: }
397:
398: /**
399: * @see javax.servlet.ServletRequest#getScheme()
400: */
401: public String getScheme() {
402: return this .delegate.getScheme();
403: }
404:
405: /**
406: * @see javax.servlet.ServletRequest#getServerName()
407: */
408: public String getServerName() {
409: return this .delegate.getServerName();
410: }
411:
412: /**
413: * @see javax.servlet.ServletRequest#getServerPort()
414: */
415: public int getServerPort() {
416: return this .delegate.getServerPort();
417: }
418:
419: /**
420: * @see javax.servlet.http.HttpServletRequest#getServletPath()
421: */
422: public String getServletPath() {
423: return this .delegate.getServletPath();
424: }
425:
426: /**
427: * @see javax.servlet.http.HttpServletRequest#getSession()
428: */
429: public HttpSession getSession() {
430: return this .delegate.getSession();
431: }
432:
433: /**
434: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
435: */
436: public HttpSession getSession(boolean arg0) {
437: return this .delegate.getSession(arg0);
438: }
439:
440: /**
441: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
442: */
443: public Principal getUserPrincipal() {
444: return this .delegate.getUserPrincipal();
445: }
446:
447: /**
448: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
449: */
450: public boolean isRequestedSessionIdFromCookie() {
451: return this .delegate.isRequestedSessionIdFromCookie();
452: }
453:
454: /**
455: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
456: * @deprecated
457: */
458: public boolean isRequestedSessionIdFromUrl() {
459: return this .delegate.isRequestedSessionIdFromUrl();
460: }
461:
462: /**
463: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
464: */
465: public boolean isRequestedSessionIdFromURL() {
466: return this .delegate.isRequestedSessionIdFromURL();
467: }
468:
469: /**
470: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
471: */
472: public boolean isRequestedSessionIdValid() {
473: return this .delegate.isRequestedSessionIdValid();
474: }
475:
476: /**
477: * @see javax.servlet.ServletRequest#isSecure()
478: */
479: public boolean isSecure() {
480: return this .delegate.isSecure();
481: }
482:
483: /**
484: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
485: */
486: public boolean isUserInRole(String arg0) {
487: return this .delegate.isUserInRole(arg0);
488: }
489:
490: /**
491: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
492: */
493: public void setCharacterEncoding(String arg0)
494: throws UnsupportedEncodingException {
495: this.delegate.setCharacterEncoding(arg0);
496: }
497: }
|