Content of file CachingRestClientSupport.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>CachingRestClientSupport.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 :: Commons for Tests</a> &gt; <a href="../index.html" class="el_bundle">it-tidalwave-bluemarine2-commons</a> &gt; <a href="index.source.html" class="el_package">it.tidalwave.bluemarine2.rest</a> &gt; <span class="el_source">CachingRestClientSupport.java</span></div><h1>CachingRestClientSupport.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.rest;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Optional;
import java.io.IOException;
import java.nio.file.Path;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import static java.util.Collections.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.springframework.http.HttpHeaders.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **********************************************************************************************************************/
<span class="nc" id="L57">@Slf4j</span>
public class CachingRestClientSupport
  {
<span class="nc" id="L60">    public enum CacheMode</span>
      {
        /** Always use the network. */
<span class="nc" id="L63">        DONT_USE_CACHE</span>
          {
            @Override @Nonnull
            public ResponseEntity&lt;String&gt; request (@Nonnull final CachingRestClientSupport api,
                                                   @Nonnull final String url)
              throws IOException, InterruptedException
              {
<span class="nc" id="L70">                return api.requestFromNetwork(url);</span>
              }
          },
        /** Never use the network. */
<span class="nc" id="L74">        ONLY_USE_CACHE</span>
          {
            @Override @Nonnull
            public ResponseEntity&lt;String&gt; request (@Nonnull final CachingRestClientSupport api,
                                                   @Nonnull final String url)
              throws IOException
              {
<span class="nc" id="L81">                return api.requestFromCache(url).get();</span>
              }
          },
        /** First try the cache, then the network. */
<span class="nc" id="L85">        USE_CACHE</span>
          {
            @Override @Nonnull
            public ResponseEntity&lt;String&gt; request (@Nonnull final CachingRestClientSupport api,
                                                   @Nonnull final String url)
              throws IOException, InterruptedException
              {
<span class="nc" id="L92">                return api.requestFromCacheAndThenNetwork(url);</span>
              }
          };

        @Nonnull
        public abstract ResponseEntity&lt;String&gt; request (@Nonnull CachingRestClientSupport api,
                                                        @Nonnull String url)
          throws IOException, InterruptedException;
      }

<span class="nc" id="L102">    private final RestTemplate restTemplate = new RestTemplate(); // FIXME: inject?</span>

<span class="nc" id="L104">    @Getter @Setter</span>
    private CacheMode cacheMode = CacheMode.USE_CACHE;

<span class="nc" id="L107">    @Getter @Setter</span>
    private Path cachePath;

<span class="nc" id="L110">    @Getter @Setter</span>
    private String accept = &quot;application/xml&quot;;

<span class="nc" id="L113">    @Getter @Setter</span>
    private String userAgent = &quot;blueMarine II (fabrizio.giudici@tidalwave.it)&quot;;

<span class="nc" id="L116">    @Getter @Setter</span>
    private long throttleLimit;

<span class="nc" id="L119">    @Getter @Setter @Nonnegative</span>
    private int maxRetry = 3;

<span class="nc" id="L122">    @Getter @Setter</span>
<span class="nc" id="L123">    private List&lt;Integer&gt; retryStatusCodes = List.of(503);</span>

<span class="nc" id="L125">    private long latestNetworkAccessTimestamp = 0;</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
<span class="nc" id="L132">    private static final ResponseErrorHandler IGNORE_HTTP_ERRORS = new ResponseErrorHandler()</span>
<span class="nc" id="L133">      {</span>
        @Override
        public boolean hasError (@Nonnull final ClientHttpResponse response)
          throws IOException
          {
<span class="nc" id="L138">            return false;</span>
          }

        @Override
        public void handleError (@Nonnull final ClientHttpResponse response)
          throws IOException
          {
<span class="nc" id="L145">          }</span>
      };

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
<span class="nc" id="L153">    private final ClientHttpRequestInterceptor interceptor = (request, body, execution) -&gt;</span>
      {
<span class="nc" id="L155">        final HttpHeaders headers = request.getHeaders();</span>
<span class="nc" id="L156">        headers.add(USER_AGENT, userAgent);</span>
<span class="nc" id="L157">        headers.add(ACCEPT, accept);</span>
<span class="nc" id="L158">        return execution.execute(request, body);</span>
      };

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public CachingRestClientSupport()
<span class="nc" id="L167">      {</span>
<span class="nc" id="L168">        restTemplate.setInterceptors(singletonList(interceptor));</span>
<span class="nc" id="L169">        restTemplate.setErrorHandler(IGNORE_HTTP_ERRORS);</span>
<span class="nc" id="L170">      }</span>

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @PostConstruct
    void initialize()
      {
<span class="nc" id="L180">      }</span>

    /*******************************************************************************************************************
     *
     * Performs a  web request.
     *
     * @return                  the response
     *
     ******************************************************************************************************************/
    @Nonnull
    protected ResponseEntity&lt;String&gt; request (@Nonnull final String url)
      throws IOException, InterruptedException
      {
<span class="nc" id="L193">        log.debug(&quot;request({})&quot;, url);</span>
<span class="nc" id="L194">        return cacheMode.request(this, url);</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private Optional&lt;ResponseEntity&lt;String&gt;&gt; requestFromCache (@Nonnull final String url)
      throws IOException
      {
<span class="nc" id="L206">        log.debug(&quot;requestFromCache({})&quot;, url);</span>
<span class="nc" id="L207">        return ResponseEntityIo.load(cachePath.resolve(fixedPath(url)));</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private synchronized ResponseEntity&lt;String&gt; requestFromNetwork (@Nonnull final String url)
      throws IOException, InterruptedException
      {
<span class="nc" id="L219">        log.debug(&quot;requestFromNetwork({})&quot;, url);</span>
<span class="nc" id="L220">        ResponseEntity&lt;String&gt; response = null;</span>

<span class="nc bnc" id="L222" title="All 2 branches missed.">        for (int retry = 0; retry &lt; maxRetry; retry++)</span>
          {
<span class="nc" id="L224">            final long now = System.currentTimeMillis();</span>
<span class="nc" id="L225">            final long delta = now - latestNetworkAccessTimestamp;</span>
<span class="nc" id="L226">            final long toWait = Math.max(throttleLimit - delta, 0);</span>

<span class="nc bnc" id="L228" title="All 2 branches missed.">            if (toWait &gt; 0)</span>
              {
<span class="nc" id="L230">                log.info(&quot;&gt;&gt;&gt;&gt; throttle limit: waiting for {} msec...&quot;, toWait);</span>
<span class="nc" id="L231">                Thread.sleep(toWait);</span>
              }

<span class="nc" id="L234">            latestNetworkAccessTimestamp = now;</span>
<span class="nc" id="L235">            response = restTemplate.getForEntity(URI.create(url), String.class);</span>
<span class="nc" id="L236">            final int httpStatusCode = response.getStatusCodeValue();</span>
<span class="nc" id="L237">            log.debug(&quot;&gt;&gt;&gt;&gt; HTTP status code: {}&quot;, httpStatusCode);</span>

<span class="nc bnc" id="L239" title="All 2 branches missed.">            if (!retryStatusCodes.contains(httpStatusCode))</span>
              {
<span class="nc" id="L241">                break;</span>
              }

<span class="nc" id="L244">            log.warn(&quot;HTTP status code: {} - retry #{}&quot;, httpStatusCode, retry + 1);</span>
          }

//        log.trace(&quot;&gt;&gt;&gt;&gt; response: {}&quot;, response);
<span class="nc" id="L248">        return response;</span>
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private ResponseEntity&lt;String&gt; requestFromCacheAndThenNetwork (@Nonnull final String url)
      throws IOException, InterruptedException
      {
<span class="nc" id="L260">        log.debug(&quot;requestFromCacheAndThenNetwork({})&quot;, url);</span>

<span class="nc" id="L262">        return requestFromCache(url).orElseGet(() -&gt;</span>
          {
            try
              {
<span class="nc" id="L266">                final ResponseEntity&lt;String&gt; response = requestFromNetwork(url);</span>
<span class="nc" id="L267">                final int httpStatusCode = response.getStatusCodeValue();</span>

<span class="nc bnc" id="L269" title="All 2 branches missed.">                if (!retryStatusCodes.contains(httpStatusCode))</span>
                  {
<span class="nc" id="L271">                    ResponseEntityIo.store(cachePath.resolve(fixedPath(url)), response, emptyList());</span>
                  }

<span class="nc" id="L274">                return response;</span>
              }
<span class="nc" id="L276">            catch (IOException | InterruptedException e)</span>
              {
<span class="nc" id="L278">                throw new RestException(e); // FIXME</span>
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    /* package */ static String fixedPath (@Nonnull final String url)
      {
<span class="nc" id="L290">        String s = url.replace(&quot;://&quot;, &quot;/&quot;);</span>
<span class="nc" id="L291">        int i = s.lastIndexOf('/');</span>

<span class="nc bnc" id="L293" title="All 2 branches missed.">        if (i &gt;= 0)</span>
          {
<span class="nc" id="L295">            final String lastSegment = s.substring(i + 1);</span>

<span class="nc bnc" id="L297" title="All 2 branches missed.">            if (lastSegment.length() &gt; 255) // FIXME: and Mac OS X</span>
and Mac OS X
{ try { <span class="nc" id="L301"> final MessageDigest digestComputer = MessageDigest.getInstance(&quot;SHA1&quot;);</span> <span class="nc" id="L302"> s = s.substring(0, i) + &quot;/&quot; + toString(digestComputer.digest(lastSegment.getBytes(UTF_8)));</span> } <span class="nc" id="L304"> catch (NoSuchAlgorithmException e)</span> { <span class="nc" id="L306"> throw new RuntimeException(e);</span> <span class="nc" id="L307"> }</span> } } <span class="nc" id="L311"> return s;</span> } /******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private static String toString (@Nonnull final byte[] bytes) { <span class="nc" id="L321"> final StringBuilder builder = new StringBuilder();</span> <span class="nc bnc" id="L323" title="All 2 branches missed."> for (final byte b : bytes)</span> { <span class="nc" id="L325"> final int value = b &amp; 0xff;</span> <span class="nc" id="L326"> builder.append(Integer.toHexString(value &gt;&gt;&gt; 4)).append(Integer.toHexString(value &amp; 0x0f));</span> } <span class="nc" id="L329"> return builder.toString();</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>