Skip to content

Commit 5d6aca1

Browse files
authored
filter chain (#526)
1 parent 85a49b6 commit 5d6aca1

File tree

4 files changed

+32
-34
lines changed

4 files changed

+32
-34
lines changed

http-generator-sigma/src/main/java/io/avaje/http/generator/sigma/ControllerMethodWriter.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ class ControllerMethodWriter {
1414
private final Append writer;
1515
private final WebMethod webMethod;
1616
private final boolean instrumentContext;
17-
private final boolean customMethod;
17+
private final boolean isFilter;
1818

1919
ControllerMethodWriter(MethodReader method, Append writer) {
2020
this.method = method;
2121
this.writer = writer;
22-
final var webM = method.webMethod();
23-
this.webMethod = webM == CoreWebMethod.FILTER ? SigmaWebMethod.BEFORE : webM;
22+
this.webMethod = method.webMethod();
2423
this.instrumentContext = method.instrumentContext();
25-
customMethod = !(webMethod instanceof CoreWebMethod);
24+
this.isFilter = webMethod == CoreWebMethod.FILTER;
25+
if (isFilter) {
26+
validateFilter();
27+
}
28+
}
29+
30+
private void validateFilter() {
31+
if (method.params().stream().map(MethodParam::shortType).noneMatch("HttpFilter.FilterChain"::equals)) {
32+
logError(method.element(), "Filters must contain a FilterChain parameter");
33+
}
2634
}
2735

2836
void write() {
@@ -33,7 +41,7 @@ void write() {
3341

3442
final var params = writeParams(segments);
3543
writer.append(" ");
36-
if (!method.isVoid() && !customMethod) {
44+
if (!method.isVoid() && !isFilter) {
3745
writer.append("var result = ");
3846
}
3947

@@ -51,6 +59,8 @@ void write() {
5159
final var param = params.get(i);
5260
if (isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
5361
writer.append("ex");
62+
} else if ("HttpFilter.FilterChain".equals(param.shortType())) {
63+
writer.append("chain");
5464
} else {
5565
param.buildParamName(writer);
5666
}
@@ -61,7 +71,7 @@ void write() {
6171
}
6272

6373
writer.append(");").eol();
64-
if (!method.isVoid() && !customMethod) {
74+
if (!method.isVoid() && !isFilter) {
6575
writeContextReturn();
6676
writer.eol();
6777
}
@@ -71,15 +81,18 @@ void write() {
7181
}
7282

7383
private void writeMethod(final String fullPath) {
74-
if (method.isErrorMethod()) {
84+
85+
if (isFilter) {
86+
writer.append(" router.filter((ctx, chain) -> {");
87+
} else if (method.isErrorMethod()) {
7588
writer
76-
.append(" router.exception(%s.class, (ex, ctx) -> {", method.exceptionShortName())
89+
.append(" router.exception(%s.class, (ctx, ex) -> {", method.exceptionShortName())
7790
.eol();
7891
} else {
7992
var methodName = webMethod.name().toLowerCase().replace("_m", "M");
8093
writer.append(" router.%s(\"%s\", ctx -> {", methodName, fullPath).eol();
8194
}
82-
if (!customMethod) {
95+
if (!isFilter) {
8396
int statusCode = method.statusCode();
8497
if (statusCode > 0) {
8598
writer.append(" ctx.status(%d);", statusCode).eol();
@@ -95,7 +108,7 @@ private List<MethodParam> writeParams(final PathSegments segments) {
95108

96109
final var params = method.params();
97110
for (final MethodParam param : params) {
98-
if (!isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
111+
if (!isExceptionOrFilterChain(param)) {
99112
param.writeCtxGet(writer, segments);
100113
}
101114
}
@@ -125,4 +138,9 @@ private void writeContextReturn() {
125138
writer.append(" ctx.contentType(\"%s\").result(%s);", produces);
126139
}
127140
}
141+
142+
private static boolean isExceptionOrFilterChain(MethodParam param) {
143+
return isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")
144+
|| "HttpFilter.FilterChain".equals(param.shortType());
145+
}
128146
}

http-generator-sigma/src/main/java/io/avaje/http/generator/sigma/SigmaWebMethod.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/test-sigma/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>io.avaje</groupId>
3030
<artifactId>avaje-sigma</artifactId>
31-
<version>0.3</version>
31+
<version>0.4</version>
3232
</dependency>
3333

3434
<dependency>
@@ -42,7 +42,7 @@
4242
<artifactId>avaje-http-api</artifactId>
4343
<version>${project.version}</version>
4444
</dependency>
45-
45+
4646
<dependency>
4747
<groupId>io.swagger.core.v3</groupId>
4848
<artifactId>swagger-annotations</artifactId>

tests/test-sigma/src/main/java/org/example/myapp/web/test/TestController2.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.avaje.http.api.Post;
1818
import io.avaje.http.api.QueryParam;
1919
import io.avaje.sigma.HttpContext;
20+
import io.avaje.sigma.HttpFilter.FilterChain;
2021

2122
@Path("test/")
2223
@Controller
@@ -84,7 +85,7 @@ void before(String s, ServerType type, HttpContext ctx) {
8485
}
8586

8687
@Filter
87-
void filter(HttpContext ctx) {
88+
void filter(HttpContext ctx, FilterChain chain) {
8889
}
8990

9091
@Form

0 commit comments

Comments
 (0)