Skip to content

Commit 5cf708a

Browse files
authored
Merge pull request #64 from SentryMan/filter
Add before/after convenience methods
2 parents 19d7cb4 + 30195d8 commit 5cf708a

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Deque;
88
import java.util.List;
99
import java.util.Set;
10+
import java.util.function.Consumer;
1011

1112
import io.avaje.jex.security.Role;
1213

@@ -179,7 +180,6 @@ public Routing filter(HttpFilter handler) {
179180
return this;
180181
}
181182

182-
183183
private static class Entry implements Routing.Entry {
184184

185185
private final Type type;

avaje-jex/src/main/java/io/avaje/jex/FilterChain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public interface FilterChain {
99

1010
/**
11-
* Calls the next filter in the chain, or else the users exchange handler, if this is the final
11+
* Calls the next filter in the chain, or else the user's exchange handler, if this is the final
1212
* filter in the chain. The {@link HttpFilter} may decide to terminate the chain, by not
1313
* calling this method. In this case, the filter <b>must</b> send the response to the request,
1414
* because the application's {@linkplain HttpExchange exchange} handler will not be invoked.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package io.avaje.jex;
22

3+
/**
4+
* A handler which is invoked to process HTTP exchanges. Each HTTP exchange is handled by one of
5+
* these handlers.
6+
*/
37
@FunctionalInterface
48
public interface Handler {
59

10+
/**
11+
* Handle the given request and generate an appropriate response. See {@link Context} for a
12+
* description of the steps involved in handling an exchange.
13+
*
14+
* @param ctx the request context containing the request from the client and used to send the
15+
* response
16+
*/
617
void handle(Context ctx);
718
}

avaje-jex/src/main/java/io/avaje/jex/Routing.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Collection;
44
import java.util.List;
55
import java.util.Set;
6+
import java.util.function.Consumer;
67

78
import io.avaje.jex.security.Role;
89

@@ -128,6 +129,26 @@ public interface Routing {
128129
*/
129130
Routing filter(HttpFilter handler);
130131

132+
/** Add a preprocessing filter for all requests. */
133+
default Routing before(Consumer<Context> handler) {
134+
135+
return filter(
136+
(ctx, chain) -> {
137+
handler.accept(ctx);
138+
chain.proceed();
139+
});
140+
}
141+
142+
/** Add a post-processing filter for all requests. */
143+
default Routing after(Consumer<Context> handler) {
144+
145+
return filter(
146+
(ctx, chain) -> {
147+
chain.proceed();
148+
handler.accept(ctx);
149+
});
150+
}
151+
131152
/**
132153
* Return all the registered handlers.
133154
*/

avaje-jex/src/main/java/io/avaje/jex/jdk/RoutingFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
4646
handleException(ctx, e);
4747
} finally {
4848
routes.dec();
49+
exchange.close();
4950
}
5051
} else {
5152
route.inc();
@@ -63,6 +64,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
6364
}
6465
} finally {
6566
route.dec();
67+
exchange.close();
6668
}
6769
}
6870
}

avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,15 @@ static TestPair init() {
2626
.get("/one", ctx -> ctx.text("one"))
2727
.get("/two", ctx -> ctx.text("two"))
2828
.get("/two/{id}", ctx -> ctx.text("two-id"))
29-
.filter(
30-
(ctx, chain) -> {
31-
ctx.header("before-all", "set");
32-
chain.proceed();
33-
})
29+
.before(ctx -> ctx.header("before-all", "set"))
3430
.filter(
3531
(ctx, chain) -> {
3632
if (ctx.url().contains("/two/")) {
3733
ctx.header("before-two", "set");
3834
}
3935
chain.proceed();
4036
})
41-
.filter(
42-
(ctx, chain) -> {
43-
chain.proceed();
44-
afterAll.set("set");
45-
})
37+
.after(ctx -> afterAll.set("set"))
4638
.filter(
4739
(ctx, chain) -> {
4840
chain.proceed();

0 commit comments

Comments
 (0)