java.lang.Object
org.eclipse.jetty.util.resource.Resource
All Implemented Interfaces:
Iterable<Resource>
Direct Known Subclasses:
CombinedResource, MemoryResource, PathResource

public abstract class Resource extends Object implements Iterable<Resource>

A Resource is a wrapper over a Path object pointing to a file or directory that can be represented by aFileSystem.

Supports real filesystems, and also ZipFS.

  • Constructor Details

    • Resource

      public Resource()
  • Method Details

    • dump

      public static String dump(Resource resource)
    • getPath

      public abstract Path getPath()
      Return the Path corresponding to this resource.
      Returns:
      the path.
    • isContainedIn

      public abstract boolean isContainedIn(Resource r)
      Return true if this resource is contained in the Resource r, either because r is a folder or a jar file or any form of resource capable of containing other resources.
      Parameters:
      r - the containing resource
      Returns:
      true if this Resource is contained, false otherwise
    • iterator

      public Iterator<Resource> iterator()

      Return an Iterator of all Resource's referenced in this Resource.

      This is meaningful if you have a Composite Resource, otherwise it will be a single entry Iterator of this resource.

      Specified by:
      iterator in interface Iterable<Resource>
      Returns:
      the iterator of Resources.
    • exists

      public boolean exists()
      Equivalent to Files.exists(Path, LinkOption...) with the following parameters: getPath() and LinkOption.NOFOLLOW_LINKS.
      Returns:
      true if the represented resource exists.
    • isDirectory

      public abstract boolean isDirectory()
      Return true if resource represents a directory of potential resources.
      Returns:
      true if the represented resource is a container/directory.
    • isReadable

      public abstract boolean isReadable()
      True if the resource is readable.
      Returns:
      true if the represented resource exists, and can read from.
    • lastModified

      public Instant lastModified()
      The time the resource was last modified.
      Returns:
      the last modified time instant, or Instant.EPOCH if unable to obtain last modified.
    • length

      public long length()
      Length of the resource.
      Returns:
      the length of the resource in bytes, or -1L if unable to provide a size (such as a directory resource).
    • getURI

      public abstract URI getURI()
      URI representing the resource.
      Returns:
      a URI representing the given resource
    • getName

      public abstract String getName()
      The full name of the resource.
      Returns:
      the full name of the resource, or null if not backed by a Path
    • getFileName

      public abstract String getFileName()

      The file name of the resource.

      This is the last segment of the path.

      Returns:
      the filename of the resource, or "" if there are no path segments (eg: path of "/"), or null if resource has no path.
      See Also:
    • newInputStream

      public InputStream newInputStream() throws IOException
      Creates a new input stream to the resource.
      Returns:
      an input stream to the resource or null if one is not available.
      Throws:
      IOException - if there is a problem opening the input stream
    • newReadableByteChannel

      public ReadableByteChannel newReadableByteChannel() throws IOException
      Readable ByteChannel for the resource.
      Returns:
      a readable ByteChannel to the resource or null if one is not available.
      Throws:
      IOException - if unable to open the readable bytechannel for the resource.
    • list

      public List<Resource> list()

      List of contents of a directory Resource.

      Ordering is FileSystem dependent, so callers may wish to sort the return value to ensure deterministic behavior.

      Returns:
      a mutable list of resources contained in the directory resource, or an empty immutable list if unable to build the list (e.g. the resource is not a directory or not readable).
      See Also:
    • resolve

      public abstract Resource resolve(String subUriPath)
      Resolve an existing Resource.
      Parameters:
      subUriPath - the encoded subUriPath
      Returns:
      an existing Resource representing the requested subUriPath, or null if resource does not exist.
      Throws:
      IllegalArgumentException - if subUriPath is invalid
    • isAlias

      public boolean isAlias()
      Returns:
      true if this Resource is an alias to another real Resource
    • getRealURI

      public URI getRealURI()

      The real URI of the resource.

      If this Resource is an alias, (isAlias()), this URI will be different from getURI(), and will point to the real name/location of the Resource.

      Returns:
      The real URI location of this resource.
    • copyTo

      public void copyTo(Path destination) throws IOException
      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
    • getAllResources

      public Collection<Resource> getAllResources()