forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActContext.java
More file actions
706 lines (574 loc) · 19.7 KB
/
ActContext.java
File metadata and controls
706 lines (574 loc) · 19.7 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package act.util;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import static act.view.ViewManager.isTemplatePath;
import act.Destroyable;
import act.act_messages;
import act.app.ActionContext;
import act.app.App;
import act.cli.CliContext;
import act.conf.AppConfig;
import act.i18n.I18n;
import act.mail.MailerContext;
import act.view.Template;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.util.ParamValueProvider;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import java.lang.reflect.Method;
import java.util.*;
import javax.enterprise.context.RequestScoped;
import javax.validation.ConstraintViolation;
public interface ActContext<CTX_TYPE extends ActContext> extends ParamValueProvider {
App app();
AppConfig config();
CTX_TYPE accept(H.Format fmt);
H.Format accept();
CTX_TYPE locale(Locale locale);
Locale locale();
Locale locale(boolean required);
/**
* Returns the template path
*
* @return the template path
*/
String templatePath();
/**
* Returns the template content
*
* @return the template content
*/
String templateContent();
/**
* Set path to template file
*
* @param path the path to template file
* @return this {@code AppContext}
*/
CTX_TYPE templatePath(String path);
/**
* Set template content
*
* @param content the template content
* @return this `ActContext`
*/
CTX_TYPE templateContent(String content);
/**
* Returns the template context
*
* @return template context
*/
String templateContext();
/**
* Set template context
*
* @param context the path to template context
* @return this {@code ActContext}
*/
CTX_TYPE templateContext(String context);
/**
* Check if the template path is implicit i.e. derived from {@link #methodPath()}
*
* @return `true` if template path is implicit; `false` otherwise
*/
boolean templatePathIsImplicit();
Template cachedTemplate();
CTX_TYPE cacheTemplate(Template template);
<T> T renderArg(String name);
/**
* Returns all render arguments
*/
Map<String, Object> renderArgs();
CTX_TYPE renderArg(String name, Object val);
CTX_TYPE addListener(Listener listener);
CTX_TYPE addDestroyable(Destroyable resource);
CTX_TYPE attribute(String name, Object attr);
<T> T attribute(String name);
Map<String, Object> attributes();
CTX_TYPE removeAttribute(String name);
CTX_TYPE addViolations(Map<String, ConstraintViolation> violations);
CTX_TYPE addViolation(String property, ConstraintViolation violation);
boolean hasViolation();
Map<String, ConstraintViolation> violations();
ConstraintViolation violation(String property);
/**
* Returns the user session with which this context is associated
*
* @return session id or null if no session associated with this context (e.g. mail context)
*/
String sessionId();
/**
* Returns data format pattern. Normally should be date time format.
*
* This method is superseded by {@link #dateFormatPattern()}
*
* @return the data format pattern
*/
@Deprecated
String pattern();
/**
* Set data format pattern.
*
* This method is superseded by {@link #dateFormatPattern(String)}
*
* @param pattern the data format pattern
* @return this context instance
*/
@Deprecated
CTX_TYPE pattern(String pattern);
String _act_i18n(String msgId, Object... args);
String i18n(boolean ignoreError, String msgId, Object... args);
String i18n(String msgId, Object... args);
String i18n(Class<?> bundleSpec, String msgId, Object... args);
String i18n(boolean ignoreError, Class<?> bundleSpec, String msgId, Object... args);
String i18n(Enum<?> msgId);
String i18n(Class<?> bundleSpec, Enum<?> msgId);
Map<String, Object> i18n(Class<? extends Enum> enumClass);
Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass);
Map<String, Object> i18n(Class<? extends Enum> enumClass, boolean outputProperties);
Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties);
String methodPath();
/**
* The method that handles the current request.
* @return the handler method
*/
Method handlerMethod();
/**
* Set the {@link #handlerMethod()}
* @param method the handler method
* @return this context
*/
CTX_TYPE handlerMethod(Method method);
/**
* The method to be invoked in the current stage. Note the
* current method might be or not be the {@link #handlerMethod()}
* as current method could be an interceptor method rather than
* handler method.
* @return the current method
*/
Method currentMethod();
/**
* Set the {@link #currentMethod()}.
*
* @param method the method to be invoked
* @return this context
*/
CTX_TYPE currentMethod(Method method);
CTX_TYPE fastjsonFilters(Class<? extends SerializeFilter>[] filters);
SerializeFilter[] fastjsonFilters();
CTX_TYPE fastjsonFeatures(SerializerFeature[] features);
SerializerFeature[] fastjsonFeatures();
CTX_TYPE dateFormatPattern(String pattern);
String dateFormatPattern();
interface Listener {
void onDestroy(ActContext context);
}
abstract class Base<CTX extends Base> extends DestroyableBase
implements ActContext<CTX> {
private App app;
private String templatePath;
private String templateContent;
private String templateContext;
private Template template;
private Map<String, Object> renderArgs;
private List<Listener> listenerList;
private List<Destroyable> destroyableList;
private Map<String, Object> attributes;
private Locale locale;
private int fieldOutputVarCount;
private boolean noTemplateCache;
private volatile SimpleProgressGauge progress;
private String jobId;
private Method handlerMethod;
private Method currentMethod;
private String pattern;
private List<String> renderArgNames;
// (violation.propertyPath, violation)
private Map<String, ConstraintViolation> violations;
private Class<? extends SerializeFilter>[] fastjsonFilters;
private SerializerFeature[] fastjsonFeatures;
private String dateFormatPattern;
public Base(App app) {
this(app, false);
}
protected Base(App app, boolean noLogger) {
super(noLogger);
E.NPE(app);
this.app = app;
renderArgs = new HashMap<>();
attributes = new HashMap<>();
listenerList = new ArrayList<>();
destroyableList = new ArrayList<>();
violations = new HashMap<>();
}
@Override
protected void releaseResources() {
for (Listener l : listenerList) {
try {
l.onDestroy(this);
} catch (Exception e) {
warn(e, "error calling listener onDestroy method");
}
}
Destroyable.Util.destroyAll(destroyableList, RequestScoped.class);
Destroyable.Util.tryDestroyAll(attributes.values(), RequestScoped.class);
this.attributes.clear();
this.renderArgs.clear();
this.template = null;
this.app = null;
this.template = null;
this.listenerList.clear();
this.destroyableList.clear();
this.violations.clear();
// note we can't destroy progress as it might still be used
// by background thread
//this.progress.destroy();
}
@Override
public App app() {
return app;
}
@Override
public AppConfig config() {
return app().config();
}
@Override
public Method handlerMethod() {
return handlerMethod;
}
@Override
public CTX handlerMethod(Method method) {
this.handlerMethod = method;
return me();
}
@Override
public Method currentMethod() {
return currentMethod;
}
@Override
public CTX currentMethod(Method method) {
this.currentMethod = method;
return me();
}
public CTX fastjsonFilters(Class<? extends SerializeFilter>[] filters) {
this.fastjsonFilters = filters;
return me();
}
public SerializeFilter[] fastjsonFilters() {
if (null == fastjsonFilters) {
return null;
}
int len = fastjsonFilters.length;
SerializeFilter[] filters = new SerializeFilter[len];
for (int i = 0; i < len; ++i) {
filters[i] = app().getInstance(fastjsonFilters[i]);
}
return filters;
}
public CTX fastjsonFeatures(SerializerFeature[] features) {
this.fastjsonFeatures = features;
return me();
}
public SerializerFeature[] fastjsonFeatures() {
return fastjsonFeatures;
}
public CTX dateFormatPattern(String pattern) {
this.dateFormatPattern = pattern;
return me();
}
public String dateFormatPattern() {
return this.dateFormatPattern;
}
@Override
public String templatePath() {
String path = templatePath;
String context = templateContext;
if (S.notBlank(path)) {
return path.startsWith("/") || S.blank(context) ? path : S.pathConcat(context, '/', path);
} else {
if (S.blank(context)) {
return methodPath().replace('.', '/');
} else {
return S.pathConcat(context, '/', S.afterLast(methodPath(), "."));
}
}
}
@Override
public CTX templatePath(String templatePath) {
this.template = null;
this.templateContent = null;
this.templatePath = templatePath;
return me();
}
@Override
public String templateContent() {
return templateContent;
}
@Override
public CTX templateContent(String content) {
this.template = null;
this.templateContent = content;
return me();
}
public CTX templateLiteral(String literal) {
if (S.empty(literal)) {
return me();
}
return isTemplatePath(literal) ? templatePath(literal) : templateContent(literal);
}
public String templateContext() {
return this.templateContext;
}
public CTX templateContext(String templateContext) {
this.template = null;
this.templateContext = templateContext;
return me();
}
/**
* Template path is implicit if {@link #templatePath(String)} is never called
* on this context instance
*
* @return `true` if template path is implicit
*/
@Override
public boolean templatePathIsImplicit() {
return null == templatePath;
}
@Override
public Template cachedTemplate() {
return template;
}
public CTX disableTemplateCaching() {
this.noTemplateCache = true;
return me();
}
@Override
public CTX cacheTemplate(Template template) {
if (!noTemplateCache) {
this.template = template;
}
return me();
}
/**
* Called by bytecode enhancer to set the name list of the render arguments that is update
* by the enhancer
*
* @param names the render argument names separated by ","
* @return this AppContext
*/
public CTX __appRenderArgNames(String names) {
renderArgNames = C.listOf(names.split(","));
return me();
}
public List<String> __appRenderArgNames() {
return renderArgNames;
}
@Override
public String pattern() {
return pattern;
}
@Override
public CTX pattern(String pattern) {
this.pattern = pattern;
return me();
}
/**
* Default session id is `null`
*
* @return the session id
*/
public String sessionId() {
return null;
}
public final CTX locale(Locale locale) {
this.locale = locale;
return me();
}
public final Locale locale() {
return this.locale;
}
public Locale locale(boolean required) {
Locale locale = this.locale;
if (null == locale) {
if (!required) {
return null;
}
locale = config().locale();
}
return locale;
}
public String i18n(boolean ignoreError, String msgId, Object... args) {
return I18n.i18n(ignoreError, locale(true), msgId, args);
}
public String i18n(String msgId, Object... args) {
return I18n.i18n(locale(true), msgId, args);
}
public String _act_i18n(String msgId, Object... args) {
return I18n.i18n(locale(true), act_messages.class, msgId, args);
}
public String i18n(Class<?> bundleSpec, String msgId, Object... args) {
return I18n.i18n(locale(true), bundleSpec, msgId, args);
}
public String i18n(boolean ignoreError, Class<?> bundleSpec, String msgId, Object... args) {
return I18n.i18n(ignoreError, locale(true), bundleSpec, msgId, args);
}
public String i18n(Enum<?> msgId) {
return I18n.i18n(locale(true), msgId);
}
public String i18n(Class<?> bundleSpec, Enum<?> msgId) {
return I18n.i18n(locale(true), bundleSpec, msgId);
}
public Map<String, Object> i18n(Class<? extends Enum> enumClass) {
return I18n.i18n(locale(true), enumClass);
}
public Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass) {
return I18n.i18n(locale(true), bundleSpec, enumClass);
}
public Map<String, Object> i18n(Class<? extends Enum> enumClass, boolean outputPropeties) {
return I18n.i18n(locale(true), enumClass, outputPropeties);
}
public Map<String, Object> i18n(Class<?> bundleSpec, Class<? extends Enum> enumClass, boolean outputProperties) {
return I18n.i18n(locale(true), bundleSpec, enumClass, outputProperties);
}
protected CTX me() {
return (CTX) this;
}
@Override
public <T> T renderArg(String name) {
return (T) renderArgs.get(name);
}
@Override
public CTX renderArg(String name, Object val) {
renderArgs.put(name, val);
return me();
}
// see https://github.com/actframework/actframework/issues/312
public CTX fieldOutputVarCount(int count) {
this.fieldOutputVarCount = count;
return me();
}
public int fieldOutputVarCount() {
return fieldOutputVarCount;
}
@Override
public Map<String, Object> renderArgs() {
return renderArgs;
}
protected boolean hasRenderArgs() {
return !renderArgs.isEmpty();
}
/**
* Associate a user attribute to the context. Could be used by third party
* libraries or user application
*
* @param name the className used to reference the attribute
* @param attr the attribute object
* @return this context
*/
public CTX attribute(String name, Object attr) {
attributes.put(name, attr);
return me();
}
public <T> T attribute(String name) {
return $.cast(attributes.get(name));
}
public CTX removeAttribute(String name) {
attributes.remove(name);
return me();
}
@Override
public Map<String, Object> attributes() {
return attributes;
}
@Override
public CTX addListener(Listener listener) {
listenerList.add(listener);
return me();
}
@Override
public CTX addDestroyable(Destroyable resource) {
destroyableList.add(resource);
return me();
}
@Override
public CTX addViolations(Map<String, ConstraintViolation> violations) {
this.violations.putAll(violations);
return me();
}
@Override
public CTX addViolation(String property, ConstraintViolation violation) {
this.violations.put(property, violation);
return me();
}
@Override
public boolean hasViolation() {
return !violations.isEmpty();
}
@Override
public Map<String, ConstraintViolation> violations() {
return C.Map(this.violations);
}
@Override
public ConstraintViolation violation(String property) {
return this.violations.get(property);
}
public void setJobId(String jobId) {
this.jobId = jobId;
app().jobManager().setJobProgressGauge(jobId, progress());
}
public ProgressGauge progress() {
if (null == progress) {
synchronized (this) {
if (null == progress) {
progress = new SimpleProgressGauge();
}
}
}
return progress;
}
public static ActContext.Base<?> currentContext() {
ActContext.Base<?> ctx = ActionContext.current();
if (null != ctx) {
return ctx;
}
ctx = CliContext.current();
if (null != ctx) {
return ctx;
}
ctx = MailerContext.current();
if (null != ctx) {
return ctx;
}
return null;
}
public static Class<? extends ActContext> currentContextType() {
ActContext ctx = currentContext();
return null == ctx ? null : ctx.getClass();
}
public static String currentDateFormatPattern() {
ActContext<?> current = currentContext();
return null == current ? null : current.dateFormatPattern();
}
}
}