@@ -14,15 +14,23 @@ class ControllerMethodWriter {
14
14
private final Append writer ;
15
15
private final WebMethod webMethod ;
16
16
private final boolean instrumentContext ;
17
- private final boolean customMethod ;
17
+ private final boolean isFilter ;
18
18
19
19
ControllerMethodWriter (MethodReader method , Append writer ) {
20
20
this .method = method ;
21
21
this .writer = writer ;
22
- final var webM = method .webMethod ();
23
- this .webMethod = webM == CoreWebMethod .FILTER ? SigmaWebMethod .BEFORE : webM ;
22
+ this .webMethod = method .webMethod ();
24
23
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
+ }
26
34
}
27
35
28
36
void write () {
@@ -33,7 +41,7 @@ void write() {
33
41
34
42
final var params = writeParams (segments );
35
43
writer .append (" " );
36
- if (!method .isVoid () && !customMethod ) {
44
+ if (!method .isVoid () && !isFilter ) {
37
45
writer .append ("var result = " );
38
46
}
39
47
@@ -51,6 +59,8 @@ void write() {
51
59
final var param = params .get (i );
52
60
if (isAssignable2Interface (param .utype ().mainType (), "java.lang.Exception" )) {
53
61
writer .append ("ex" );
62
+ } else if ("HttpFilter.FilterChain" .equals (param .shortType ())) {
63
+ writer .append ("chain" );
54
64
} else {
55
65
param .buildParamName (writer );
56
66
}
@@ -61,7 +71,7 @@ void write() {
61
71
}
62
72
63
73
writer .append (");" ).eol ();
64
- if (!method .isVoid () && !customMethod ) {
74
+ if (!method .isVoid () && !isFilter ) {
65
75
writeContextReturn ();
66
76
writer .eol ();
67
77
}
@@ -71,15 +81,18 @@ void write() {
71
81
}
72
82
73
83
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 ()) {
75
88
writer
76
- .append (" router.exception(%s.class, (ex, ctx ) -> {" , method .exceptionShortName ())
89
+ .append (" router.exception(%s.class, (ctx, ex ) -> {" , method .exceptionShortName ())
77
90
.eol ();
78
91
} else {
79
92
var methodName = webMethod .name ().toLowerCase ().replace ("_m" , "M" );
80
93
writer .append (" router.%s(\" %s\" , ctx -> {" , methodName , fullPath ).eol ();
81
94
}
82
- if (!customMethod ) {
95
+ if (!isFilter ) {
83
96
int statusCode = method .statusCode ();
84
97
if (statusCode > 0 ) {
85
98
writer .append (" ctx.status(%d);" , statusCode ).eol ();
@@ -95,7 +108,7 @@ private List<MethodParam> writeParams(final PathSegments segments) {
95
108
96
109
final var params = method .params ();
97
110
for (final MethodParam param : params ) {
98
- if (!isAssignable2Interface (param . utype (). mainType (), "java.lang.Exception" )) {
111
+ if (!isExceptionOrFilterChain (param )) {
99
112
param .writeCtxGet (writer , segments );
100
113
}
101
114
}
@@ -125,4 +138,9 @@ private void writeContextReturn() {
125
138
writer .append (" ctx.contentType(\" %s\" ).result(%s);" , produces );
126
139
}
127
140
}
141
+
142
+ private static boolean isExceptionOrFilterChain (MethodParam param ) {
143
+ return isAssignable2Interface (param .utype ().mainType (), "java.lang.Exception" )
144
+ || "HttpFilter.FilterChain" .equals (param .shortType ());
145
+ }
128
146
}
0 commit comments