Package net.shibboleth.shared.spring.httpclient.resource

# Class FileBackedHTTPResource

java.lang.Object
net.shibboleth.shared.component.AbstractInitializableComponent
net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent
net.shibboleth.shared.spring.httpclient.resource.HTTPResource
net.shibboleth.shared.spring.httpclient.resource.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 Summary

Fields
Modifier and Type
Field
Description
`private final Resource`
`backingResource`
Backing resource file.
`private final org.slf4j.Logger`
`log`
Logger.

## Constructor Summary

Constructors
Constructor
Description
`FileBackedHTTPResource(String backingFile, org.apache.hc.client5.http.classic.HttpClient client, String url)`
Constructor.
`FileBackedHTTPResource(String backingFile, org.apache.hc.client5.http.classic.HttpClient client, URL url)`
Constructor.

## Method Summary

Modifier and Type
Method
Description
`long`
`contentLength()`
Determine the content length for this resource.
`HTTPResource`
`createRelative(String relativePath)`
Based on `UrlResource`.
`boolean`
`exists()`
Return whether this resource actually exists in physical form.
`String`
`getDescription()`
Return a description for this resource, to be used for error output when working with the resource.
`InputStream`
`getInputStream()`
Return an `InputStream`.
`long`
`lastModified()`
Determine the last-modified timestamp for this resource.
`protected InputStream`
`saveAndClone(InputStream input)`
saveAndClone.

### Methods inherited from class net.shibboleth.shared.spring.httpclient.resource.HTTPResource

`afterPropertiesSet, buildHttpClientContext, closeResponse, createRelative, createRelativeResource, getFile, getFilename, getResourceHeaders, getResponseHeader, getURI, getURL, isFile, isOpen, isReadable, reportCachingStatus, setBeanName, setHttpClientContextHandler`

### Methods inherited from class net.shibboleth.shared.component.AbstractIdentifiedInitializableComponent

`doInitialize, ensureId, getId, ifDestroyedThrowDestroyedComponentException, ifInitializedThrowUnmodifiabledComponentException, ifNotInitializedThrowUninitializedComponentException, setId`

### Methods inherited from class net.shibboleth.shared.component.AbstractInitializableComponent

`checkComponentActive, checkSetterPreconditions, destroy, doDestroy, initialize, isDestroyed, isInitialized`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.core.io.Resource

`getContentAsByteArray, getContentAsString, getFilePath, readableChannel`

## 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:
  - `Object.toString()`