use of com.networknt.codegen.Generator in project light-codegen by networknt.
the class GeneratorServiceHandler method handle.
@Override
public ByteBuffer handle(HttpServerExchange exchange, Object input) {
logger.entry(input);
// generate a destination folder name.
String output = HashUtil.generateUUID();
String zipFile = output + ".zip";
String projectFolder = codegenWebConfig.getTmpFolder() + separator + output;
List<Map<String, Object>> generators = (List<Map<String, Object>>) input;
if (generators == null || generators.size() == 0) {
logger.error("Did not receive any generators in the request.");
Status status = new Status(STATUS_MISSING_GENERATOR_ITEM);
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
return NioUtils.toByteBuffer(status.toString());
}
for (Map<String, Object> generatorMap : generators) {
String framework = (String) generatorMap.get("framework");
// should be a JSON of spec or IDL
Object model = Any.wrap(generatorMap.get("model"));
Map<String, Object> config = (Map<String, Object>) generatorMap.get("config");
if (!FrameworkRegistry.getInstance().getFrameworks().contains(framework)) {
Status status = new Status(STATUS_INVALID_FRAMEWORK, framework);
return NioUtils.toByteBuffer(status.toString());
}
// TODO validate the model and config with json schema
try {
Generator generator = FrameworkRegistry.getInstance().getGenerator(framework);
generator.generate(projectFolder, model, Any.wrap(config));
} catch (Exception e) {
e.printStackTrace();
logger.error("Exception:", e);
}
}
try {
// TODO generated code is in tmp folder, zip and move to the target folder
NioUtils.create(codegenWebConfig.getZipFolder() + separator + zipFile, projectFolder);
// delete the project folder.
Files.walk(Paths.get(projectFolder), FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()).map(Path::toFile).peek(System.out::println).forEach(File::delete);
// check if any zip file that needs to be deleted from zipFolder
NioUtils.deleteOldFiles(codegenWebConfig.getZipFolder(), codegenWebConfig.getZipKeptMinute());
} catch (Exception e) {
e.printStackTrace();
logger.error("Exception:", e);
}
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/zip").add(new HttpString("Content-Disposition"), "attachment");
// return the zip file
File file = new File(codegenWebConfig.getZipFolder() + separator + zipFile);
return NioUtils.toByteBuffer(file);
}
use of com.networknt.codegen.Generator in project light-codegen by networknt.
the class SchemaGetHandler method handle.
@Override
public ByteBuffer handle(HttpServerExchange exchange, Object input) {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
logger.entry(input);
Map<String, Object> map = (Map<String, Object>) input;
String framework = (String) map.get("framework");
Generator generator = FrameworkRegistry.getInstance().getGenerator(framework);
ByteBuffer bf = null;
try {
bf = generator.getConfigSchema();
} catch (IOException e) {
// return empty back in this case.
}
return bf;
}
Aggregations