01: package org.vraptor.scope;
02:
03: import java.util.HashMap;
04: import java.util.HashSet;
05: import java.util.Map;
06: import java.util.Set;
07:
08: import javax.servlet.http.HttpServletRequest;
09:
10: /**
11: * A simple request context.
12: *
13: * @author Guilherme Silveira
14: */
15: public class DefaultRequestContext implements RequestContext {
16:
17: private final HttpServletRequest request;
18: private Set<String> attributeSet = new HashSet<String>();
19:
20: public DefaultRequestContext(HttpServletRequest request) {
21: this .request = request;
22: }
23:
24: public boolean hasAttribute(String name) {
25: return request.getAttribute(name) != null;
26: }
27:
28: public void setAttribute(String name, Object value) {
29: attributeSet.add(name);
30: request.setAttribute(name, value);
31: }
32:
33: public Object getAttribute(String name) {
34: return request.getAttribute(name);
35: }
36:
37: public Object removeAttribute(String name) {
38: attributeSet.remove(name);
39: Object value = request.getAttribute(name);
40: request.removeAttribute(name);
41: return value;
42: }
43:
44: @SuppressWarnings("unchecked")
45: public Map<String, Object> getParameterMap() {
46: return this .request.getParameterMap();
47: }
48:
49: @SuppressWarnings("unchecked")
50: /**
51: * Returns the attributes that were set and not removed through
52: * this interface.
53: *
54: * Note: this method will not return internal attributes registered
55: * by the servlet container that were not registered through this
56: * wrapper.
57: */
58: public Map<String, Object> getAttributeMap() {
59: Map<String, Object> attributes = new HashMap<String, Object>();
60: for (String key : attributeSet) {
61: attributes.put(key, request.getAttribute(key));
62: }
63:
64: return attributes;
65: }
66:
67: }
|