1
+ package io .swagger .codegen .languages ;
2
+
3
+ import io .swagger .codegen .*;
4
+ import io .swagger .models .Operation ;
5
+ import io .swagger .models .Path ;
6
+ import io .swagger .models .Swagger ;
7
+ import org .apache .commons .lang .StringUtils ;
8
+
9
+ import java .io .File ;
10
+ import java .util .*;
11
+
12
+ public class JavaResteasyServerCodegen extends JavaClientCodegen implements CodegenConfig {
13
+
14
+ protected String dateLibrary = "default" ;
15
+ protected String title = "Swagger Server" ;
16
+ protected String implFolder = "src/main/java" ;
17
+
18
+ public static final String DATE_LIBRARY = "dateLibrary" ;
19
+
20
+ public JavaResteasyServerCodegen () {
21
+
22
+ super ();
23
+
24
+ sourceFolder = "src/gen/java" ;
25
+ invokerPackage = "io.swagger.api" ;
26
+ artifactId = "swagger-jaxrs-resteasy-server" ;
27
+
28
+ outputFolder = "generated-code/javaJaxRS" ;
29
+ modelTemplateFiles .put ("model.mustache" , ".java" );
30
+ apiTemplateFiles .put ("api.mustache" , ".java" );
31
+ apiTemplateFiles .put ("apiService.mustache" , ".java" );
32
+ apiTemplateFiles .put ("apiServiceImpl.mustache" , ".java" );
33
+ apiTemplateFiles .put ("apiServiceFactory.mustache" , ".java" );
34
+ apiPackage = "io.swagger.api" ;
35
+ modelPackage = "io.swagger.model" ;
36
+
37
+ additionalProperties .put ("title" , title );
38
+
39
+ embeddedTemplateDir = templateDir = "JavaJaxRS" + File .separator + "resteasy" ;
40
+
41
+ for (int i = 0 ; i < cliOptions .size (); i ++) {
42
+ if (CodegenConstants .LIBRARY .equals (cliOptions .get (i ).getOpt ())) {
43
+ cliOptions .remove (i );
44
+ break ;
45
+ }
46
+ }
47
+
48
+ CliOption dateLibrary = new CliOption (DATE_LIBRARY , "Option. Date library to use" );
49
+ Map <String , String > dateOptions = new HashMap <String , String >();
50
+ dateOptions .put ("java8" , "Java 8 native" );
51
+ dateOptions .put ("joda" , "Joda" );
52
+ dateLibrary .setEnum (dateOptions );
53
+
54
+ cliOptions .add (dateLibrary );
55
+
56
+ CliOption library = new CliOption (CodegenConstants .LIBRARY , "library template (sub-template) to use" );
57
+ library .setDefault (DEFAULT_LIBRARY );
58
+
59
+ Map <String , String > supportedLibraries = new LinkedHashMap <String , String >();
60
+
61
+ supportedLibraries .put (DEFAULT_LIBRARY , "Resteasy core 3.0.11" );
62
+ library .setEnum (supportedLibraries );
63
+
64
+ cliOptions .add (library );
65
+ cliOptions .add (new CliOption (CodegenConstants .IMPL_FOLDER , CodegenConstants .IMPL_FOLDER_DESC ));
66
+ }
67
+
68
+ @ Override
69
+ public CodegenType getTag () {
70
+ return CodegenType .SERVER ;
71
+ }
72
+
73
+ @ Override
74
+ public String getName () {
75
+ return "jaxrs-resteasy" ;
76
+ }
77
+
78
+ @ Override
79
+ public String getHelp () {
80
+ return "Generates a Java JAXRS-Resteasy Server application." ;
81
+ }
82
+
83
+ @ Override
84
+ public void processOpts () {
85
+ super .processOpts ();
86
+
87
+ if (additionalProperties .containsKey (CodegenConstants .IMPL_FOLDER )) {
88
+ implFolder = (String ) additionalProperties .get (CodegenConstants .IMPL_FOLDER );
89
+ }
90
+
91
+ supportingFiles .clear ();
92
+ supportingFiles .add (new SupportingFile ("pom.mustache" , "" , "pom.xml" ));
93
+ supportingFiles .add (new SupportingFile ("gradle.mustache" , "" , "build.gradle" ));
94
+ supportingFiles .add (new SupportingFile ("settingsGradle.mustache" , "" , "settings.gradle" ));
95
+ supportingFiles .add (new SupportingFile ("README.mustache" , "" , "README.md" ));
96
+ supportingFiles .add (new SupportingFile ("ApiException.mustache" ,
97
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "ApiException.java" ));
98
+ supportingFiles .add (new SupportingFile ("ApiOriginFilter.mustache" ,
99
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "ApiOriginFilter.java" ));
100
+ supportingFiles .add (new SupportingFile ("ApiResponseMessage.mustache" ,
101
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "ApiResponseMessage.java" ));
102
+ supportingFiles .add (new SupportingFile ("NotFoundException.mustache" ,
103
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "NotFoundException.java" ));
104
+ supportingFiles .add (new SupportingFile ("web.mustache" ,
105
+ ("src/main/webapp/WEB-INF" ), "web.xml" ));
106
+ supportingFiles .add (new SupportingFile ("jboss-web.mustache" ,
107
+ ("src/main/webapp/WEB-INF" ), "jboss-web.xml" ));
108
+ supportingFiles .add (new SupportingFile ("RestApplication.mustache" ,
109
+ (sourceFolder + '/' + invokerPackage ).replace ("." , "/" ), "RestApplication.java" ));
110
+ supportingFiles .add (new SupportingFile ("StringUtil.mustache" ,
111
+ (sourceFolder + '/' + invokerPackage ).replace ("." , "/" ), "StringUtil.java" ));
112
+
113
+ if (additionalProperties .containsKey ("dateLibrary" )) {
114
+ setDateLibrary (additionalProperties .get ("dateLibrary" ).toString ());
115
+ additionalProperties .put (dateLibrary , "true" );
116
+ }
117
+
118
+ if ("joda" .equals (dateLibrary )) {
119
+ typeMapping .put ("date" , "LocalDate" );
120
+ typeMapping .put ("DateTime" , "DateTime" );
121
+
122
+ importMapping .put ("LocalDate" , "org.joda.time.LocalDate" );
123
+ importMapping .put ("DateTime" , "org.joda.time.DateTime" );
124
+
125
+ supportingFiles .add (new SupportingFile ("JacksonConfig.mustache" ,
126
+ (sourceFolder + '/' + invokerPackage ).replace ("." , "/" ), "JacksonConfig.java" ));
127
+
128
+ supportingFiles .add (new SupportingFile ("JodaDateTimeProvider.mustache" ,
129
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "JodaDateTimeProvider.java" ));
130
+ supportingFiles .add (new SupportingFile ("JodaLocalDateProvider.mustache" ,
131
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "JodaLocalDateProvider.java" ));
132
+ } else if ("java8" .equals (dateLibrary )) {
133
+ additionalProperties .put ("java8" , "true" );
134
+ additionalProperties .put ("javaVersion" , "1.8" );
135
+ typeMapping .put ("date" , "LocalDate" );
136
+ typeMapping .put ("DateTime" , "LocalDateTime" );
137
+ importMapping .put ("LocalDate" , "java.time.LocalDate" );
138
+ importMapping .put ("LocalDateTime" , "java.time.LocalDateTime" );
139
+
140
+ supportingFiles .add (new SupportingFile ("LocalDateTimeProvider.mustache" ,
141
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "LocalDateTimeProvider.java" ));
142
+ supportingFiles .add (new SupportingFile ("LocalDateProvider.mustache" ,
143
+ (sourceFolder + '/' + apiPackage ).replace ("." , "/" ), "LocalDateProvider.java" ));
144
+ }
145
+ }
146
+
147
+ @ Override
148
+ public void addOperationToGroup (String tag , String resourcePath , Operation operation , CodegenOperation co , Map <String , List <CodegenOperation >> operations ) {
149
+ String basePath = resourcePath ;
150
+ if (basePath .startsWith ("/" )) {
151
+ basePath = basePath .substring (1 );
152
+ }
153
+ int pos = basePath .indexOf ("/" );
154
+ if (pos > 0 ) {
155
+ basePath = basePath .substring (0 , pos );
156
+ }
157
+
158
+ if (basePath == "" ) {
159
+ basePath = "default" ;
160
+ } else {
161
+ if (co .path .startsWith ("/" + basePath )) {
162
+ co .path = co .path .substring (("/" + basePath ).length ());
163
+ }
164
+ co .subresourceOperation = !co .path .isEmpty ();
165
+ }
166
+ List <CodegenOperation > opList = operations .get (basePath );
167
+ if (opList == null ) {
168
+ opList = new ArrayList <CodegenOperation >();
169
+ operations .put (basePath , opList );
170
+ }
171
+ opList .add (co );
172
+ co .baseName = basePath ;
173
+ }
174
+
175
+
176
+ @ Override
177
+ public void preprocessSwagger (Swagger swagger ) {
178
+ if ("/" .equals (swagger .getBasePath ())) {
179
+ swagger .setBasePath ("" );
180
+ }
181
+
182
+ String host = swagger .getHost ();
183
+ String port = "8080" ;
184
+ if (host != null ) {
185
+ String [] parts = host .split (":" );
186
+ if (parts .length > 1 ) {
187
+ port = parts [1 ];
188
+ }
189
+ }
190
+ this .additionalProperties .put ("serverPort" , port );
191
+ if (swagger != null && swagger .getPaths () != null ) {
192
+ for (String pathname : swagger .getPaths ().keySet ()) {
193
+ Path path = swagger .getPath (pathname );
194
+ if (path .getOperations () != null ) {
195
+ for (Operation operation : path .getOperations ()) {
196
+ if (operation .getTags () != null ) {
197
+ List <Map <String , String >> tags = new ArrayList <Map <String , String >>();
198
+ for (String tag : operation .getTags ()) {
199
+ Map <String , String > value = new HashMap <String , String >();
200
+ value .put ("tag" , tag );
201
+ value .put ("hasMore" , "true" );
202
+ tags .add (value );
203
+ }
204
+ if (tags .size () > 0 ) {
205
+ tags .get (tags .size () - 1 ).remove ("hasMore" );
206
+ }
207
+ if (operation .getTags ().size () > 0 ) {
208
+ String tag = operation .getTags ().get (0 );
209
+ operation .setTags (Arrays .asList (tag ));
210
+ }
211
+ operation .setVendorExtension ("x-tags" , tags );
212
+ }
213
+ }
214
+ }
215
+ }
216
+ }
217
+ }
218
+
219
+
220
+ @ Override
221
+ public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
222
+
223
+ Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
224
+ if (operations != null ) {
225
+ List <CodegenOperation > ops = (List <CodegenOperation >) operations .get ("operation" );
226
+ for (CodegenOperation operation : ops ) {
227
+ if (operation .hasConsumes == Boolean .TRUE ) {
228
+ Map <String , String > firstType = operation .consumes .get (0 );
229
+ if (firstType != null ) {
230
+ if ("multipart/form-data" .equals (firstType .get ("mediaType" ))) {
231
+ operation .isMultipart = Boolean .TRUE ;
232
+ }
233
+ }
234
+ }
235
+ List <CodegenResponse > responses = operation .responses ;
236
+ if (responses != null ) {
237
+ for (CodegenResponse resp : responses ) {
238
+ if ("0" .equals (resp .code )) {
239
+ resp .code = "200" ;
240
+ }
241
+ }
242
+ }
243
+ if (operation .returnType == null ) {
244
+ operation .returnType = "Void" ;
245
+ } else if (operation .returnType .startsWith ("List" )) {
246
+ String rt = operation .returnType ;
247
+ int end = rt .lastIndexOf (">" );
248
+ if (end > 0 ) {
249
+ operation .returnType = rt .substring ("List<" .length (), end ).trim ();
250
+ operation .returnContainer = "List" ;
251
+ }
252
+ } else if (operation .returnType .startsWith ("Map" )) {
253
+ String rt = operation .returnType ;
254
+ int end = rt .lastIndexOf (">" );
255
+ if (end > 0 ) {
256
+ operation .returnType = rt .substring ("Map<" .length (), end ).split ("," )[1 ].trim ();
257
+ operation .returnContainer = "Map" ;
258
+ }
259
+ } else if (operation .returnType .startsWith ("Set" )) {
260
+ String rt = operation .returnType ;
261
+ int end = rt .lastIndexOf (">" );
262
+ if (end > 0 ) {
263
+ operation .returnType = rt .substring ("Set<" .length (), end ).trim ();
264
+ operation .returnContainer = "Set" ;
265
+ }
266
+ }
267
+ }
268
+ }
269
+ return objs ;
270
+ }
271
+
272
+ @ Override
273
+ public String toApiName (String name ) {
274
+ if (name .length () == 0 ) {
275
+ return "DefaultApi" ;
276
+ }
277
+ name = sanitizeName (name );
278
+ return camelize (name ) + "Api" ;
279
+ }
280
+
281
+
282
+ @ Override
283
+ public String apiFilename (String templateName , String tag ) {
284
+
285
+ String result = super .apiFilename (templateName , tag );
286
+
287
+ if (templateName .endsWith ("Impl.mustache" )) {
288
+ int ix = result .lastIndexOf ('/' );
289
+ result = result .substring (0 , ix ) + "/impl" + result .substring (ix , result .length () - 5 ) + "ServiceImpl.java" ;
290
+
291
+ result = result .replace (apiFileFolder (), implFileFolder (implFolder ));
292
+ } else if (templateName .endsWith ("Factory.mustache" )) {
293
+ int ix = result .lastIndexOf ('/' );
294
+ result = result .substring (0 , ix ) + "/factories" + result .substring (ix , result .length () - 5 ) + "ServiceFactory.java" ;
295
+
296
+ result = result .replace (apiFileFolder (), implFileFolder (implFolder ));
297
+ } else if (templateName .endsWith ("Service.mustache" )) {
298
+ int ix = result .lastIndexOf ('.' );
299
+ result = result .substring (0 , ix ) + "Service.java" ;
300
+ }
301
+
302
+ return result ;
303
+ }
304
+
305
+
306
+ private String implFileFolder (String output ) {
307
+ return outputFolder + "/" + output + "/" + apiPackage ().replace ('.' , '/' );
308
+ }
309
+
310
+ @ Override
311
+ public boolean shouldOverwrite (String filename ) {
312
+ return super .shouldOverwrite (filename ) && !filename .endsWith ("ServiceImpl.java" ) && !filename .endsWith ("ServiceFactory.java" );
313
+ }
314
+
315
+ public void setDateLibrary (String library ) {
316
+ this .dateLibrary = library ;
317
+ }
318
+
319
+ @ Override
320
+ public void postProcessModelProperty (CodegenModel model , CodegenProperty property ) {
321
+ if (serializeBigDecimalAsString ) {
322
+ if (property .baseType .equals ("BigDecimal" )) {
323
+ // we serialize BigDecimal as `string` to avoid precision loss
324
+ property .vendorExtensions .put ("extraAnnotation" , "@JsonSerialize(using = ToStringSerializer.class)" );
325
+
326
+ // this requires some more imports to be added for this model...
327
+ model .imports .add ("ToStringSerializer" );
328
+ model .imports .add ("JsonSerialize" );
329
+ }
330
+ }
331
+
332
+ if (model .isEnum == null || model .isEnum ) {
333
+
334
+ final String lib = getLibrary ();
335
+ if (StringUtils .isEmpty (lib )) {
336
+ model .imports .add ("JsonProperty" );
337
+
338
+ if (model .hasEnums != null || model .hasEnums == true ) {
339
+ model .imports .add ("JsonValue" );
340
+ }
341
+ }
342
+ }
343
+ return ;
344
+ }
345
+ }
0 commit comments