Search in sources :

Example 1 with Generator

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);
}
Also used : Status(com.networknt.status.Status) Path(java.nio.file.Path) HttpString(io.undertow.util.HttpString) InvalidParameterException(java.security.InvalidParameterException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) HttpString(io.undertow.util.HttpString) Generator(com.networknt.codegen.Generator)

Example 2 with Generator

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;
}
Also used : HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) Map(java.util.Map) ByteBuffer(java.nio.ByteBuffer) HttpString(io.undertow.util.HttpString) Generator(com.networknt.codegen.Generator)

Aggregations

Generator (com.networknt.codegen.Generator)2 HttpString (io.undertow.util.HttpString)2 IOException (java.io.IOException)2 Map (java.util.Map)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Status (com.networknt.status.Status)1 File (java.io.File)1 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 InvalidParameterException (java.security.InvalidParameterException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1