Skip to content

Commit a5ff055

Browse files
committed
goal init, Quarkus and Micronaut generation based on freemarker macros.
1 parent 6a6e78c commit a5ff055

File tree

5 files changed

+72
-173
lines changed

5 files changed

+72
-173
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- [fj-doc-maven-plugin] goal init, Quarkus and Micronaut generation based on freemarker macros.
13+
14+
### Fixed
15+
16+
- [fj-doc-maven-plugin] goal init, micronaut-4 typo in Controller class name
17+
1018
## [8.8.1] - 2024-09-10
1119

1220
### Changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
11
<#macro toProjectPackageFolder context>${context.groupId?replace(".","/")}/${context.artifactId?replace("-","")}</#macro>
22

33
<#macro toProjectPackage context>${context.groupId}.${context.artifactId?replace("-","")}</#macro>
4+
5+
<#macro createDocumentProcess context exceptionType>
6+
byte[] processDocument(String handlerId) {
7+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
8+
// creates the doc helper
9+
DocHelper docHelper = new DocHelper();
10+
// create custom data for the fremarker template 'document.ftl'
11+
List<People> listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King"));
12+
// output generation
13+
docHelper.getDocProcessConfig().fullProcess("document", DocProcessContext.newContext("listPeople", listPeople), handlerId, baos);
14+
// return the output
15+
return baos.toByteArray();
16+
} catch (Exception e) {
17+
String message = String.format("Error processing %s, error:%s", handlerId, e);
18+
log.error(message, e);
19+
throw new ${exceptionType}(message, e);
20+
}
21+
}
22+
</#macro>
23+
24+
<#macro createPathMethod context outputMime outputExtension outputDescription>
25+
public byte[] ${outputDescription?lower_case}Example() {
26+
return processDocument(DocConfig.TYPE_${outputExtension?upper_case});
27+
}
28+
</#macro>
29+
30+
<#macro createQuarkusPath context outputMime outputExtension outputDescription>
31+
@APIResponse(responseCode = "200", description = "The ${outputDescription} document content" )
32+
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
33+
@Tags( { @Tag( name = "document" ), @Tag( name = "${outputDescription?lower_case}" ) } )
34+
@Operation( operationId = "${outputDescription}Example", summary = "Generated an example ${outputDescription} document using Fugerit Venus Doc handler" )
35+
@GET
36+
@Produces("${outputMime}")
37+
@Path("/example.${outputExtension}")
38+
<@createPathMethod context=context outputMime=outputMime outputExtension=outputExtension outputDescription=outputDescription/>
39+
</#macro>
40+
41+
<#macro createMicronautPath context outputMime outputExtension outputDescription>
42+
@Get(uri="/example.${outputExtension}", produces="${outputMime}")
43+
<@createPathMethod context=context outputMime=outputMime outputExtension=outputExtension outputDescription=outputDescription/>
44+
</#macro>
Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<#import
2-
'../flavour-macro.ftl' as fhm>
1+
<#import '../flavour-macro.ftl' as fhm>
32
package <@fhm.toProjectPackage context=context/>;
43

54
import io.micronaut.http.annotation.*;
@@ -18,51 +17,22 @@ import java.util.List;
1817
@Controller("/doc")
1918
public class DocController {
2019

21-
byte[] processDocument(String handlerId) {
22-
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
23-
// creates the doc helper
24-
DocHelper docHelper = new DocHelper();
25-
// create custom data for the fremarker template 'document.ftl'
26-
List<People> listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King"));
27-
// output generation
28-
docHelper.getDocProcessConfig().fullProcess("document", DocProcessContext.newContext("listPeople", listPeople), handlerId, baos);
29-
// return the output
30-
return baos.toByteArray();
31-
} catch (Exception e) {
32-
log.error(String.format("Error processing %s, error:%s", handlerId, e), e);
33-
throw new ConfigRuntimeException(e);
34-
}
35-
}
36-
37-
@Get(uri="/example.md", produces="text/markdown")
38-
public byte[] markdownExample() {
39-
return processDocument(DocConfig.TYPE_MD);
40-
}
41-
42-
@Get(uri="/example.html", produces="text/html")
43-
public byte[] htmlExample() {
44-
return processDocument(DocConfig.TYPE_HTML);
45-
}
20+
<@fhm.createDocumentProcess context=context exceptionType='ConfigRuntimeException'/>
4621

