Search in sources :

Example 1 with Writable

use of io.micronaut.core.io.Writable 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)

Example 2 with Writable

use of io.micronaut.core.io.Writable in project micronaut-views by micronaut-projects.

the class ViewsFilter method doFilter.

@Override
public final Publisher<MutableHttpResponse<?>> doFilter(HttpRequest<?> request, ServerFilterChain chain) {
    return Flux.from(chain.proceed(request)).switchMap(response -> {
        Optional<String> optionalView = viewsResolver.resolveView(request, response);
        if (!optionalView.isPresent()) {
            LOG.debug("no view found");
            return Flux.just(response);
        }
        Object body = response.body();
        MediaType type = resolveMediaType(response, body);
        String view = optionalView.get();
        try {
            Optional<ViewsRenderer> optionalViewsRenderer = viewsRendererLocator.resolveViewsRenderer(view, type, body);
            if (!optionalViewsRenderer.isPresent()) {
                LOG.debug("no view renderer found for media type: {}, ignoring", type.toString());
                return Flux.just(response);
            }
            ModelAndView<?> modelAndView = new ModelAndView<>(view, body instanceof ModelAndView ? ((ModelAndView<?>) body).getModel().orElse(null) : body);
            viewsModelDecorator.decorate(request, modelAndView);
            Writable writable = optionalViewsRenderer.get().render(view, modelAndView.getModel().orElse(null), request);
            response.contentType(type);
            response.body(writable);
            return Flux.just(response);
        } catch (ViewNotFoundException e) {
            return Flux.error(e);
        }
    });
}
Also used : MediaType(io.micronaut.http.MediaType) Writable(io.micronaut.core.io.Writable) ViewNotFoundException(io.micronaut.views.exceptions.ViewNotFoundException)

Example 3 with Writable

use of io.micronaut.core.io.Writable in project micronaut-starter by micronaut-projects.

the class ApplicationController method home.

/**
 * Provides a description of the API.
 * @param request The request
 * @return A description of the API.
 */
@Get("/")
@Produces(MediaType.TEXT_PLAIN)
@ApiResponse(responseCode = "200", description = "A textual description of the API", content = @Content(mediaType = MediaType.TEXT_PLAIN))
HttpResponse<Writable> home(HttpRequest<?> request, @Parameter(hidden = true) RequestInfo info) {
    Collection<MediaType> accept = request.accept();
    URI redirectURI = configuration.getRedirectUri().orElse(null);
    if (accept.contains(MediaType.TEXT_HTML_TYPE) && redirectURI != null) {
        return HttpResponse.permanentRedirect(redirectURI);
    } else {
        return HttpResponse.ok(new Writable() {

            @Override
            public void writeTo(Writer out) {
            // no-op
            }

            @Override
            public void writeTo(OutputStream outputStream, @Nullable Charset charset) {
                new RockerWritable(new starterApi().serverURL(info.getServerURL()).micronautVersion(VersionInfo.getMicronautVersion())).write(outputStream);
            }
        });
    }
}
Also used : RockerWritable(io.micronaut.starter.template.RockerWritable) io.micronaut.starter.template.api.starterApi(io.micronaut.starter.template.api.starterApi) OutputStream(java.io.OutputStream) MediaType(io.micronaut.http.MediaType) RockerWritable(io.micronaut.starter.template.RockerWritable) Writable(io.micronaut.core.io.Writable) Charset(java.nio.charset.Charset) URI(java.net.URI) Writer(java.io.Writer) Produces(io.micronaut.http.annotation.Produces) Get(io.micronaut.http.annotation.Get) ApiResponse(io.swagger.v3.oas.annotations.responses.ApiResponse)

Aggregations

Writable (io.micronaut.core.io.Writable)3 MediaType (io.micronaut.http.MediaType)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 Charset (java.nio.charset.Charset)2 Get (io.micronaut.http.annotation.Get)1 Produces (io.micronaut.http.annotation.Produces)1 GeneratorContext (io.micronaut.starter.application.generator.GeneratorContext)1 ZipOutputHandler (io.micronaut.starter.io.ZipOutputHandler)1 RockerWritable (io.micronaut.starter.template.RockerWritable)1 io.micronaut.starter.template.api.starterApi (io.micronaut.starter.template.api.starterApi)1 ViewNotFoundException (io.micronaut.views.exceptions.ViewNotFoundException)1 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)1 IOException (java.io.IOException)1 URI (java.net.URI)1