Class FileCacheDataLoader

java.lang.Object
eu.europa.esig.dss.service.http.commons.FileCacheDataLoader
All Implemented Interfaces:
DataLoader, DSSFileLoader, Serializable

public class FileCacheDataLoader extends Object implements DataLoader, DSSFileLoader
This class provides some caching features to handle the resources. The default cache folder is set to java.io.tmpdir. The urls of the resources is transformed to the file name by replacing the special characters by _
See Also:
  • Constructor Details

    • FileCacheDataLoader

      public FileCacheDataLoader()
      Empty constructor
    • FileCacheDataLoader

      public FileCacheDataLoader(DataLoader dataLoader)
      Default constructor
      Parameters:
      dataLoader - DataLoader to use for remote access (e.g. online)
  • Method Details

    • getDataLoader

      public DataLoader getDataLoader()
      Gets the dataloader
      Returns:
      DataLoader
    • setDataLoader

      public void setDataLoader(DataLoader dataLoader)
      Sets the data loader for a remote documents access (e.g. online)
      Parameters:
      dataLoader - DataLoader
    • setFileCacheDirectory

      public void setFileCacheDirectory(File fileCacheDirectory)
      This method allows to set the file cache directory. If the cache folder does not exists then it's created.
      Parameters:
      fileCacheDirectory - File pointing the cache folder to be used.
    • setCacheExpirationTime

      public void setCacheExpirationTime(long cacheExpirationTimeInMilliseconds)
      Sets the expiration time for the cached files in milliseconds. If the defined time has passed after the cache file's last modification time, then a fresh copy is downloaded and cached, otherwise a cached copy is used. A negative value is interpreted as undefined (cache does not expire). Default: -1
      Parameters:
      cacheExpirationTimeInMilliseconds - value in milliseconds
    • setResourceLoader

      public void setResourceLoader(ResourceLoader resourceLoader)
      Sets the ResourceLoader for an absolute path creation
      Parameters:
      resourceLoader - ResourceLoader
    • addToBeLoaded

      public void addToBeLoaded(String url)
      This methods allows to indicate if the resource must be obtained. If this method has been invoked then only the provided URL will be processed.
      Parameters:
      url - to be processed
    • addToBeIgnored

      public void addToBeIgnored(String urlString)
      This methods allows to indicate which resources must be ignored. It is useful in a test environment where some of fake sources a not available. It prevents to wait for the timeout.
      Parameters:
      urlString - to be ignored. It can be the original URL or the cache file name
    • get

      public byte[] get(String url, boolean refresh)
      Executes a GET request to the provided URL, with a forced cache refresh when defined
      Specified by:
      get in interface DataLoader
      Parameters:
      url - to access
      refresh - if true indicates that the cached data should be refreshed
      Returns:
      binaries of the extracted data object
    • get

      public byte[] get(String url) throws DSSException
      Description copied from interface: DataLoader
      Execute a HTTP GET operation.
      Specified by:
      get in interface DataLoader
      Parameters:
      url - the url to access
      Returns:
      byte array of obtained data or null
      Throws:
      DSSException
    • getDocument

      public DSSDocument getDocument(String url)
      Description copied from interface: DSSFileLoader
      Returns DSSDocument from the provided url
      Specified by:
      getDocument in interface DSSFileLoader
      Parameters:
      url - String url of the document to obtain
      Returns:
      DSSDocument retrieved document
    • remove

      public boolean remove(String url)
      Description copied from interface: DSSFileLoader
      Removes the file from FileSystem with the given url
      Specified by:
      remove in interface DSSFileLoader
      Parameters:
      url - String url of the remote file location (the same what was used on file saving)
      Returns:
      TRUE when file was successfully deleted, FALSE otherwise
    • isNetworkProtocol

      protected boolean isNetworkProtocol(String urlString)
      Checks if the URL defines a network protocol
      Parameters:
      urlString - String url to check
      Returns:
      TRUE if the URL defines a network protocol, FALSE otherwise
    • createFile

      public File createFile(String urlString, byte[] bytes)
      Allows to add a given array of byte as a cache file representing by the urlString.
      Parameters:
      urlString - the URL to add to the cache
      bytes - the content of the cache file
      Returns:
      File
    • loadFileFromCache

      public byte[] loadFileFromCache(String urlString) throws DSSException
      Allows to load the file for a given file name from the cache folder.
      Parameters:
      urlString - String url
      Returns:
      the content of the file or null if the file does not exist
      Throws:
      DSSException - in case if the file does not exist in the cache
    • post

      public byte[] post(String urlString, byte[] content) throws DSSException
      Description copied from interface: DataLoader
      Executes a HTTP POST operation
      Specified by:
      post in interface DataLoader
      Parameters:
      urlString - to access
      content - the content to post
      Returns:
      byte array of obtained data
      Throws:
      DSSException
    • get

      public DataLoader.DataAndUrl get(List<String> urlStrings)
      Description copied from interface: DataLoader
      Execute a HTTP GET operation. This method is used when many URls are available to access the same resource. The operation stops after the first successful download.
      Specified by:
      get in interface DataLoader
      Parameters:
      urlStrings - List of Strings representing the URLs to be used in sequential way to obtain the data.
      Returns:
      DataAndUrl representing the array of obtained data and used url, or null
    • setContentType

      public void setContentType(String contentType)
      Description copied from interface: DataLoader
      This allows to set the content type. Example: Content-Type "application/ocsp-request"
      Specified by:
      setContentType in interface DataLoader
      Parameters:
      contentType - to set the Content-Type