Skip to content

Javalin 6-beta Support #341

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 5 commits into from
Nov 1, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.http.api.javalin;

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 that handles Javalin afterMatched requests.
*
* <pre>{@code
*
* @AfterMatched
* void save(Customer customer) {
* ...
* }
*
* }</pre>
*/
@Target(METHOD)
@Retention(SOURCE)
public @interface AfterMatched {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.avaje.http.api.javalin;

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 that handles Javalin beforeMatched requests.
*
* <pre>{@code
*
* @BeforeMatched
* void save(Customer customer) {
* ...
* }
*
* }</pre>
*/
@Target(METHOD)
@Retention(SOURCE)
public @interface BeforeMatched {}
8 changes: 8 additions & 0 deletions http-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
<artifactId>avaje-http-inject-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>6.0.0-beta.1</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.avaje.http.api;

import io.javalin.plugin.Plugin;

public abstract class AvajeJavalinPlugin extends Plugin<Void> {

protected AvajeJavalinPlugin() {}

}
5 changes: 3 additions & 2 deletions http-api/src/main/java/io/avaje/http/api/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Locale.LanguageRange;

/** Validator for form beans or request beans. */
@FunctionalInterface
public interface Validator {

/**
Expand All @@ -18,11 +19,11 @@ public interface Validator {
*/
void validate(Object bean, String acceptLanguage, Class<?>... groups) throws ValidationException;

default Locale resolveLocale(String acceptLanguage, Collection<Locale> acceptLocales) {
default Locale resolveLocale(String acceptLanguage, Collection<Locale> acceptedLocales) {
if (acceptLanguage == null) {
return null;
}
final List<LanguageRange> list = LanguageRange.parse(acceptLanguage);
return Locale.lookup(list, acceptLocales);
return Locale.lookup(list, acceptedLocales);
}
}
2 changes: 2 additions & 0 deletions http-api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
exports io.avaje.http.api;
exports io.avaje.http.api.context;
exports io.avaje.http.api.spi;

requires static io.javalin;
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void overrideVarName(String name, ParamType paramType) {
}

public void overrideVarName(int position) {
if (paramName.equals("arg" + position)) {
if (("arg" + position).equals(paramName)) {
overrideVarNameError = true;
// varName += " /** @QueryParam(name=...) required */ ";
} else {
Expand Down Expand Up @@ -298,7 +298,7 @@ void writeValidate(Append writer) {
if (useValidation) {
writer.append("%s validator.validate(%s, ", indent, varName);
platform().writeAcceptLanguage(writer);
validationGroups.forEach(g -> writer.append(", %s", Util.shortName(g)));
validationGroups.forEach(g -> writer.append(", %s.class", Util.shortName(g)));
writer.append(");").eol();
} else {
writer.append("%s // no validation required on %s", indent, varName).eol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private static final class Ctx {
private final String diAnnotation;
private final boolean instrumentAllMethods;
private final boolean disableDirectWrites;
private final boolean javalin6;
private ModuleElement module;
private boolean validated;

Expand Down Expand Up @@ -85,6 +86,7 @@ private static final class Ctx {
} else {
useJavax = (javax);
}
this.javalin6 = elementUtils.getTypeElement("io.javalin.config.JavalinConfig") != null;
}
}

Expand Down Expand Up @@ -210,6 +212,10 @@ public static boolean disabledDirectWrites() {
return CTX.get().disableDirectWrites;
}

public static boolean javalin6() {
return CTX.get().javalin6;
}

public static Filer filer() {
return CTX.get().filer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.avaje.http.generator.javalin;

import static io.avaje.http.generator.javalin.JavalinWebMethod.*;
import io.avaje.http.generator.core.CustomWebMethod;
import io.avaje.http.generator.core.WebMethod;

Expand All @@ -8,9 +9,13 @@ public abstract class AbstractCustomMethodPrism implements CustomWebMethod {
@Override
public WebMethod webMethod() {
if (this instanceof AfterPrism) {
return JavalinWebMethod.AFTER;
return AFTER;
} else if (this instanceof BeforePrism) {
return BEFORE;
} else if (this instanceof AfterMatchedPrism) {
return AFTER_MATCHED;
} else {
return JavalinWebMethod.BEFORE;
return BEFORE_MATCHED;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ private void writeMethod(final String fullPath) {
if (method.isErrorMethod()) {
writer.append(" app.exception(%s.class, (ex, ctx) -> {", method.exceptionShortName()).eol();
} else {
writer.append(" app.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol();
var methodName = webMethod.name().toLowerCase().replace("_m", "M");
writer.append(" app.%s(\"%s\", ctx -> {", methodName, fullPath).eol();
}
if (!customMethod) {
int statusCode = method.statusCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.avaje.http.generator.core.JsonBUtil;
import io.avaje.http.generator.core.MethodReader;
import io.avaje.http.generator.core.PrimitiveUtil;
import io.avaje.http.generator.core.ProcessingContext;
import io.avaje.http.generator.core.UType;

/**
Expand All @@ -21,6 +22,7 @@ class ControllerWriter extends BaseControllerWriter {
private static final String AT_GENERATED = "@Generated(\"avaje-javalin-generator\")";
private final boolean useJsonB;
private final Map<String, UType> jsonTypes;
private final boolean javalin6 = ProcessingContext.javalin6();

ControllerWriter(ControllerReader reader, boolean jsonb) throws IOException {
super(reader);
Expand All @@ -36,7 +38,15 @@ class ControllerWriter extends BaseControllerWriter {
this.jsonTypes = Map.of();
}
reader.addImportType("io.javalin.plugin.Plugin");
reader.addImportType("io.javalin.Javalin");

if (javalin6) {

reader.addImportType("io.javalin.config.JavalinConfig");
reader.addImportType("io.javalin.router.JavalinDefaultRouting");
reader.addImportType("io.avaje.http.api.AvajeJavalinPlugin");
} else {
reader.addImportType("io.javalin.Javalin");
}
}

void write() {
Expand All @@ -49,7 +59,17 @@ void write() {

private void writeAddRoutes() {
writer.append(" @Override").eol();
writer.append(" public void apply(Javalin app) {").eol().eol();

if (javalin6) {
writer.append(" public void onStart(JavalinConfig cfg) {").eol();
writer.append(" cfg.router.mount(this::routes);").eol();
writer.append(" }").eol().eol();

writer.append(" private void routes(JavalinDefaultRouting app) {").eol().eol();
} else {
writer.append(" public void apply(Javalin app) {").eol().eol();
}

for (final MethodReader method : reader.methods()) {
if (method.isWebMethod()) {
writeForMethod(method);
Expand All @@ -69,11 +89,11 @@ private void writeClassStart() {
writer.append(AT_GENERATED).eol();
writer.append(diAnnotation()).eol();
writer
.append("public class ")
.append(shortName)
.append("$Route implements Plugin {")
.eol()
.eol();
.append("public class ")
.append(shortName)
.append(javalin6 ? "$Route extends AvajeJavalinPlugin {" : "$Route implements Plugin {")
.eol()
.eol();

var controllerName = "controller";
var controllerType = shortName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,15 @@ public void writeAcceptLanguage(Append writer) {

@Override
public List<Function<Element, Optional<CustomWebMethod>>> customHandlers() {
final Function<Element, AfterPrism> f = AfterPrism::getInstanceOn;
final Function<Element, BeforePrism> f2 = BeforePrism::getInstanceOn;

return List.of(f.andThen(Optional::ofNullable), f2.andThen(Optional::ofNullable));
final Function<Element, AfterPrism> after = AfterPrism::getInstanceOn;
final Function<Element, BeforePrism> before = BeforePrism::getInstanceOn;
final Function<Element, AfterMatchedPrism> afterMatched = AfterMatchedPrism::getInstanceOn;
final Function<Element, BeforeMatchedPrism> beforeMatched = BeforeMatchedPrism::getInstanceOn;

return List.of(
after.andThen(Optional::ofNullable),
before.andThen(Optional::ofNullable),
afterMatched.andThen(Optional::ofNullable),
beforeMatched.andThen(Optional::ofNullable));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.io.IOException;

import io.avaje.http.api.javalin.After;
import io.avaje.http.api.javalin.AfterMatched;
import io.avaje.http.api.javalin.Before;
import io.avaje.http.api.javalin.BeforeMatched;
import io.avaje.http.generator.core.BaseProcessor;
import io.avaje.http.generator.core.ControllerReader;
import io.avaje.http.generator.core.PlatformAdapter;
Expand All @@ -12,6 +14,8 @@

@GeneratePrism(value = After.class, superClass = AbstractCustomMethodPrism.class)
@GeneratePrism(value = Before.class, superClass = AbstractCustomMethodPrism.class)
@GeneratePrism(value = AfterMatched.class, superClass = AbstractCustomMethodPrism.class)
@GeneratePrism(value = BeforeMatched.class, superClass = AbstractCustomMethodPrism.class)
public class JavalinProcessor extends BaseProcessor {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

public enum JavalinWebMethod implements WebMethod {
BEFORE(0),
AFTER(0);
BEFORE_MATCHED(0),
AFTER(0),
AFTER_MATCHED(0);

private int statusCode;
private final int statusCode;

JavalinWebMethod(int statusCode) {
this.statusCode = statusCode;
Expand Down
2 changes: 1 addition & 1 deletion tests/test-javalin-jsonb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<main.class>org.example.myapp.Main</main.class>
<javalin.version>5.6.3</javalin.version>
<javalin.version>6.0.0-beta.1</javalin.version>
<swagger.version>2.2.18</swagger.version>
<kotlin.version>1.3.71</kotlin.version>
</properties>
Expand Down
18 changes: 11 additions & 7 deletions tests/test-javalin-jsonb/src/main/java/org/example/myapp/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.avaje.http.api.AvajeJavalinPlugin;
import io.avaje.http.api.InvalidPathArgumentException;
import io.avaje.http.api.InvalidTypeArgumentException;
import io.avaje.http.api.ValidationException;
import io.avaje.http.api.Validator;
import io.avaje.inject.BeanScope;
import io.avaje.inject.InjectModule;
import io.avaje.inject.spi.GenericType;
import io.javalin.Javalin;
import io.javalin.config.JavalinConfig;
import io.javalin.http.staticfiles.Location;
import io.javalin.plugin.Plugin;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
Expand All @@ -37,19 +40,20 @@ public static Javalin start(int port) {

// All WebRoutes / Controllers ... from DI Context
final var beanScope = BeanScope.builder().build();
final List<Plugin> webRoutes = beanScope.list(Plugin.class);
final List<AvajeJavalinPlugin> webRoutes =
beanScope.list(AvajeJavalinPlugin.class);

final var app =
Javalin.create(
config -> {
config.showJavalinBanner = false;
config.staticFiles.add("public", Location.CLASSPATH);
config.accessManager(
(handler, ctx, permittedRoles) -> {
log.debug("allow access ...");
handler.handle(ctx);
});
webRoutes.forEach(config.plugins::register);
// config.accessManager(
// (handler, ctx, permittedRoles) -> {
// log.debug("allow access ...");
// handler.handle(ctx);
// });
webRoutes.forEach(config::registerPlugin);
});

app.exception(ValidationException.class, (exception, ctx) -> {
Expand Down
2 changes: 1 addition & 1 deletion tests/test-javalin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
<main.class>org.example.myapp.Main</main.class>
<javalin.version>5.6.3</javalin.version>
<javalin.version>6.0.0-beta.1</javalin.version>
<swagger.version>2.2.18</swagger.version>
<kotlin.version>1.3.71</kotlin.version>
</properties>
Expand Down
Loading