1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.context;
35
36 import info.magnolia.cms.util.ServletUtil;
37
38 import java.io.Serializable;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpSession;
45
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52 public class RequestAttributeStrategy implements AttributeStrategy {
53 private static final Logger log = LoggerFactory.getLogger(RequestAttributeStrategy.class);
54
55 private final WebContext ctx;
56
57 public RequestAttributeStrategy(WebContext ctx) {
58 this.ctx = ctx;
59 }
60
61 public HttpServletRequest getRequest() {
62 return ctx.getRequest();
63 }
64
65 @Override
66 public Object getAttribute(String name, int scope) {
67 switch (scope) {
68 case Context.LOCAL_SCOPE:
69 Object obj = getRequest().getAttribute(name);
70 if (obj == null) {
71 obj = getRequest().getParameter(name);
72 }
73 if (obj == null) {
74
75 if (WebContext.ATTRIBUTE_REQUEST_CHARACTER_ENCODING.equals(name)) {
76 obj = getRequest().getCharacterEncoding();
77 } else if (WebContext.ATTRIBUTE_REQUEST_URI.equals(name)) {
78 obj = ServletUtil.stripPathParameters(getRequest().getRequestURI());
79 }
80 }
81 return obj;
82 case Context.SESSION_SCOPE:
83 HttpSession httpsession = getRequest().getSession(false);
84 if (httpsession == null) {
85 return null;
86 }
87 return httpsession.getAttribute(name);
88 case Context.APPLICATION_SCOPE:
89 return MgnlContext.getSystemContext().getAttribute(name, Context.APPLICATION_SCOPE);
90 default:
91 log.error("illegal scope passed");
92 return null;
93 }
94 }
95
96 @Override
97 public Map getAttributes(int scope) {
98 Map map = new HashMap();
99 Enumeration keysEnum;
100 switch (scope) {
101 case Context.LOCAL_SCOPE:
102
103 Enumeration paramEnum = getRequest().getParameterNames();
104 while (paramEnum.hasMoreElements()) {
105 final String name = (String) paramEnum.nextElement();
106 map.put(name, getRequest().getParameter(name));
107 }
108
109 keysEnum = getRequest().getAttributeNames();
110 while (keysEnum.hasMoreElements()) {
111 String key = (String) keysEnum.nextElement();
112 Object value = getAttribute(key, scope);
113 map.put(key, value);
114 }
115 return map;
116 case Context.SESSION_SCOPE:
117 HttpSession httpsession = getRequest().getSession(false);
118 if (httpsession == null) {
119 return map;
120 }
121 keysEnum = httpsession.getAttributeNames();
122 while (keysEnum.hasMoreElements()) {
123 String key = (String) keysEnum.nextElement();
124 Object value = getAttribute(key, scope);
125 map.put(key, value);
126 }
127 return map;
128 case Context.APPLICATION_SCOPE:
129 return MgnlContext.getSystemContext().getAttributes(Context.APPLICATION_SCOPE);
130 default:
131 log.error("no illegal scope passed");
132 return map;
133 }
134 }
135
136 @Override
137 public void removeAttribute(String name, int scope) {
138 switch (scope) {
139 case Context.LOCAL_SCOPE:
140 getRequest().removeAttribute(name);
141 break;
142 case Context.SESSION_SCOPE:
143 HttpSession httpsession = getRequest().getSession(false);
144 if (httpsession != null) {
145 httpsession.removeAttribute(name);
146 }
147 break;
148 case Context.APPLICATION_SCOPE:
149 MgnlContext.getSystemContext().removeAttribute(name, Context.APPLICATION_SCOPE);
150 break;
151 default:
152 log.error("no illegal scope passed");
153 }
154 }
155
156 @Override
157 public void setAttribute(String name, Object value, int scope) {
158 if (value == null) {
159 removeAttribute(name, scope);
160 return;
161 }
162
163 switch (scope) {
164 case Context.LOCAL_SCOPE:
165 getRequest().setAttribute(name, value);
166 break;
167 case Context.SESSION_SCOPE:
168 if (!(value instanceof Serializable)) {
169 log.warn("Trying to store a non-serializable attribute in session: {}. Object type is {}", name, value.getClass().getName(), new Throwable("This stacktrace has been added to provide debugging information"));
170 return;
171 }
172
173 HttpSession httpsession = getRequest().getSession(false);
174 if (httpsession == null) {
175 log.debug("Session initialized in order to set attribute '{}' to '{}'. You should avoid using session when possible!", name, value);
176 httpsession = getRequest().getSession(true);
177 }
178
179 httpsession.setAttribute(name, value);
180 break;
181 case Context.APPLICATION_SCOPE:
182 MgnlContext.getSystemContext().setAttribute(name, value, Context.APPLICATION_SCOPE);
183 break;
184 default:
185 getRequest().setAttribute(name, value);
186 log.debug("Undefined scope, setting attribute [{}] in request scope", name);
187 }
188 }
189
190 }