<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>SpringMvcSiteView.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">NorthernWind :: Frontend :: Spring MVC</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.frontend.ui.springmvc</a> > <span class="el_source">SpringMvcSiteView.java</span></div><h1>SpringMvcSiteView.java</h1><pre class="source lang-java linenums">/*
* #%L
* *********************************************************************************************************************
*
* NorthernWind - lightweight CMS
* http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
* %%
* Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
* %%
* *********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* *********************************************************************************************************************
*
*
* *********************************************************************************************************************
* #L%
*/
package it.tidalwave.northernwind.frontend.ui.springmvc;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import it.tidalwave.northernwind.core.model.HttpStatusException;
import it.tidalwave.northernwind.core.model.Request;
import it.tidalwave.northernwind.core.model.RequestContext;
import it.tidalwave.northernwind.core.model.SiteNode;
import it.tidalwave.northernwind.frontend.springmvc.SpringMvcResponseHolder;
import it.tidalwave.northernwind.frontend.ui.Layout;
import it.tidalwave.northernwind.frontend.ui.RenderContext;
import it.tidalwave.northernwind.frontend.ui.SiteView;
import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.HtmlHolder;
import it.tidalwave.northernwind.frontend.ui.component.htmltemplate.TextHolder;
import it.tidalwave.northernwind.frontend.ui.spi.DefaultRenderContext;
import it.tidalwave.northernwind.frontend.ui.spi.NodeViewRenderer;
import it.tidalwave.northernwind.frontend.ui.spi.ViewAndControllerLayoutBuilder;
import lombok.extern.slf4j.Slf4j;
import static java.nio.charset.StandardCharsets.UTF_8;
/***********************************************************************************************************************
*
* The Spring MVC implementation of {@link SiteView}.
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L57">@Configurable @Scope("session") @Slf4j</span>
<span class="nc bnc" id="L58" title="All 18 branches missed.">public class SpringMvcSiteView implements SiteView</span>
{
@Inject
private RequestContext requestContext;
@Inject
private SpringMvcResponseHolder responseHolder;
<span class="nc bnc" id="L66" title="All 4 branches missed."> private final ThreadLocal<HttpStatus> httpStatus = new ThreadLocal<>();</span>
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
@Override
public void renderSiteNode (@Nonnull final Request request, @Nonnull final SiteNode siteNode)
{
<span class="nc" id="L76"> log.info("renderSiteNode({})", siteNode);</span>
<span class="nc" id="L77"> httpStatus.set(HttpStatus.OK);</span>
<span class="nc" id="L78"> final RenderContext renderContext = new DefaultRenderContext(request, requestContext);</span>
<span class="nc" id="L79"> final var vacBuilder = new ViewAndControllerLayoutBuilder(siteNode, renderContext, this::createErrorView);</span>
<span class="nc" id="L80"> siteNode.getLayout().accept(vacBuilder);</span>
<span class="nc" id="L81"> final var renderer = new NodeViewRenderer<>(request, requestContext, vacBuilder, SpringMvcSiteView::attach);</span>
<span class="nc" id="L82"> siteNode.getLayout().accept(renderer);</span>
<span class="nc" id="L83"> final var textHolder = renderer.getRootComponent();</span>
<span class="nc" id="L84"> responseHolder.response().withStatus(httpStatus.get().value())</span>
<span class="nc" id="L85"> .withBody(textHolder.asBytes(UTF_8))</span>
<span class="nc" id="L86"> .withContentType(textHolder.getMimeType())</span>
<span class="nc" id="L87"> .put();</span>
<span class="nc" id="L88"> }</span>
/*******************************************************************************************************************
*
* TODO: sometimes this gets wrapped in a layout (e.g. when it's thrown by a Node Controller), otherwise it just
sometimes this gets wrapped in a layout (e.g. when it's thrown by a Node Controller), otherwise it just
* renders as bare markup. If there is no container, it should be wrapped by a small embedded template.
*
* Only for codes such as 404 and 500, you could configure a wrapping template from a Node, so that the usual
* layout is rendered.
*
******************************************************************************************************************/
@Nonnull
private TextHolder createErrorView (@Nonnull final Layout layout, @Nonnull final Throwable t)
{
<span class="nc" id="L102"> log.warn("While processing " + layout, t);</span>
<span class="nc bnc" id="L103" title="All 2 branches missed."> final var status = (t instanceof HttpStatusException)</span>
<span class="nc" id="L104"> ? HttpStatus.valueOf(((HttpStatusException)t).getHttpStatus())</span>
<span class="nc" id="L105"> : HttpStatus.INTERNAL_SERVER_ERROR;</span>
<span class="nc" id="L106"> httpStatus.set(status);</span>
<span class="nc" id="L107"> return new HtmlHolder("<div><h1>" + status.getReasonPhrase() + "</h1></div>");</span>
}
/*******************************************************************************************************************
*
******************************************************************************************************************/
private static void attach (@Nonnull final TextHolder parent, @Nonnull final TextHolder child)
{
<span class="nc" id="L115"> parent.addComponent(child);</span>
<span class="nc" id="L116"> }</span>
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.9.202303310957</span></div></body></html>