Content of file SimpleHttpCacheStorage.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>SimpleHttpCacheStorage.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">blueMarine II :: Headless Service</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-downloader</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.downloader.impl</a> &gt; <span class="el_source">SimpleHttpCacheStorage.java</span></div><h1>SimpleHttpCacheStorage.java</h1><pre class="source lang-java linenums">/*
 * *********************************************************************************************************************
 *
 * blueMarine II: Semantic Media Centre
 * http://tidalwave.it/projects/bluemarine2
 *
 * Copyright (C) 2015 - 2021 by 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.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/bluemarine2-src
 * git clone https://github.com/tidalwave-it/bluemarine2-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.bluemarine2.downloader.impl;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.HttpCacheUpdateCallback;
import org.apache.http.client.cache.HttpCacheUpdateException;
import org.apache.http.client.cache.Resource;
import org.apache.http.impl.client.cache.FileResource;
import org.apache.http.impl.io.DefaultHttpResponseParser;
import org.apache.http.impl.io.DefaultHttpResponseWriter;
import org.apache.http.impl.io.HttpTransportMetricsImpl;
import org.apache.http.impl.io.SessionInputBufferImpl;
import org.apache.http.impl.io.SessionOutputBufferImpl;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpResponse;
import it.tidalwave.util.annotation.VisibleForTesting;
import lombok.Cleanup;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import static java.nio.file.Files.*;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.nio.file.StandardOpenOption.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L72">@Slf4j</span>
<span class="nc" id="L73">public class SimpleHttpCacheStorage implements HttpCacheStorage</span>
  {
    private static final String PATH_CONTENT = &quot;content&quot;;
    private static final String PATH_HEADERS = &quot;headers&quot;;

<span class="nc" id="L78">    private static final Collection&lt;String&gt; NEVER_EXPIRING_HEADERS = List.of(&quot;Cache-Control&quot;, &quot;Expires&quot;, &quot;Pragma&quot;);</span>
    
<span class="nc" id="L80">    @Getter @Setter</span>
<span class="nc" id="L81">    private Path folderPath = Paths.get(System.getProperty(&quot;java.io.tmpdir&quot;));</span>
    
    /** When this field is {@code true} the headers of items extracted from the cache are manipulated so it appears
     *  they never expire. */
<span class="nc" id="L85">    @Getter @Setter</span>
    private boolean neverExpiring;
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void putEntry (@Nonnull final String key, @Nonnull final HttpCacheEntry entry)
      throws IOException 
      {
        try 
          {
<span class="nc" id="L99">            log.debug(&quot;putEntry({}, {})&quot;, key, entry);</span>
<span class="nc" id="L100">            final Path cachePath = getCacheItemPath(new URL(key));</span>
<span class="nc" id="L101">            createDirectories(cachePath);</span>
<span class="nc" id="L102">            final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);</span>
<span class="nc" id="L103">            final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);</span>

<span class="nc bnc" id="L105" title="All 2 branches missed.">            @Cleanup final OutputStream os = newOutputStream(cacheHeadersPath, CREATE);</span>
<span class="nc" id="L106">            final SessionOutputBufferImpl sob = sessionOutputBufferFrom(os);</span>
<span class="nc" id="L107">            final DefaultHttpResponseWriter writer = new DefaultHttpResponseWriter(sob);</span>
<span class="nc" id="L108">            writer.write(responseFrom(entry));</span>
<span class="nc" id="L109">            sob.flush();</span>

<span class="nc bnc" id="L111" title="All 2 branches missed.">            if (entry.getResource().length() &gt; 0)</span>
              {
<span class="nc" id="L113">                copy(entry.getResource().getInputStream(), cacheContentPath, REPLACE_EXISTING);</span>
              }
          }
<span class="nc" id="L116">        catch (HttpException e)</span>
          {
<span class="nc" id="L118">            throw new IOException(e);</span>
<span class="nc" id="L119">          }</span>
<span class="nc" id="L120">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public HttpCacheEntry getEntry (@Nonnull final String key)
      throws IOException 
      {
<span class="nc" id="L131">        log.debug(&quot;getEntry({})&quot;, key);</span>
<span class="nc" id="L132">        final Path cachePath = getCacheItemPath(new URL(key));</span>
<span class="nc" id="L133">        final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);</span>
<span class="nc" id="L134">        final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);</span>

<span class="nc bnc" id="L136" title="All 2 branches missed.">        if (!exists(cacheHeadersPath))</span>
          {
<span class="nc" id="L138">            log.trace(&quot;&gt;&gt;&gt;&gt; cache miss: {}&quot;, cacheHeadersPath);</span>
<span class="nc" id="L139">            return null;  </span>
          }

        try
          {
<span class="nc bnc" id="L144" title="All 2 branches missed.">            @Cleanup final InputStream is = newInputStream(cacheHeadersPath);</span>
<span class="nc" id="L145">            final SessionInputBufferImpl sib = sessionInputBufferFrom(is);</span>
<span class="nc" id="L146">            final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(sib);</span>
<span class="nc" id="L147">            return entryFrom(cacheContentPath, parser.parse());</span>
          }
