Skip to content

Add Javalin @Before/@After and Helidon @Filter #255

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 9 commits into from
Jul 31, 2023
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
14 changes: 14 additions & 0 deletions http-api/src/main/java/io/avaje/http/api/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.avaje.http.api;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Marks a method as a Filter
*/
@Target(METHOD)
@Retention(SOURCE)
public @interface Filter {}
13 changes: 6 additions & 7 deletions http-api/src/main/java/io/avaje/http/api/HttpMethod.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.avaje.http.api;

import java.lang.annotation.ElementType;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Base for Http verb based annotations.
*/
@Target(value= ElementType.ANNOTATION_TYPE)
@Retention(value= RetentionPolicy.RUNTIME)
/** Base for Http verb based annotations. */
@Target(ANNOTATION_TYPE)
@Retention(RUNTIME)
public @interface HttpMethod {

String value();
Expand Down
16 changes: 8 additions & 8 deletions http-generator-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@

<properties>
<java.version>11</java.version>
<avaje.prisms.version>1.9</avaje.prisms.version>
</properties>
<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-prisms</artifactId>
<version>${avaje.prisms.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-http-generator-core</artifactId>
Expand All @@ -41,6 +33,14 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-prisms</artifactId>
<version>${avaje.prisms.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

</dependencies>

<build>
Expand Down
5 changes: 1 addition & 4 deletions http-generator-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@

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

<properties>
<avaje.prisms.version>1.10</avaje.prisms.version>
</properties>
<dependencies>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-prisms</artifactId>
<version>${avaje.prisms.version}</version>
<optional>true</optional>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.avaje.http.generator.core;

public enum CoreWebMethod implements WebMethod {
GET(200),
POST(201),
PUT(200, 204),
PATCH(200, 204),
DELETE(200, 204),
ERROR(500),
FILTER(0),
OTHER(0, 0);

private int statusCode;
private int voidStatusCode;

CoreWebMethod(int statusCode, int voidStatusCode) {
this.statusCode = statusCode;
this.voidStatusCode = voidStatusCode;
}

CoreWebMethod(int statusCode) {
this.statusCode = statusCode;
this.voidStatusCode = statusCode;
}

@Override
public int statusCode(boolean isVoid) {
return isVoid ? voidStatusCode : statusCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.avaje.http.generator.core;

public interface CustomWebMethod {

WebMethod webMethod();

default String value() {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.avaje.http.generator.core;

import static java.util.function.Predicate.not;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -17,6 +15,8 @@ public static Map<String, UType> jsonTypes(ControllerReader reader) {

reader.methods().stream()
.filter(MethodReader::isWebMethod)
.filter(m -> m.webMethod() instanceof CoreWebMethod)
.filter(m -> m.webMethod() != CoreWebMethod.FILTER)
.filter(m -> !"byte[]".equals(m.returnType().toString()))
.filter(m -> m.produces() == null || m.produces().toLowerCase().contains("json"))
.forEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,7 @@ private boolean initResolver() {
}

private boolean hasInstrument(Element e) {

if (InstrumentServerContextPrism.getOptionalOn(e).isPresent()) {
return true;
}

for (final var a : e.getAnnotationMirrors()) {
if (HttpMethodPrism.isPresent(a.getAnnotationType().asElement())) {
return a.getElementValues().values().stream().anyMatch(v -> v.getValue().equals(true));
}
}
return false;
return InstrumentServerContextPrism.getOptionalOn(e).isPresent();
}

private Javadoc buildJavadoc(ExecutableElement element) {
Expand Down Expand Up @@ -146,21 +136,29 @@ private void initWebMethodViaAnnotation() {
}

findAnnotation(GetPrism::getOptionalOn)
.ifPresent(get -> initSetWebMethod(WebMethod.GET, get.value()));
.ifPresent(get -> initSetWebMethod(CoreWebMethod.GET, get.value()));

findAnnotation(PutPrism::getOptionalOn)
.ifPresent(put -> initSetWebMethod(WebMethod.PUT, put.value()));
.ifPresent(put -> initSetWebMethod(CoreWebMethod.PUT, put.value()));

findAnnotation(PostPrism::getOptionalOn)
.ifPresent(post -> initSetWebMethod(WebMethod.POST, post.value()));
.ifPresent(post -> initSetWebMethod(CoreWebMethod.POST, post.value()));

findAnnotation(PatchPrism::getOptionalOn)
.ifPresent(patch -> initSetWebMethod(WebMethod.PATCH, patch.value()));
.ifPresent(patch -> initSetWebMethod(CoreWebMethod.PATCH, patch.value()));

findAnnotation(DeletePrism::getOptionalOn)
.ifPresent(delete -> initSetWebMethod(WebMethod.DELETE, delete.value()));
.ifPresent(delete -> initSetWebMethod(CoreWebMethod.DELETE, delete.value()));

findAnnotation(ExceptionHandlerPrism::getOptionalOn)
.ifPresent(error -> initSetWebMethod(WebMethod.ERROR, error.value()));
.ifPresent(error -> initSetWebMethod(CoreWebMethod.ERROR, error.value()));

findAnnotation(FilterPrism::getOptionalOn)
.ifPresent(filter -> initSetWebMethod(CoreWebMethod.FILTER, ""));

platform()
.customHandlers()
.forEach(f -> findAnnotation(f).ifPresent(m -> initSetWebMethod(m.webMethod(), m.value())));
}

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

if (!isErrorMethod()) {
if (!isErrorMethod()
&& webMethod instanceof CoreWebMethod
&& webMethod != CoreWebMethod.FILTER) {
new MethodDocBuilder(this, doc()).build();
}
}
Expand All @@ -333,7 +333,7 @@ public boolean isWebMethod() {
}

public boolean isErrorMethod() {
return webMethod == WebMethod.ERROR;
return webMethod == CoreWebMethod.ERROR;
}

public WebMethod webMethod() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
package io.avaje.http.generator.core;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;

/**
* Adapter to specific platforms like Javalin and Helidon.
*/
import javax.lang.model.element.Element;

/** Adapter to specific platforms like Javalin and Helidon. */
public interface PlatformAdapter {

/**
* Return true if this type is the platform specific request, response or context type.
* For example Javalin Context, Helidon ServerRequest or ServerResponse type).
* Return true if this type is the platform specific request, response or context type. For
* example Javalin Context, Helidon ServerRequest or ServerResponse type).
*/
boolean isContextType(String rawType);

/**
* Return the platform specific parameter (request, response or context).
*/
/** Return the platform specific parameter (request, response or context). */
String platformVariable(String rawType);

/**
* Return platform specific code to return the body content.
*/
/** Return platform specific code to return the body content. */
String bodyAsClass(UType type);

/**
* Return true if body is passed as a method parameter.
*/
/** Return true if body is passed as a method parameter. */
boolean isBodyMethodParam();

/**
* Return whitespace indent for setting parameter values.
*/
/** Return whitespace indent for setting parameter values. */
String indent();

/**
* Handle controller level roles.
*/
/** Handle controller level roles. */
void controllerRoles(List<String> roles, ControllerReader controller);

/**
* Handle method level roles.
*/
/** Handle method level roles. */
void methodRoles(List<String> roles, ControllerReader controller);

void writeReadParameter(Append writer, ParamType paramType, String paramName);
Expand All @@ -62,4 +52,8 @@ default void writeReadCollectionParameter(
Append writer, ParamType paramType, String paramName, List<String> paramDefault) {
throw new UnsupportedOperationException("Unsupported MultiValue Parameter");
}

default List<Function<Element, Optional<CustomWebMethod>>> customHandlers() {
return List.of();
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
package io.avaje.http.generator.core;

public enum WebMethod {
GET(200),
POST(201),
PUT(200, 204),
PATCH(200, 204),
DELETE(200, 204),
ERROR(500);
public interface WebMethod {

private int statusCode;
private int voidStatusCode;
int statusCode(boolean isVoid);

WebMethod(int statusCode, int voidStatusCode) {
this.statusCode = statusCode;
this.voidStatusCode = voidStatusCode;
}

WebMethod(int statusCode) {
this.statusCode = statusCode;
this.voidStatusCode = statusCode;
}

int statusCode(boolean isVoid) {
return isVoid ? voidStatusCode : statusCode;
}
String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;

import io.avaje.http.generator.core.ConsumesPrism;
import io.avaje.http.generator.core.CoreWebMethod;
import io.avaje.http.generator.core.HiddenPrism;
import io.avaje.http.generator.core.MethodParam;
import io.avaje.http.generator.core.MethodReader;
Expand Down Expand Up @@ -49,7 +50,7 @@ public void build() {
}

PathItem pathItem = ctx.pathItem(methodReader.fullPath());
switch (methodReader.webMethod()) {
switch ((CoreWebMethod) methodReader.webMethod()) {
case GET:
pathItem.setGet(operation);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
@GeneratePrism(value = io.avaje.http.api.FormParam.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Get.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Header.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.HttpMethod.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.MatrixParam.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Patch.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Path.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Post.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Produces.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Consumes.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Put.class, publicAccess = true)
@GeneratePrism(value = io.avaje.http.api.Filter.class)
@GeneratePrism(value = io.avaje.http.api.InstrumentServerContext.class)
@GeneratePrism(value = io.avaje.http.api.ExceptionHandler.class)
@GeneratePrism(value = io.swagger.v3.oas.annotations.OpenAPIDefinition.class, publicAccess = true)
Expand Down
4 changes: 1 addition & 3 deletions http-generator-helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
<artifactId>avaje-http-generator-core</artifactId>
<version>${project.version}</version>
</dependency>

</dependencies>

<build>
Expand All @@ -33,9 +32,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<!-- Turn off annotation processing for building -->
<release>20</release>
<compilerArgument>-proc:none</compilerArgument>
<release>20</release>
</configuration>
</plugin>
</plugins>
Expand Down
Loading