22+
<@fhm.createMicronautPath context=context outputMime="text/markdown" outputExtension="md" outputDescription="Markdown"/>
23+
24+
<@fhm.createMicronautPath context=context outputMime="text/html" outputExtension="html" outputDescription="HTML"/>
25+
4726
<#if context.modules?seq_contains("fj-doc-mod-fop")>
48-
@Get(uri="/example.pdf", produces="application/pdf")
49-
public byte[] pdfExample() {
50-
return processDocument(DocConfig.TYPE_PDF);
51-
}
27+
<@fhm.createMicronautPath context=context outputMime="application/pdf" outputExtension="pdf" outputDescription="PDF"/>
5228
</#if>
53-
29+
5430
<#if context.modules?seq_contains("fj-doc-mod-poi")>
55-
@Get(uri="/example.xlsx", produces="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
56-
public byte[] xlsxExample() {
57-
return processDocument(DocConfig.TYPE_XLSX);
58-
}
31+
<@fhm.createMicronautPath context=context outputMime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" outputExtension="xlsx" outputDescription="Excel"/>
5932
</#if>
60-
33+
6134
<#if context.modules?seq_contains("fj-doc-mod-opencsv")>
62-
@Get(uri="/example.csv", produces="text/csv")
63-
public byte[] csvExample() {
64-
return processDocument(DocConfig.TYPE_CSV);
65-
}
35+
<@fhm.createMicronautPath context=context outputMime="text/csv" outputExtension="csv" outputDescription="CSV"/>
6636
</#if>
6737

6838
}

fj-doc-maven-plugin/src/main/resources/config/template/flavour/quarkus-2/DocResource.ftl

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,82 +23,22 @@ import org.eclipse.microprofile.openapi.annotations.tags.Tags;
2323
@Path("/doc")
2424
public class DocResource {
2525

26-
byte[] processDocument(String handlerId) {
27-
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
28-
// creates the doc helper
29-
DocHelper docHelper = new DocHelper();
30-
// create custom data for the fremarker template 'document.ftl'
31-
List<People> listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King"));
32-
// output generation
33-
docHelper.getDocProcessConfig().fullProcess("document", DocProcessContext.newContext("listPeople", listPeople), handlerId, baos);
34-
// return the output
35-
return baos.toByteArray();
36-
} catch (Exception e) {
37-
log.error(String.format("Error processing %s, error:%s", handlerId, e), e);
38-
throw new WebApplicationException(e);
39-
}
40-
}
26+
<@fhm.createDocumentProcess context=context exceptionType='WebApplicationException'/>
4127

28+
<@fhm.createQuarkusPath context=context outputMime="text/markdown" outputExtension="md" outputDescription="Markdown"/>
4229

43-
@APIResponse(responseCode = "200", description = "The Markdown document content" )
44-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
45-
@Tags( { @Tag( name = "document" ), @Tag( name = "markdown" ) } )
46-
@Operation( operationId = "markdownExample", summary = "Generated an example Markdown document using Fugerit Venus Doc handler" )
47-
@GET
48-
@Produces("text/markdown")
49-
@Path("/example.md")
50-
public byte[] markdownExample() {
51-
return processDocument(DocConfig.TYPE_MD);
52-
}
53-
54-
@APIResponse(responseCode = "200", description = "The HTML document content" )
55-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
56-
@Tags( { @Tag( name = "document" ), @Tag( name = "html" ) } )
57-
@Operation( operationId = "htmlExample", summary = "Generated an example HTML document using Fugerit Venus Doc handler" )
58-
@GET
59-
@Produces(MediaType.TEXT_HTML)
60-
@Path("/example.html")
61-
public byte[] htmlExample() {
62-
return processDocument(DocConfig.TYPE_HTML);
63-
}
30+
<@fhm.createQuarkusPath context=context outputMime="text/html" outputExtension="html" outputDescription="HTML"/>
6431

6532
<#if context.modules?seq_contains("fj-doc-mod-fop")>
66-
@APIResponse(responseCode = "200", description = "The PDF document content" )
67-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
68-
@Tags( { @Tag( name = "document" ), @Tag( name = "pdf" ) } )
69-
@Operation( operationId = "pdfExample", summary = "Generated an example PDF document using Fugerit Venus Doc handler" )
70-
@GET
71-
@Produces("application/pdf")
72-
@Path("/example.pdf")
73-
public byte[] pdfExample() {
74-
return processDocument(DocConfig.TYPE_PDF);
75-
}
33+
<@fhm.createQuarkusPath context=context outputMime="application/pdf" outputExtension="pdf" outputDescription="PDF"/>
7634
</#if>
7735

7836
<#if context.modules?seq_contains("fj-doc-mod-poi")>
79-
@APIResponse(responseCode = "200", description = "The Excel document content" )
80-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
81-
@Tags( { @Tag( name = "document" ), @Tag( name = "excel" ) } )
82-
@Operation( operationId = "xlsxExample", summary = "Generated an example Excel document using Fugerit Venus Doc handler" )
83-
@GET
84-
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
85-
@Path("/example.xlsx")
86-
public byte[] xlsxExample() {
87-
return processDocument(DocConfig.TYPE_XLSX);
88-
}
37+
<@fhm.createQuarkusPath context=context outputMime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" outputExtension="xlsx" outputDescription="Excel"/>
8938
</#if>
9039

9140
<#if context.modules?seq_contains("fj-doc-mod-opencsv")>
92-
@APIResponse(responseCode = "200", description = "The CSV document content" )
93-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
94-
@Tags( { @Tag( name = "document" ), @Tag( name = "csv" ) } )
95-
@Operation( operationId = "csvExample", summary = "Generated an example CSV document using Fugerit Venus Doc handler" )
96-
@GET
97-
@Produces("text/csv")
98-
@Path("/example.csv")
99-
public byte[] csvExample() {
100-
return processDocument(DocConfig.TYPE_CSV);
101-
}
41+
<@fhm.createQuarkusPath context=context outputMime="text/csv" outputExtension="csv" outputDescription="CSV"/>
10242
</#if>
10343

