DownloadComplete.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *********************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  12.  * the License. You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.downloader;

  28. import java.io.IOException;
  29. import java.net.URI;
  30. import javax.annotation.Nonnegative;
  31. import javax.annotation.Nonnull;
  32. import javax.annotation.concurrent.Immutable;
  33. import java.net.URL;
  34. import java.nio.file.Files;
  35. import java.nio.file.Path;
  36. import lombok.Getter;
  37. import lombok.RequiredArgsConstructor;
  38. import lombok.ToString;

  39. /***********************************************************************************************************************
  40.  *
  41.  * @author  Fabrizio Giudici
  42.  *
  43.  **********************************************************************************************************************/
  44. @Immutable @RequiredArgsConstructor @Getter @ToString(exclude = "bytes")
  45. public class DownloadComplete
  46.   {
  47.     public enum Origin
  48.       {
  49.         NETWORK,
  50.         CACHE
  51.       }

  52.     @Nonnull
  53.     private final URL url;

  54.     @Nonnegative
  55.     private final int statusCode;
  56.    
  57.     @Nonnull
  58.     private final byte[] bytes;
  59.    
  60.     @Nonnull
  61.     private final Origin origin;
  62.    
  63.     @Nonnull
  64.     public URI getCachedUri()
  65.       {
  66.         // FIXME: pass the URI of the cached datum instead
  67.         // Refactor getBytes() so it loads on demand from the URI
  68.         try
  69.           {
  70.             final Path tempFile = Files.createTempFile("tmp", "image");
  71.             Files.write(tempFile, bytes);
  72.             return tempFile.toUri();
  73.           }
  74.         catch (IOException e)
  75.           {
  76.             throw new RuntimeException(e);
  77.           }
  78.       }
  79.   }