Package spark.resource
Class AbstractFileResolvingResource
- java.lang.Object
-
- spark.resource.AbstractResource
-
- spark.resource.AbstractFileResolvingResource
-
- All Implemented Interfaces:
InputStreamResource
,Resource
- Direct Known Subclasses:
ClassPathResource
,ExternalResource
public abstract class AbstractFileResolvingResource extends AbstractResource
Abstract base class for resources which resolve URLs into File references, such as UrlResource orClassPathResource
.Detects the "file" protocol as well as the JBoss "vfs" protocol in URLs, resolving file system references accordingly.
-
-
Constructor Summary
Constructors Constructor Description AbstractFileResolvingResource()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
contentLength()
This implementation reads the entire InputStream to calculate the content length.protected void
customizeConnection(java.net.HttpURLConnection con)
Customize the givenHttpURLConnection
, obtained in the course of anexists()
,contentLength()
orlastModified()
call.protected void
customizeConnection(java.net.URLConnection con)
Customize the givenURLConnection
, obtained in the course of anexists()
,contentLength()
orlastModified()
call.boolean
exists()
This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened.java.io.File
getFile()
This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.protected java.io.File
getFileForLastModifiedCheck()
This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).boolean
isReadable()
This implementation always returnstrue
.long
lastModified()
This implementation checks the timestamp of the underlying File, if available.-
Methods inherited from class spark.resource.AbstractResource
createRelative, equals, getFilename, getURI, getURL, hashCode, isOpen, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface spark.resource.InputStreamResource
getInputStream
-
Methods inherited from interface spark.resource.Resource
getDescription
-
-
-
-
Method Detail
-
getFile
public java.io.File getFile() throws java.io.IOException
This implementation returns a File reference for the underlying class path resource, provided that it refers to a file in the file system.- Specified by:
getFile
in interfaceResource
- Overrides:
getFile
in classAbstractResource
- Returns:
- a File handle for this resource.
- Throws:
java.io.IOException
- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
getFileForLastModifiedCheck
protected java.io.File getFileForLastModifiedCheck() throws java.io.IOException
This implementation determines the underlying File (or jar file, in case of a resource in a jar/zip).- Overrides:
getFileForLastModifiedCheck
in classAbstractResource
- Returns:
- the File to use for timestamp checking (never
null
) - Throws:
java.io.IOException
- if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
-
exists
public boolean exists()
Description copied from class:AbstractResource
This implementation checks whether a File can be opened, falling back to whether an InputStream can be opened. This will cover both directories and content resources.- Specified by:
exists
in interfaceResource
- Overrides:
exists
in classAbstractResource
- Returns:
- if exists
-
isReadable
public boolean isReadable()
Description copied from class:AbstractResource
This implementation always returnstrue
.- Specified by:
isReadable
in interfaceResource
- Overrides:
isReadable
in classAbstractResource
- Returns:
- if readable
- See Also:
InputStreamResource.getInputStream()
-
contentLength
public long contentLength() throws java.io.IOException
Description copied from class:AbstractResource
This implementation reads the entire InputStream to calculate the content length. Subclasses will almost always be able to provide a more optimal version of this, e.g. checking a File length.- Specified by:
contentLength
in interfaceResource
- Overrides:
contentLength
in classAbstractResource
- Returns:
- the content length for this resource.
- Throws:
java.io.IOException
- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
InputStreamResource.getInputStream()
-
lastModified
public long lastModified() throws java.io.IOException
Description copied from class:AbstractResource
This implementation checks the timestamp of the underlying File, if available.- Specified by:
lastModified
in interfaceResource
- Overrides:
lastModified
in classAbstractResource
- Returns:
- the last-modified timestamp for this resource.
- Throws:
java.io.IOException
- if the resource cannot be resolved (in the file system or as some other known physical resource type)- See Also:
AbstractResource.getFileForLastModifiedCheck()
-
customizeConnection
protected void customizeConnection(java.net.URLConnection con) throws java.io.IOException
Customize the givenURLConnection
, obtained in the course of anexists()
,contentLength()
orlastModified()
call.Calls
ResourceUtils.useCachesIfNecessary(URLConnection)
and delegates tocustomizeConnection(HttpURLConnection)
if possible. Can be overridden in subclasses.- Parameters:
con
- the URLConnection to customize- Throws:
java.io.IOException
- if thrown from URLConnection methods
-
customizeConnection
protected void customizeConnection(java.net.HttpURLConnection con) throws java.io.IOException
Customize the givenHttpURLConnection
, obtained in the course of anexists()
,contentLength()
orlastModified()
call.Sets request method "HEAD" by default. Can be overridden in subclasses.
- Parameters:
con
- the HttpURLConnection to customize- Throws:
java.io.IOException
- if thrown from HttpURLConnection methods
-
-