-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathRouter.java
More file actions
1199 lines (1094 loc) · 30.4 KB
/
Copy pathRouter.java
File metadata and controls
1199 lines (1094 loc) · 30.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
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 static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;
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.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 org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import com.typesafe.config.Config;
import io.jooby.exception.MissingValueException;
import io.jooby.handler.AssetHandler;
import io.jooby.handler.AssetSource;
import io.jooby.output.OutputFactory;
import io.jooby.value.ValueFactory;
/**
* 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.
*/
Route route();
/**
* Executes matched route.
*
* @param context Web Context.
* @param pipeline Route pipeline.
* @return route response.
*/
Object execute(Context context, Route.Handler pipeline);
/**
* Executes matched route.
*
* @param context Web Context.
* @return route response.
*/
default Object execute(Context context) {
return execute(context, route().getPipeline());
}
/**
* Path pattern variables.
*
* @return Path pattern variables.
*/
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 = List.of(GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, TRACE);
/** Web socket. */
String WS = "WS";
/** Sever-Sent events. */
String SSE = "SSE";
/**
* Application configuration.
*
* @return Application configuration.
*/
Config getConfig();
/**
* Application environment.
*
* @return Application environment.
*/
Environment getEnvironment();
/**
* Returns the supported locales.
*
* @return The supported locales.
*/
List<Locale> getLocales();
/**
* Mutable map of application attributes.
*
* @return Mutable map of application attributes.
*/
Map<String, Object> getAttributes();
/**
* Get an attribute by his key. This is just a utility method around {@link #getAttributes()}.
*
* @param key Attribute key.
* @param <T> Attribute type.
* @return Attribute value.
*/
default <T> T getAttribute(String key) {
@SuppressWarnings("unchecked")
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.
*/
default Router setAttribute(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.
*
* <p>This method returns a mutable registry. You are free to modify/alter the registry.
*
* @return Service registry.
*/
ServiceRegistry getServices();
/**
* Server options.
*
* @return Server options.
*/
ServerOptions getServerOptions();
/**
* Set application context path. Context path is the base path for all routes. Default is: <code>/
* </code>.
*
* @param contextPath Context path.
* @return This router.
*/
Router setContextPath(String contextPath);
/**
* Get application context path (a.k.a as base path).
*
* @return Application context path (a.k.a as base path).
*/
String getContextPath();
/**
* True when router started.
*
* @return True when router started.
*/
boolean isStarted();
/**
* True when router stopped.
*
* @return True when router stopped.
*/
boolean isStopped();
/**
* Provides a way to override the current HTTP method. Request must be:
*
* <p>- POST Form/multipart request
*
* <p>For alternative strategy use the {@link #setHiddenMethod(Function)} method.
*
* @param parameterName Form field name.
* @return This router.
*/
Router setHiddenMethod(String parameterName);
/**
* Provides a way to override the current HTTP method using lookup strategy.
*
* @param provider Lookup strategy.
* @return This router.
*/
Router setHiddenMethod(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.
*/
Router setCurrentUser(Function<Context, Object> provider);
/* ***********************************************************************************************
* 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
* RouterOptions#setTrustProxy(boolean)}.
*
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param domain Predicate
* @param subrouter Subrouter.
* @return Created routes.
*/
Route.Set domain(String domain, 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
* RouterOptions#setTrustProxy(boolean)}.
*
* @param domain Predicate
* @param body Route action.
* @return Created routes.
*/
Route.Set domain(String domain, Runnable body);
/**
* Import routes from given router. Predicate works like a filter and only when predicate pass the
* routes match against the current request.
*
* <p>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.
*
* <p>NOTE: if you run behind a reverse proxy you might to enabled {@link
* RouterOptions#setTrustProxy(boolean)}.
*
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param predicate Context predicate.
* @param router Router to import.
* @return Created routes.
*/
Route.Set mount(Predicate<Context> predicate, Router router);
/**
* Import routes from given action. Predicate works like a filter and only when predicate pass the
* routes match against the current request.
*
* <p>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
* RouterOptions#setTrustProxy(boolean)}.
*
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param predicate Context predicate.
* @param body Route action.
* @return Created routes.
*/
Route.Set mount(Predicate<Context> predicate, Runnable body);
/**
* Import all routes from the given router and prefix them with the given path.
*
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param path Prefix path.
* @param router Router to import.
* @return Created routes.
*/
Route.Set mount(String path, Router router);
/**
* Import all routes from the given router.
*
* <p>NOTE: ONLY routes are imported. Services, callback, etc.. are ignored.
*
* @param router Router to import.
* @return Created routes.
*/
Route.Set mount(Router router);
/* ***********************************************************************************************
* Mvc
* ***********************************************************************************************
*/
/**
* Add a websocket handler.
*
* @param pattern WebSocket path pattern.
* @param handler WebSocket handler.
* @return A new route.
*/
Route ws(String pattern, WebSocket.Initializer handler);
/**
* Add a server-sent event handler.
*
* @param pattern Path pattern.
* @param handler Handler.
* @return A new route.
*/
Route sse(String pattern, ServerSentEmitter.Handler handler);
/**
* Returns all routes.
*
* @return All routes.
*/
List<Route> getRoutes();
/**
* Register a route response encoder.
*
* @param encoder MessageEncoder instance.
* @return This router.
*/
Router encoder(MessageEncoder encoder);
/**
* Register a route response encoder.
*
* @param contentType Accept header should match the content-type.
* @param encoder MessageEncoder instance.
* @return This router.
*/
Router encoder(MediaType contentType, MessageEncoder encoder);
/**
* Application temporary directory. This method initialize the {@link Environment} when isn't set
* manually.
*
* @return Application temporary directory.
*/
Path getTmpdir();
/**
* Register a decoder for the given content type.
*
* @param contentType Content type to match.
* @param decoder MessageDecoder.
* @return This router.
*/
Router decoder(MediaType contentType, MessageDecoder decoder);
/**
* Returns the worker thread pool. This thread pool is used to run application blocking code.
*
* @return Worker thread pool.
*/
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.
*/
Router setWorker(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.
*
* <p>A call to {@link #getWorker()} returns the default thread pool, unless you explicitly set
* one.
*
* @param worker Default worker thread pool.
* @return This router.
*/
Router setDefaultWorker(Executor worker);
/**
* Output factory.
*
* @return Output factory.
*/
OutputFactory getOutputFactory();
/**
* Attach a filter to the route pipeline.
*
* @param filter Filter.
* @return This router.
*/
Router use(Route.Filter filter);
/**
* Add a before route decorator to the route pipeline.
*
* @param before Before decorator.
* @return This router.
*/
Router before(Route.Before before);
/**
* Add an after route decorator to the route pipeline.
*
* @param after After decorator.
* @return This router.
*/
Router after(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.
*/
Router dispatch(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.
*/
Router dispatch(Executor executor, 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.
*/
Route.Set routes(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.
*/
Route.Set path(String pattern, Runnable body);
/**
* Add a HTTP GET handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route get(String pattern, Route.Handler handler) {
return route(GET, pattern, handler);
}
/**
* Add a HTTP POST handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route post(String pattern, Route.Handler handler) {
return route(POST, pattern, handler);
}
/**
* Add a HTTP PUT handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route put(String pattern, Route.Handler handler) {
return route(PUT, pattern, handler);
}
/**
* Add a HTTP DELETE handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route delete(String pattern, Route.Handler handler) {
return route(DELETE, pattern, handler);
}
/**
* Add a HTTP PATCH handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route patch(String pattern, Route.Handler handler) {
return route(PATCH, pattern, handler);
}
/**
* Add a HTTP HEAD handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route head(String pattern, Route.Handler handler) {
return route(HEAD, pattern, handler);
}
/**
* Add a HTTP OPTIONS handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route options(String pattern, Route.Handler handler) {
return route(OPTIONS, pattern, handler);
}
/**
* Add a HTTP TRACE handler.
*
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
default Route trace(String pattern, 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 AssetHandler assets(String pattern, Path source) {
return assets(pattern, AssetSource.create(source));
}
/**
* Add a static resource handler. Static resources are resolved from:
*
* <p>- file-system if the source folder exists in the current user directory - or fallback to
* classpath when file-system folder doesn't exist.
*
* <p>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 AssetHandler.
*/
default AssetHandler assets(String pattern, 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 source Asset source.
* @param sources additional Asset sources.
* @return A route.
*/
default AssetHandler assets(String pattern, AssetSource source, AssetSource... sources) {
AssetSource[] allSources;
if (sources.length == 0) {
allSources = new AssetSource[] {source};
} else {
allSources = new AssetSource[1 + sources.length];
allSources[0] = source;
System.arraycopy(sources, 0, allSources, 1, sources.length);
}
return assets(pattern, new AssetHandler(allSources));
}
/**
* Add a static resource handler.
*
* @param pattern Path pattern.
* @param handler Asset handler.
* @return A route.
*/
default AssetHandler assets(String pattern, AssetHandler handler) {
route(GET, pattern, handler);
return handler;
}
/**
* Add a route.
*
* @param method HTTP method.
* @param pattern Path pattern.
* @param handler Application code.
* @return A route.
*/
Route route(String method, String pattern, Route.Handler handler);
/**
* Find a matching route using the given context.
*
* <p>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.
*/
Match match(Context ctx);
/**
* Find a matching route using the given context.
*
* <p>If no match exists this method returns a route with a <code>404</code> handler. See {@link
* Route#NOT_FOUND}.
*
* @param pattern Pattern to match.
* @param path Path to match.
* @return A route match result.
*/
boolean match(String pattern, String path);
/* Error handler: */
/**
* Map an exception type to a status code.
*
* @param type Exception type.
* @param statusCode Status code.
* @return This router.
*/
Router errorCode(Class<? extends Throwable> type, StatusCode statusCode);
/**
* Computes the status code for the given exception.
*
* @param cause Exception.
* @return Status code.
*/
StatusCode errorCode(Throwable cause);
/**
* Add a custom error handler that matches the given status code.
*
* @param statusCode Status code.
* @param handler Error handler.
* @return This router.
*/
default Router error(StatusCode statusCode, 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.
*/
default Router error(Class<? extends Throwable> type, 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.
*/
default Router error(Predicate<StatusCode> predicate, 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.
*/
Router error(ErrorHandler handler);
/**
* Get the error handler.
*
* @return An error handler.
*/
ErrorHandler getErrorHandler();
/**
* Application logger.
*
* @return Application logger.
*/
Logger getLog();
/**
* Router options.
*
* @return Router options.
*/
RouterOptions getRouterOptions();
/**
* Set router options.
*
* @param options router options.
* @return This router.
*/
Router setRouterOptions(RouterOptions options);
/**
* Session store. Default is {@link SessionStore#UNSUPPORTED}.
*
* @return Session store.
*/
SessionStore getSessionStore();
/**
* Set session store.
*
* @param store Session store.
* @return This router.
*/
Router setSessionStore(SessionStore store);
/**
* Get an executor from application registry.
*
* @param name Executor name.
* @return Executor.
*/
default Executor executor(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.
*/
Router executor(String name, Executor executor);
/**
* Template for the flash cookie. Default name is: <code>jooby.flash</code>.
*
* @return Template for the flash cookie.
*/
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.
*/
Router setFlashCookie(Cookie flashCookie);
/**
* Value factory.
*
* @return Value factory.
*/
ValueFactory getValueFactory();
/**
* Retrieves a list of available template engines.
*
* @return a list of TemplateEngine objects representing the available template engines.
*/
List<TemplateEngine> getTemplateEngines();
/**
* Set value factory, useful for custom value factory.
*
* @param valueFactory Value factory.
* @return This router.
*/
Router setValueFactory(ValueFactory valueFactory);
/**
* Ensure path start with a <code>/</code>(leading slash).
*
* @param path Path to process.
* @return Path with leading slash.
*/
static String leadingSlash(@Nullable String path) {
if (path == null || path.length() == 0 || path.equals("/")) {
return "/";
}
return path.charAt(0) == '/' ? path : "/" + path;
}
/**
* Strip trailing slashes.
*
* @param path Path to process.
* @return Path without trailing slashes.
*/
static String noTrailingSlash(String path) {
StringBuilder buff = new StringBuilder(path);
int i = buff.length() - 1;
while (i > 0 && buff.charAt(i) == '/') {
buff.setLength(i);
i -= 1;
}
if (path.length() != buff.length()) {
return buff.toString();
}
return path;
}
/**
* Normalize a path by removing consecutive <code>/</code>(slashes).
*
* @param path Path to process.
* @return Safe path pattern.
*/
static String normalizePath(@Nullable String path) {
if (path == null || path.length() == 0 || path.equals("/")) {
return "/";
}
int len = path.length();
boolean modified = false;
int p = 0;
char[] buff = new char[len + 1];
if (path.charAt(0) != '/') {
buff[p++] = '/';
modified = true;
}
for (int i = 0; i < path.length(); i++) {
char ch = path.charAt(i);
if (ch != '/') {
buff[p++] = ch;
} else if (i == 0 || path.charAt(i - 1) != '/') {
buff[p++] = ch;
} else {
// double slash
modified = true;
}
}
// creates string?
return modified ? new String(buff, 0, p) : path;
}
/**
* Extract path keys from given path pattern. A path key (a.k.a path variable) looks like:
*
* <pre>/product/{id}</pre>
*
* @param pattern Path pattern.
* @return Path keys.
*/
static List<String> pathKeys(String pattern) {
return pathKeys(pattern, (k, v) -> {});
}
/**
* Extract path keys from given path pattern. A path key (a.k.a path variable) looks like:
*
* <pre>/product/{id}</pre>
*
* @param pattern Path pattern.
* @param consumer Listen for key and regex variables found.
* @return Path keys.
*/
static List<String> pathKeys(String pattern, BiConsumer<String, String> consumer) {
List<String> result = new ArrayList<>();
int start = -1;
int end = Integer.MAX_VALUE;
int len = pattern.length();
int curly = 0;
for (int i = 0; i < len; i++) {
char ch = pattern.charAt(i);
if (ch == '{') {
if (curly == 0) {
start = i + 1;
end = Integer.MAX_VALUE;
}
curly += 1;
} else if (ch == ':') {
end = i;
} else if (ch == '}') {
curly -= 1;