Skip to content

Commit f02971a

Browse files
authored
Add Javalin @Before/@After and Helidon @Filter (#255)
* start * Javalin Before/After * Helidon Filter * Update pom.xml * fuse AbstractPrisms * Move filter to http-api
1 parent 0fc3f36 commit f02971a

File tree

31 files changed

+384
-160
lines changed

31 files changed

+384
-160
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.avaje.http.api;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.RetentionPolicy.SOURCE;
5+
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Marks a method as a Filter
11+
*/
12+
@Target(METHOD)
13+
@Retention(SOURCE)
14+
public @interface Filter {}

http-api/src/main/java/io/avaje/http/api/HttpMethod.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package io.avaje.http.api;
22

3-
import java.lang.annotation.ElementType;
3+
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
4+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
5+
46
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
67
import java.lang.annotation.Target;
78

8-
/**
9-
* Base for Http verb based annotations.
10-
*/
11-
@Target(value= ElementType.ANNOTATION_TYPE)
12-
@Retention(value= RetentionPolicy.RUNTIME)
9+
/** Base for Http verb based annotations. */
10+
@Target(ANNOTATION_TYPE)
11+
@Retention(RUNTIME)
1312
public @interface HttpMethod {
1413

1514
String value();

http-generator-client/pom.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@
1111

1212
<properties>
1313
<java.version>11</java.version>
14-
<avaje.prisms.version>1.9</avaje.prisms.version>
1514
</properties>
1615
<dependencies>
17-
<dependency>
18-
<groupId>io.avaje</groupId>
19-
<artifactId>avaje-prisms</artifactId>
20-
<version>${avaje.prisms.version}</version>
21-
<optional>true</optional>
22-
<scope>provided</scope>
23-
</dependency>
2416
<dependency>
2517
<groupId>io.avaje</groupId>
2618
<artifactId>avaje-http-generator-core</artifactId>
@@ -41,6 +33,14 @@
4133
<scope>provided</scope>
4234
</dependency>
4335

36+
<dependency>
37+
<groupId>io.avaje</groupId>
38+
<artifactId>avaje-prisms</artifactId>
39+
<version>${avaje.prisms.version}</version>
40+
<scope>provided</scope>
41+
<optional>true</optional>
42+
</dependency>
43+
4444
</dependencies>
4545

4646
<build>

http-generator-core/pom.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99

1010
<artifactId>avaje-http-generator-core</artifactId>
1111

12-
<properties>
13-
<avaje.prisms.version>1.10</avaje.prisms.version>
14-
</properties>
1512
<dependencies>
1613
<dependency>
1714
<groupId>io.avaje</groupId>
1815
<artifactId>avaje-prisms</artifactId>
1916
<version>${avaje.prisms.version}</version>
20-
<optional>true</optional>
2117
<scope>provided</scope>
18+
<optional>true</optional>
2219
</dependency>
2320

2421
<dependency>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.avaje.http.generator.core;
2+
3+
public enum CoreWebMethod implements WebMethod {
4+
GET(200),
5+
POST(201),
6+
PUT(200, 204),
7+
PATCH(200, 204),
8+
DELETE(200, 204),
9+
ERROR(500),
10+
FILTER(0),
11+
OTHER(0, 0);
12+
13+
private int statusCode;
14+
private int voidStatusCode;
15+
16+
CoreWebMethod(int statusCode, int voidStatusCode) {
17+
this.statusCode = statusCode;
18+
this.voidStatusCode = voidStatusCode;
19+
}
20+
21+
CoreWebMethod(int statusCode) {
22+
this.statusCode = statusCode;
23+
this.voidStatusCode = statusCode;
24+
}
25+
26+
@Override
27+
public int statusCode(boolean isVoid) {
28+
return isVoid ? voidStatusCode : statusCode;
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.avaje.http.generator.core;
2+
3+
public interface CustomWebMethod {
4+
5+
WebMethod webMethod();
6+
7+
default String value() {
8+
return "";
9+
}
10+
}

http-generator-core/src/main/java/io/avaje/http/generator/core/JsonBUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.avaje.http.generator.core;
22

3-
import static java.util.function.Predicate.not;
4-
53
import java.util.LinkedHashMap;
64
import java.util.Map;
75
import java.util.function.Consumer;
@@ -17,6 +15,8 @@ public static Map<String, UType> jsonTypes(ControllerReader reader) {
1715

1816
reader.methods().stream()
1917
.filter(MethodReader::isWebMethod)
18+
.filter(m -> m.webMethod() instanceof CoreWebMethod)
19+
.filter(m -> m.webMethod() != CoreWebMethod.FILTER)
2020
.filter(m -> !"byte[]".equals(m.returnType().toString()))
2121
.filter(m -> m.produces() == null || m.produces().toLowerCase().contains("json"))
2222
.forEach(

http-generator-core/src/main/java/io/avaje/http/generator/core/MethodReader.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,7 @@ private boolean initResolver() {
101101
}
102102

103103
private boolean hasInstrument(Element e) {
104-
105-
if (InstrumentServerContextPrism.getOptionalOn(e).isPresent()) {
106-
return true;
107-
}
108-
109-
for (final var a : e.getAnnotationMirrors()) {
110-
if (HttpMethodPrism.isPresent(a.getAnnotationType().asElement())) {
111-
return a.getElementValues().values().stream().anyMatch(v -> v.getValue().equals(true));
112-
}
113-
}
114-
return false;
104+
return InstrumentServerContextPrism.getOptionalOn(e).isPresent();
115105
}
116106

117107
private Javadoc buildJavadoc(ExecutableElement element) {
@@ -146,21 +136,29 @@ private void initWebMethodViaAnnotation() {
146136
}
147137

148138
findAnnotation(GetPrism::getOptionalOn)
149-
.ifPresent(get -> initSetWebMethod(WebMethod.GET, get.value()));
139+
.ifPresent(get -> initSetWebMethod(CoreWebMethod.GET, get.value()));
150140

151141
findAnnotation(PutPrism::getOptionalOn)
152-
.ifPresent(put -> initSetWebMethod(WebMethod.PUT, put.value()));
142+
.ifPresent(put -> initSetWebMethod(CoreWebMethod.PUT, put.value()));
153143

154144
findAnnotation(PostPrism::getOptionalOn)
155-
.ifPresent(post -> initSetWebMethod(WebMethod.POST, post.value()));
145+
.ifPresent(post -> initSetWebMethod(CoreWebMethod.POST, post.value()));
156146

157147
findAnnotation(PatchPrism::getOptionalOn)
158-
.ifPresent(patch -> initSetWebMethod(WebMethod.PATCH, patch.value()));
148+
.ifPresent(patch -> initSetWebMethod(CoreWebMethod.PATCH, patch.value()));
149+
159150
findAnnotation(DeletePrism::getOptionalOn)
160-
.ifPresent(delete -> initSetWebMethod(WebMethod.DELETE, delete.value()));
151+
.ifPresent(delete -> initSetWebMethod(CoreWebMethod.DELETE, delete.value()));
161152

162153
findAnnotation(ExceptionHandlerPrism::getOptionalOn)
163-
.ifPresent(error -> initSetWebMethod(WebMethod.ERROR, error.value()));
154+
.ifPresent(error -> initSetWebMethod(CoreWebMethod.ERROR, error.value()));
155+
156+
findAnnotation(FilterPrism::getOptionalOn)
157+
.ifPresent(filter -> initSetWebMethod(CoreWebMethod.FILTER, ""));
158+
159+
platform()
160+
.customHandlers()
161+
.forEach(f -> findAnnotation(f).ifPresent(m -> initSetWebMethod(m.webMethod(), m.value())));
164162
}
165163

166164
private void initSetWebMethod(WebMethod webMethod, String value) {
@@ -317,7 +315,9 @@ public void buildApiDoc() {
317315
/** Build the OpenAPI documentation for the method / operation. */
318316
public void buildApiDocumentation() {
319317

320-
if (!isErrorMethod()) {
318+
if (!isErrorMethod()
319+
&& webMethod instanceof CoreWebMethod
320+
&& webMethod != CoreWebMethod.FILTER) {
321321
new MethodDocBuilder(this, doc()).build();
322322
}
323323
}
@@ -333,7 +333,7 @@ public boolean isWebMethod() {
333333
}
334334

335335
public boolean isErrorMethod() {
336-
return webMethod == WebMethod.ERROR;
336+
return webMethod == CoreWebMethod.ERROR;
337337
}
338338

339339
public WebMethod webMethod() {
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,36 @@
11
package io.avaje.http.generator.core;
22

33
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.function.Function;
46

5-
/**
6-
* Adapter to specific platforms like Javalin and Helidon.
7-
*/
7+
import javax.lang.model.element.Element;
8+
9+
/** Adapter to specific platforms like Javalin and Helidon. */
810
public interface PlatformAdapter {
911

1012
/**
11-
* Return true if this type is the platform specific request, response or context type.
12-
* For example Javalin Context, Helidon ServerRequest or ServerResponse type).
13+
* Return true if this type is the platform specific request, response or context type. For
14+
* example Javalin Context, Helidon ServerRequest or ServerResponse type).
1315
*/
1416
boolean isContextType(String rawType);
1517

16-
/**
17-
* Return the platform specific parameter (request, response or context).
18-
*/
18+
/** Return the platform specific parameter (request, response or context). */
1919
String platformVariable(String rawType);
2020

21-
/**
22-
* Return platform specific code to return the body content.
23-
*/
21+
/** Return platform specific code to return the body content. */
2422
String bodyAsClass(UType type);
2523

26-
/**
27-
* Return true if body is passed as a method parameter.
28-
*/
24+
/** Return true if body is passed as a method parameter. */
2925
boolean isBodyMethodParam();
3026

31-
/**
32-
* Return whitespace indent for setting parameter values.
33-
*/
27+
/** Return whitespace indent for setting parameter values. */
3428
String indent();
3529

36-
/**
37-
* Handle controller level roles.
38-
*/
30+
/** Handle controller level roles. */
3931
void controllerRoles(List<String> roles, ControllerReader controller);
4032

41-
/**
42-
* Handle method level roles.
43-
*/
33+
/** Handle method level roles. */
4434
void methodRoles(List<String> roles, ControllerReader controller);
4535

4636
void writeReadParameter(Append writer, ParamType paramType, String paramName);
@@ -62,4 +52,8 @@ default void writeReadCollectionParameter(
6252
Append writer, ParamType paramType, String paramName, List<String> paramDefault) {
6353
throw new UnsupportedOperationException("Unsupported MultiValue Parameter");
6454
}
55+
56+
default List<Function<Element, Optional<CustomWebMethod>>> customHandlers() {
57+
return List.of();
58+
}
6559
}
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
package io.avaje.http.generator.core;
22

3-
public enum WebMethod {
4-
GET(200),
5-
POST(201),
6-
PUT(200, 204),
7-
PATCH(200, 204),
8-
DELETE(200, 204),
9-
ERROR(500);
3+
public interface WebMethod {
104

11-
private int statusCode;
12-
private int voidStatusCode;
5+
int statusCode(boolean isVoid);
136

14-
WebMethod(int statusCode, int voidStatusCode) {
15-
this.statusCode = statusCode;
16-
this.voidStatusCode = voidStatusCode;
17-
}
18-
19-
WebMethod(int statusCode) {
20-
this.statusCode = statusCode;
21-
this.voidStatusCode = statusCode;
22-
}
23-
24-
int statusCode(boolean isVoid) {
25-
return isVoid ? voidStatusCode : statusCode;
26-
}
7+
String name();
278
}

http-generator-core/src/main/java/io/avaje/http/generator/core/openapi/MethodDocBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Optional;
44

55
import io.avaje.http.generator.core.ConsumesPrism;
6+
import io.avaje.http.generator.core.CoreWebMethod;
67
import io.avaje.http.generator.core.HiddenPrism;
78
import io.avaje.http.generator.core.MethodParam;
89
import io.avaje.http.generator.core.MethodReader;
@@ -49,7 +50,7 @@ public void build() {
4950
}
5051

5152
PathItem pathItem = ctx.pathItem(methodReader.fullPath());
52-
switch (methodReader.webMethod()) {
53+
switch ((CoreWebMethod) methodReader.webMethod()) {
5354
case GET:
5455
pathItem.setGet(operation);
5556
break;

http-generator-core/src/main/java/io/avaje/http/generator/core/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
@GeneratePrism(value = io.avaje.http.api.FormParam.class, publicAccess = true)
1313
@GeneratePrism(value = io.avaje.http.api.Get.class, publicAccess = true)
1414
@GeneratePrism(value = io.avaje.http.api.Header.class, publicAccess = true)
15-
@GeneratePrism(value = io.avaje.http.api.HttpMethod.class, publicAccess = true)
1615
@GeneratePrism(value = io.avaje.http.api.MatrixParam.class, publicAccess = true)
1716
@GeneratePrism(value = io.avaje.http.api.Patch.class, publicAccess = true)
1817
@GeneratePrism(value = io.avaje.http.api.Path.class, publicAccess = true)
1918
@GeneratePrism(value = io.avaje.http.api.Post.class, publicAccess = true)
2019
@GeneratePrism(value = io.avaje.http.api.Produces.class, publicAccess = true)
2120
@GeneratePrism(value = io.avaje.http.api.Consumes.class, publicAccess = true)
2221
@GeneratePrism(value = io.avaje.http.api.Put.class, publicAccess = true)
22+
@GeneratePrism(value = io.avaje.http.api.Filter.class)
2323
@GeneratePrism(value = io.avaje.http.api.InstrumentServerContext.class)
2424
@GeneratePrism(value = io.avaje.http.api.ExceptionHandler.class)
2525
@GeneratePrism(value = io.swagger.v3.oas.annotations.OpenAPIDefinition.class, publicAccess = true)

http-generator-helidon/pom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<artifactId>avaje-http-generator-core</artifactId>
2424
<version>${project.version}</version>
2525
</dependency>
26-
2726
</dependencies>
2827

2928
<build>
@@ -33,9 +32,8 @@
3332
<artifactId>maven-compiler-plugin</artifactId>
3433
<version>3.10.1</version>
3534
<configuration>
36-
<!-- Turn off annotation processing for building -->
35+
<release>20</release>
3736
<compilerArgument>-proc:none</compilerArgument>
38-
<release>20</release>
3937
</configuration>
4038
</plugin>
4139
</plugins>

0 commit comments

Comments
 (0)