10444
}

fj-doc-maven-plugin/src/main/resources/config/template/flavour/quarkus-3/DocResource.ftl

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,82 +23,22 @@ import org.eclipse.microprofile.openapi.annotations.tags.Tags;
2323
@Path("/doc")
2424
public class DocResource {
2525

26-
byte[] processDocument(String handlerId) {
27-
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
28-
// creates the doc helper
29-
DocHelper docHelper = new DocHelper();
30-
// create custom data for the fremarker template 'document.ftl'
31-
List<People> listPeople = Arrays.asList(new People("Luthien", "Tinuviel", "Queen"), new People("Thorin", "Oakshield", "King"));
32-
// output generation
33-
docHelper.getDocProcessConfig().fullProcess("document", DocProcessContext.newContext("listPeople", listPeople), handlerId, baos);
34-
// return the output
35-
return baos.toByteArray();
36-
} catch (Exception e) {
37-
log.error(String.format("Error processing %s, error:%s", handlerId, e), e);
38-
throw new WebApplicationException(e);
39-
}
40-
}
26+
<@fhm.createDocumentProcess context=context exceptionType='WebApplicationException'/>
4127

28+
<@fhm.createQuarkusPath context=context outputMime="text/markdown" outputExtension="md" outputDescription="Markdown"/>
4229

43-
@APIResponse(responseCode = "200", description = "The Markdown document content" )
44-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
45-
@Tags( { @Tag( name = "document" ), @Tag( name = "markdown" ) } )
46-
@Operation( operationId = "markdownExample", summary = "Generated an example Markdown document using Fugerit Venus Doc handler" )
47-
@GET
48-
@Produces("text/markdown")
49-
@Path("/example.md")
50-
public byte[] markdownExample() {
51-
return processDocument(DocConfig.TYPE_MD);
52-
}
53-
54-
@APIResponse(responseCode = "200", description = "The HTML document content" )
55-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
56-
@Tags( { @Tag( name = "document" ), @Tag( name = "html" ) } )
57-
@Operation( operationId = "htmlExample", summary = "Generated an example HTML document using Fugerit Venus Doc handler" )
58-
@GET
59-
@Produces(MediaType.TEXT_HTML)
60-
@Path("/example.html")
61-
public byte[] htmlExample() {
62-
return processDocument(DocConfig.TYPE_HTML);
63-
}
30+
<@fhm.createQuarkusPath context=context outputMime="text/html" outputExtension="html" outputDescription="HTML"/>
6431

6532
<#if context.modules?seq_contains("fj-doc-mod-fop")>
66-
@APIResponse(responseCode = "200", description = "The PDF document content" )
67-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
68-
@Tags( { @Tag( name = "document" ), @Tag( name = "pdf" ) } )
69-
@Operation( operationId = "pdfExample", summary = "Generated an example PDF document using Fugerit Venus Doc handler" )
70-
@GET
71-
@Produces("application/pdf")
72-
@Path("/example.pdf")
73-
public byte[] pdfExample() {
74-
return processDocument(DocConfig.TYPE_PDF);
75-
}
33+
<@fhm.createQuarkusPath context=context outputMime="application/pdf" outputExtension="pdf" outputDescription="PDF"/>
7634
</#if>
7735

7836
<#if context.modules?seq_contains("fj-doc-mod-poi")>
79-
@APIResponse(responseCode = "200", description = "The Excel document content" )
80-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
81-
@Tags( { @Tag( name = "document" ), @Tag( name = "excel" ) } )
82-
@Operation( operationId = "xlsxExample", summary = "Generated an example Excel document using Fugerit Venus Doc handler" )
83-
@GET
84-
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
85-
@Path("/example.xlsx")
86-
public byte[] xlsxExample() {
87-
return processDocument(DocConfig.TYPE_XLSX);
88-
}
37+
<@fhm.createQuarkusPath context=context outputMime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" outputExtension="xlsx" outputDescription="Excel"/>
8938
</#if>
9039

9140
<#if context.modules?seq_contains("fj-doc-mod-opencsv")>
92-
@APIResponse(responseCode = "200", description = "The CSV document content" )
93-
@APIResponse(responseCode = "500", description = "In case of an unexpected error" )
94-
@Tags( { @Tag( name = "document" ), @Tag( name = "csv" ) } )
95-
@Operation( operationId = "csvExample", summary = "Generated an example CSV document using Fugerit Venus Doc handler" )
96-
@GET
97-
@Produces("text/csv")
98-
@Path("/example.csv")
99-
public byte[] csvExample() {
100-
return processDocument(DocConfig.TYPE_CSV);
101-
}
41+
<@fhm.createQuarkusPath context=context outputMime="text/csv" outputExtension="csv" outputDescription="CSV"/>
10242
</#if>
10343

10444
}

0 commit comments

Comments
 (0)