Package org.eclipse.jetty.util.resource
Class Resource
java.lang.Object
org.eclipse.jetty.util.resource.Resource
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ResourceFactory
- Direct Known Subclasses:
EmptyResource
,PathResource
,ResourceCollection
,URLResource
Abstract resource class.
This class provides a resource abstraction, where a resource may be a file, a URL or an entry in a jar file.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract Resource
Returns the resource contained inside the current resource with the given name, which may or may not exist.abstract void
close()
Release any temporary resources held by the resource.void
Copy the Resource to the new destination file.abstract boolean
delete()
Deletes the given resourceabstract boolean
exists()
Parse a list of String delimited resources and return the List of Resources instances it represents.fromList
(String resources, boolean globDirs, ResourceFactory resourceFactory) Parse a delimited String of resource references and return the List of Resources instances it represents.getAlias()
static boolean
abstract File
getFile()
File representing the given resource.abstract InputStream
Input stream to the resourcegetListHTML
(String base, boolean parent, String query) Get the resource list as a HTML directory listing.abstract String
getName()
The name of the resource.abstract ReadableByteChannel
Readable ByteChannel for the resource.getResource
(String path) Get a resource from within this resource.abstract URI
getURI()
URI representing the resource.Generate a weak ETag reference for this Resource.getWeakETag
(String suffix) boolean
isAlias()
abstract boolean
static boolean
isContainedIn
(Resource r, Resource containingResource) abstract boolean
boolean
Return true if the passed Resource represents the same resource as the Resource.abstract long
Time resource was last modified.abstract long
length()
Length of the resource.abstract String[]
list()
list of resource names contained in the given resource.static Resource
newClassPathResource
(String resource) Find a classpath resource.static Resource
newClassPathResource
(String name, boolean useCaches, boolean checkParents) Find a classpath resource.static Resource
newResource
(File file) static Resource
newResource
(String resource) Construct a resource from a string.static Resource
newResource
(String resource, boolean useCaches) Construct a resource from a string.static Resource
newResource
(URI uri) Construct a resource from a uri.static Resource
newResource
(URL url) Construct a resource from a url.static Resource
newResource
(Path path) Construct a Resource from provided pathstatic Resource
newSystemResource
(String resource) Construct a system resource from a string.abstract boolean
Rename the given resourcestatic Resource
resolveAlias
(Resource resource) Attempt to resolve the real path of a Resource to potentially remove any symlinks causing the Resource to be an alias.void
static void
setDefaultUseCaches
(boolean useCaches) Change the default setting for url connection caches.static URL
Generate a properly encoded URL from aFile
instance.
-
Field Details
-
__defaultUseCaches
public static boolean __defaultUseCaches
-
-
Constructor Details
-
Resource
public Resource()
-
-
Method Details
-
setDefaultUseCaches
public static void setDefaultUseCaches(boolean useCaches) Change the default setting for url connection caches. Subsequent URLConnections will use this default.- Parameters:
useCaches
- true to enable URL connection caches, false otherwise.
-
getDefaultUseCaches
public static boolean getDefaultUseCaches() -
resolveAlias
Attempt to resolve the real path of a Resource to potentially remove any symlinks causing the Resource to be an alias.- Parameters:
resource
- the resource to resolve.- Returns:
- a new Resource resolved to the real path of the original Resource, or the original resource if it was not an alias.
-
newResource
Construct a resource from a uri.- Parameters:
uri
- A URI.- Returns:
- A Resource object.
- Throws:
MalformedURLException
- Problem accessing URI
-
newResource
Construct a resource from a url.- Parameters:
url
- A URL.- Returns:
- A Resource object.
-
newResource
Construct a resource from a string.- Parameters:
resource
- A URL or filename.- Returns:
- A Resource object.
- Throws:
MalformedURLException
- Problem accessing URIIOException
-
newResource
Construct a resource from a string.- Parameters:
resource
- A URL or filename.useCaches
- controls URLConnection caching- Returns:
- A Resource object.
- Throws:
MalformedURLException
- Problem accessing URIIOException
-
newResource
-
newResource
Construct a Resource from provided path- Parameters:
path
- the path- Returns:
- the Resource for the provided path
- Since:
- 9.4.10
-
newSystemResource
Construct a system resource from a string. The resource is tried as classloader resource before being treated as a normal resource.- Parameters:
resource
- Resource as string representation- Returns:
- The new Resource
- Throws:
IOException
- Problem accessing resource.
-
newClassPathResource
Find a classpath resource.- Parameters:
resource
- the relative name of the resource- Returns:
- Resource or null
-
newClassPathResource
Find a classpath resource. TheClass.getResource(String)
method is used to lookup the resource. If it is not found, then theLoader.getResource(String)
method is used. If it is still not found, thenClassLoader.getSystemResource(String)
is used. UnlikeClassLoader.getSystemResource(String)
this method does not check for normal resources.- Parameters:
name
- The relative name of the resourceuseCaches
- True if URL caches are to be used.checkParents
- True if forced searching of parent Classloaders is performed to work around loaders with inverted priorities- Returns:
- Resource or null
-
isContainedIn
public static boolean isContainedIn(Resource r, Resource containingResource) throws MalformedURLException - Throws:
MalformedURLException
-
isContainedIn
- Throws:
MalformedURLException
-
isSame
Return true if the passed Resource represents the same resource as the Resource. For many resource types, this is equivalent toObject.equals(Object)
, however for resources types that support aliasing, this maybe some other check (e.g.Files.isSameFile(Path, Path)
).- Parameters:
resource
- The resource to check- Returns:
- true if the passed resource represents the same resource.
-
close
public abstract void close()Release any temporary resources held by the resource.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
exists
public abstract boolean exists()- Returns:
- true if the represented resource exists.
-
isDirectory
public abstract boolean isDirectory()- Returns:
- true if the represented resource is a container/directory.
-
lastModified
public abstract long lastModified()Time resource was last modified.- Returns:
- the last modified time as milliseconds since unix epoch
-
length
public abstract long length()Length of the resource.- Returns:
- the length of the resource
-
getURI
URI representing the resource.- Returns:
- an URI representing the given resource
-
getFile
File representing the given resource.- Returns:
- an File representing the given resource or NULL if this is not possible.
- Throws:
IOException
- if unable to get the resource due to permissions
-
getName
The name of the resource.- Returns:
- the name of the resource
-
getInputStream
Input stream to the resource- Returns:
- an input stream to the resource
- Throws:
IOException
- if unable to open the input stream
-
getReadableByteChannel
Readable ByteChannel for the resource.- Returns:
- an readable bytechannel to the resource or null if one is not available.
- Throws:
IOException
- if unable to open the readable bytechannel for the resource.
-
delete
Deletes the given resource- Returns:
- true if resource was found and successfully deleted, false if resource didn't exist or was unable to be deleted.
- Throws:
SecurityException
- if unable to delete due to permissions
-
renameTo
Rename the given resource- Parameters:
dest
- the destination name for the resource- Returns:
- true if the resource was renamed, false if the resource didn't exist or was unable to be renamed.
- Throws:
SecurityException
- if unable to rename due to permissions
-
list
list of resource names contained in the given resource. Ordering is unspecified, so callers may wish to sort the return value to ensure deterministic behavior.- Returns:
- a list of resource names contained in the given resource, or null. Note: The resource names are not URL encoded.
-
addPath
Returns the resource contained inside the current resource with the given name, which may or may not exist.- Parameters:
path
- The path segment to add, which is not encoded. The path may be non canonical, but if so then the resulting Resource will return true fromisAlias()
.- Returns:
- the Resource for the resolved path within this Resource, never null
- Throws:
IOException
- if unable to resolve the pathMalformedURLException
- if the resolution of the path fails because the input path parameter is malformed, or a relative path attempts to access above the root resource.
-
getResource
Get a resource from within this resource.- Specified by:
getResource
in interfaceResourceFactory
- Parameters:
path
- The path to the resource- Returns:
- The resource, that might not actually exist (yet).
- Throws:
IOException
- if unable to create Resource
-
getAssociate
-
setAssociate
-
isAlias
public boolean isAlias()- Returns:
- true if this Resource is an alias to another real Resource
-
getAlias
- Returns:
- The canonical Alias of this resource or null if none.
-
getListHTML
Get the resource list as a HTML directory listing.- Parameters:
base
- The base URLparent
- True if the parent directory should be includedquery
- query params- Returns:
- String of HTML
- Throws:
IOException
- on failure to generate a list.
-
copyTo
Copy the Resource to the new destination file.Will not replace existing destination file.
- Parameters:
destination
- the destination file to create- Throws:
IOException
- if unable to copy the resource
-
getWeakETag
Generate a weak ETag reference for this Resource.- Returns:
- the weak ETag reference for this resource.
-
getWeakETag
-
getAllResources
-
toURL
Generate a properly encoded URL from aFile
instance.- Parameters:
file
- Target file.- Returns:
- URL of the target file.
- Throws:
MalformedURLException
- if unable to convert File to URL
-
fromList
Parse a list of String delimited resources and return the List of Resources instances it represents.Supports glob references that end in
/*
or\*
. Glob references will only iterate through the level specified and will not traverse found directories within the glob reference.- Parameters:
resources
- the comma,
or semicolon;
delimited String of resource references.globDirs
- true to return directories in addition to files at the level of the glob- Returns:
- the list of resources parsed from input string.
- Throws:
IOException
-
fromList
public static List<Resource> fromList(String resources, boolean globDirs, ResourceFactory resourceFactory) throws IOException Parse a delimited String of resource references and return the List of Resources instances it represents.Supports glob references that end in
/*
or\*
. Glob references will only iterate through the level specified and will not traverse found directories within the glob reference.- Parameters:
resources
- the comma,
or semicolon;
delimited String of resource references.globDirs
- true to return directories in addition to files at the level of the globresourceFactory
- the ResourceFactory used to create new Resource references- Returns:
- the list of resources parsed from input string.
- Throws:
IOException
-