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()));
}
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);
}
});
}
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);
}
});
}
}
Aggregations