Skip to content

Commit 9db6d88

Browse files
authored
Javalin 6-beta Support (#341)
* more valid test * javalin 6 * fix validation groups * fix tests * actually fix
1 parent f56fd40 commit 9db6d88

File tree

22 files changed

+195
-49
lines changed

22 files changed

+195
-49
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.avaje.http.api.javalin;
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 that handles Javalin afterMatched requests.
11+
*
12+
* <pre>{@code
13+
*
14+
* @AfterMatched
15+
* void save(Customer customer) {
16+
* ...
17+
* }
18+
*
19+
* }</pre>
20+
*/
21+
@Target(METHOD)
22+
@Retention(SOURCE)
23+
public @interface AfterMatched {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.avaje.http.api.javalin;
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 that handles Javalin beforeMatched requests.
11+
*
12+
* <pre>{@code
13+
*
14+
* @BeforeMatched
15+
* void save(Customer customer) {
16+
* ...
17+
* }
18+
*
19+
* }</pre>
20+
*/
21+
@Target(METHOD)
22+
@Retention(SOURCE)
23+
public @interface BeforeMatched {}

http-api/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
<artifactId>avaje-http-inject-plugin</artifactId>
2222
<version>${project.version}</version>
2323
</dependency>
24+
<dependency>
25+
<groupId>io.javalin</groupId>
26+
<artifactId>javalin</artifactId>
27+
<version>6.0.0-beta.1</version>
28+
<scope>provided</scope>
29+
<optional>true</optional>
30+
</dependency>
31+
2432
</dependencies>
2533

2634
<build>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.avaje.http.api;
2+
3+
import io.javalin.plugin.Plugin;
4+
5+
public abstract class AvajeJavalinPlugin extends Plugin<Void> {
6+
7+
protected AvajeJavalinPlugin() {}
8+
9+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Locale.LanguageRange;
77

88
/** Validator for form beans or request beans. */
9+
@FunctionalInterface
910
public interface Validator {
1011

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

21-
default Locale resolveLocale(String acceptLanguage, Collection<Locale> acceptLocales) {
22+
default Locale resolveLocale(String acceptLanguage, Collection<Locale> acceptedLocales) {
2223
if (acceptLanguage == null) {
2324
return null;
2425
}
2526
final List<LanguageRange> list = LanguageRange.parse(acceptLanguage);
26-
return Locale.lookup(list, acceptLocales);
27+
return Locale.lookup(list, acceptedLocales);
2728
}
2829
}

http-api/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
exports io.avaje.http.api;
44
exports io.avaje.http.api.context;
55
exports io.avaje.http.api.spi;
6+
7+
requires static io.javalin;
68
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void overrideVarName(String name, ParamType paramType) {
240240
}
241241

242242
public void overrideVarName(int position) {
243-
if (paramName.equals("arg" + position)) {
243+
if (("arg" + position).equals(paramName)) {
244244
overrideVarNameError = true;
245245
// varName += " /** @QueryParam(name=...) required */ ";
246246
} else {
@@ -298,7 +298,7 @@ void writeValidate(Append writer) {
298298
if (useValidation) {
299299
writer.append("%s validator.validate(%s, ", indent, varName);
300300
platform().writeAcceptLanguage(writer);
301-
validationGroups.forEach(g -> writer.append(", %s", Util.shortName(g)));
301+
validationGroups.forEach(g -> writer.append(", %s.class", Util.shortName(g)));
302302
writer.append(");").eol();
303303
} else {
304304
writer.append("%s // no validation required on %s", indent, varName).eol();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private static final class Ctx {
5151
private final String diAnnotation;
5252
private final boolean instrumentAllMethods;
5353
private final boolean disableDirectWrites;
54+
private final boolean javalin6;
5455
private ModuleElement module;
5556
private boolean validated;
5657

@@ -85,6 +86,7 @@ private static final class Ctx {
8586
} else {
8687
useJavax = (javax);
8788
}
89+
this.javalin6 = elementUtils.getTypeElement("io.javalin.config.JavalinConfig") != null;
8890
}
8991
}
9092

@@ -210,6 +212,10 @@ public static boolean disabledDirectWrites() {
210212
return CTX.get().disableDirectWrites;
211213
}
212214

215+
public static boolean javalin6() {
216+
return CTX.get().javalin6;
217+
}
218+
213219
public static Filer filer() {
214220
return CTX.get().filer;
215221
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.avaje.http.generator.javalin;
22

3+
import static io.avaje.http.generator.javalin.JavalinWebMethod.*;
34
import io.avaje.http.generator.core.CustomWebMethod;
45
import io.avaje.http.generator.core.WebMethod;
56

@@ -8,9 +9,13 @@ public abstract class AbstractCustomMethodPrism implements CustomWebMethod {
89
@Override
910
public WebMethod webMethod() {
1011
if (this instanceof AfterPrism) {
11-
return JavalinWebMethod.AFTER;
12+
return AFTER;
13+
} else if (this instanceof BeforePrism) {
14+
return BEFORE;
15+
} else if (this instanceof AfterMatchedPrism) {
16+
return AFTER_MATCHED;
1217
} else {
13-
return JavalinWebMethod.BEFORE;
18+
return BEFORE_MATCHED;
1419
}
1520
}
1621
}

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerMethodWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ private void writeMethod(final String fullPath) {
9898
if (method.isErrorMethod()) {
9999
writer.append(" app.exception(%s.class, (ex, ctx) -> {", method.exceptionShortName()).eol();
100100
} else {
101-
writer.append(" app.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol();
101+
var methodName = webMethod.name().toLowerCase().replace("_m", "M");
102+
writer.append(" app.%s(\"%s\", ctx -> {", methodName, fullPath).eol();
102103
}
103104
if (!customMethod) {
104105
int statusCode = method.statusCode();

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/ControllerWriter.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.avaje.http.generator.core.JsonBUtil;
1212
import io.avaje.http.generator.core.MethodReader;
1313
import io.avaje.http.generator.core.PrimitiveUtil;
14+
import io.avaje.http.generator.core.ProcessingContext;
1415
import io.avaje.http.generator.core.UType;
1516

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

2527
ControllerWriter(ControllerReader reader, boolean jsonb) throws IOException {
2628
super(reader);
@@ -36,7 +38,15 @@ class ControllerWriter extends BaseControllerWriter {
3638
this.jsonTypes = Map.of();
3739
}
3840
reader.addImportType("io.javalin.plugin.Plugin");
39-
reader.addImportType("io.javalin.Javalin");
41+
42+
if (javalin6) {
43+
44+
reader.addImportType("io.javalin.config.JavalinConfig");
45+
reader.addImportType("io.javalin.router.JavalinDefaultRouting");
46+
reader.addImportType("io.avaje.http.api.AvajeJavalinPlugin");
47+
} else {
48+
reader.addImportType("io.javalin.Javalin");
49+
}
4050
}
4151

4252
void write() {
@@ -49,7 +59,17 @@ void write() {
4959

5060
private void writeAddRoutes() {
5161
writer.append(" @Override").eol();
52-
writer.append(" public void apply(Javalin app) {").eol().eol();
62+
63+
if (javalin6) {
64+
writer.append(" public void onStart(JavalinConfig cfg) {").eol();
65+
writer.append(" cfg.router.mount(this::routes);").eol();
66+
writer.append(" }").eol().eol();
67+
68+
writer.append(" private void routes(JavalinDefaultRouting app) {").eol().eol();
69+
} else {
70+
writer.append(" public void apply(Javalin app) {").eol().eol();
71+
}
72+
5373
for (final MethodReader method : reader.methods()) {
5474
if (method.isWebMethod()) {
5575
writeForMethod(method);
@@ -69,11 +89,11 @@ private void writeClassStart() {
6989
writer.append(AT_GENERATED).eol();
7090
writer.append(diAnnotation()).eol();
7191
writer
72-
.append("public class ")
73-
.append(shortName)
74-
.append("$Route implements Plugin {")
75-
.eol()
76-
.eol();
92+
.append("public class ")
93+
.append(shortName)
94+
.append(javalin6 ? "$Route extends AvajeJavalinPlugin {" : "$Route implements Plugin {")
95+
.eol()
96+
.eol();
7797

7898
var controllerName = "controller";
7999
var controllerType = shortName;

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/JavalinAdapter.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ public void writeAcceptLanguage(Append writer) {
141141

142142
@Override
143143
public List<Function<Element, Optional<CustomWebMethod>>> customHandlers() {
144-
final Function<Element, AfterPrism> f = AfterPrism::getInstanceOn;
145-
final Function<Element, BeforePrism> f2 = BeforePrism::getInstanceOn;
146-
147-
return List.of(f.andThen(Optional::ofNullable), f2.andThen(Optional::ofNullable));
144+
final Function<Element, AfterPrism> after = AfterPrism::getInstanceOn;
145+
final Function<Element, BeforePrism> before = BeforePrism::getInstanceOn;
146+
final Function<Element, AfterMatchedPrism> afterMatched = AfterMatchedPrism::getInstanceOn;
147+
final Function<Element, BeforeMatchedPrism> beforeMatched = BeforeMatchedPrism::getInstanceOn;
148+
149+
return List.of(
150+
after.andThen(Optional::ofNullable),
151+
before.andThen(Optional::ofNullable),
152+
afterMatched.andThen(Optional::ofNullable),
153+
beforeMatched.andThen(Optional::ofNullable));
148154
}
149155
}

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/JavalinProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.IOException;
44

55
import io.avaje.http.api.javalin.After;
6+
import io.avaje.http.api.javalin.AfterMatched;
67
import io.avaje.http.api.javalin.Before;
8+
import io.avaje.http.api.javalin.BeforeMatched;
79
import io.avaje.http.generator.core.BaseProcessor;
810
import io.avaje.http.generator.core.ControllerReader;
911
import io.avaje.http.generator.core.PlatformAdapter;
@@ -12,6 +14,8 @@
1214

1315
@GeneratePrism(value = After.class, superClass = AbstractCustomMethodPrism.class)
1416
@GeneratePrism(value = Before.class, superClass = AbstractCustomMethodPrism.class)
17+
@GeneratePrism(value = AfterMatched.class, superClass = AbstractCustomMethodPrism.class)
18+
@GeneratePrism(value = BeforeMatched.class, superClass = AbstractCustomMethodPrism.class)
1519
public class JavalinProcessor extends BaseProcessor {
1620

1721
@Override

http-generator-javalin/src/main/java/io/avaje/http/generator/javalin/JavalinWebMethod.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
public enum JavalinWebMethod implements WebMethod {
66
BEFORE(0),
7-
AFTER(0);
7+
BEFORE_MATCHED(0),
8+
AFTER(0),
9+
AFTER_MATCHED(0);
810

9-
private int statusCode;
11+
private final int statusCode;
1012

1113
JavalinWebMethod(int statusCode) {
1214
this.statusCode = statusCode;

tests/test-javalin-jsonb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<properties>
1414
<maven.deploy.skip>true</maven.deploy.skip>
1515
<main.class>org.example.myapp.Main</main.class>
16-
<javalin.version>5.6.3</javalin.version>
16+
<javalin.version>6.0.0-beta.1</javalin.version>
1717
<swagger.version>2.2.18</swagger.version>
1818
<kotlin.version>1.3.71</kotlin.version>
1919
</properties>

tests/test-javalin-jsonb/src/main/java/org/example/myapp/Main.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10+
import io.avaje.http.api.AvajeJavalinPlugin;
1011
import io.avaje.http.api.InvalidPathArgumentException;
1112
import io.avaje.http.api.InvalidTypeArgumentException;
1213
import io.avaje.http.api.ValidationException;
1314
import io.avaje.http.api.Validator;
1415
import io.avaje.inject.BeanScope;
1516
import io.avaje.inject.InjectModule;
17+
import io.avaje.inject.spi.GenericType;
1618
import io.javalin.Javalin;
19+
import io.javalin.config.JavalinConfig;
1720
import io.javalin.http.staticfiles.Location;
1821
import io.javalin.plugin.Plugin;
1922
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
@@ -37,19 +40,20 @@ public static Javalin start(int port) {
3740

3841
// All WebRoutes / Controllers ... from DI Context
3942
final var beanScope = BeanScope.builder().build();
40-
final List<Plugin> webRoutes = beanScope.list(Plugin.class);
43+
final List<AvajeJavalinPlugin> webRoutes =
44+
beanScope.list(AvajeJavalinPlugin.class);
4145

4246
final var app =
4347
Javalin.create(
4448
config -> {
4549
config.showJavalinBanner = false;
4650
config.staticFiles.add("public", Location.CLASSPATH);
47-
config.accessManager(
48-
(handler, ctx, permittedRoles) -> {
49-
log.debug("allow access ...");
50-
handler.handle(ctx);
51-
});
52-
webRoutes.forEach(config.plugins::register);
51+
// config.accessManager(
52+
// (handler, ctx, permittedRoles) -> {
53+
// log.debug("allow access ...");
54+
// handler.handle(ctx);
55+
// });
56+
webRoutes.forEach(config::registerPlugin);
5357
});
5458

5559
app.exception(ValidationException.class, (exception, ctx) -> {

tests/test-javalin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<properties>
1313
<maven.deploy.skip>true</maven.deploy.skip>
1414
<main.class>org.example.myapp.Main</main.class>
15-
<javalin.version>5.6.3</javalin.version>
15+
<javalin.version>6.0.0-beta.1</javalin.version>
1616
<swagger.version>2.2.18</swagger.version>
1717
<kotlin.version>1.3.71</kotlin.version>
1818
</properties>

0 commit comments

Comments
 (0)