|
| 1 | +package io.swagger.codegen.languages; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +import org.apache.commons.lang3.BooleanUtils; |
| 10 | + |
| 11 | +import io.swagger.codegen.CliOption; |
| 12 | +import io.swagger.codegen.CodegenModel; |
| 13 | +import io.swagger.codegen.CodegenOperation; |
| 14 | +import io.swagger.codegen.CodegenProperty; |
| 15 | +import io.swagger.codegen.CodegenResponse; |
| 16 | +import io.swagger.codegen.SupportingFile; |
| 17 | +import io.swagger.codegen.languages.features.BeanValidationFeatures; |
| 18 | +import io.swagger.codegen.languages.features.JbossFeature; |
| 19 | +import io.swagger.codegen.languages.features.SwaggerFeatures; |
| 20 | +import io.swagger.models.Operation; |
| 21 | + |
| 22 | +public class JavaResteasyEapServerCodegen extends AbstractJavaJAXRSServerCodegen |
| 23 | + implements JbossFeature, BeanValidationFeatures, SwaggerFeatures { |
| 24 | + |
| 25 | + protected boolean useBeanValidation = true; |
| 26 | + protected boolean generateJbossDeploymentDescriptor = true; |
| 27 | + protected boolean useSwaggerFeature = false; |
| 28 | + |
| 29 | + public JavaResteasyEapServerCodegen() { |
| 30 | + |
| 31 | + super(); |
| 32 | + |
| 33 | + artifactId = "swagger-jaxrs-resteasy-eap-server"; |
| 34 | + |
| 35 | + outputFolder = "generated-code/JavaJaxRS-Resteasy-eap"; |
| 36 | + apiTemplateFiles.put("apiServiceImpl.mustache", ".java"); |
| 37 | + apiTestTemplateFiles.clear(); // TODO: add test template |
| 38 | + |
| 39 | + // clear model and api doc template as AbstractJavaJAXRSServerCodegen |
| 40 | + // does not support auto-generated markdown doc at the moment |
| 41 | + //TODO: add doc templates |
| 42 | + modelDocTemplateFiles.remove("model_doc.mustache"); |
| 43 | + apiDocTemplateFiles.remove("api_doc.mustache"); |
| 44 | + |
| 45 | + dateLibrary = "legacy";// TODO: change to joda |
| 46 | + |
| 47 | + embeddedTemplateDir = templateDir = "JavaJaxRS" + File.separator + "resteasy" + File.separator + "eap"; |
| 48 | + |
| 49 | + cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); |
| 50 | + cliOptions.add(CliOption.newBoolean(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR, "Generate Jboss Deployment Descriptor")); |
| 51 | + cliOptions.add(CliOption.newBoolean(USE_SWAGGER_FEATURE, "Use dynamic Swagger generator")); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getName() { |
| 57 | + return "jaxrs-resteasy-eap"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getHelp() { |
| 62 | + return "Generates a Java JAXRS-Resteasy Server application."; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void processOpts() { |
| 67 | + super.processOpts(); |
| 68 | + |
| 69 | + if (additionalProperties.containsKey(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR)) { |
| 70 | + boolean generateJbossDeploymentDescriptorProp = convertPropertyToBooleanAndWriteBack(GENERATE_JBOSS_DEPLOYMENT_DESCRIPTOR); |
| 71 | + this.setGenerateJbossDeploymentDescriptor(generateJbossDeploymentDescriptorProp); |
| 72 | + } |
| 73 | + |
| 74 | + if (additionalProperties.containsKey(USE_BEANVALIDATION)) { |
| 75 | + this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION)); |
| 76 | + } |
| 77 | + |
| 78 | + if (useBeanValidation) { |
| 79 | + writePropertyBack(USE_BEANVALIDATION, useBeanValidation); |
| 80 | + } |
| 81 | + |
| 82 | + if (additionalProperties.containsKey(USE_SWAGGER_FEATURE)) { |
| 83 | + this.setUseSwaggerFeature(convertPropertyToBoolean(USE_SWAGGER_FEATURE)); |
| 84 | + } |
| 85 | + |
| 86 | + if (useSwaggerFeature) { |
| 87 | + writePropertyBack(USE_SWAGGER_FEATURE, useSwaggerFeature); |
| 88 | + } |
| 89 | + |
| 90 | + writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml")); |
| 91 | + writeOptional(outputFolder, new SupportingFile("gradle.mustache", "", "build.gradle")); |
| 92 | + writeOptional(outputFolder, new SupportingFile("settingsGradle.mustache", "", "settings.gradle")); |
| 93 | + writeOptional(outputFolder, new SupportingFile("README.mustache", "", "README.md")); |
| 94 | + writeOptional(outputFolder, new SupportingFile("web.mustache", ("src/main/webapp/WEB-INF"), "web.xml")); |
| 95 | + |
| 96 | + supportingFiles.add(new SupportingFile("JacksonConfig.mustache", (projectFolder + File.separator + "java" + '/' + invokerPackage).replace(".", "/"), "JacksonConfig.java")); |
| 97 | + |
| 98 | + if (generateJbossDeploymentDescriptor) { |
| 99 | + writeOptional(outputFolder, new SupportingFile("jboss-web.mustache", ("src/main/webapp/WEB-INF"), "jboss-web.xml")); |
| 100 | + } |
| 101 | + |
| 102 | + writeOptional(outputFolder, new SupportingFile("RestApplication.mustache", (projectFolder + File.separator + "java" + '/' + invokerPackage).replace(".", "/"), "RestApplication.java")); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) { |
| 108 | + String basePath = resourcePath; |
| 109 | + if (basePath.startsWith("/")) { |
| 110 | + basePath = basePath.substring(1); |
| 111 | + } |
| 112 | + int pos = basePath.indexOf("/"); |
| 113 | + if (pos > 0) { |
| 114 | + basePath = basePath.substring(0, pos); |
| 115 | + } |
| 116 | + |
| 117 | + if (basePath == "") { |
| 118 | + basePath = "default"; |
| 119 | + } else { |
| 120 | + if (co.path.startsWith("/" + basePath)) { |
| 121 | + co.path = co.path.substring(("/" + basePath).length()); |
| 122 | + } |
| 123 | + co.subresourceOperation = !co.path.isEmpty(); |
| 124 | + } |
| 125 | + List<CodegenOperation> opList = operations.get(basePath); |
| 126 | + if (opList == null) { |
| 127 | + opList = new ArrayList<CodegenOperation>(); |
| 128 | + operations.put(basePath, opList); |
| 129 | + } |
| 130 | + opList.add(co); |
| 131 | + co.baseName = basePath; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Map<String, Object> postProcessOperations(Map<String, Object> objs) { |
| 136 | + |
| 137 | + Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); |
| 138 | + if (operations != null) { |
| 139 | + List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); |
| 140 | + for (CodegenOperation operation : ops) { |
| 141 | + if (operation.hasConsumes == Boolean.TRUE) { |
| 142 | + Map<String, String> firstType = operation.consumes.get(0); |
| 143 | + if (firstType != null) { |
| 144 | + if ("multipart/form-data".equals(firstType.get("mediaType"))) { |
| 145 | + operation.isMultipart = Boolean.TRUE; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + List<CodegenResponse> responses = operation.responses; |
| 150 | + if (responses != null) { |
| 151 | + for (CodegenResponse resp : responses) { |
| 152 | + if ("0".equals(resp.code)) { |
| 153 | + resp.code = "200"; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + if (operation.returnType == null) { |
| 158 | + operation.returnType = "Void"; |
| 159 | + } else if (operation.returnType.startsWith("List")) { |
| 160 | + String rt = operation.returnType; |
| 161 | + int end = rt.lastIndexOf(">"); |
| 162 | + if (end > 0) { |
| 163 | + operation.returnType = rt.substring("List<".length(), end).trim(); |
| 164 | + operation.returnContainer = "List"; |
| 165 | + } |
| 166 | + } else if (operation.returnType.startsWith("Map")) { |
| 167 | + String rt = operation.returnType; |
| 168 | + int end = rt.lastIndexOf(">"); |
| 169 | + if (end > 0) { |
| 170 | + operation.returnType = rt.substring("Map<".length(), end).split(",")[1].trim(); |
| 171 | + operation.returnContainer = "Map"; |
| 172 | + } |
| 173 | + } else if (operation.returnType.startsWith("Set")) { |
| 174 | + String rt = operation.returnType; |
| 175 | + int end = rt.lastIndexOf(">"); |
| 176 | + if (end > 0) { |
| 177 | + operation.returnType = rt.substring("Set<".length(), end).trim(); |
| 178 | + operation.returnContainer = "Set"; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + return objs; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { |
| 188 | + // Add imports for Jackson |
| 189 | + if (!BooleanUtils.toBoolean(model.isEnum)) { |
| 190 | + model.imports.add("JsonProperty"); |
| 191 | + |
| 192 | + if (BooleanUtils.toBoolean(model.hasEnums)) { |
| 193 | + model.imports.add("JsonValue"); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public Map<String, Object> postProcessModelsEnum(Map<String, Object> objs) { |
| 200 | + objs = super.postProcessModelsEnum(objs); |
| 201 | + |
| 202 | + // Add imports for Jackson |
| 203 | + List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports"); |
| 204 | + List<Object> models = (List<Object>) objs.get("models"); |
| 205 | + for (Object _mo : models) { |
| 206 | + Map<String, Object> mo = (Map<String, Object>) _mo; |
| 207 | + CodegenModel cm = (CodegenModel) mo.get("model"); |
| 208 | + // for enum model |
| 209 | + if (Boolean.TRUE.equals(cm.isEnum) && cm.allowableValues != null) { |
| 210 | + cm.imports.add(importMapping.get("JsonValue")); |
| 211 | + Map<String, String> item = new HashMap<String, String>(); |
| 212 | + item.put("import", importMapping.get("JsonValue")); |
| 213 | + imports.add(item); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return objs; |
| 218 | + } |
| 219 | + |
| 220 | + public void setUseBeanValidation(boolean useBeanValidation) { |
| 221 | + this.useBeanValidation = useBeanValidation; |
| 222 | + } |
| 223 | + |
| 224 | + public void setGenerateJbossDeploymentDescriptor(boolean generateJbossDeploymentDescriptor) { |
| 225 | + this.generateJbossDeploymentDescriptor = generateJbossDeploymentDescriptor; |
| 226 | + } |
| 227 | + |
| 228 | + public void setUseSwaggerFeature(boolean useSwaggerFeature) { |
| 229 | + this.useSwaggerFeature = useSwaggerFeature; |
| 230 | + } |
| 231 | +} |
0 commit comments