<span class="nc" id="L149">        catch (HttpException e)</span>
          {
<span class="nc" id="L151">            throw new IOException(e);</span>
          }
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void removeEntry (@Nonnull final String key)
      throws IOException
      {
<span class="nc" id="L164">        log.debug(&quot;removeEntry({})&quot;, key);</span>
        // FIXME
<span class="nc" id="L166">      }</span>

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void updateEntry (@Nonnull final String key, @Nonnull final HttpCacheUpdateCallback callback)
      throws IOException, HttpCacheUpdateException 
      {
<span class="nc" id="L177">        log.debug(&quot;updateEntry({}, {})&quot;, key, callback);</span>
        // FIXME
<span class="nc" id="L179">      }</span>

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @VisibleForTesting boolean isCachedResourcePresent (@Nonnull final String key)
      throws MalformedURLException
      {
<span class="nc" id="L189">        final Path cachePath = getCacheItemPath(new URL(key));</span>
<span class="nc" id="L190">        final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);</span>
<span class="nc" id="L191">        final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);</span>
<span class="nc" id="L192">        log.trace(&quot;&gt;&gt;&gt;&gt; probing cached entry at {}&quot;, cachePath);</span>
        
<span class="nc bnc" id="L194" title="All 4 branches missed.">        return exists(cacheHeadersPath) &amp;&amp; exists(cacheContentPath);</span>
      }
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Nonnull
    private Path getCacheItemPath (@Nonnull final URL url)
      throws MalformedURLException 
      {
<span class="nc" id="L206">        final int port = url.getPort();</span>
<span class="nc bnc" id="L207" title="All 2 branches missed.">        final URL url2 = new URL(url.getProtocol(), url.getHost(), (port == 80) ? -1 : port, url.getFile());</span>
<span class="nc" id="L208">        final Path cachePath = Paths.get(url2.toString().replaceAll(&quot;:&quot;, &quot;&quot;));</span>
<span class="nc" id="L209">        return folderPath.resolve(cachePath);</span>
      } 

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Nonnull
    private static SessionInputBufferImpl sessionInputBufferFrom (@Nonnull final InputStream is)
      {
<span class="nc" id="L220">        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();</span>
<span class="nc" id="L221">        final SessionInputBufferImpl sib = new SessionInputBufferImpl(metrics, 100);</span>
<span class="nc" id="L222">        sib.bind(is);</span>
<span class="nc" id="L223">        return sib;</span>
      }

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Nonnull
    private static SessionOutputBufferImpl sessionOutputBufferFrom (@Nonnull final OutputStream os)
      {
<span class="nc" id="L234">        final HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();</span>
<span class="nc" id="L235">        final SessionOutputBufferImpl sob = new SessionOutputBufferImpl(metrics, 100);</span>
<span class="nc" id="L236">        sob.bind(os);</span>
<span class="nc" id="L237">        return sob;</span>
      }

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Nonnull
    private static HttpResponse responseFrom (@Nonnull final HttpCacheEntry entry)
      {
<span class="nc" id="L248">        final BasicHttpResponse response = new BasicHttpResponse(entry.getStatusLine());</span>
<span class="nc" id="L249">        response.setHeaders(entry.getAllHeaders());</span>
<span class="nc" id="L250">        return response;</span>
      }
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Nonnull
    private HttpCacheEntry entryFrom (@Nonnull final Path cacheContentPath,
                                      @Nonnull final HttpResponse response)
      {
<span class="nc" id="L262">        final Date date = new Date(); // FIXME: force hit?</span>
force hit?
// new Date(Files.getLastModifiedTime(cacheHeadersPath).toMillis()); <span class="nc bnc" id="L264" title="All 2 branches missed."> final Resource resource = exists(cacheContentPath) ? new FileResource(cacheContentPath.toFile()) : null;</span> <span class="nc" id="L266"> List&lt;Header&gt; headers = new ArrayList&lt;&gt;(List.of(response.getAllHeaders()));</span> <span class="nc bnc" id="L268" title="All 2 branches missed."> if (neverExpiring)</span> { <span class="nc bnc" id="L270" title="All 2 branches missed."> headers = headers.stream().filter(header -&gt; !NEVER_EXPIRING_HEADERS.contains(header.getName()))</span> <span class="nc" id="L271"> .collect(Collectors.toList());</span> <span class="nc" id="L272"> headers.add(new BasicHeader(&quot;Expires&quot;, &quot;Mon, 31 Dec 2099 00:00:00 GMT&quot;));</span> } <span class="nc" id="L275"> return new HttpCacheEntry(date, date, response.getStatusLine(), headers.toArray(new Header[0]), resource);</span> } } </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.7.202105040129</span></div></body></html>