forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouter.java
More file actions
1337 lines (1231 loc) · 36.7 KB
/
Router.java
File metadata and controls
1337 lines (1231 loc) · 36.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
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import com.typesafe.config.Config;
import io.jooby.exception.MissingValueException;
import org.slf4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Provider;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
/**
* Routing DSL functions.
*
* @since 2.0.0
* @author edgar
*/
public interface Router extends Registry {
/**
* Find route result.
*/
interface Match {
/**
* True for matching route.
*
* @return True for matching route.
*/
boolean matches();
/**
* Matched route.
*
* @return Matched route.
*/
@Nonnull Route route();
void execute(@Nonnull Context context);
/**
* Path pattern variables.
*
* @return Path pattern variables.
*/
@Nonnull Map<String, String> pathMap();
}
/** HTTP GET. */
String GET = "GET";
/** HTTP POST. */
String POST = "POST";
/** HTTP PUT. */
String PUT = "PUT";
/** HTTP DELETE. */
String DELETE = "DELETE";
/** HTTP PATCH. */
String PATCH = "PATCH";
/** HTTP HEAD. */
String HEAD = "HEAD";
/** HTTP OPTIONS. */
String OPTIONS = "OPTIONS";
/** HTTP TRACE. */
String TRACE = "TRACE";
/** HTTP Methods. */
List<String> METHODS = unmodifiableList(
asList(GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE));
/** Web socket. */
String WS = "WS";
/** Sever-Sent events. */
String SSE = "SSE";
/**
* Application configuration.
*
* @return Application configuration.
*/
@Nonnull Config getConfig();
/**
* Application environment.
*
* @return Application environment.
*/
@Nonnull Environment getEnvironment();
/**
* Returns the supported locales.
*
* @return The supported locales.
*/
@Nonnull List<Locale> getLocales();
/**
* Mutable map of application attributes.
*
* @return Mutable map of application attributes.
*/
@Nonnull Map<String, Object> getAttributes();
/**
* Get an attribute by his key. This is just an utility method around {@link #getAttributes()}.
*
* @param key Attribute key.
* @param <T> Attribute type.
* @return Attribute value.
*/
@Nonnull default <T> T attribute(@Nonnull String key) {
T attribute = (T) getAttributes().get(key);
if (attribute == null) {
throw new MissingValueException(key);
}
return attribute;
}
/**
* Set an application attribute.
*
* @param key Attribute key.
* @param value Attribute value.
* @return This router.
*/
@Nonnull default Router attribute(@Nonnull String key, Object value) {
getAttributes().put(key, value);
return this;
}
/**
* Application service registry. Services are accessible via this registry or
* {@link Jooby#require(Class)} calls.
*
* This method returns a mutable registry. You are free to modify/alter the registry.
*
* @return Service registry.
*/
@Nonnull ServiceRegistry getServices();
/**
* Set application context path. Context path is the base path for all routes. Default is:
* <code>/</code>.
*
* @param contextPath Context path.
* @return This router.
*/
@Nonnull Router setContextPath(@Nonnull String contextPath);
/**
* Get application context path (a.k.a as base path).
*
* @return Application context path (a.k.a as base path).
*/
@Nonnull String getContextPath();
/**
* When true handles X-Forwarded-* headers by updating the values on the current context to
* match what was sent in the header(s).
*
* This should only be installed behind a reverse proxy that has been configured to send the
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by
* sending a header with bogus values.
*
* The headers that are read/set are:
* <ul>
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.</li>
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.</li>
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.</li>
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.</li>
* </ul>
*
* @return True when enabled. Default is false.
*/
boolean isTrustProxy();
/**
* When true handles X-Forwarded-* headers by updating the values on the current context to
* match what was sent in the header(s).
*
* This should only be installed behind a reverse proxy that has been configured to send the
* <code>X-Forwarded-*</code> header, otherwise a remote user can spoof their address by
* sending a header with bogus values.
*
* The headers that are read/set are:
* <ul>
* <li>X-Forwarded-For: Set/update the remote address {@link Context#setRemoteAddress(String)}.</li>
* <li>X-Forwarded-Proto: Set/update request scheme {@link Context#setScheme(String)}.</li>
* <li>X-Forwarded-Host: Set/update the request host {@link Context#setHost(String)}.</li>
* <li>X-Forwarded-Port: Set/update the request port {@link Context#setPort(int)}.</li>
* </ul>
*
* @param trustProxy True to enabled.
* @return This router.
*/
@Nonnull Router setTrustProxy(boolean trustProxy);
/**
* Provides a way to override the current HTTP method. Request must be:
*
* - POST Form/multipart request
*
* For alternative strategy use the {@link #setHiddenMethod(Function)} method.
*
* @param parameterName Form field name.
* @return This router.
*/
@Nonnull Router setHiddenMethod(@Nonnull String parameterName);
/**
* Provides a way to override the current HTTP method using lookup strategy.
*
* @param provider Lookup strategy.
* @return This router.
*/
@Nonnull Router setHiddenMethod(@Nonnull Function<Context, Optional<String>> provider);
/**
* Provides a way to set the current user from a {@link Context}. Current user can be retrieve it
* using {@link Context#getUser()}.
*
* @param provider User provider/factory.
* @return This router.
*/
@Nonnull Router setCurrentUser(@Nullable Function<Context, Object> provider);
/**
* If enabled, allows to retrieve the {@link Context} object associated with the current
* request via the service registry while the request is being processed.
*
* @param contextAsService whether to enable or disable this feature
* @return This router.
*/
@Nonnull Router setContextAsService(boolean contextAsService);
/* ***********************************************************************************************
* use(Router)
* ***********************************************************************************************
*/
/**
* Enabled routes for specific domain. Domain matching is done using the <code>host</code> header.
*
* <pre>{@code
* {
* domain("foo.com", new FooApp());
* domain("bar.com", new BarApp());
* }
* }</pre>
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param domain Predicate
* @param subrouter Subrouter.
* @return This router.
*/
@Nonnull Router domain(@Nonnull String domain, @Nonnull Router subrouter);
/**
* Enabled routes for specific domain. Domain matching is done using the <code>host</code> header.
*
* <pre>{@code
* {
* domain("foo.com", () -> {
* get("/", ctx -> "foo");
* });
* domain("bar.com", () -> {
* get("/", ctx -> "bar");
* });
* }
* }</pre>
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* @param domain Predicate
* @param body Route action.
* @return This router.
*/
@Nonnull RouteSet domain(@Nonnull String domain, @Nonnull Runnable body);
/**
* Import routes from given router. Predicate works like a filter and only when predicate pass
* the routes match against the current request.
*
* Example of domain predicate filter:
*
* <pre>{@code
* {
* use(ctx -> ctx.getHost().equals("foo.com"), new FooApp());
* use(ctx -> ctx.getHost().equals("bar.com"), new BarApp());
* }
* }</pre>
*
* Imported routes are matched only when predicate pass.
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param predicate Context predicate.
* @param router Router to import.
* @return This router.
* @deprecated Use {@link #mount(Predicate, Router)}
*/
@Deprecated
@Nonnull default Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router) {
return mount(predicate, router);
}
/**
* Import routes from given router. Predicate works like a filter and only when predicate pass
* the routes match against the current request.
*
* Example of domain predicate filter:
*
* <pre>{@code
* {
* use(ctx -> ctx.getHost().equals("foo.com"), new FooApp());
* use(ctx -> ctx.getHost().equals("bar.com"), new BarApp());
* }
* }</pre>
*
* Imported routes are matched only when predicate pass.
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param predicate Context predicate.
* @param router Router to import.
* @return This router.
*/
@Nonnull Router mount(@Nonnull Predicate<Context> predicate, @Nonnull Router router);
/**
* Import routes from given action. Predicate works like a filter and only when predicate pass
* the routes match against the current request.
*
* Example of domain predicate filter:
*
* <pre>{@code
* {
* use(ctx -> ctx.getHost().equals("foo.com"), () -> {
* get("/", ctx -> "foo");
* });
* use(ctx -> ctx.getHost().equals("bar.com"), () -> {
* get("/", ctx -> "bar");
* });
* }
* }</pre>
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* @param predicate Context predicate.
* @param body Route action.
* @return This router.
* @deprecated Use {@link #mount(Predicate, Runnable)}
*/
@Deprecated
@Nonnull default RouteSet use(@Nonnull Predicate<Context> predicate, @Nonnull Runnable body) {
return mount(predicate, body);
}
/**
* Import routes from given action. Predicate works like a filter and only when predicate pass
* the routes match against the current request.
*
* Example of domain predicate filter:
*
* <pre>{@code
* {
* mount(ctx -> ctx.getHost().equals("foo.com"), () -> {
* get("/", ctx -> "foo");
* });
* mount(ctx -> ctx.getHost().equals("bar.com"), () -> {
* get("/", ctx -> "bar");
* });
* }
* }</pre>
*
* NOTE: if you run behind a reverse proxy you might to enabled {@link #setTrustProxy(boolean)}.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param predicate Context predicate.
* @param body Route action.
* @return This router.
*/
@Nonnull RouteSet mount(@Nonnull Predicate<Context> predicate, @Nonnull Runnable body);
/**
* Import all routes from the given router and prefix them with the given path.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param path Prefix path.
* @param router Router to import.
* @return This router.
* @deprecated Use {@link #mount(String, Router)}
*/
@Deprecated
@Nonnull default Router use(@Nonnull String path, @Nonnull Router router) {
return mount(path, router);
}
/**
* Import all routes from the given router and prefix them with the given path.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param path Prefix path.
* @param router Router to import.
* @return This router.
*/
@Nonnull Router mount(@Nonnull String path, @Nonnull Router router);
/**
* Import all routes from the given router.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param router Router to import.
* @return This router.
* @deprecated Use {@link #mount(Router)}
*/
@Deprecated
@Nonnull default Router use(@Nonnull Router router) {
return mount(router);
}
/**
* Import all routes from the given router.
*
* NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param router Router to import.
* @return This router.
*/
@Nonnull Router mount(@Nonnull Router router);
/* ***********************************************************************************************
* Mvc
* ***********************************************************************************************
*/
/**
* Import all route method from the given controller class. At runtime the controller instance
* is resolved by calling {@link Jooby#require(Class)}.
*
* @param router Controller class.
* @return This router.
*/
@Nonnull Router mvc(@Nonnull Class router);
/**
* Import all route method from the given controller class.
*
* @param router Controller class.
* @param provider Controller provider.
* @param <T> Controller type.
* @return This router.
*/
@Nonnull <T> Router mvc(@Nonnull Class<T> router, @Nonnull Provider<T> provider);
/**
* Import all route methods from given controller instance.
*
* @param router Controller instance.
* @return This routes.
*/
@Nonnull Router mvc(@Nonnull Object router);
/**
* Add a websocket handler.
*
* @param pattern WebSocket path pattern.
* @param handler WebSocket handler.
* @return A new route.
*/
@Nonnull Route ws(@Nonnull String pattern, @Nonnull WebSocket.Initializer handler);
/**
* Add a server-sent event handler.
*
* @param pattern Path pattern.
* @param handler Handler.
* @return A new route.
*/
@Nonnull Route sse(@Nonnull String pattern, @Nonnull ServerSentEmitter.Handler handler);
/**
* Returns all routes.
*
* @return All routes.
*/
@Nonnull List<Route> getRoutes();
/**
* Register a route response encoder.
*
* @param encoder MessageEncoder instance.
* @return This router.
*/
@Nonnull Router encoder(@Nonnull MessageEncoder encoder);
/**
* Register a route response encoder.
*
* @param contentType Accept header should matches the content-type.
* @param encoder MessageEncoder instance.
* @return This router.
*/
@Nonnull Router encoder(@Nonnull MediaType contentType, @Nonnull MessageEncoder encoder);
/**
* Application temporary directory. This method initialize the {@link Environment} when isn't
* set manually.
*
* @return Application temporary directory.
*/
@Nonnull Path getTmpdir();
/**
* Register a decoder for the given content type.
*
* @param contentType Content type to match.
* @param decoder MessageDecoder.
* @return This router.
*/
@Nonnull Router decoder(@Nonnull MediaType contentType, @Nonnull MessageDecoder decoder);
/**
* Returns the worker thread pool. This thread pool is used to run application blocking code.
*
* @return Worker thread pool.
*/
@Nonnull Executor getWorker();
/**
* Set a worker thread pool. This thread pool is used to run application blocking code.
*
* @param worker Worker thread pool.
* @return This router.
*/
@Nonnull Router setWorker(@Nonnull Executor worker);
/**
* Set the default worker thread pool. Via this method the underlying web server set/suggests the
* worker thread pool that should be used it.
*
* A call to {@link #getWorker()} returns the default thread pool, unless you explicitly set one.
*
* @param worker Default worker thread pool.
* @return This router.
*/
@Nonnull Router setDefaultWorker(@Nonnull Executor worker);
/**
* Add a route decorator to the route pipeline.
*
* @param decorator Decorator.
* @return This router.
*/
@Nonnull Router decorator(@Nonnull Route.Decorator decorator);
/**
* Add a before route decorator to the route pipeline.
*
* @param before Before decorator.
* @return This router.
*/
@Nonnull Router before(@Nonnull Route.Before before);
/**
* Add an after route decorator to the route pipeline.
*
* @param after After decorator.
* @return This router.
*/
@Nonnull Router after(@Nonnull Route.After after);
/**
* Dispatch route pipeline to the {@link #getWorker()} worker thread pool. After dispatch
* application code is allowed to do blocking calls.
*
* @param body Dispatch body.
* @return This router.
*/
@Nonnull Router dispatch(@Nonnull Runnable body);
/**
* Dispatch route pipeline to the given executor. After dispatch application code is allowed to
* do blocking calls.
*
* @param executor Executor. {@link java.util.concurrent.ExecutorService} instances automatically
* shutdown at application exit.
* @param body Dispatch body.
* @return This router.
*/
@Nonnull Router dispatch(@Nonnull Executor executor, @Nonnull Runnable body);
/**
* Group one or more routes. Useful for applying cross cutting concerns to the enclosed routes.
*
* @param body Route body.
* @return All routes created.
*/
@Nonnull RouteSet routes(@Nonnull Runnable body);
/**
* Group one or more routes under a common path prefix. Useful for applying cross cutting
* concerns to the enclosed routes.
*
* @param pattern Path pattern.
* @param body Route body.
* @return All routes created.
*/
@Nonnull RouteSet path(@Nonnull String pattern, @Nonnull Runnable body);
/**
* Add a HTTP GET handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route get(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(GET, pattern, handler);
}
/**
* Add a HTTP POST handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route post(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(POST, pattern, handler);
}
/**
* Add a HTTP PUT handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route put(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(PUT, pattern, handler);
}
/**
* Add a HTTP DELETE handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route delete(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(DELETE, pattern, handler);
}
/**
* Add a HTTP PATCH handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route patch(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(PATCH, pattern, handler);
}
/**
* Add a HTTP HEAD handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route head(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(HEAD, pattern, handler);
}
/**
* Add a HTTP OPTIONS handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route options(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(OPTIONS, pattern, handler);
}
/**
* Add a HTTP TRACE handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull default Route trace(@Nonnull String pattern, @Nonnull Route.Handler handler) {
return route(TRACE, pattern, handler);
}
/**
* Add a static resource handler. Static resources are resolved from file system.
*
* @param pattern Path pattern.
* @param source File system directory.
* @return A route.
*/
default @Nonnull Route assets(@Nonnull String pattern, @Nonnull Path source) {
return assets(pattern, AssetSource.create(source));
}
/**
* Add a static resource handler. Static resources are resolved from:
*
* - file-system if the source folder exists in the current user directory
* - or fallback to classpath when file-system folder doesn't exist.
*
* NOTE: This method choose file-system or classpath, it doesn't merge them.
*
* @param pattern Path pattern.
* @param source File-System folder when exists, or fallback to a classpath folder.
* @return A route.
*/
default @Nonnull Route assets(@Nonnull String pattern, @Nonnull String source) {
Path path = Stream.of(source.split("/"))
.reduce(Paths.get(System.getProperty("user.dir")), Path::resolve, Path::resolve);
if (Files.exists(path)) {
return assets(pattern, path);
}
return assets(pattern, AssetSource.create(getClass().getClassLoader(), source));
}
/**
* Add a static resource handler.
*
* @param pattern Path pattern.
* @param sources Asset sources. At least one source is required.
* @return A route.
*/
default @Nonnull Route assets(@Nonnull String pattern, @Nonnull AssetSource... sources) {
return assets(pattern, new AssetHandler(sources));
}
/**
* Add a static resource handler.
*
* @param pattern Path pattern.
* @param handler Asset handler.
* @return A route.
*/
default @Nonnull Route assets(@Nonnull String pattern, @Nonnull AssetHandler handler) {
return route(GET, pattern, handler);
}
/**
* Add a route.
*
* @param method HTTP method.
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
@Nonnull Route route(@Nonnull String method, @Nonnull String pattern, @Nonnull
Route.Handler handler);
/**
* Find a matching route using the given context.
*
* If no match exists this method returns a route with a <code>404</code> handler.
* See {@link Route#NOT_FOUND}.
*
* @param ctx Web Context.
* @return A route match result.
*/
@Nonnull Match match(@Nonnull Context ctx);
/**
* Find a matching route using the given context.
*
* If no match exists this method returns a route with a <code>404</code> handler.
* See {@link Route#NOT_FOUND}.
*
* @param method Method to match.
* @param path Path to match.
* @return A route match result.
*/
boolean match(@Nonnull String method, @Nonnull String path);
/* Error handler: */
/**
* Map an exception type to a status code.
*
* @param type Exception type.
* @param statusCode Status code.
* @return This router.
*/
@Nonnull Router errorCode(@Nonnull Class<? extends Throwable> type,
@Nonnull StatusCode statusCode);
/**
* Computes the status code for the given exception.
*
* @param cause Exception.
* @return Status code.
*/
@Nonnull StatusCode errorCode(@Nonnull Throwable cause);
/**
* Add a custom error handler that matches the given status code.
*
* @param statusCode Status code.
* @param handler Error handler.
* @return This router.
*/
@Nonnull
default Router error(@Nonnull StatusCode statusCode, @Nonnull ErrorHandler handler) {
return error(statusCode::equals, handler);
}
/**
* Add a custom error handler that matches the given exception type.
*
* @param type Exception type.
* @param handler Error handler.
* @return This router.
*/
@Nonnull
default Router error(@Nonnull Class<? extends Throwable> type,
@Nonnull ErrorHandler handler) {
return error((ctx, x, statusCode) -> {
if (type.isInstance(x) || type.isInstance(x.getCause())) {
handler.apply(ctx, x, statusCode);
}
});
}
/**
* Add a custom error handler that matches the given predicate.
*
* @param predicate Status code filter.
* @param handler Error handler.
* @return This router.
*/
@Nonnull
default Router error(@Nonnull Predicate<StatusCode> predicate,
@Nonnull ErrorHandler handler) {
return error((ctx, x, statusCode) -> {
if (predicate.test(statusCode)) {
handler.apply(ctx, x, statusCode);
}
});
}
/**
* Add a custom error handler.
*
* @param handler Error handler.
* @return This router.
*/
@Nonnull Router error(@Nonnull ErrorHandler handler);
/**
* Get the error handler.
*
* @return An error handler.
*/
@Nonnull ErrorHandler getErrorHandler();
/**
* Application logger.
*
* @return Application logger.
*/
@Nonnull Logger getLog();
/**
* Add a response handler factory.
*
* @param factory Response handler factory.
* @return This router.
*/
@Nonnull Router responseHandler(@Nonnull ResponseHandler factory);
/**
* Router options.
*
* @return Router options.
*/
@Nonnull Set<RouterOption> getRouterOptions();
/**
* Set router options.
*
* @param options router options.
* @return This router.
*/
@Nonnull Router setRouterOptions(@Nonnull RouterOption... options);
/**
* Session store. Default use a cookie ID with a memory storage.
*
* See {@link SessionStore#memory()}.
*
* @return Session store.
*/
@Nonnull SessionStore getSessionStore();
/**
* Set session store.
*
* @param store Session store.
* @return This router.
*/
@Nonnull Router setSessionStore(@Nonnull SessionStore store);
/**
* Get an executor from application registry.
*
* @param name Executor name.
* @return Executor.
*/
default @Nonnull Executor executor(@Nonnull String name) {
return require(Executor.class, name);
}
/**
* Put an executor into the application registry.
*
* @param name Executor's name.
* @param executor Executor.
* @return This router.
*/
@Nonnull Router executor(@Nonnull String name, @Nonnull Executor executor);
/**
* Set flash cookie name.
*
* @param name Flash cookie name.
* @return This router.
* @deprecated Use {@link #setFlashCookie(Cookie)} instead.
*/
@Deprecated @Nonnull Router setFlashCookie(@Nonnull String name);
/**
* Template for the flash cookie. Default name is: <code>jooby.flash</code>.
*
* @return Template for the flash cookie.
*/
@Nonnull Cookie getFlashCookie();
/**
* Sets a cookie used as a template to generate the flash cookie, allowing
* to customize the cookie name and other cookie parameters.
*
* @param flashCookie The cookie template.
* @return This router.
*/
@Nonnull Router setFlashCookie(@Nonnull Cookie flashCookie);
/**
* Add a custom string value converter.
*
* @param converter Custom value converter.
* @return This router.