Content of file HttpStatusException.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>HttpStatusException.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 :: Profiling</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-northernwind-core</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.northernwind.core.model</a> &gt; <span class="el_source">HttpStatusException.java</span></div><h1>HttpStatusException.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.model;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.Getter;
import lombok.ToString;
import static javax.servlet.http.HttpServletResponse.*;

/***********************************************************************************************************************
 *
 * An exceptional response representing a situation that should be reported to the client with a specific HTTP status
 * code. Note that this class doesn't necessarily represent an error, but it could be e.g. a redirect and such. This
 * class can also carry headers to be included as part of the response
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L47">@ToString</span>
public class HttpStatusException extends Exception
  {
    /** Status codes that don't imply an error. */
<span class="fc" id="L51">    private static final List&lt;Integer&gt; GOOD_CODES = List.of(SC_FOUND, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY);</span>

<span class="fc" id="L53">    @Getter</span>
    private final int httpStatus;

<span class="fc" id="L56">    @Getter</span>
    private final Map&lt;String, String&gt; headers;

    /*******************************************************************************************************************
     *
     * Creates an exception representing a temporary redirect.
     *
     * @param  site         the {@link Site}
     * @param  target       the target to redirect to
     * @return              the exception
     *
     ******************************************************************************************************************/
    @Nonnull
    public static HttpStatusException temporaryRedirect (@Nonnull final Site site, @Nonnull final String target)
      {
        // FIXME: inject Site
inject Site
<span class="nc" id="L72"> return new HttpStatusException(SC_MOVED_TEMPORARILY).withHeader(&quot;Location&quot;, createUrl(site, target));</span> } /******************************************************************************************************************* * * Creates an exception representing a permanent redirect. * * @param site the {@link Site} * @param target the target to redirect to * @return the exception * ******************************************************************************************************************/ @Nonnull public static HttpStatusException permanentRedirect (@Nonnull final Site site, @Nonnull final String target) { // FIXME: inject Site <span class="nc" id="L88"> return new HttpStatusException(SC_MOVED_PERMANENTLY).withHeader(&quot;Location&quot;, createUrl(site, target));</span> } /******************************************************************************************************************* * * Creates an instance with the given HTTP status. * * @param httpStatus the status * ******************************************************************************************************************/ public HttpStatusException (final int httpStatus) { <span class="fc" id="L100"> this(httpStatus, Collections.emptyMap());</span> <span class="fc" id="L101"> }</span> /******************************************************************************************************************* * * ******************************************************************************************************************/ private HttpStatusException (final int httpStatus, @Nonnull final Map&lt;String, String&gt; headers) { <span class="fc" id="L109"> super(String.format(&quot;httpStatus=%d, headers=%s&quot;, httpStatus, headers));</span> <span class="fc" id="L110"> this.httpStatus = httpStatus;</span> <span class="fc" id="L111"> this.headers = headers;</span> <span class="fc" id="L112"> }</span> /******************************************************************************************************************* * * Creates a clone with the given header. * * @param name the header name * @param value the header value * @return the clone * ******************************************************************************************************************/ @Nonnull public HttpStatusException withHeader (@Nonnull final String name, @Nonnull final String value) { <span class="nc" id="L126"> final Map&lt;String, String&gt; newHeaders = new HashMap&lt;&gt;(headers);</span> <span class="nc" id="L127"> newHeaders.put(name, value);</span> <span class="nc" id="L128"> return new HttpStatusException(httpStatus, newHeaders);</span> } /******************************************************************************************************************* * * Return {@code true} whether this exception represents an error. * * @return {@code true} in case of error * ******************************************************************************************************************/ public boolean isError() { <span class="nc bnc" id="L140" title="All 2 branches missed."> return !GOOD_CODES.contains(httpStatus);</span> } /******************************************************************************************************************* * * @param site * @param target * @return * ******************************************************************************************************************/ @Nonnull private static String createUrl (@Nonnull final Site site, @Nonnull final String target) { <span class="nc bnc" id="L153" title="All 4 branches missed."> return target.startsWith(&quot;http://&quot;) || target.startsWith(&quot;https://&quot;)</span> <span class="nc" id="L154"> ? target</span> <span class="nc" id="L155"> : site.createLink(ResourcePath.of(target));</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>