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