Content of file DefaultRedirectProcessor.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>DefaultRedirectProcessor.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 :: Filesystems :: SCM :: Mercurial</a> > <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core-default</a> > <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.model.spi</a> > <span class="el_source">DefaultRedirectProcessor.java</span></div><h1>DefaultRedirectProcessor.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.core.model.spi;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.core.annotation.Order;
import it.tidalwave.util.Id;
import it.tidalwave.util.Key;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.HttpStatusException;
import it.tidalwave.northernwind.core.model.Request;
import it.tidalwave.northernwind.core.model.RequestProcessor;
import it.tidalwave.northernwind.core.model.Site;
import it.tidalwave.northernwind.core.model.SiteProvider;
import lombok.extern.slf4j.Slf4j;
import static java.util.Collections.emptyList;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import static it.tidalwave.northernwind.core.model.SiteNode._SiteNode_;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
*
**********************************************************************************************************************/
<span class="nc" id="L54">@Configurable @Slf4j @Order(HIGHEST_PRECEDENCE+1)</span>
<span class="nc bnc" id="L55" title="All 18 branches missed.">public class DefaultRedirectProcessor implements RequestProcessor</span>
{
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
static class Mapping
{
private final String regex;
private final String replacement;
public Mapping (@Nonnull final String configuration)
<span class="nc" id="L67"> {</span>
<span class="nc" id="L68"> final var parts = configuration.split(" -> ");</span>
<span class="nc" id="L69"> regex = parts[0];</span>
<span class="nc" id="L70"> replacement = parts[1];</span>
<span class="nc" id="L71"> }</span>
@Nonnull
public String replace (@Nonnull final String string)
{
<span class="nc" id="L76"> return string.replaceAll(regex, replacement);</span>
}
@Override @Nonnull
public String toString()
{
<span class="nc" id="L82"> return regex + " -> " + replacement;</span>
}
}
<span class="nc" id="L86"> /* VisibleForTesting */ static final Id P_GROUP_ID = new Id("Redirector");</span>
<span class="nc" id="L88"> /* VisibleForTesting */ static final Key<List<String>> P_PERMANENT_REDIRECTS = new Key<>("permanentRedirects") {};</span>
<span class="nc" id="L90"> /* VisibleForTesting */ static final Key<List<String>> P_TEMPORARY_REDIRECTS = new Key<>("temporaryRedirects") {};</span>
@Inject
private Provider<SiteProvider> siteProvider;
private Site site;
<span class="nc" id="L97"> private final List<Mapping> permanentMappings = new ArrayList<>();</span>
<span class="nc bnc" id="L99" title="All 4 branches missed."> private final List<Mapping> temporaryMappings = new ArrayList<>();</span>
private boolean initialized;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
// @PostConstruct // FIXME: see NW-224
/* VisibleForTesting */ void initialize()
throws NotFoundException
{
<span class="nc" id="L111"> site = siteProvider.get().getSite();</span>
<span class="nc" id="L112"> final var rootSiteNode = site.find(_SiteNode_).withRelativeUri("/").result();</span>
<span class="nc" id="L113"> final var rootSiteNodeProperties = rootSiteNode.getProperties();</span>
<span class="nc" id="L114"> final var properties = rootSiteNodeProperties.getGroup(P_GROUP_ID);</span>
<span class="nc bnc" id="L116" title="All 2 branches missed."> for (final var permanentRedirectConfig : properties.getProperty(P_PERMANENT_REDIRECTS).orElse(emptyList()))</span>
{
<span class="nc" id="L118"> permanentMappings.add(new Mapping(permanentRedirectConfig));</span>
<span class="nc" id="L119"> }</span>
<span class="nc bnc" id="L121" title="All 2 branches missed."> for (final var temporaryRedirectConfig : properties.getProperty(P_TEMPORARY_REDIRECTS).orElse(emptyList()))</span>
{
<span class="nc" id="L123"> temporaryMappings.add(new Mapping(temporaryRedirectConfig));</span>
<span class="nc" id="L124"> }</span>
<span class="nc" id="L126"> log.info("Permanent redirect mappings:");</span>
<span class="nc bnc" id="L128" title="All 2 branches missed."> for (final var mapping : permanentMappings)</span>
{
<span class="nc" id="L130"> log.info(">>>> {}", mapping);</span>
<span class="nc" id="L131"> }</span>
<span class="nc" id="L133"> log.info("Temporary redirect mappings:");</span>
<span class="nc bnc" id="L135" title="All 2 branches missed."> for (final var mapping : temporaryMappings)</span>
{
<span class="nc" id="L137"> log.info(">>>> {}", mapping);</span>
<span class="nc" id="L138"> }</span>
<span class="nc" id="L139"> }</span>
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override @Nonnull
public Status process (@Nonnull final Request request)
throws HttpStatusException
{
<span class="nc bnc" id="L149" title="All 2 branches missed."> if (siteProvider.get().isSiteAvailable()) // FIXME no "/" SiteNode available at this point</span>
| no "/" SiteNode available at this point | |
{
<span class="nc" id="L151"> synchronized (this) // FIXME: see NW-224</span>
{
<span class="nc bnc" id="L153" title="All 2 branches missed."> if (!initialized)</span>
{
try
{
<span class="nc" id="L157"> initialize();</span>
}
<span class="nc" id="L159"> catch (NotFoundException e)</span>
{
<span class="nc" id="L161"> throw new RuntimeException(e);</span>
<span class="nc" id="L162"> }</span>
<span class="nc" id="L164"> initialized = true;</span>
}
<span class="nc" id="L166"> } // END FIXME</span>
<span class="nc" id="L168"> final var relativeUri = request.getRelativeUri();</span>
<span class="nc bnc" id="L170" title="All 2 branches missed."> for (final var mapping : permanentMappings)</span>
{
<span class="nc" id="L172"> final var newRelativeUri = mapping.replace(relativeUri);</span>
<span class="nc bnc" id="L174" title="All 2 branches missed."> if (!newRelativeUri.equals(relativeUri))</span>
{
<span class="nc" id="L176"> log.info(">>>> permanently redirecting from {} to {} ...", relativeUri, newRelativeUri);</span>
<span class="nc" id="L177"> throw HttpStatusException.permanentRedirect(site, newRelativeUri);</span>
}
<span class="nc" id="L179"> }</span>
<span class="nc bnc" id="L181" title="All 2 branches missed."> for (final var mapping : temporaryMappings)</span>
{
<span class="nc" id="L183"> final var newRelativeUri = mapping.replace(relativeUri);</span>
<span class="nc bnc" id="L185" title="All 2 branches missed."> if (!newRelativeUri.equals(relativeUri))</span>
{
<span class="nc" id="L187"> log.info(">>>> temporarily redirecting from {} to {} ...", relativeUri, newRelativeUri);</span>
<span class="nc" id="L188"> throw HttpStatusException.temporaryRedirect(site, newRelativeUri);</span>
}
<span class="nc" id="L190"> }</span>
}
<span class="nc" id="L193"> return Status.CONTINUE;</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>