Skip to content

Commit 4f9d9aa

Browse files
committed
#2 - Support @roles ...
1 parent be698f6 commit 4f9d9aa

File tree

7 files changed

+111
-17
lines changed

7 files changed

+111
-17
lines changed

src/main/java/io/dinject/javalin/generator/BeanReader.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,26 @@
1111
import java.util.Set;
1212
import java.util.TreeSet;
1313

14+
import static io.dinject.javalin.generator.Constants.JAVALIN_ROLES;
15+
1416
/**
1517
* Reads the type information for the Controller (bean).
1618
*/
1719
class BeanReader {
1820

1921
private final TypeElement beanType;
2022

21-
private final ProcessingContext ctx;
23+
private final List<String> roles;
2224

2325
private final List<MethodReader> methods = new ArrayList<>();
2426

27+
private final Set<String> staticImportTypes = new TreeSet<>();
28+
2529
private final Set<String> importTypes = new TreeSet<>();
2630

27-
BeanReader(TypeElement beanType, ProcessingContext ctx) {
31+
BeanReader(TypeElement beanType) {
2832
this.beanType = beanType;
29-
this.ctx = ctx;
33+
this.roles = Util.findRoles(beanType);
3034

3135
importTypes.add(Constants.GENERATED);
3236
importTypes.add(Constants.SINGLETON);
@@ -40,12 +44,16 @@ TypeElement getBeanType() {
4044
}
4145

4246
void read() {
47+
if (!roles.isEmpty()) {
48+
addStaticImportType(JAVALIN_ROLES);
49+
for (String role : roles) {
50+
addStaticImportType(role);
51+
}
52+
}
53+
4354
for (Element element : beanType.getEnclosedElements()) {
44-
ElementKind kind = element.getKind();
45-
switch (kind) {
46-
case METHOD:
47-
readMethod(element);
48-
break;
55+
if (element.getKind() == ElementKind.METHOD) {
56+
readMethod(element);
4957
}
5058
}
5159
}
@@ -59,15 +67,14 @@ private void readMethod(Element element) {
5967
methods.add(methodReader);
6068
}
6169

70+
List<String> getRoles() {
71+
return roles;
72+
}
6273

6374
List<MethodReader> getMethods() {
6475
return methods;
6576
}
6677

67-
ProcessingContext getContext() {
68-
return ctx;
69-
}
70-
7178
String getPath() {
7279
Path path = beanType.getAnnotation(Path.class);
7380
if (path == null) {
@@ -80,7 +87,16 @@ void addImportType(String rawType) {
8087
importTypes.add(rawType);
8188
}
8289

90+
void addStaticImportType(String rawType) {
91+
staticImportTypes.add(rawType);
92+
}
93+
94+
Set<String> getStaticImportTypes() {
95+
return staticImportTypes;
96+
}
97+
8398
Set<String> getImportTypes() {
8499
return importTypes;
85100
}
101+
86102
}

src/main/java/io/dinject/javalin/generator/Constants.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
class Constants {
44

5-
static final String IO_JAVALIN_CONTEXT = "io.javalin.Context";
5+
static final String JAVALIN_CONTEXT = "io.javalin.Context";
6+
7+
static final String JAVALIN_ROLES = "io.javalin.security.SecurityUtil.roles";
68

79
static final String SINGLETON = "javax.inject.Singleton";
810
static final String GENERATED = "javax.annotation.Generated";

src/main/java/io/dinject/javalin/generator/ControllerRouteWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ private void writeAddRoutes() {
5454
private void writeImports() {
5555

5656
writer.append(Constants.IMPORT_PATH_TYPE_CONVERT).eol();
57+
for (String type : reader.getStaticImportTypes()) {
58+
writer.append("import static %s;", type).eol();
59+
}
60+
writer.eol();
5761
for (String type : reader.getImportTypes()) {
5862
writer.append("import %s;", type).eol();
5963
}

src/main/java/io/dinject/javalin/generator/MethodParam.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MethodParam {
1919
}
2020

2121
private boolean isJavalinContext() {
22-
return Constants.IO_JAVALIN_CONTEXT.equals(rawType);
22+
return Constants.JAVALIN_CONTEXT.equals(rawType);
2323
}
2424

2525
void buildCtxGet(Append writer, Set<String> pathParams) {

src/main/java/io/dinject/javalin/generator/MethodReader.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.List;
1313
import java.util.Set;
1414

15+
import static io.dinject.javalin.generator.Constants.JAVALIN_ROLES;
16+
1517
class MethodReader {
1618

1719
private final BeanReader bean;
@@ -24,14 +26,27 @@ class MethodReader {
2426
private WebMethod webMethod;
2527
private String webMethodPath;
2628

29+
/**
30+
* Holds enum Roles that are required for the method.
31+
*/
32+
private final List<String> methodRoles;
33+
2734
MethodReader(BeanReader bean, ExecutableElement element) {
2835
this.bean = bean;
2936
this.beanPath = bean.getPath();
3037
this.element = element;
3138
this.isVoid = element.getReturnType().toString().equals("void");
39+
this.methodRoles = Util.findRoles(element);
3240
}
3341

3442
void read() {
43+
if (!methodRoles.isEmpty()) {
44+
bean.addStaticImportType(JAVALIN_ROLES);
45+
for (String role : methodRoles) {
46+
bean.addStaticImportType(role);
47+
}
48+
}
49+
3550
for (VariableElement p : element.getParameters()) {
3651
MethodParam param = new MethodParam(p);
3752
params.add(param);
@@ -71,11 +86,28 @@ void addRoute(Append writer) {
7186
}
7287
writer.append(";").eol();
7388
writer.append(" ctx.status(%s);", httpStatusCode()).eol();
74-
writer.append(" });");
89+
writer.append(" }");
90+
91+
List<String> roles = roles();
92+
if (!roles.isEmpty()) {
93+
writer.append(", roles(");
94+
for (int i = 0; i < roles.size(); i++) {
95+
if (i > 0) {
96+
writer.append(", ");
97+
}
98+
writer.append(Util.shortName(roles.get(i)));
99+
}
100+
writer.append(")");
101+
}
102+
writer.append(");");
75103
writer.eol().eol();
76104
}
77105
}
78106

107+
private List<String> roles() {
108+
return methodRoles.isEmpty() ? bean.getRoles() : methodRoles;
109+
}
110+
79111
private int httpStatusCode() {
80112
return webMethod.statusCode();
81113
}

src/main/java/io/dinject/javalin/generator/Processor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
4747

4848
private void writeControllerAdapter(Element controller) {
4949
if (controller instanceof TypeElement) {
50-
TypeElement te = (TypeElement) controller;
51-
BeanReader reader = new BeanReader(te, processingContext);
50+
BeanReader reader = new BeanReader((TypeElement) controller);
5251
reader.read();
5352
try {
5453
ControllerRouteWriter writer = new ControllerRouteWriter(reader, processingContext);

src/main/java/io/dinject/javalin/generator/Util.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package io.dinject.javalin.generator;
22

3+
import javax.lang.model.element.AnnotationMirror;
4+
import javax.lang.model.element.AnnotationValue;
5+
import javax.lang.model.element.Element;
6+
import javax.lang.model.type.DeclaredType;
7+
import java.util.ArrayList;
38
import java.util.LinkedHashSet;
9+
import java.util.List;
410
import java.util.Set;
511

612
class Util {
@@ -62,4 +68,39 @@ static String snakeCase(String name) {
6268
}
6369
return sb.toString();
6470
}
71+
72+
/**
73+
* Find and return the list of roles on the given element.
74+
* <p>
75+
* This assumes the application uses either <code>@Role</code> annotation
76+
* or <code>@PermittedRoles</code> annotation.
77+
* </p>
78+
*
79+
* @param element The bean or method
80+
*/
81+
static List<String> findRoles(Element element) {
82+
83+
List<String> roles = new ArrayList<>();
84+
85+
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
86+
DeclaredType annotationType = annotationMirror.getAnnotationType();
87+
if (isRolesAnnotation(annotationType)) {
88+
for (AnnotationValue value : annotationMirror.getElementValues().values()) {
89+
String raw = value.toString();
90+
if (raw.startsWith("{")) {
91+
raw = raw.substring(1, raw.length() - 1);
92+
}
93+
for (String singleRole : raw.split(",")) {
94+
roles.add(singleRole.trim());
95+
}
96+
}
97+
}
98+
}
99+
return roles;
100+
}
101+
102+
private static boolean isRolesAnnotation(DeclaredType annotationType) {
103+
String name = annotationType.asElement().getSimpleName().toString();
104+
return name.endsWith("Roles") || name.endsWith("PermittedRoles");
105+
}
65106
}

0 commit comments

Comments
 (0)