01: package org.vraptor.scope;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import javax.servlet.http.HttpServletRequest;
07:
08: /**
09: * A flash context lives through the current and following request only. It is
10: * then copied to the request scope and replaced with a new empty
11: * implementation.
12: *
13: * @author Guilherme Silveira
14: * @since 2.4
15: */
16: public class FlashContext implements Context {
17:
18: private static final String KEY = ScopeType.class.getName() + "_"
19: + ScopeType.FLASH.name();
20:
21: private final HttpServletRequest request;
22:
23: public FlashContext(HttpServletRequest request) {
24: this .request = request;
25: if (getCurrentFlash() == null) {
26: renew();
27: }
28: }
29:
30: public Object getAttribute(String name) {
31: return getOldFlash().getAttribute(name);
32: }
33:
34: private HttpServletRequest getOldFlash() {
35: return request;
36: }
37:
38: public boolean hasAttribute(String name) {
39: return getOldFlash().getAttribute(name) != null;
40: }
41:
42: public Object removeAttribute(String name) {
43: throw new UnsupportedOperationException();
44: }
45:
46: public void setAttribute(String name, Object value) {
47: getCurrentFlash().put(name, value);
48: }
49:
50: @SuppressWarnings("unchecked")
51: private Map<String, Object> getCurrentFlash() {
52: return (Map<String, Object>) request.getSession().getAttribute(
53: KEY);
54: }
55:
56: public void dumpToRequest() {
57: Map<String, Object> currentFlash = getCurrentFlash();
58: for (Map.Entry<String, Object> entry : currentFlash.entrySet()) {
59: request.setAttribute(entry.getKey(), entry.getValue());
60: }
61: }
62:
63: public void renew() {
64: this .request.getSession().setAttribute(KEY,
65: new HashMap<String, Object>());
66: }
67:
68: }
|