Search in sources :

Example 1 with ZipOutputHandler

use of io.micronaut.starter.io.ZipOutputHandler in project micronaut-starter by micronaut-projects.

the class ZipCreateController method generateAppIntoZipFile.

public HttpResponse<Writable> generateAppIntoZipFile(@NotNull ApplicationType type, @NotNull String name, @Nullable List<String> features, @Nullable BuildTool buildTool, @Nullable TestFramework testFramework, @Nullable Language lang, @Nullable JdkVersion javaVersion, @Nullable String userAgent) {
    GeneratorContext generatorContext = createProjectGeneratorContext(type, name, features, buildTool, testFramework, lang, javaVersion, userAgent);
    MutableHttpResponse<Writable> response = HttpResponse.created(new Writable() {

        @Override
        public void writeTo(OutputStream outputStream, @Nullable Charset charset) throws IOException {
            try {
                projectGenerator.generate(type, generatorContext.getProject(), new ZipOutputHandler(outputStream), generatorContext);
                outputStream.flush();
            } catch (Exception e) {
                LOG.error("Error generating application: " + e.getMessage(), e);
                throw new IOException(e.getMessage(), e);
            }
        }

        @Override
        public void writeTo(Writer out) {
        // no-op, output stream used
        }
    });
    return response.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + getFilename(generatorContext.getProject()));
}
Also used : OutputStream(java.io.OutputStream) Writable(io.micronaut.core.io.Writable) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ZipOutputHandler(io.micronaut.starter.io.ZipOutputHandler) GeneratorContext(io.micronaut.starter.application.generator.GeneratorContext) IOException(java.io.IOException) Writer(java.io.Writer)

Aggregations

Writable (io.micronaut.core.io.Writable)1 GeneratorContext (io.micronaut.starter.application.generator.GeneratorContext)1 ZipOutputHandler (io.micronaut.starter.io.ZipOutputHandler)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Writer (java.io.Writer)1 Charset (java.nio.charset.Charset)1