Search in sources :

Example 1 with ViewNotFoundException

use of io.micronaut.views.exceptions.ViewNotFoundException 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)

Aggregations

Writable (io.micronaut.core.io.Writable)1 MediaType (io.micronaut.http.MediaType)1 ViewNotFoundException (io.micronaut.views.exceptions.ViewNotFoundException)1