Content of file DefaultSiteNode.java.html

<?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>DefaultSiteNode.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 :: Components :: HTML Template</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.impl.model</a> &gt; <span class="el_source">DefaultSiteNode.java</span></div><h1>DefaultSiteNode.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.core.impl.model;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Configurable;
import it.tidalwave.util.Finder;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.ModelFactory;
import it.tidalwave.northernwind.core.model.RequestLocaleManager;
import it.tidalwave.northernwind.core.model.ResourceFile;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.SiteNode;
import it.tidalwave.northernwind.core.model.spi.SiteNodeSupport;
import it.tidalwave.northernwind.frontend.ui.Layout;
import it.tidalwave.northernwind.frontend.impl.ui.DefaultLayout;
import it.tidalwave.northernwind.frontend.impl.ui.LayoutLoggerVisitor;
import lombok.Cleanup;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;
import static it.tidalwave.northernwind.util.UrlEncoding.decodedUtf8;

/***********************************************************************************************************************
 *
 * A node of the site, mapped to a given URL.
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="pc" id="L63">@Configurable(preConstruction = true) @Slf4j @ToString(callSuper = false, of = &quot;relativeUri&quot;)</span>
/* package */ class DefaultSiteNode extends SiteNodeSupport
  {
<span class="fc" id="L66">    @Nonnull</span>
    private final Map&lt;Locale, Layout&gt; layoutMapByLocale = new HashMap&lt;&gt;();

<span class="nc" id="L69">    @Getter @Nonnull</span>
    final /* package */ InternalSite site;

    @Inject
    private InheritanceHelper inheritanceHelper;

    @Inject
    private RequestLocaleManager localeRequestManager;

    @CheckForNull
    private ResourcePath relativeUri;

    /* package */ int uriComputationCounter;

    /*******************************************************************************************************************
     *
     * Creates a new instance with the given configuration file and mapped to the given URI.
     *
     * @param  file          the file with the configuration
     * @param  relativeUri   the bound URI
     *
     ******************************************************************************************************************/
    public DefaultSiteNode (@Nonnull final ModelFactory modelFactory,
                            @Nonnull final InternalSite site,
                            @Nonnull final ResourceFile file)
      {
<span class="pc bpc" id="L95" title="12 of 18 branches missed.">        super(modelFactory, file);</span>
<span class="fc" id="L96">        this.site = site;</span>
        try
          {
<span class="fc" id="L99">            loadLayouts();</span>
          }
<span class="nc" id="L101">        catch (IOException e)</span>
          {
<span class="nc" id="L103">            throw new RuntimeException(e);</span>
<span class="fc" id="L104">          }</span>
<span class="pc bpc" id="L105" title="3 of 4 branches missed.">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Nonnull @Override
    public synchronized ResourcePath getRelativeUri()
      {
<span class="fc bfc" id="L115" title="All 2 branches covered.">        if (relativeUri == null) // FIXME: is lazy evaluation really needed?</span>
          {
<span class="fc" id="L117">            uriComputationCounter++;</span>

<span class="fc" id="L119">            relativeUri = ResourcePath.EMPTY;</span>
<span class="fc" id="L120">            final var file = getResource().getFile();</span>

<span class="fc bfc" id="L122" title="All 2 branches covered.">            if (!file.equals(site.getNodeFolder()))</span>
              {
                try
                  {
<span class="fc" id="L126">                    final var segment = getResource().getProperty(P_EXPOSED_URI).orElse(decodedUtf8(file.getName()));</span>
<span class="fc" id="L127">                    relativeUri = relativeUri.appendedWith(getParent().getRelativeUri()).appendedWith(segment);</span>
                  }
<span class="nc" id="L129">                catch (NotFoundException e) // FIXME: for getParent()</span>
for getParent()
{ <span class="nc" id="L131"> log.error(&quot;&quot;, e); // should never occur</span> <span class="nc" id="L132"> throw new RuntimeException(e);</span> <span class="fc" id="L133"> }</span> } } <span class="fc" id="L137"> log.trace(&quot;&gt;&gt;&gt;&gt; relativeUri: {}&quot;, relativeUri);</span> <span class="fc" id="L139"> return relativeUri;</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Layout getLayout() { <span class="fc" id="L150"> return layoutMapByLocale.get(localeRequestManager.getLocales().get(0));</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public Finder&lt;SiteNode&gt; findChildren() { <span class="nc" id="L161"> throw new UnsupportedOperationException(&quot;Not supported yet.&quot;);</span> } /******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ private void loadLayouts() throws IOException { <span class="fc bfc" id="L172" title="All 2 branches covered."> for (final var locale : localeRequestManager.getLocales())</span> { <span class="fc" id="L174"> Layout layout = null;</span> // Cannot be implemented by recursion, since each SiteNode could have a local override for its Layout - // local overrides are not inherited. Perhaps you could do if you keep two layouts per Node, one without the override. // On the other hand, inheritanceHelper encapsulates the local override policy, which applies also to Properties... <span class="fc" id="L178"> final var layoutFiles = inheritanceHelper.getInheritedPropertyFiles(getResource().getFile(),</span> locale, &quot;Components&quot;); <span class="pc bpc" id="L181" title="1 of 2 branches missed."> for (final var layoutFile : layoutFiles)</span> { <span class="nc" id="L183"> final var overridingLayout = loadLayout(layoutFile);</span> <span class="nc bnc" id="L184" title="All 2 branches missed."> layout = (layout == null) ? overridingLayout : layout.withOverride(overridingLayout);</span> <span class="nc bnc" id="L186" title="All 2 branches missed."> if (log.isDebugEnabled())</span> { <span class="nc" id="L188"> overridingLayout.accept(new LayoutLoggerVisitor(LayoutLoggerVisitor.Level.DEBUG));</span> } <span class="nc" id="L190"> }</span> <span class="pc bpc" id="L192" title="1 of 2 branches missed."> layout = (layout != null) ? layout : modelFactory.createLayout()</span> <span class="fc" id="L193"> .withId(new Id(&quot;&quot;))</span> <span class="fc" id="L194"> .withType(&quot;emptyPlaceholder&quot;)</span> <span class="fc" id="L195"> .build();</span> <span class="pc bpc" id="L197" title="2 of 4 branches missed."> if (site.isLogConfigurationEnabled() || log.isDebugEnabled())</span> { <span class="fc" id="L199"> log.debug(&quot;&gt;&gt;&gt;&gt; layout for {} {}:&quot;, getResource().getFile().getPath().asString(), locale);</span> <span class="fc" id="L200"> layout.accept(new LayoutLoggerVisitor(LayoutLoggerVisitor.Level.INFO));</span> } <span class="fc" id="L203"> layoutMapByLocale.put(locale, layout);</span> <span class="fc" id="L204"> }</span> <span class="fc" id="L205"> }</span> /******************************************************************************************************************* * * Returns the parent {@code SiteNode}. * * @return the parent node * @throws NotFoundException if the parent doesn't exist * ******************************************************************************************************************/ @Nonnull private SiteNode getParent() throws NotFoundException { <span class="fc" id="L219"> final var parentRelativePath = getResource().getFile().getParent().getPath().urlDecoded()</span> <span class="fc" id="L220"> .relativeTo(site.getNodeFolder().getPath());</span> <span class="fc" id="L222"> return site.find(_SiteNode_).withRelativePath(parentRelativePath).result();</span> } /******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private DefaultLayout loadLayout (@Nonnull final ResourceFile layoutFile) throws IOException { <span class="nc" id="L232"> log.trace(&quot;&gt;&gt;&gt;&gt; reading layout from {}...&quot;, layoutFile.getPath().asString());</span> <span class="nc bnc" id="L233" title="All 2 branches missed."> @Cleanup final var is = layoutFile.getInputStream();</span> <span class="nc" id="L234"> return modelFactory.createLayout().build().as(_Unmarshallable_).unmarshal(is);</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>