|
11 | 11 |
|
12 | 12 | import com.fasterxml.jackson.databind.JsonNode;
|
13 | 13 |
|
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
14 | 17 | import java.io.File;
|
15 | 18 | import java.util.List;
|
16 | 19 | import java.util.ArrayList;
|
17 | 20 |
|
18 | 21 | public class Generator {
|
| 22 | + static Logger LOGGER = LoggerFactory.getLogger(Generator.class); |
| 23 | + |
19 | 24 | public static String generateClient(String language, GeneratorInput opts) throws ApiException {
|
| 25 | + LOGGER.debug("generate client for " + language); |
20 | 26 | if(opts == null) {
|
21 | 27 | throw new BadRequestException(400, "No options were supplied");
|
22 | 28 | }
|
@@ -67,8 +73,56 @@ public static String generateClient(String language, GeneratorInput opts) throws
|
67 | 73 | return outputFilename;
|
68 | 74 | }
|
69 | 75 |
|
70 |
| - public static String generateServer(String language, GeneratorInput opts) { |
71 |
| - return ""; |
| 76 | + public static String generateServer(String language, GeneratorInput opts) throws ApiException { |
| 77 | + LOGGER.debug("generate server for " + language); |
| 78 | + if(opts == null) { |
| 79 | + throw new BadRequestException(400, "No options were supplied"); |
| 80 | + } |
| 81 | + JsonNode node = opts.getSpec(); |
| 82 | + if(node == null) { |
| 83 | + throw new BadRequestException(400, "No swagger specification was supplied"); |
| 84 | + } |
| 85 | + Swagger swagger = new SwaggerParser().read(node); |
| 86 | + if(swagger == null) { |
| 87 | + throw new BadRequestException(400, "The swagger specification supplied was not valid"); |
| 88 | + } |
| 89 | + |
| 90 | + ClientOptInput clientOptInput = new ClientOptInput(); |
| 91 | + ClientOpts clientOpts = new ClientOpts(); |
| 92 | + String outputFolder = getTmpFolder().getAbsolutePath() + File.separator + language + "-server"; |
| 93 | + String outputFilename = outputFolder + "-bundle.zip"; |
| 94 | + |
| 95 | + clientOptInput |
| 96 | + .opts(clientOpts) |
| 97 | + .swagger(swagger); |
| 98 | + |
| 99 | + CodegenConfig codegenConfig = Codegen.getConfig(language); |
| 100 | + if(codegenConfig == null) { |
| 101 | + throw new BadRequestException(400, "Unsupported target " + language + " supplied"); |
| 102 | + } |
| 103 | + |
| 104 | + codegenConfig.setOutputDir(outputFolder); |
| 105 | + |
| 106 | + Json.prettyPrint(clientOpts); |
| 107 | + |
| 108 | + clientOptInput.setConfig(codegenConfig); |
| 109 | + |
| 110 | + try{ |
| 111 | + List<File> files = new Codegen().opts(clientOptInput).generate(); |
| 112 | + if(files.size() > 0) { |
| 113 | + List<File> filesToAdd = new ArrayList<File>(); |
| 114 | + filesToAdd.add(new File(outputFolder)); |
| 115 | + ZipUtil zip = new ZipUtil(); |
| 116 | + zip.compressFiles(filesToAdd, outputFilename); |
| 117 | + } |
| 118 | + else { |
| 119 | + throw new BadRequestException(400, "A target generation was attempted, but no files were created!"); |
| 120 | + } |
| 121 | + } |
| 122 | + catch (Exception e) { |
| 123 | + throw new BadRequestException(500, "Unable to build target: " + e.getMessage()); |
| 124 | + } |
| 125 | + return outputFilename; |
72 | 126 | }
|
73 | 127 |
|
74 | 128 | public static InputOption clientOptions(String language) {
|
|
0 commit comments