Repository navigation
Expand file tree
/
Copy pathController.java
More file actions
684 lines (615 loc) · 24.6 KB
/
Copy pathController.java
File metadata and controls
684 lines (615 loc) · 24.6 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
package act.controller;
import act.app.ActionContext;
import act.conf.AppConfigKey;
import act.controller.meta.HandlerMethodMetaInfo;
import act.util.DisableFastJsonCircularReferenceDetect;
import act.util.FastJsonIterable;
import act.util.PropertySpec;
import act.view.*;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.result.*;
import org.osgl.storage.ISObject;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;
import java.io.File;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.Map;
import static org.osgl.http.H.Format.*;
/**
* Mark a class as Controller, which contains at least one of the following:
* <ul>
* <li>Action handler method</li>
* <li>Any one of Before/After/Exception/Finally interceptor</li>
* </ul>
* <p>The framework will scan all Classes under the {@link AppConfigKey#CONTROLLER_PACKAGE}
* or any class outside of the package but with this annotation marked</p>
*/
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface Controller {
/**
* Indicate the context path for all action methods declared
* in this controller.
* <p/>
* <p>Default value: "{@code /}"</p>
*
* @return the controller context path
*/
String value() default "/";
/**
* Specify the port(s) this controller's action method shall be
* routed from.
*
* @return the port name
* @see AppConfigKey#NAMED_PORTS
*/
String[] port() default {};
/**
* Provides utilities for controller action methods to emit rendering results
*/
public static class Util {
private static final Ok OK = Ok.INSTANCE;
private static final NotFound NOT_FOUND = NotFound.INSTANCE;
private static final Forbidden FORBIDDEN = Forbidden.INSTANCE;
private static final BadRequest BAD_REQUEST = BadRequest.INSTANCE;
/**
* Returns an {@link Ok} result
*/
public static Result ok() {
return OK;
}
/**
* Returns a {@link Created} result
*
* @param resourceGetUrl the URL to access the new resource been created
* @return the result as described
*/
public static Result created(String resourceGetUrl) {
return new Created(resourceGetUrl);
}
public static Result notModified() {
return NotModified.INSTANCE;
}
public static Result notModified(String etag, Object... args) {
return new NotModified(etag, args);
}
/**
* Returns a {@link Accepted} result
*
* @param statusMonitorUrl the URL to check the request process status
* @return the result as described
*/
public static Result accepted(String statusMonitorUrl) {
return new Accepted(statusMonitorUrl);
}
/**
* Returns an {@link NotFound} result
*/
public static Result notFound() {
return ActNotFound.create();
}
/**
* Returns an {@link NotFound} result with custom message
* template and arguments. The final message is rendered with
* the template and arguments using {@link String#format(String, Object...)}
*
* @param msg the message template
* @param args the message argument
*/
public static Result notFound(String msg, Object... args) {
return ActNotFound.create(msg, args);
}
/**
* Throws out an {@link NotFound} result if the object specified is
* {@code null}
*
* @param o the object to be evaluated
*/
public static <T> T notFoundIfNull(T o) {
if (null == o) {
throw ActNotFound.create();
}
return o;
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the object specified is {@code null}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param o the object to be evaluated
* @param msg the message template
* @param args the message argument
*/
public static <T> T notFoundIfNull(T o, String msg, Object... args) {
if (null == o) {
throw ActNotFound.create(msg, args);
}
return o;
}
/**
* Throws out an {@link NotFound} result if the boolean expression specified
* is {@code true}
* {@code null}
*
* @param test the boolean expression to be evaluated
*/
public static void notFoundIf(boolean test) {
if (test) {
throw ActNotFound.create();
}
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the expression specified is {@code true}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param test the boolean expression
* @param msg the message template
* @param args the message argument
*/
public static void notFoundIf(boolean test, String msg, Object... args) {
if (test) {
throw ActNotFound.create(msg, args);
}
}
/**
* Throws out an {@link NotFound} result if the boolean expression specified
* is {@code false}
* {@code null}
*
* @param test the boolean expression to be evaluated
*/
public static void notFoundIfNot(boolean test) {
notFoundIf(!test);
}
/**
* Throws out an {@link NotFound} result with custom message template and
* arguments if the expression specified is {@code false}. The final message is
* rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param test the boolean expression
* @param msg the message template
* @param args the message argument
*/
public static void notFoundIfNot(boolean test, String msg, Object... args) {
notFoundIf(!test, msg, args);
}
public static Result badRequest() {
return ActBadRequest.create();
}
public static void badRequestIf(boolean test) {
if (test) {
throw ActBadRequest.create();
}
}
public static void badRequestIf(boolean test, String msg, Object... args) {
if (test) {
throw ActBadRequest.create(msg, args);
}
}
public static void badRequestIfNot(boolean test) {
if (!test) {
throw ActBadRequest.create();
}
}
public static void badRequestIfNot(boolean test, String msg, Object... args) {
badRequestIf(!test, msg, args);
}
public static Result conflict() {
return ActConflict.create();
}
public static Result conflict(String message, Object... args) {
return ActConflict.create(message, args);
}
public static void conflictIf(boolean test) {
if (test) {
throw ActConflict.create();
}
}
public static void conflictIf(boolean test, String message, Object... args) {
if (test) {
throw ActConflict.create(message, args);
}
}
public static void conflictIfNot(boolean test) {
conflictIf(!test);
}
public static void conflictIfNot(boolean test, String message, Object... args) {
conflictIf(!test, message, args);
}
public static Result unauthorized() {
return ActUnauthorized.create();
}
public static Result unauthorized(String realm) {
return ActUnauthorized.create(realm);
}
public static void unauthorizedIf(boolean test) {
if (test) {
throw ActUnauthorized.create();
}
}
public static void unauthorizedIf(boolean test, String realm) {
if (test) {
throw ActUnauthorized.create(realm);
}
}
public static void unauthorizedIfNot(boolean test) {
unauthorizedIf(!test);
}
public static void unauthorizedIfNot(boolean test, String realm) {
unauthorizedIf(!test, realm);
}
/**
* Returns a {@link Forbidden} result
*/
public static Result forbidden() {
return ActForbidden.create();
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code true}
*
* @param test the test condition
*/
public static void forbiddenIf(boolean test) {
if (test) {
throw ActForbidden.create();
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test the test condition
*/
public static void forbiddenIfNot(boolean test) {
forbiddenIf(!test);
}
/**
* Returns a {@link Forbidden} result with custom message
* template and arguments. The final message is rendered with
* the template and arguments using {@link String#format(String, Object...)}
*
* @param msg the message template
* @param args the message argument
*/
public static Result forbidden(String msg, Object... args) {
return ActForbidden.create(msg, args);
}
/**
* Throws a {@link Forbidden} result if test condition is {@code true}
*
* @param test the test condition
* @param msg the message format template
* @param args the message format arguments
*/
public static void forbiddenIf(boolean test, String msg, Object... args) {
if (test) {
throw ActForbidden.create(msg, args);
}
}
/**
* Throws a {@link Forbidden} result if the test condition is {@code false}
*
* @param test the test condition
* @param msg the message format template
* @param args the message format arguments
*/
public static void forbiddenIfNot(boolean test, String msg, Object... args) {
forbiddenIf(!test, msg, args);
}
public static Result redirect(String url, Object... args) {
return new Redirect(url, args);
}
/**
* Returns a {@link RenderText} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg the message format template
* @param args the message format arguments
*/
public static Result text(String msg, Object... args) {
return new RenderText(msg, args);
}
/**
* Returns a {@link RenderText} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg the message format template
* @param args the message format arguments
*/
public static Result html(String msg, Object... args) {
return new RenderHtml(msg, args);
}
/**
* Returns a {@link RenderJSON} result with specified message template
* and args. The final message is rendered with the template and arguments using
* {@link String#format(String, Object...)}
*
* @param msg the message format template
* @param args the message format arguments
*/
public static Result json(String msg, Object... args) {
return new RenderJSON(msg, args);
}
/**
* Returns a {@link RenderJSON} result with any object. This method will
* call underline JSON serializer to transform the object into a JSON string
*
* @param data the data to be rendered as JSON string
*/
public static Result json(Object data) {
return new RenderJSON(data);
}
/**
* Returns a {@link RenderBinary} result with an {@link ISObject} instance. The result will render
* the binary using "inline" content disposition
*
* @param sobj the {@link ISObject} instance
*/
public static Result binary(ISObject sobj) {
return new RenderBinary(sobj.asInputStream(), sobj.getAttribute(ISObject.ATTR_FILE_NAME), sobj.getAttribute(ISObject.ATTR_CONTENT_TYPE), true);
}
/**
* Returns a {@link RenderBinary} result with an {@link ISObject} instance. The result will render
* the binary using "attachment" content disposition
*
* @param sobj the {@link ISObject} instance
*/
public static Result download(ISObject sobj) {
return new RenderBinary(sobj.asInputStream(), sobj.getAttribute(ISObject.ATTR_FILE_NAME), sobj.getAttribute(ISObject.ATTR_CONTENT_TYPE), false);
}
/**
* Returns a {@link RenderBinary} result with a file. The result will render
* the binary using "inline" content disposition.
*
* @param file the file to be rendered
*/
public static Result binary(File file) {
return new RenderBinary(file);
}
/**
* Returns a {@link RenderBinary} result with a file. The result will render
* the binary using "attachment" content disposition.
*
* @param file the file to be rendered
*/
public static Result download(File file) {
return new RenderBinary(file, file.getName(), false);
}
/**
* Returns a {@link RenderTemplate} result with a render arguments map.
* Note the template path should be set via {@link ActionContext#templatePath(String)}
* method
*
* @param args
*/
public static Result template(Map<String, Object> args) {
return new RenderTemplate(args);
}
/**
* The caller to this magic {@code render} method is subject to byte code enhancement. All
* parameter passed into this method will be put into the application context via
* {@link ActionContext#renderArg(String, Object)} using the variable name found in the
* local variable table. If the first argument is of type String and there is no variable name
* associated with that variable then it will be treated as template path and get set to the
* context via {@link ActionContext#templatePath(String)} method.
* <p>This method returns different render results depends on the request format</p>
* <table>
* <tr>
* <th>Format</th>
* <th>Result type</th>
* </tr>
* <tr>
* <td>{@link org.osgl.http.H.Format#json}</td>
* <td>A JSON string that map the arguments to their own local variable names</td>
* </tr>
* <tr>
* <td>{@link org.osgl.http.H.Format#html} or any other text formats</td>
* <td>{@link RenderTemplate}</td>
* </tr>
* <tr>
* <td>{@link org.osgl.http.H.Format#pdf} or any other binary format</td>
* <td>If first argument is of type File or InputStream, then outbound the
* content as a binary stream, otherwise throw out {@link org.osgl.exception.UnsupportedException}</td>
* </tr>
* </table>
*
* @param args
*/
public static Result render(Object... args) {
return new RenderAny();
}
/**
* Kind of like {@link #render(Object...)}, the only differences is this method force to render a template
* without regarding to the request format
*
* @param args
*/
public static Result renderTemplate(Object... args) {
return new RenderTemplate();
}
private static void setDefaultContextType(H.Request req, H.Response resp) {
resp.contentType(req.contentType().contentType());
}
public static Result inferResult(Result r, ActionContext actionContext) {
return r;
}
public static Result inferResult(String s, ActionContext actionContext) {
if (actionContext.isJSON()) {
s = s.trim();
if (!s.startsWith("[") && !s.startsWith("{")) {
s = S.fmt("{\"result\": \"%s\"}", s);
}
return new RenderJSON(s);
}
H.Format fmt = actionContext.accept();
if (HTML == fmt || H.Format.UNKNOWN == fmt) {
return html(s);
}
if (TXT == fmt || CSV == fmt) {
return new RenderText(fmt, s);
}
if (XML == fmt) {
s = s.trim();
if (!s.startsWith("<") && !s.endsWith(">")) {
s = S.fmt("<result>%s</result>", s);
}
return new RenderText(fmt, s);
}
throw E.unexpected("Cannot apply text result to format: %s", fmt);
}
public static Result inferResult(Map<String, Object> map, ActionContext actionContext) {
if (actionContext.isJSON()) {
return new RenderJSON(map);
}
return new RenderTemplate(map);
}
/**
* @param array
* @param actionContext
* @return
*/
public static Result inferResult(Object[] array, ActionContext actionContext) {
if (actionContext.isJSON()) {
return new RenderJSON(array);
}
throw E.tbd("render template with render args in array");
}
/**
* Infer {@link Result} from an {@link InputStream}. If the current context is in
* {@code JSON} format then it will render a {@link RenderJSON JSON} result from the content of the
* input stream. Otherwise, it will render a {@link RenderBinary binary} result from the inputstream
*
* @param is the inputstream
* @param actionContext
* @return a Result inferred from the inputstream specified
*/
public static Result inferResult(InputStream is, ActionContext actionContext) {
if (actionContext.isJSON()) {
return new RenderJSON(IO.readContentAsString(is));
} else {
return new RenderBinary(is, null, true);
}
}
/**
* Infer {@link Result} from an {@link File}. If the current context is in
* {@code JSON} format then it will render a {@link RenderJSON JSON} result from the content of the
* file. Otherwise, it will render a {@link RenderBinary binary} result from the file specified
*
* @param file the file
* @param actionContext
* @return a Result inferred from the file specified
*/
public static Result inferResult(File file, ActionContext actionContext) {
if (actionContext.isJSON()) {
return new RenderJSON(IO.readContentAsString(file));
} else {
return new RenderBinary(file);
}
}
public static Result inferResult(ISObject sobj, ActionContext context) {
if (context.isJSON()) {
return new RenderJSON(sobj.asString());
} else {
return binary(sobj);
}
}
/**
* Infer a {@link Result} from a {@link Object object} value v:
* <ul>
* <li>If v is {@code null} then null returned</li>
* <li>If v is instance of {@code Result} then it is returned directly</li>
* <li>If v is instance of {@code String} then {@link #inferResult(String, ActionContext)} is used
* to infer the {@code Result}</li>
* <li>If v is instance of {@code InputStream} then {@link #inferResult(InputStream, ActionContext)} is used
* to infer the {@code Result}</li>
* <li>If v is instance of {@code File} then {@link #inferResult(File, ActionContext)} is used
* to infer the {@code Result}</li>
* <li>If v is instance of {@code Map} then {@link #inferResult(Map, ActionContext)} is used
* to infer the {@code Result}</li>
* <li>If v is an array of {@code Object} then {@link #inferResult(Object[], ActionContext)} is used
* to infer the {@code Result}</li>
* </ul>
*
* @param v the value to be rendered
* @param actionContext the action context
* @return the rendered result
*/
public static Result inferResult(HandlerMethodMetaInfo meta, Object v, ActionContext actionContext, boolean hasTemplate) {
if (null == v && !hasTemplate) {
return null;
} else if (v instanceof Result) {
return (Result) v;
} else if (v instanceof String) {
if (hasTemplate) {
return inferToTemplate(v, actionContext);
}
return inferResult((String) v, actionContext);
} else if (v instanceof InputStream) {
return inferResult((InputStream) v, actionContext);
} else if (v instanceof File) {
return inferResult((File) v, actionContext);
} else if (v instanceof ISObject) {
return inferResult((ISObject) v, actionContext);
} else if (v instanceof Map) {
if (hasTemplate) {
return inferToTemplate((Map) v, actionContext);
}
return new RenderJSON(v);
} else if (hasTemplate && v instanceof Object[]) {
throw E.tbd("Render template with array");
} else {
if (hasTemplate) {
return inferToTemplate(v, actionContext);
}
if (actionContext.isJSON()) {
// patch https://github.com/alibaba/fastjson/issues/478
if (meta.disableJsonCircularRefDetect()) {
DisableFastJsonCircularReferenceDetect.option.set(true);
}
if (v instanceof Iterable && !(v instanceof Collection)) {
v = new FastJsonIterable<>((Iterable) v);
}
PropertySpec.MetaInfo propertySpec = (null == meta) ? null : meta.propertySpec();
try {
if (null == propertySpec) {
return new RenderJSON(v);
}
return new FilteredRenderJSON(v, propertySpec, actionContext);
} finally {
if (meta.disableJsonCircularRefDetect()) {
DisableFastJsonCircularReferenceDetect.option.set(false);
}
}
} else if (actionContext.isXML()) {
PropertySpec.MetaInfo propertySpec = (null == meta) ? null : meta.propertySpec();
return new FilteredRenderXML(v, propertySpec, actionContext);
} else if (actionContext.accept() == H.Format.CSV) {
PropertySpec.MetaInfo propertySpec = (null == meta) ? null : meta.propertySpec();
return new RenderCSV(v, propertySpec, actionContext);
} else {
String s = meta.returnType().getDescriptor().startsWith("[") ? $.toString2(v) : v.toString();
return inferResult(s, actionContext);
}
}
}
private static Result inferToTemplate(Object v, ActionContext actionContext) {
actionContext.renderArg("result", v);
return new RenderTemplate();
}
private static Result inferToTemplate(Map map, ActionContext actionContext) {
return new RenderTemplate(map);
}
}
}