Skip to content

Commit 0f66743

Browse files
committed
Simplify before filter
1 parent 2f3f519 commit 0f66743

File tree

2 files changed

+47
-18
lines changed

2 files changed

+47
-18
lines changed

jooby/src/main/java/io/jooby/Route.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,40 @@ public interface Decorator {
8686
* @author edgar
8787
* @since 2.0.0
8888
*/
89-
public interface Before extends Decorator {
90-
@Nonnull @Override default Handler apply(@Nonnull Handler next) {
89+
public interface Before {
90+
/**
91+
* Execute application code before next handler.
92+
*
93+
* @param ctx Web context.
94+
* @throws Exception If something goes wrong.
95+
*/
96+
void apply(@Nonnull Context ctx) throws Exception;
97+
98+
/**
99+
* Chain this filter with next one and produces a new before filter.
100+
*
101+
* @param next Next decorator.
102+
* @return A new decorator.
103+
*/
104+
@Nonnull default Before then(@Nonnull Before next) {
91105
return ctx -> {
92-
before(ctx);
93-
return next.apply(ctx);
106+
apply(ctx);
107+
next.apply(ctx);
94108
};
95109
}
96110

97111
/**
98-
* Execute application code before next handler.
112+
* Chain this decorator with a handler and produces a new handler.
99113
*
100-
* @param ctx Web context.
101-
* @throws Exception If something goes wrong.
114+
* @param next Next handler.
115+
* @return A new handler.
102116
*/
103-
void before(@Nonnull Context ctx) throws Exception;
117+
@Nonnull default Handler then(@Nonnull Handler next) {
118+
return ctx -> {
119+
apply(ctx);
120+
return next.apply(ctx);
121+
};
122+
}
104123
}
105124

106125
/**
@@ -205,7 +224,7 @@ public interface Handler extends Serializable {
205224

206225
private List<String> pathKeys;
207226

208-
private Decorator before;
227+
private Before before;
209228

210229
private Handler handler;
211230

@@ -241,7 +260,7 @@ public Route(@Nonnull String method,
241260
@Nonnull List<String> pathKeys,
242261
@Nonnull Type returnType,
243262
@Nonnull Handler handler,
244-
@Nullable Decorator before,
263+
@Nullable Before before,
245264
@Nullable After after,
246265
@Nonnull Renderer renderer,
247266
@Nonnull Map<String, Parser> parsers) {
@@ -281,7 +300,7 @@ public Route(@Nonnull String method,
281300
@Nonnull String pattern,
282301
@Nonnull Type returnType,
283302
@Nonnull Handler handler,
284-
@Nullable Decorator before,
303+
@Nullable Before before,
285304
@Nullable After after,
286305
@Nonnull Renderer renderer,
287306
@Nonnull Map<String, Parser> parsers) {
@@ -350,7 +369,7 @@ public Route(@Nonnull String method,
350369
*
351370
* @return Before pipeline or <code>null</code>.
352371
*/
353-
public @Nullable Decorator getBefore() {
372+
public @Nullable Before getBefore() {
354373
return before;
355374
}
356375

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ private static class Stack {
6363
private String pattern;
6464
private Executor executor;
6565
private List<Route.Decorator> filters = new ArrayList<>();
66+
private List<Route.Before> beforeList = new ArrayList<>();
6667
private List<Route.After> afters = new ArrayList<>();
6768

6869
public Stack(String pattern) {
@@ -77,6 +78,10 @@ public void then(Route.After after) {
7778
afters.add(after);
7879
}
7980

81+
public void then(Route.Before before) {
82+
beforeList.add(before);
83+
}
84+
8085
public Stream<Route.Decorator> toFilter() {
8186
return filters.stream();
8287
}
@@ -85,9 +90,14 @@ public Stream<Route.After> toAfter() {
8590
return afters.stream();
8691
}
8792

93+
public Stream<Route.Before> toBefore() {
94+
return beforeList.stream();
95+
}
96+
8897
public void clear() {
8998
this.filters.clear();
9099
this.afters.clear();
100+
this.beforeList.clear();
91101
executor = null;
92102
}
93103

@@ -295,7 +305,8 @@ public Router renderer(@Nonnull MediaType contentType, @Nonnull Renderer rendere
295305
}
296306

297307
@Nonnull @Override public Router before(@Nonnull Route.Before before) {
298-
return decorator(before);
308+
stack.peekLast().then(before);
309+
return this;
299310
}
300311

301312
@Nonnull @Override public Router error(@Nonnull ErrorHandler handler) {
@@ -351,10 +362,9 @@ private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
351362
.collect(Collectors.toList());
352363

353364
/** Before: */
354-
Route.Decorator before = null;
355-
for (Route.Decorator filter : filters) {
356-
before = before == null ? filter : before.then(filter);
357-
}
365+
Route.Before before = stack.stream()
366+
.flatMap(Stack::toBefore)
367+
.reduce(null, (it, next) -> it == null ? next : it.then(next));
358368

359369
/** Pipeline: */
360370
// Route.Handler pipeline = before == null ? handler : before.then(handler);
@@ -405,7 +415,7 @@ private Route defineRoute(@Nonnull String method, @Nonnull String pattern,
405415
route.setReturnType(analyzer.returnType(route.getHandle()));
406416
}
407417
/** Pipeline: */
408-
Route.Decorator before = route.getBefore();
418+
Route.Before before = route.getBefore();
409419
if (route.getProduces().size() > 0) {
410420
before = before == null ? ACCEPT : ACCEPT.then(before);
411421
}

0 commit comments

Comments
 (0)