This repository was archived by the owner on Mar 3, 2026. It is now read-only.
forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.java
More file actions
103 lines (84 loc) · 2.39 KB
/
Route.java
File metadata and controls
103 lines (84 loc) · 2.39 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
package io.jooby;
import javax.annotation.Nonnull;
public interface Route {
interface Pipeline {
void resume(@Nonnull Context ctx);
void next(@Nonnull Context ctx);
}
interface Filter {
void handle(@Nonnull Context ctx, @Nonnull Pipeline chain) throws Throwable;
}
interface Before extends Filter {
default void handle(@Nonnull Context ctx, @Nonnull Pipeline chain) throws Throwable {
handle(ctx);
chain.next(ctx);
}
void handle(@Nonnull Context ctx) throws Throwable;
}
interface After extends Filter {
default void handle(@Nonnull Context ctx, @Nonnull Pipeline chain) throws Throwable {
ctx.after(this);
chain.next(ctx);
}
default @Nonnull After then(@Nonnull After next) {
return ctx -> {
handle(ctx);
next.handle(ctx);
};
}
void handle(@Nonnull Context ctx) throws Throwable;
}
interface Handler extends Filter {
default void handle(@Nonnull Context ctx, @Nonnull Pipeline chain) throws Throwable {
Object result = handle(ctx);
if (!ctx.committed()) {
if (result != ctx) {
ctx.send(result.toString());
} else {
chain.next(ctx);
}
}
}
Object handle(@Nonnull Context ctx) throws Throwable;
}
interface ErrHandler {
void handle(@Nonnull Context ctx, @Nonnull Err problem);
default @Nonnull ErrHandler then(@Nonnull Route.ErrHandler next) {
return (ctx, problem) -> {
handle(ctx, problem);
if (!ctx.committed()) {
next.handle(ctx, problem);
}
};
}
}
@Nonnull String method();
@Nonnull String pattern();
@Nonnull Filter handler();
default void handle(@Nonnull Context ctx, @Nonnull Pipeline chain) throws Throwable {
handler().handle(ctx, chain);
}
static @Nonnull String normalize(@Nonnull String pattern) {
if (pattern.equals("*")) {
return "/**";
}
StringBuilder buff = new StringBuilder();
char prev = '/';
buff.append(prev);
for (int i = 0; i < pattern.length(); i++) {
char ch = pattern.charAt(i);
if (ch != '/' || prev != '/') {
buff.append(ch);
}
prev = ch;
}
int last = buff.length() - 1;
if (last > 1 && buff.charAt(last) == '/') {
buff.setLength(last);
}
if (buff.length() == pattern.length()) {
return pattern;
}
return buff.toString();
}
}