diff --git a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java index da6d99a8..9c45addb 100644 --- a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java +++ b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java @@ -14,7 +14,7 @@ class ControllerMethodWriter { private final MethodReader method; private final Append writer; private final WebMethod webMethod; - private boolean instrumentContext; + private final boolean instrumentContext; ControllerMethodWriter(MethodReader method, Append writer) { this.method = method; @@ -23,16 +23,37 @@ class ControllerMethodWriter { this.instrumentContext = method.instrumentContext(); } - void write(boolean requestScoped) { + void writeRouting() { final PathSegments segments = method.pathSegments(); final String fullPath = segments.fullPath(); + writer.append(" routing.%s(\"%s\", this::_%s)", webMethod.name().toLowerCase(), fullPath, method.simpleName()); + List roles = method.roles(); + if (!roles.isEmpty()) { + writer.append(".withRoles("); + for (int i = 0; i < roles.size(); i++) { + if (i > 0) { + writer.append(", "); + } + writer.append(Util.shortName(roles.get(i), true)); + } + writer.append(")"); + } + writer.append(";").eol(); + } - writer.append(" routing.%s(\"%s\", ctx -> {", webMethod.name().toLowerCase(), fullPath).eol(); + void writeHandler(boolean requestScoped) { + writer.append(" private void _%s(Context ctx) {", method.simpleName()).eol(); + write(requestScoped); + writer.append(" }").eol().eol(); + } + + private void write(boolean requestScoped) { int statusCode = method.statusCode(); if (statusCode > 0) { - writer.append(" ctx.status(%d);", statusCode).eol(); + writer.append(" ctx.status(%d);", statusCode).eol(); } + final PathSegments segments = method.pathSegments(); List matrixSegments = segments.matrixSegments(); for (PathSegments.Segment matrixSegment : matrixSegments) { matrixSegment.writeCreateSegment(writer, platform()); @@ -47,7 +68,7 @@ void write(boolean requestScoped) { param.writeValidate(writer); } } - writer.append(" "); + writer.append(" "); if (!method.isVoid()) { writeContextReturn(); } @@ -74,19 +95,6 @@ void write(boolean requestScoped) { writer.append(")"); } writer.append(";").eol(); - writer.append(" }"); - - List roles = method.roles(); - if (!roles.isEmpty()) { - writer.append(").withRoles("); - for (int i = 0; i < roles.size(); i++) { - if (i > 0) { - writer.append(", "); - } - writer.append(Util.shortName(roles.get(i), true)); - } - } - writer.append(");").eol().eol(); } private void writeContextReturn() { diff --git a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerWriter.java b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerWriter.java index 5f8bcad0..96d0bee6 100644 --- a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerWriter.java +++ b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerWriter.java @@ -11,11 +11,13 @@ class ControllerWriter extends BaseControllerWriter { private static final String AT_GENERATED = "@Generated(\"avaje-jex-generator\")"; + private static final String API_CONTEXT = "io.avaje.jex.Context"; private static final String API_ROUTING = "io.avaje.jex.Routing"; private static final String API_ROUTING_SERVICE = "io.avaje.jex.Routing.Service"; ControllerWriter(ControllerReader reader) throws IOException { super(reader); + reader.addImportType(API_CONTEXT); reader.addImportType(API_ROUTING); reader.addImportType(API_ROUTING_SERVICE); } @@ -25,27 +27,36 @@ void write() { writeImports(); writeClassStart(); writeAddRoutes(); + writeHandlers(); writeClassEnd(); } private void writeAddRoutes() { writer.append(" @Override").eol(); - writer.append(" public void add(Routing routing) {").eol().eol(); + writer.append(" public void add(Routing routing) {").eol(); for (MethodReader method : reader.methods()) { if (method.isWebMethod()) { - writeForMethod(method); + writeRouting(method); } } writer.append(" }").eol().eol(); } - private void writeForMethod(MethodReader method) { - new ControllerMethodWriter(method, writer).write(isRequestScoped()); - if (!reader.isDocHidden()) { - method.buildApiDocumentation(); + private void writeHandlers() { + for (MethodReader method : reader.methods()) { + if (method.isWebMethod()) { + new ControllerMethodWriter(method, writer).writeHandler(isRequestScoped()); + if (!reader.isDocHidden()) { + method.buildApiDocumentation(); + } + } } } + private void writeRouting(MethodReader method) { + new ControllerMethodWriter(method, writer).writeRouting(); + } + private void writeClassStart() { writer.append(AT_GENERATED).eol(); writer.append(diAnnotation()).eol(); @@ -62,7 +73,7 @@ private void writeClassStart() { if (reader.isIncludeValidator()) { writer.append(" private final Validator validator;").eol(); } - + if (instrumentContext) { writer.append(" private final RequestContextResolver resolver;").eol(); } diff --git a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/JexAdapter.java b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/JexAdapter.java index fc0daba4..9943c1e3 100644 --- a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/JexAdapter.java +++ b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/JexAdapter.java @@ -38,7 +38,7 @@ public String bodyAsClass(UType uType) { @Override public String indent() { - return " "; + return " "; } @Override @@ -85,7 +85,7 @@ public void writeReadCollectionParameter( } writer.append("withDefault(ctx.queryParams(\"%s\"), java.util.List.of(\"%s\"))", paramName, String.join(",", paramDefault)); } - + @Override public void writeAcceptLanguage(Append writer) { writer.append("ctx.header(\"%s\")", Constants.ACCEPT_LANGUAGE);