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.beans.runtime.MultipartForm;
37 import info.magnolia.cms.core.AggregationState;
38 import info.magnolia.cms.core.HierarchyManager;
39 import info.magnolia.cms.core.search.QueryManager;
40 import info.magnolia.cms.i18n.Messages;
41 import info.magnolia.cms.security.AccessManager;
42 import info.magnolia.cms.security.User;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import javax.servlet.ServletContext;
47 import javax.servlet.http.HttpServletRequest;
48 import javax.servlet.http.HttpServletResponse;
49 import java.util.Locale;
50 import java.util.Map;
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class MgnlContext {
65 private static final Logger log = LoggerFactory.getLogger(MgnlContext.class);
66
67
68
69
70 private static ThreadLocal<Context> localContext = new ThreadLocal<Context>();
71
72
73
74
75 public MgnlContext() {
76 }
77
78
79
80
81
82 public static User getUser() {
83 return getInstance().getUser();
84 }
85
86
87
88
89 public static void setLocale(Locale locale) {
90 getInstance().setLocale(locale);
91 }
92
93
94
95
96
97 public static Locale getLocale() {
98 return getInstance().getLocale();
99 }
100
101 public static Messages getMessages() {
102 return getInstance().getMessages();
103 }
104
105 public static Messages getMessages(String basename) {
106 return getInstance().getMessages(basename);
107 }
108
109 public static void login(User user) {
110 ((UserContext)getInstance()).login(user);
111 }
112
113
114
115
116 public static HierarchyManager getHierarchyManager(String repositoryId) {
117 return getInstance().getHierarchyManager(repositoryId);
118 }
119
120
121
122
123 public static HierarchyManager getHierarchyManager(String repositoryId, String workspaceId) {
124 return getInstance().getHierarchyManager(repositoryId, workspaceId);
125 }
126
127
128
129
130 public static AccessManager getAccessManager(String repositoryId) {
131 return getInstance().getAccessManager(repositoryId);
132 }
133
134
135
136
137 public static AccessManager getAccessManager(String repositoryId, String workspaceId) {
138 return getInstance().getAccessManager(repositoryId, workspaceId);
139 }
140
141
142
143
144 public static QueryManager getQueryManager(String repositoryId) {
145 return getInstance().getQueryManager(repositoryId);
146 }
147
148
149
150
151 public static QueryManager getQueryManager(String repositoryId, String workspaceId) {
152 return getInstance().getQueryManager(repositoryId, workspaceId);
153 }
154
155
156
157
158
159
160 public static MultipartForm getPostedForm() {
161 WebContext ctx = getWebContextOrNull();
162 if (ctx != null) {
163 return ctx.getPostedForm();
164 }
165 return null;
166 }
167
168
169
170
171 public static String getParameter(String name) {
172 WebContext ctx = getWebContextOrNull();
173 if (ctx != null) {
174 return ctx.getParameter(name);
175 }
176 return null;
177
178 }
179
180 public static String[] getParameterValues(String name) {
181 WebContext ctx = getWebContextOrNull();
182 if (ctx != null) {
183 return ctx.getParameterValues(name);
184 }
185 return null;
186
187 }
188
189
190
191
192 public static Map<String, String> getParameters() {
193 WebContext ctx = getWebContextOrNull();
194 if (ctx != null) {
195 return ctx.getParameters();
196 }
197 return null;
198 }
199
200
201
202
203 public static String getContextPath() {
204 WebContext ctx = getWebContextOrNull();
205 if (ctx != null) {
206 return ctx.getContextPath();
207 } else {
208 throw new IllegalStateException("Can only get the context path within a WebContext.");
209 }
210 }
211
212
213
214
215
216 public static AggregationState getAggregationState() {
217 final WebContext ctx = getWebContextOrNull();
218 if (ctx != null) {
219 return ctx.getAggregationState();
220 } else {
221 throw new IllegalStateException("Can only get the aggregation state within a WebContext.");
222 }
223 }
224
225
226
227
228 public static void resetAggregationState() {
229 final WebContext ctx = getWebContextOrNull();
230 if (ctx != null) {
231 ctx.resetAggregationState();
232 }
233 else {
234 throw new IllegalStateException("Can only reset the aggregation state within a WebContext.");
235 }
236 }
237
238
239
240
241 public static void setAttribute(String name, Object value) {
242 getInstance().setAttribute(name, value, Context.LOCAL_SCOPE);
243 }
244
245
246
247
248
249 public static void setAttribute(String name, Object value, int scope) {
250 getInstance().setAttribute(name, value, scope);
251 }
252
253
254
255
256 public static Object getAttribute(String name) {
257 return getInstance().getAttribute(name);
258 }
259
260
261
262
263 public static Object getAttribute(String name, int scope) {
264 return getInstance().getAttribute(name, scope);
265 }
266
267
268
269
270 public static boolean hasAttribute(String name){
271 return getInstance().getAttribute(name, Context.LOCAL_SCOPE) != null;
272 }
273
274
275
276
277 public static void removeAttribute(String name){
278 getInstance().removeAttribute(name, Context.LOCAL_SCOPE);
279 }
280
281
282
283
284 public static void setInstance(Context context) {
285 localContext.set(context);
286 }
287
288
289
290
291 public static Context getInstance() {
292 Context context = localContext.get();
293
294 if (context == null) {
295 IllegalStateException ise = new IllegalStateException("MgnlContext is not set for this thread");
296 log.error("MgnlContext is not initialized. This could happen if the request does not go through the Magnolia default filters.", ise);
297 throw ise;
298 }
299 return context;
300 }
301
302
303
304
305
306 public static WebContext getWebContext() {
307 return getWebContext(null);
308 }
309
310
311
312
313
314
315
316 public static WebContext getWebContext(String exceptionMessage) {
317 final WebContext wc = getWebContextIfExisting(getInstance());
318 if (wc == null) {
319 throw new IllegalStateException(exceptionMessage == null ? "The current context is not an instance of WebContext (" + localContext.get() + ")" : exceptionMessage);
320 }
321 return wc;
322 }
323
324
325
326
327
328 public static WebContext getWebContextOrNull() {
329 return hasInstance() ? getWebContextIfExisting(getInstance()) : null;
330 }
331
332
333
334
335
336 public static boolean hasInstance() {
337 return localContext.get() != null;
338 }
339
340 public static boolean isSystemInstance() {
341 return localContext.get() instanceof SystemContext;
342 }
343
344
345
346
347 public static boolean isWebContext() {
348 return hasInstance() && getWebContextIfExisting(getInstance()) != null;
349 }
350
351
352
353
354 public static SystemContext getSystemContext() {
355 return ContextFactory.getInstance().getSystemContext();
356 }
357
358
359
360
361 public static void doInSystemContext(final SystemContextOperation op) {
362 doInSystemContext(op, false);
363 }
364
365
366
367
368
369
370 public static <T, E extends Throwable> T doInSystemContext(final Op<T, E> op) throws E {
371 return doInSystemContext(op, false);
372 }
373
374
375
376
377 public static void doInSystemContext(final SystemContextOperation op, boolean releaseAfterExecution) {
378 doInSystemContext(new VoidOp() {
379 public void doExec() {
380 op.exec();
381 }
382 }, releaseAfterExecution);
383 }
384
385
386
387
388
389
390
391 public static <T, E extends Throwable> T doInSystemContext(final Op<T, E> op, boolean releaseAfterExecution) throws E {
392 final Context originalCtx = MgnlContext.hasInstance() ? MgnlContext.getInstance() : null;
393 T result;
394 try {
395 MgnlContext.setInstance(MgnlContext.getSystemContext());
396 result = op.exec();
397 if (releaseAfterExecution) {
398 MgnlContext.release();
399 }
400 } finally {
401 MgnlContext.setInstance(originalCtx);
402 }
403 return result;
404 }
405
406
407
408
409
410
411
412 public static interface SystemContextOperation {
413 void exec();
414 }
415
416
417
418
419
420
421
422
423
424
425 public static interface Op<T, E extends Throwable> {
426 T exec() throws E;
427 }
428
429
430
431
432 public abstract static class VoidOp implements Op<Void, RuntimeException> {
433 public Void exec() {
434 doExec();
435 return null;
436 }
437
438 abstract public void doExec();
439 }
440
441
442
443
444
445
446
447 public static void initAsWebContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext) {
448 WebContext ctx = ContextFactory.getInstance().createWebContext(request, response, servletContext);
449 setInstance(ctx);
450 }
451
452
453
454
455
456 private static WebContext getWebContextIfExisting(Context ctx) {
457 if (ctx instanceof WebContext) {
458 return (WebContext) ctx;
459 }
460 else if (ctx instanceof ContextDecorator) {
461 return getWebContextIfExisting(((ContextDecorator) ctx).getWrappedContext());
462 }
463 return null;
464 }
465
466
467
468
469 public static void release() {
470 if(hasInstance() && !(getInstance() instanceof SystemContext)){
471 getInstance().release();
472 }
473 SystemContext systemContext = getSystemContext();
474 if(systemContext instanceof ThreadDependentSystemContext){
475 ((ThreadDependentSystemContext)systemContext).releaseThread();
476 }
477 }
478
479 public static void push(HttpServletRequest request, HttpServletResponse response) {
480 if (isWebContext()) {
481 WebContext wc = getWebContext();
482 wc.push(request,response);
483 }
484 }
485
486 public static void pop() {
487 if (isWebContext()) {
488 WebContext wc = getWebContext();
489 wc.pop();
490 }
491 }
492 }