Skip to content

[Sigma-Generator] Filter Chain #526

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

Merged
merged 1 commit into from
Dec 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,23 @@ class ControllerMethodWriter {
private final Append writer;
private final WebMethod webMethod;
private final boolean instrumentContext;
private final boolean customMethod;
private final boolean isFilter;

ControllerMethodWriter(MethodReader method, Append writer) {
this.method = method;
this.writer = writer;
final var webM = method.webMethod();
this.webMethod = webM == CoreWebMethod.FILTER ? SigmaWebMethod.BEFORE : webM;
this.webMethod = method.webMethod();
this.instrumentContext = method.instrumentContext();
customMethod = !(webMethod instanceof CoreWebMethod);
this.isFilter = webMethod == CoreWebMethod.FILTER;
if (isFilter) {
validateFilter();
}
}

private void validateFilter() {
if (method.params().stream().map(MethodParam::shortType).noneMatch("HttpFilter.FilterChain"::equals)) {
logError(method.element(), "Filters must contain a FilterChain parameter");
}
}

void write() {
Expand All @@ -33,7 +41,7 @@ void write() {

final var params = writeParams(segments);
writer.append(" ");
if (!method.isVoid() && !customMethod) {
if (!method.isVoid() && !isFilter) {
writer.append("var result = ");
}

Expand All @@ -51,6 +59,8 @@ void write() {
final var param = params.get(i);
if (isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
writer.append("ex");
} else if ("HttpFilter.FilterChain".equals(param.shortType())) {
writer.append("chain");
} else {
param.buildParamName(writer);
}
Expand All @@ -61,7 +71,7 @@ void write() {
}

writer.append(");").eol();
if (!method.isVoid() && !customMethod) {
if (!method.isVoid() && !isFilter) {
writeContextReturn();
writer.eol();
}
Expand All @@ -71,15 +81,18 @@ void write() {
}

private void writeMethod(final String fullPath) {
if (method.isErrorMethod()) {

if (isFilter) {
writer.append(" router.filter((ctx, chain) -> {");
} else if (method.isErrorMethod()) {
writer
.append(" router.exception(%s.class, (ex, ctx) -> {", method.exceptionShortName())
.append(" router.exception(%s.class, (ctx, ex) -> {", method.exceptionShortName())
.eol();
} else {
var methodName = webMethod.name().toLowerCase().replace("_m", "M");
writer.append(" router.%s(\"%s\", ctx -> {", methodName, fullPath).eol();
}
if (!customMethod) {
if (!isFilter) {
int statusCode = method.statusCode();
if (statusCode > 0) {
writer.append(" ctx.status(%d);", statusCode).eol();
Expand All @@ -95,7 +108,7 @@ private List<MethodParam> writeParams(final PathSegments segments) {

final var params = method.params();
for (final MethodParam param : params) {
if (!isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")) {
if (!isExceptionOrFilterChain(param)) {
param.writeCtxGet(writer, segments);
}
}
Expand Down Expand Up @@ -125,4 +138,9 @@ private void writeContextReturn() {
writer.append(" ctx.contentType(\"%s\").result(%s);", produces);
}
}

private static boolean isExceptionOrFilterChain(MethodParam param) {
return isAssignable2Interface(param.utype().mainType(), "java.lang.Exception")
|| "HttpFilter.FilterChain".equals(param.shortType());
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test-sigma/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-sigma</artifactId>
<version>0.3</version>
<version>0.4</version>
</dependency>

<dependency>
Expand All @@ -42,7 +42,7 @@
<artifactId>avaje-http-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.avaje.http.api.Post;
import io.avaje.http.api.QueryParam;
import io.avaje.sigma.HttpContext;
import io.avaje.sigma.HttpFilter.FilterChain;

@Path("test/")
@Controller
Expand Down Expand Up @@ -84,7 +85,7 @@ void before(String s, ServerType type, HttpContext ctx) {
}

@Filter
void filter(HttpContext ctx) {
void filter(HttpContext ctx, FilterChain chain) {
}

@Form
Expand Down
Loading