Class FileBackedHTTPResource

All Implemented Interfaces:
Component, DestructableComponent, IdentifiedComponent, InitializableComponent, Resource, Aware, BeanNameAware, InitializingBean, InputStreamSource, Resource

public class FileBackedHTTPResource extends HTTPResource
A resource representing a file read from an HTTP(S) location. Every time the file is successfully read from the URL location it is written to a backing file. If the file can not be read from the URL it is read from this backing file, if available.
  • Field Details

    • log

      @Nonnull private final org.slf4j.Logger log
      Logger.
    • backingResource

      @Nonnull private final Resource backingResource
      Backing resource file.
  • Constructor Details

    • FileBackedHTTPResource

      public FileBackedHTTPResource(@Nonnull @ParameterName(name="backingFile") String backingFile, @Nonnull @ParameterName(name="client") org.apache.hc.client5.http.classic.HttpClient client, @NotEmpty @Nonnull @ParameterName(name="url") String url) throws IOException
      Constructor.
      Parameters:
      backingFile - the file to use as backing store
      client - the client we use to connect with.
      url - URL to the remote data
      Throws:
      IOException - if the URL was badly formed
    • FileBackedHTTPResource

      public FileBackedHTTPResource(@Nonnull @ParameterName(name="backingFile") String backingFile, @Nonnull @ParameterName(name="client") org.apache.hc.client5.http.classic.HttpClient client, @Nonnull @ParameterName(name="url") URL url) throws IOException
      Constructor.
      Parameters:
      backingFile - the file to use as backing store
      client - the client we use to connect with.
      url - URL to the remote data
      Throws:
      IOException - if the URL was badly formed
  • Method Details

    • saveAndClone

      @Nonnull protected InputStream saveAndClone(@Nonnull InputStream input) throws IOException
      saveAndClone. Read the contents into memory and then write out to the backing file. Finally
      Parameters:
      input - the input stream
      Returns:
      the cloned stream.
      Throws:
      IOException - if an error happens. If the backing file might have been corrupted we delete it.
    • getInputStream

      @Nonnull public InputStream getInputStream() throws IOException
      Return an InputStream.

      It is expected that each call creates a fresh stream.

      This requirement is particularly important when you consider an API such as JavaMail, which needs to be able to read the stream multiple times when creating mail attachments. For such a use case, it is required that each getInputStream() call returns a fresh stream.

      Specified by:
      getInputStream in interface InputStreamSource
      Specified by:
      getInputStream in interface Resource
      Overrides:
      getInputStream in class HTTPResource
      Returns:
      the input stream for the underlying resource (must not be null)
      Throws:
      IOException - if the stream could not be opened
    • exists

      public boolean exists()
      Return whether this resource actually exists in physical form.

      This method performs a definitive existence check, whereas the existence of a Resource handle only guarantees a valid descriptor handle.

      Specified by:
      exists in interface Resource
      Specified by:
      exists in interface Resource
      Overrides:
      exists in class HTTPResource
      Returns:
      whether this resource actually exists in physical form.
    • contentLength

      public long contentLength() throws IOException
      Determine the content length for this resource.
      Specified by:
      contentLength in interface Resource
      Specified by:
      contentLength in interface Resource
      Overrides:
      contentLength in class HTTPResource
      Returns:
      the content length for this resource.
      Throws:
      IOException - if the resource cannot be resolved (in the file system or as some other known physical resource type)
    • lastModified

      public long lastModified() throws IOException
      Determine the last-modified timestamp for this resource.
      Specified by:
      lastModified in interface Resource
      Specified by:
      lastModified in interface Resource
      Overrides:
      lastModified in class HTTPResource
      Returns:
      the last-modified timestamp for this resource.
      Throws:
      IOException - if the resource cannot be resolved (in the file system or as some other known physical resource type)
    • createRelative

      @Nonnull public HTTPResource createRelative(@Nonnull String relativePath) throws IOException
      Based on UrlResource.
      Specified by:
      createRelative in interface Resource
      Overrides:
      createRelative in class HTTPResource
      Throws:
      IOException
    • getDescription

      @Nonnull public String getDescription()
      Return a description for this resource, to be used for error output when working with the resource.

      Implementations are also encouraged to return this value from their toString method.

      Specified by:
      getDescription in interface Resource
      Specified by:
      getDescription in interface Resource
      Overrides:
      getDescription in class HTTPResource
      Returns:
      a description for this resource.
      See Also: