16
16
17
17
package io .spring .initializr .web .mapper ;
18
18
19
+ import java .util .ArrayList ;
20
+ import java .util .Collection ;
21
+ import java .util .Collections ;
19
22
import java .util .List ;
20
23
import java .util .function .Function ;
21
24
import java .util .stream .Collectors ;
49
52
*
50
53
* @author Stephane Nicoll
51
54
* @author Guillaume Gerbaud
55
+ * @author Moritz Halbritter
52
56
*/
53
57
public class InitializrMetadataV2JsonMapper implements InitializrMetadataJsonMapper {
54
58
55
59
private static final JsonNodeFactory nodeFactory = JsonNodeFactory .instance ;
56
60
57
61
private final TemplateVariables templateVariables ;
58
62
63
+ /**
64
+ * Create a new instance.
65
+ */
59
66
public InitializrMetadataV2JsonMapper () {
60
- this .templateVariables = new TemplateVariables (
61
- new TemplateVariable ("dependencies" , TemplateVariable .VariableType .REQUEST_PARAM ),
62
- new TemplateVariable ("packaging" , TemplateVariable .VariableType .REQUEST_PARAM ),
63
- new TemplateVariable ("javaVersion" , TemplateVariable .VariableType .REQUEST_PARAM ),
64
- new TemplateVariable ("language" , TemplateVariable .VariableType .REQUEST_PARAM ),
65
- new TemplateVariable ("bootVersion" , TemplateVariable .VariableType .REQUEST_PARAM ),
66
- new TemplateVariable ("groupId" , TemplateVariable .VariableType .REQUEST_PARAM ),
67
- new TemplateVariable ("artifactId" , TemplateVariable .VariableType .REQUEST_PARAM ),
68
- new TemplateVariable ("version" , TemplateVariable .VariableType .REQUEST_PARAM ),
69
- new TemplateVariable ("name" , TemplateVariable .VariableType .REQUEST_PARAM ),
70
- new TemplateVariable ("description" , TemplateVariable .VariableType .REQUEST_PARAM ),
71
- new TemplateVariable ("packageName" , TemplateVariable .VariableType .REQUEST_PARAM ));
67
+ this (Collections .emptyList ());
68
+ }
69
+
70
+ /**
71
+ * Create a new instance using the additional template variables.
72
+ * @param additionalTemplateVariables the additional template variables
73
+ */
74
+ public InitializrMetadataV2JsonMapper (Collection <? extends TemplateVariable > additionalTemplateVariables ) {
75
+ List <TemplateVariable > templateVariables = new ArrayList <>();
76
+ templateVariables .add (new TemplateVariable ("dependencies" , TemplateVariable .VariableType .REQUEST_PARAM ));
77
+ templateVariables .add (new TemplateVariable ("packaging" , TemplateVariable .VariableType .REQUEST_PARAM ));
78
+ templateVariables .add (new TemplateVariable ("javaVersion" , TemplateVariable .VariableType .REQUEST_PARAM ));
79
+ templateVariables .add (new TemplateVariable ("language" , TemplateVariable .VariableType .REQUEST_PARAM ));
80
+ templateVariables .add (new TemplateVariable ("bootVersion" , TemplateVariable .VariableType .REQUEST_PARAM ));
81
+ templateVariables .add (new TemplateVariable ("groupId" , TemplateVariable .VariableType .REQUEST_PARAM ));
82
+ templateVariables .add (new TemplateVariable ("artifactId" , TemplateVariable .VariableType .REQUEST_PARAM ));
83
+ templateVariables .add (new TemplateVariable ("version" , TemplateVariable .VariableType .REQUEST_PARAM ));
84
+ templateVariables .add (new TemplateVariable ("name" , TemplateVariable .VariableType .REQUEST_PARAM ));
85
+ templateVariables .add (new TemplateVariable ("description" , TemplateVariable .VariableType .REQUEST_PARAM ));
86
+ templateVariables .add (new TemplateVariable ("packageName" , TemplateVariable .VariableType .REQUEST_PARAM ));
87
+ templateVariables .addAll (additionalTemplateVariables );
88
+ this .templateVariables = new TemplateVariables (templateVariables );
72
89
}
73
90
74
91
protected JsonNodeFactory nodeFactory () {
@@ -77,21 +94,30 @@ protected JsonNodeFactory nodeFactory() {
77
94
78
95
@ Override
79
96
public String write (InitializrMetadata metadata , String appUrl ) {
80
- ObjectNode delegate = nodeFactory .objectNode ();
81
- links (delegate , metadata .getTypes ().getContent (), appUrl );
82
- dependencies (delegate , metadata .getDependencies ());
83
- type (delegate , metadata .getTypes ());
84
- singleSelect (delegate , metadata .getPackagings ());
85
- singleSelect (delegate , metadata .getJavaVersions ());
86
- singleSelect (delegate , metadata .getLanguages ());
87
- singleSelect (delegate , metadata .getBootVersions (), this ::mapVersionMetadata , this ::formatVersion );
88
- text (delegate , metadata .getGroupId ());
89
- text (delegate , metadata .getArtifactId ());
90
- text (delegate , metadata .getVersion ());
91
- text (delegate , metadata .getName ());
92
- text (delegate , metadata .getDescription ());
93
- text (delegate , metadata .getPackageName ());
94
- return delegate .toString ();
97
+ ObjectNode parent = nodeFactory .objectNode ();
98
+ links (parent , metadata .getTypes ().getContent (), appUrl );
99
+ dependencies (parent , metadata .getDependencies ());
100
+ type (parent , metadata .getTypes ());
101
+ singleSelect (parent , metadata .getPackagings ());
102
+ singleSelect (parent , metadata .getJavaVersions ());
103
+ singleSelect (parent , metadata .getLanguages ());
104
+ singleSelect (parent , metadata .getBootVersions (), this ::mapVersionMetadata , this ::formatVersion );
105
+ text (parent , metadata .getGroupId ());
106
+ text (parent , metadata .getArtifactId ());
107
+ text (parent , metadata .getVersion ());
108
+ text (parent , metadata .getName ());
109
+ text (parent , metadata .getDescription ());
110
+ text (parent , metadata .getPackageName ());
111
+ customizeParent (parent , metadata );
112
+ return parent .toString ();
113
+ }
114
+
115
+ /**
116
+ * Customizes the parent.
117
+ * @param parent the parent
118
+ * @param metadata the metadata
119
+ */
120
+ protected void customizeParent (ObjectNode parent , InitializrMetadata metadata ) {
95
121
}
96
122
97
123
protected ObjectNode links (ObjectNode parent , List <Type > types , String appUrl ) {
0 commit comments