- java.lang.Object
-
- org.eclipse.jetty.util.resource.Resource
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ResourceFactory
- Direct Known Subclasses:
EmptyResource,PathResource,ResourceCollection,URLResource
public abstract class Resource extends java.lang.Object implements ResourceFactory, java.io.Closeable
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
Fields Modifier and Type Field Description static boolean__defaultUseCaches
-
Constructor Summary
Constructors Constructor Description Resource()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract ResourceaddPath(java.lang.String path)Returns the resource contained inside the current resource with the given name, which may or may not exist.abstract voidclose()Release any temporary resources held by the resource.voidcopyTo(java.io.File destination)Copy the Resource to the new destination file.abstract booleandelete()Deletes the given resourceabstract booleanexists()static java.util.List<Resource>fromList(java.lang.String resources, boolean globDirs)Parse a list of String delimited resources and return the List of Resources instances it represents.static java.util.List<Resource>fromList(java.lang.String resources, boolean globDirs, ResourceFactory resourceFactory)Parse a delimited String of resource references and return the List of Resources instances it represents.java.net.URIgetAlias()java.util.Collection<Resource>getAllResources()java.lang.ObjectgetAssociate()static booleangetDefaultUseCaches()abstract java.io.FilegetFile()File representing the given resource.abstract java.io.InputStreamgetInputStream()Input stream to the resourcejava.lang.StringgetListHTML(java.lang.String base, boolean parent, java.lang.String query)Get the resource list as a HTML directory listing.abstract java.lang.StringgetName()The name of the resource.abstract java.nio.channels.ReadableByteChannelgetReadableByteChannel()Readable ByteChannel for the resource.ResourcegetResource(java.lang.String path)Get a resource from within this resource.abstract java.net.URIgetURI()URI representing the resource.java.lang.StringgetWeakETag()Generate a weak ETag reference for this Resource.java.lang.StringgetWeakETag(java.lang.String suffix)booleanisAlias()abstract booleanisContainedIn(Resource r)static booleanisContainedIn(Resource r, Resource containingResource)abstract booleanisDirectory()booleanisSame(Resource resource)Return true if the passed Resource represents the same resource as the Resource.abstract longlastModified()Time resource was last modified.abstract longlength()Length of the resource.abstract java.lang.String[]list()list of resource names contained in the given resource.static ResourcenewClassPathResource(java.lang.String resource)Find a classpath resource.static ResourcenewClassPathResource(java.lang.String name, boolean useCaches, boolean checkParents)Find a classpath resource.static ResourcenewResource(java.io.File file)static ResourcenewResource(java.lang.String resource)Construct a resource from a string.static ResourcenewResource(java.lang.String resource, boolean useCaches)Construct a resource from a string.static ResourcenewResource(java.net.URI uri)Construct a resource from a uri.static ResourcenewResource(java.net.URL url)Construct a resource from a url.static ResourcenewResource(java.nio.file.Path path)Construct a Resource from provided pathstatic ResourcenewSystemResource(java.lang.String resource)Construct a system resource from a string.abstract booleanrenameTo(Resource dest)Rename the given resourcevoidsetAssociate(java.lang.Object o)static voidsetDefaultUseCaches(boolean useCaches)Change the default setting for url connection caches.static java.net.URLtoURL(java.io.File file)Generate a properly encoded URL from aFileinstance.
-
-
-
Method Detail
-
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()
-
newResource
public static Resource newResource(java.net.URI uri) throws java.net.MalformedURLException
Construct a resource from a uri.- Parameters:
uri- A URI.- Returns:
- A Resource object.
- Throws:
java.net.MalformedURLException- Problem accessing URI
-
newResource
public static Resource newResource(java.net.URL url)
Construct a resource from a url.- Parameters:
url- A URL.- Returns:
- A Resource object.
-
newResource
public static Resource newResource(java.lang.String resource) throws java.io.IOException
Construct a resource from a string.- Parameters:
resource- A URL or filename.- Returns:
- A Resource object.
- Throws:
java.net.MalformedURLException- Problem accessing URIjava.io.IOException
-
newResource
public static Resource newResource(java.lang.String resource, boolean useCaches) throws java.io.IOException
Construct a resource from a string.- Parameters:
resource- A URL or filename.useCaches- controls URLConnection caching- Returns:
- A Resource object.
- Throws:
java.net.MalformedURLException- Problem accessing URIjava.io.IOException
-
newResource
public static Resource newResource(java.io.File file)
-
newResource
public static Resource newResource(java.nio.file.Path path)
Construct a Resource from provided path- Parameters:
path- the path- Returns:
- the Resource for the provided path
- Since:
- 9.4.10
-
newSystemResource
public static Resource newSystemResource(java.lang.String resource) throws java.io.IOException
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:
java.io.IOException- Problem accessing resource.
-
newClassPathResource
public static Resource newClassPathResource(java.lang.String resource)
Find a classpath resource.- Parameters:
resource- the relative name of the resource- Returns:
- Resource or null
-
newClassPathResource
public static Resource newClassPathResource(java.lang.String name, boolean useCaches, boolean checkParents)
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 java.net.MalformedURLException
- Throws:
java.net.MalformedURLException
-
isContainedIn
public abstract boolean isContainedIn(Resource r) throws java.net.MalformedURLException
- Throws:
java.net.MalformedURLException
-
isSame
public boolean isSame(Resource resource)
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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
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
public abstract java.net.URI getURI()
URI representing the resource.- Returns:
- an URI representing the given resource
-
getFile
public abstract java.io.File getFile() throws java.io.IOExceptionFile representing the given resource.- Returns:
- an File representing the given resource or NULL if this is not possible.
- Throws:
java.io.IOException- if unable to get the resource due to permissions
-
getName
public abstract java.lang.String getName()
The name of the resource.- Returns:
- the name of the resource
-
getInputStream
public abstract java.io.InputStream getInputStream() throws java.io.IOExceptionInput stream to the resource- Returns:
- an input stream to the resource
- Throws:
java.io.IOException- if unable to open the input stream
-
getReadableByteChannel
public abstract java.nio.channels.ReadableByteChannel getReadableByteChannel() throws java.io.IOExceptionReadable ByteChannel for the resource.- Returns:
- an readable bytechannel to the resource or null if one is not available.
- Throws:
java.io.IOException- if unable to open the readable bytechannel for the resource.
-
delete
public abstract boolean delete() throws java.lang.SecurityExceptionDeletes 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:
java.lang.SecurityException- if unable to delete due to permissions
-
renameTo
public abstract boolean renameTo(Resource dest) throws java.lang.SecurityException
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:
java.lang.SecurityException- if unable to rename due to permissions
-
list
public abstract java.lang.String[] 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
public abstract Resource addPath(java.lang.String path) throws java.io.IOException, java.net.MalformedURLException
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:
java.io.IOException- if unable to resolve the pathjava.net.MalformedURLException- 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
public Resource getResource(java.lang.String path) throws java.io.IOException
Get a resource from within this resource.- Specified by:
getResourcein interfaceResourceFactory- Parameters:
path- The path to the resource- Returns:
- The resource, that might not actually exist (yet).
- Throws:
java.io.IOException- if unable to create Resource
-
getAssociate
public java.lang.Object getAssociate()
-
setAssociate
public void setAssociate(java.lang.Object o)
-
isAlias
public boolean isAlias()
- Returns:
- true if this Resource is an alias to another real Resource
-
getAlias
public java.net.URI getAlias()
- Returns:
- The canonical Alias of this resource or null if none.
-
getListHTML
public java.lang.String getListHTML(java.lang.String base, boolean parent, java.lang.String query) throws java.io.IOExceptionGet 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:
java.io.IOException- on failure to generate a list.
-
copyTo
public void copyTo(java.io.File destination) throws java.io.IOExceptionCopy the Resource to the new destination file.Will not replace existing destination file.
- Parameters:
destination- the destination file to create- Throws:
java.io.IOException- if unable to copy the resource
-
getWeakETag
public java.lang.String getWeakETag()
Generate a weak ETag reference for this Resource.- Returns:
- the weak ETag reference for this resource.
-
getWeakETag
public java.lang.String getWeakETag(java.lang.String suffix)
-
getAllResources
public java.util.Collection<Resource> getAllResources()
-
toURL
public static java.net.URL toURL(java.io.File file) throws java.net.MalformedURLExceptionGenerate a properly encoded URL from aFileinstance.- Parameters:
file- Target file.- Returns:
- URL of the target file.
- Throws:
java.net.MalformedURLException- if unable to convert File to URL
-
fromList
public static java.util.List<Resource> fromList(java.lang.String resources, boolean globDirs) throws java.io.IOException
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:
java.io.IOException
-
fromList
public static java.util.List<Resource> fromList(java.lang.String resources, boolean globDirs, ResourceFactory resourceFactory) throws java.io.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:
java.io.IOException
-
-