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

import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import java.time.Duration;
import java.io.IOException;
import org.springframework.core.annotation.Order;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.northernwind.core.model.Request;
import it.tidalwave.northernwind.core.model.RequestProcessor;
import it.tidalwave.northernwind.core.model.ResourcePath;
import it.tidalwave.northernwind.core.model.SiteProvider;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import static it.tidalwave.northernwind.core.model.Media._Media_;
import static it.tidalwave.northernwind.core.model.RequestProcessor.Status.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L52">@Slf4j @Order(HIGHEST_PRECEDENCE+2)</span>
<span class="nc" id="L53">public class DefaultMediaRequestProcessor&lt;ResponseType&gt; implements RequestProcessor</span>
  {
    @Inject
    private Provider&lt;SiteProvider&gt; siteProvider;

    @Inject
    protected ResponseHolder&lt;ResponseType&gt; responseHolder;

<span class="nc" id="L61">    @Getter @Setter</span>
<span class="nc" id="L62">    private Duration duration = Duration.ofDays(7); // FIXME: rename to expirationDuration</span>

<span class="nc" id="L64">    @Getter @Setter</span>
    private String uriPrefix = &quot;media&quot;; // FIXME

    @Override @Nonnull
    public Status process (@Nonnull final Request request)
      throws NotFoundException, IOException
      {
<span class="nc" id="L71">        var mediaUri = ResourcePath.of(request.getRelativeUri());</span>

<span class="nc bnc" id="L73" title="All 2 branches missed.">        if (!mediaUri.startsWith(uriPrefix))</span>
          {
<span class="nc" id="L75">            return CONTINUE;</span>
          }

<span class="nc" id="L78">        mediaUri = mediaUri.withoutLeading(); // media</span>
        //
        // Media that can be served at different sizes are mapped to URLs such as:
        //
        //     /media/stillimages/&lt;media-id&gt;/&lt;size&gt;/image.jpg
        //     /media/movies/&lt;media-id&gt;/&lt;size&gt;/movie.jpg
        //
        // TODO: perhaps this logic should be moved to the media finder? Such as:
perhaps this logic should be moved to the media finder? Such as:
// find(Media).withSize(1920).withRelativePath(uri).result() // <span class="nc bnc" id="L88" title="All 4 branches missed."> if (mediaUri.startsWith(&quot;stillimages&quot;) || mediaUri.startsWith(&quot;movies&quot;))</span> { // // TODO: retrocompatibility with StoppingDown and Bluette // http://stoppingdown.net/media/stillimages/1920/20120802-0010.jpg // Should be dealt with a specific redirector in the website and removed from here. // <span class="nc bnc" id="L95" title="All 2 branches missed."> if (mediaUri.getSegmentCount() == 3)</span> { <span class="nc" id="L97"> final var extension = mediaUri.getExtension();</span> <span class="nc" id="L98"> final var fileName = mediaUri.getTrailing(); // 20120802-0010.jpg</span> <span class="nc" id="L99"> mediaUri = mediaUri.withoutTrailing();</span> <span class="nc" id="L100"> final var size = mediaUri.getTrailing(); // 1920</span> <span class="nc" id="L101"> mediaUri = mediaUri.withoutTrailing();</span> <span class="nc" id="L102"> mediaUri = mediaUri.appendedWith(fileName.replaceAll(&quot;\\..*$&quot;, &quot;&quot;))</span> <span class="nc" id="L103"> .appendedWith(&quot;&quot; + size)</span> <span class="nc" id="L104"> .appendedWith(&quot;image.&quot; + extension);</span> <span class="nc" id="L105"> mediaUri = mediaUri.prependedWith(uriPrefix);</span> <span class="nc" id="L106"> final var redirect = mediaUri.asString();</span> <span class="nc" id="L107"> log.info(&quot;&gt;&gt;&gt;&gt; permanently redirecting to {}&quot;, redirect);</span> <span class="nc" id="L108"> responseHolder.response().permanentRedirect(redirect).put();</span> <span class="nc" id="L109"> return BREAK;</span> } // END TODO <span class="nc" id="L113"> final var extension = mediaUri.getExtension(); // jpg</span> // final String fileName = mediaUri.getTrailing(); // image.jpg <span class="nc" id="L115"> mediaUri = mediaUri.withoutTrailing();</span> <span class="nc" id="L116"> final var size = mediaUri.getTrailing(); // 1920</span> <span class="nc" id="L117"> mediaUri = mediaUri.withoutTrailing();</span> <span class="nc" id="L118"> final var mediaId = mediaUri.getTrailing(); // 20120802-0010</span> <span class="nc" id="L119"> mediaUri = mediaUri.withoutTrailing();</span> <span class="nc" id="L120"> mediaUri = mediaUri.appendedWith(size).appendedWith(mediaId + &quot;.&quot; + extension);</span> } <span class="nc" id="L123"> final var media = siteProvider.get().getSite().find(_Media_).withRelativePath(mediaUri).result();</span> <span class="nc" id="L124"> final var file = media.getFile();</span> <span class="nc" id="L125"> log.info(&quot;&gt;&gt;&gt;&gt; serving contents of {} ...&quot;, file.getPath().asString());</span> <span class="nc" id="L127"> responseHolder.response().fromFile(file)</span> <span class="nc" id="L128"> .withExpirationTime(duration)</span> <span class="nc" id="L129"> .forRequest(request)</span> <span class="nc" id="L130"> .put();</span> <span class="nc" id="L131"> return BREAK;</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>