Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before/after convenience methods #64

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/DefaultRouting.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Deque;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import io.avaje.jex.security.Role;

Expand Down Expand Up @@ -179,7 +180,6 @@ public Routing filter(HttpFilter handler) {
return this;
}


private static class Entry implements Routing.Entry {

private final Type type;
Expand Down
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/FilterChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface FilterChain {

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

/**
* A handler which is invoked to process HTTP exchanges. Each HTTP exchange is handled by one of
* these handlers.
*/
@FunctionalInterface
public interface Handler {

/**
* Handle the given request and generate an appropriate response. See {@link Context} for a
* description of the steps involved in handling an exchange.
*
* @param ctx the request context containing the request from the client and used to send the
* response
*/
void handle(Context ctx);
}
21 changes: 21 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/Routing.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import io.avaje.jex.security.Role;

Expand Down Expand Up @@ -128,6 +129,26 @@ public interface Routing {
*/
Routing filter(HttpFilter handler);

/** Add a preprocessing filter for all requests. */
default Routing before(Consumer<Context> handler) {

return filter(
(ctx, chain) -> {
handler.accept(ctx);
chain.proceed();
});
}

/** Add a post-processing filter for all requests. */
default Routing after(Consumer<Context> handler) {

return filter(
(ctx, chain) -> {
chain.proceed();
handler.accept(ctx);
});
}

/**
* Return all the registered handlers.
*/
Expand Down
2 changes: 2 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/RoutingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
handleException(ctx, e);
} finally {
routes.dec();
exchange.close();
}
} else {
route.inc();
Expand All @@ -63,6 +64,7 @@ public void doFilter(HttpExchange exchange, Filter.Chain chain) {
}
} finally {
route.dec();
exchange.close();
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions avaje-jex/src/test/java/io/avaje/jex/jdk/FilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,15 @@ static TestPair init() {
.get("/one", ctx -> ctx.text("one"))
.get("/two", ctx -> ctx.text("two"))
.get("/two/{id}", ctx -> ctx.text("two-id"))
.filter(
(ctx, chain) -> {
ctx.header("before-all", "set");
chain.proceed();
})
.before(ctx -> ctx.header("before-all", "set"))
.filter(
(ctx, chain) -> {
if (ctx.url().contains("/two/")) {
ctx.header("before-two", "set");
}
chain.proceed();
})
.filter(
(ctx, chain) -> {
chain.proceed();
afterAll.set("set");
})
.after(ctx -> afterAll.set("set"))
.filter(
(ctx, chain) -> {
chain.proceed();
Expand Down