Repository navigation
Expand file tree
/
Copy pathActionContext.java
More file actions
846 lines (739 loc) · 25.4 KB
/
Copy pathActionContext.java
File metadata and controls
846 lines (739 loc) · 25.4 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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
package act.app;
import act.Act;
import act.Destroyable;
import act.data.MapUtil;
import act.data.RequestBodyParser;
import act.event.ActEvent;
import act.event.EventBus;
import act.handler.RequestHandler;
import act.route.Router;
import act.util.ActContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.osgl.$;
import org.osgl.concurrent.ContextLocal;
import org.osgl.http.H;
import org.osgl.http.H.Cookie;
import org.osgl.mvc.util.ParamValueProvider;
import org.osgl.storage.ISObject;
import org.osgl.util.C;
import org.osgl.util.E;
import org.osgl.util.S;
import javax.validation.ConstraintViolation;
import java.util.*;
/**
* {@code AppContext} encapsulate contextual properties needed by
* an application session
*/
public class ActionContext extends ActContext.ActContextBase<ActionContext> implements ActContext<ActionContext>, ParamValueProvider, Destroyable {
public static final String ATTR_HANDLER = "__act_handler__";
public static final String REQ_BODY = "_body";
private String portId;
private H.Request request;
private H.Response response;
private H.Session session;
private H.Flash flash;
private Set<Map.Entry<String, String[]>> requestParamCache;
private Map<String, String> extraParams;
private volatile Map<String, String[]> bodyParams;
private volatile JSONObject jsonObject;
private volatile JSONArray jsonArray;
private Map<String, String[]> allParams;
private String actionPath; // e.g. com.mycorp.myapp.controller.AbcController.foo
private Map<String, Object> attributes;
private State state;
private Map<String, Object> controllerInstances;
private Map<String, ISObject> uploads;
private Set<ConstraintViolation> violations;
private Router router;
private RequestHandler handler;
private ActionContext(App app, H.Request request, H.Response response) {
super(app);
E.NPE(app, request, response);
this.request = request;
this.response = response;
this._init();
this.state = State.CREATED;
this.saveLocal();
}
public State state() {
return state;
}
public boolean isSessionDissolved() {
return state == State.SESSION_DISSOLVED;
}
public boolean isSessionResolved() {
return state == State.SESSION_RESOLVED;
}
public H.Request req() {
return request;
}
public H.Response resp() {
return response;
}
public H.Cookie cookie(String name) {
return req().cookie(name);
}
public H.Session session() {
return session;
}
public H.Flash flash() {
return flash;
}
public Router router() {
return router;
}
public ActionContext router(Router router) {
this.router = $.notNull(router);
this.portId = router.portId();
return this;
}
// !!!IMPORTANT! the following methods needs to be kept to allow enhancer work correctly
@Override
public <T> T renderArg(String name) {
return super.renderArg(name);
}
@Override
public ActionContext renderArg(String name, Object val) {
return super.renderArg(name, val);
}
@Override
public Map<String, Object> renderArgs() {
return super.renderArgs();
}
@Override
public ActionContext templatePath(String templatePath) {
return super.templatePath(templatePath);
}
public RequestHandler handler() {
return handler;
}
public ActionContext handler(RequestHandler handler) {
E.NPE(handler);
this.handler = handler;
return this;
}
public H.Format accept() {
return req().accept();
}
public ActionContext accept(H.Format fmt) {
req().accept(fmt);
return this;
}
public String portId() {
return portId;
}
public Locale locale() {
return config().localeResolver().resolve(this);
}
public boolean isJSON() {
return accept() == H.Format.JSON;
}
public boolean isXML() {
return accept() == H.Format.XML;
}
public boolean isAjax() {
return req().isAjax();
}
public ActionContext param(String name, String value) {
extraParams.put(name, value);
return this;
}
@Override
public String paramVal(String name) {
String val = extraParams.get(name);
if (null != val) {
return val;
}
val = request.paramVal(name);
if (null == val) {
String[] sa = getBody(name);
if (null != sa && sa.length > 0) {
val = sa[0];
}
}
return val;
}
public String[] paramVals(String name) {
String val = extraParams.get(name);
if (null != val) {
return new String[]{val};
}
String[] sa = request.paramVals(name);
if (null == sa) {
sa = getBody(name);
}
return sa;
}
private String[] getBody(String name) {
Map<String, String[]> body = bodyParams();
String[] sa = body.get(name);
if (null != sa) {
return sa;
}
if (body.size() == 1 && body.containsKey(REQ_BODY)) {
if (null != jsonArray) {
int len = jsonArray.size();
sa = new String[len];
for (int i = 0; i < len; ++i) {
sa[i] = S.string(jsonArray.get(i));
}
} else if (null != jsonObject) {
} else {
sa = body.get(REQ_BODY);
}
}
return sa;
}
public Object tryParseJson(String name, Class<?> paramType, Class<?> paramComponentType, int paramCount) {
if (null != jsonObject) {
Object o = jsonObject.get(name);
if (null != o) {
if (o instanceof JSONObject) {
return JSON.parseObject(((JSONObject) o).toJSONString(), paramType);
} else if (o instanceof JSONArray) {
return JSON.parseArray(((JSONArray) o).toJSONString(), paramComponentType);
} else {
return o;
}
} else if (!$.isSimpleType(paramType)) {
if (Iterable.class.isAssignableFrom(paramType)) {
if (List.class.equals(paramType)) {
o = C.list();
} else if (Set.class.equals(paramType)) {
o = C.newSet();
}
} else if (Map.class.equals(paramType)) {
o = C.map();
} else if (paramType.isArray()) {
o = new Object[]{};
}
// the extra params might already been consumed in field setting
boolean singleParam = paramCount == 1 || (paramCount - extraParams.size() == 1) ;
return singleParam ? JSON.parseObject(jsonObject.toJSONString(), paramType) : o;
}
} else if (null != jsonArray) {
paramCount = paramCount - extraParams.size();
boolean singleParam = paramCount == 1;
if (!singleParam) {
return null;
}
List list = JSON.parseArray(jsonArray.toJSONString(), paramComponentType);
if (Iterable.class.isAssignableFrom(paramType)) {
if (List.class.equals(paramType)) {
return list;
} else if (Set.class.equals(paramType)) {
return C.newSet(list);
}
}
}
return null;
}
public JSONArray jsonArray() {
return jsonArray;
}
private Map<String, String[]> bodyParams() {
if (null == bodyParams) {
synchronized (this) {
if (null == bodyParams) {
Map<String, String[]> map = C.newMap();
H.Method method = request.method();
if (H.Method.POST == method || H.Method.PUT == method) {
RequestBodyParser parser = RequestBodyParser.get(request);
map = parser.parse(this);
}
bodyParams = map;
// try to check if the body is a JSON string
if (bodyParams.size() == 1) {
String[] sa = bodyParams.get(REQ_BODY);
if (null != sa && sa.length == 1) {
String s = sa[0].trim();
if (s.startsWith("{") && s.endsWith("}")) {
jsonObject = (JSONObject) JSON.parse(s);
} else if (s.startsWith("[") && s.endsWith("]")) {
jsonArray = (JSONArray) JSON.parse(s);
}
}
}
}
}
}
return bodyParams;
}
public Map<String, String[]> allParams() {
return allParams;
}
public ISObject upload(String name) {
return uploads.get(name);
}
public ActionContext addUpload(String name, ISObject sobj) {
uploads.put(name, sobj);
return this;
}
/**
* 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 ActionContext __appRenderArgNames(String names) {
return renderArg("__arg_names__", C.listOf(names.split(",")));
}
public List<String> __appRenderArgNames() {
return renderArg("__arg_names__");
}
public ActionContext __controllerInstance(String className, Object instance) {
if (null == controllerInstances) {
controllerInstances = C.newMap();
}
controllerInstances.put(className, instance);
return this;
}
public Object __controllerInstance(String className) {
return null == controllerInstances ? null : controllerInstances.get(className);
}
/**
* 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 ActionContext attribute(String name, Object attr) {
attributes.put(name, attr);
return this;
}
public <T> T attribute(String name) {
return $.cast(attributes.get(name));
}
public <T> T newIntance(String className) {
return app().newInstance(className, this);
}
public <T> T newInstance(Class<? extends T> clazz) {
if (clazz == ActionContext.class) return $.cast(this);
return app().newInstance(clazz, this);
}
/**
* Return cached object by key. The key will be concatenated with
* current session id when fetching the cached object
*
* @param key
* @param <T> the object type
* @return the cached object
*/
public <T> T cached(String key) {
H.Session sess = session();
if (null != sess) {
return sess.cached(key);
} else {
return app().cache().get(key);
}
}
/**
* Add an object into cache by key. The key will be used in conjunction with session id if
* there is a session instance
*
* @param key the key to index the object within the cache
* @param obj the object to be cached
*/
public void cache(String key, Object obj) {
H.Session sess = session();
if (null != sess) {
sess.cache(key, obj);
} else {
app().cache().put(key, obj);
}
}
/**
* Add an object into cache by key with expiration time specified
*
* @param key the key to index the object within the cache
* @param obj the object to be cached
* @param expiration the seconds after which the object will be evicted from the cache
*/
public void cache(String key, Object obj, int expiration) {
H.Session session = this.session;
if (null != session) {
session.cache(key, obj, expiration);
} else {
app().cache().put(key, obj, expiration);
}
}
/**
* Add an object into cache by key and expired after one hour
*
* @param key the key to index the object within the cache
* @param obj the object to be cached
*/
public void cacheForOneHour(String key, Object obj) {
cache(key, obj, 60 * 60);
}
/**
* Add an object into cache by key and expired after half hour
*
* @param key the key to index the object within the cache
* @param obj the object to be cached
*/
public void cacheForHalfHour(String key, Object obj) {
cache(key, obj, 30 * 60);
}
/**
* Add an object into cache by key and expired after 10 minutes
*
* @param key the key to index the object within the cache
* @param obj the object to be cached
*/
public void cacheForTenMinutes(String key, Object obj) {
cache(key, obj, 10 * 60);
}
/**
* Add an object into cache by key and expired after one minute
*
* @param key the key to index the object within the cache+
* @param obj the object to be cached
*/
public void cacheForOneMinute(String key, Object obj) {
cache(key, obj, 60);
}
/**
* Evict cached object
*
* @param key the key indexed the cached object to be evicted
*/
public void evictCache(String key) {
H.Session sess = session();
if (null != sess) {
sess.evict(key);
} else {
app().cache().evict(key);
}
}
public ActionContext addViolations(Set<ConstraintViolation<?>> violations) {
this.violations.addAll(violations);
return this;
}
public ActionContext addViolation(ConstraintViolation<?> violation) {
this.violations.add(violation);
return this;
}
public boolean hasViolation() {
return !violations.isEmpty();
}
public Set<ConstraintViolation> violations() {
return C.set(this.violations);
}
public StringBuilder buildViolationMessage(StringBuilder builder) {
return buildViolationMessage(builder, "\n");
}
public StringBuilder buildViolationMessage(StringBuilder builder, String separator) {
if (violations.isEmpty()) return builder;
for (ConstraintViolation violation : violations) {
builder.append(violation.getMessage()).append(separator);
}
int n = builder.length();
builder.delete(n - separator.length(), n);
return builder;
}
public String violationMessage(String separator) {
return buildViolationMessage(S.builder(), separator).toString();
}
public String violationMessage() {
return violationMessage("\n");
}
public ActionContext flashViolationMessage() {
return flashViolationMessage("\n");
}
public ActionContext flashViolationMessage(String separator) {
if (violations.isEmpty()) return this;
flash().error(violationMessage(separator));
return this;
}
public String actionPath() {
return actionPath;
}
public ActionContext actionPath(String path) {
actionPath = path;
return this;
}
/**
* If {@link #templatePath(String) template path has been set before} then return
* the template path. Otherwise returns the {@link #actionPath()}
*
* @return either template path or action path if template path not set before
*/
public String templatePath() {
String path = super.templatePath();
if (S.notBlank(path)) {
return path;
} else {
return actionPath().replace('.', '/');
}
}
public void startIntercepting() {
state = State.INTERCEPTING;
}
public void startHandling() {
state = State.HANDLING;
}
/**
* Initialize params/renderArgs/attributes and then
* resolve session and flash from cookies
*/
public void resolve() {
E.illegalStateIf(state != State.CREATED);
resolveSession();
resolveFlash();
state = State.SESSION_RESOLVED;
EventBus eventBus = app().eventBus();
eventBus.emit(new PreFireSessionResolvedEvent(session, this));
Act.sessionManager().fireSessionResolved(this);
eventBus.emit(new SessionResolvedEvent(session, this));
}
/**
* Dissolve session and flash into cookies.
* <p><b>Note</b> this method must be called
* before any content has been committed to
* response output stream/writer</p>
*/
public void dissolve() {
if (state == State.SESSION_DISSOLVED) {
return;
}
app().eventBus().emit(new SessionWillDissolveEvent(this));
try {
dissolveFlash();
dissolveSession();
state = State.SESSION_DISSOLVED;
} finally {
app().eventBus().emit(new SessionDissolvedEvent(this));
}
}
/**
* Clear all internal data store/cache and then
* remove this context from thread local
*/
@Override
protected void releaseResources() {
super.releaseResources();
if (this.state != State.DESTROYED) {
this.allParams = null;
this.extraParams = null;
this.requestParamCache = null;
this.attributes.clear();
this.router = null;
this.handler = null;
// xio impl might need this this.request = null;
// xio impl might need this this.response = null;
this.flash = null;
this.session = null;
this.controllerInstances = null;
this.violations.clear();
clearLocal();
this.uploads.clear();
for (Object o : this.attributes.values()) {
if (o instanceof Destroyable) {
((Destroyable) o).destroy();
}
}
this.attributes.clear();
}
this.state = State.DESTROYED;
}
public void saveLocal() {
_local.set(this);
}
public static void clearLocal() {
_local.remove();
}
private Set<Map.Entry<String, String[]>> requestParamCache() {
if (null != requestParamCache) {
return requestParamCache;
}
requestParamCache = new HashSet<Map.Entry<String, String[]>>();
Map<String, String[]> map = C.newMap();
// url queries
Iterator<String> paramNames = request.paramNames().iterator();
while (paramNames.hasNext()) {
final String key = paramNames.next();
final String[] val = request.paramVals(key);
MapUtil.mergeValueInMap(map, key, val);
}
// post bodies
Map<String, String[]> map2 = bodyParams();
for (String key : map2.keySet()) {
String[] val = map2.get(key);
if (null != val) {
MapUtil.mergeValueInMap(map, key, val);
}
}
requestParamCache.addAll(map.entrySet());
return requestParamCache;
}
private void _init() {
uploads = C.newMap();
extraParams = C.newMap();
violations = C.newSet();
attributes = C.newMap();
final Set<Map.Entry<String, String[]>> paramEntrySet = new AbstractSet<Map.Entry<String, String[]>>() {
@Override
public Iterator<Map.Entry<String, String[]>> iterator() {
final Iterator<Map.Entry<String, String[]>> extraItr = new Iterator<Map.Entry<String, String[]>>() {
Iterator<Map.Entry<String, String>> parent = extraParams.entrySet().iterator();
@Override
public boolean hasNext() {
return parent.hasNext();
}
@Override
public Map.Entry<String, String[]> next() {
final Map.Entry<String, String> parentEntry = parent.next();
return new Map.Entry<String, String[]>() {
@Override
public String getKey() {
return parentEntry.getKey();
}
@Override
public String[] getValue() {
return new String[]{parentEntry.getValue()};
}
@Override
public String[] setValue(String[] value) {
throw E.unsupport();
}
};
}
@Override
public void remove() {
throw E.unsupport();
}
};
final Iterator<Map.Entry<String, String[]>> reqParamItr = requestParamCache().iterator();
return new Iterator<Map.Entry<String, String[]>>() {
@Override
public boolean hasNext() {
return extraItr.hasNext() || reqParamItr.hasNext();
}
@Override
public Map.Entry<String, String[]> next() {
if (extraItr.hasNext()) return extraItr.next();
return reqParamItr.next();
}
@Override
public void remove() {
throw E.unsupport();
}
};
}
@Override
public int size() {
int size = extraParams.size();
if (null != request) {
size += requestParamCache().size();
}
return size;
}
};
allParams = new AbstractMap<String, String[]>() {
@Override
public Set<Entry<String, String[]>> entrySet() {
return paramEntrySet;
}
};
}
private void resolveSession() {
this.session = Act.sessionManager().resolveSession(this);
}
private void resolveFlash() {
this.flash = Act.sessionManager().resolveFlash(this);
}
private void dissolveSession() {
Cookie c = Act.sessionManager().dissolveSession(this);
if (null != c) {
config().sessionMapper().serializeSession(c, this);
}
}
private void dissolveFlash() {
Cookie c = Act.sessionManager().dissolveFlash(this);
if (null != c) {
config().sessionMapper().serializeFlash(c, this);
}
}
private static ContextLocal<ActionContext> _local = $.contextLocal();
public static final String METHOD_GET_CURRENT = "current";
public static ActionContext current() {
return _local.get();
}
public static void clearCurrent() {
_local.remove();
}
/**
* Create an new {@code AppContext} and return the new instance
*/
public static ActionContext create(App app, H.Request request, H.Response resp) {
return new ActionContext(app, request, resp);
}
public enum State {
CREATED,
SESSION_RESOLVED,
SESSION_DISSOLVED,
INTERCEPTING,
HANDLING,
DESTROYED;
public boolean isHandling() {
return this == HANDLING;
}
public boolean isIntercepting() {
return this == INTERCEPTING;
}
}
public static class ActionContextEvent extends ActEvent<ActionContext> {
public ActionContextEvent(ActionContext source) {
super(source);
}
public ActionContext context() {
return source();
}
}
private static class SessionEvent extends ActionContextEvent {
private H.Session session;
public SessionEvent(H.Session session, ActionContext source) {
super(source);
this.session = session;
}
public H.Session session() {
return session;
}
}
/**
* This event is fired after session resolved and before any
* {@link act.util.SessionManager.Listener} get called
*/
public static class PreFireSessionResolvedEvent extends SessionEvent {
public PreFireSessionResolvedEvent(H.Session session, ActionContext context) {
super(session, context);
}
}
/**
* This event is fired after session resolved and after all
* {@link act.util.SessionManager.Listener} get notified and
* in turn after all event listeners that listen to the
* {@link PreFireSessionResolvedEvent} get notified
*/
public static class SessionResolvedEvent extends SessionEvent {
public SessionResolvedEvent(H.Session session, ActionContext context) {
super(session, context);
}
}
public static class SessionWillDissolveEvent extends ActionContextEvent {
public SessionWillDissolveEvent(ActionContext source) {
super(source);
}
}
public static class SessionDissolvedEvent extends ActionContextEvent {
public SessionDissolvedEvent(ActionContext source) {
super(source);
}
}
}