Class PathResource

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

public class PathResource extends Resource
Java NIO Path Resource.
  • Field Details

    • SUPPORTED_SCHEMES

      @Deprecated(since="12.0.4", forRemoval=true) public static Index<String> SUPPORTED_SCHEMES
      Deprecated, for removal: This API element is subject to removal in a future version.
      Using ResourceFactoryInternals.isSupported() instead.
  • Method Details

    • isSameName

      public static boolean isSameName(Path pathA, Path pathB)
      Test if the paths are the same name.

      If the real path is not the same as the absolute path then we know that the real path is the alias for the provided path.

      For OS's that are case insensitive, this should return the real (on-disk / case correct) version of the path.

      We have to be careful on Windows and OSX.

      Assume we have the following scenario:

         Path a = new File("foo").toPath();
         Files.createFile(a);
         Path b = new File("FOO").toPath();
       

      There now exists a file called foo on disk. Using Windows or OSX, with a Path reference of FOO, Foo, fOO, etc.. means the following

                              |  OSX    |  Windows   |  Linux
       -----------------------+---------+------------+---------
       Files.exists(a)        |  True   |  True      |  True
       Files.exists(b)        |  True   |  True      |  False
       Files.isSameFile(a,b)  |  True   |  True      |  False
       a.equals(b)            |  False  |  True      |  False
       

      See the javadoc for Path.equals() for details about this FileSystem behavior difference

      We also cannot rely on a.compareTo(b) as this is roughly equivalent in implementation to a.equals(b)

    • exists

      public boolean exists()
      Description copied from class: Resource
      Equivalent to Files.exists(Path, LinkOption...) with the following parameters: Resource.getPath() and LinkOption.NOFOLLOW_LINKS.
      Overrides:
      exists in class Resource
      Returns:
      true if the represented resource exists.
    • getPath

      public Path getPath()
      Description copied from class: Resource
      Return the Path corresponding to this resource.
      Specified by:
      getPath in class Resource
      Returns:
      the path or null if there is no Path representation.
    • contains

      public boolean contains(Resource other)
      Description copied from class: Resource
      Return true if this resource deeply contains the other Resource. This resource must be a directory or a jar file or any form of resource capable of containing other resources.
      Overrides:
      contains in class Resource
      Parameters:
      other - the resource
      Returns:
      true if this Resource is deeply contains the other Resource, false otherwise
      See Also:
    • getRealPath

      public Path getRealPath()
    • getRealURI

      public URI getRealURI()
      Description copied from class: Resource

      The real URI of the resource.

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

      Overrides:
      getRealURI in class Resource
      Returns:
      The real URI location of this resource.
    • list

      public List<Resource> list()
      Description copied from class: Resource

      List of contents of a directory Resource.

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

      Overrides:
      list in class Resource
      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:
    • isAlias

      public boolean isAlias()
      Overrides:
      isAlias in class Resource
      Returns:
      true if this Resource is an alias to another real Resource
    • getName

      public String getName()
      Description copied from class: Resource
      The full name of the resource.
      Specified by:
      getName in class Resource
      Returns:
      the full name of the resource, or null if there is no name for the resource.
    • getFileName

      public String getFileName()
      Description copied from class: Resource

      The file name of the resource.

      This is the last segment of the path.

      Specified by:
      getFileName in class Resource
      Returns:
      the filename of the resource, or "" if there are no path segments (eg: path of "/"), or null if resource cannot determine a filename.
      See Also:
    • getURI

      public URI getURI()
      Description copied from class: Resource
      URI representing the resource.
      Specified by:
      getURI in class Resource
      Returns:
      a URI representing the given resource, or null if there is no URI representation of the resource.
    • resolve

      public Resource resolve(String subUriPath)
      Description copied from class: Resource
      Resolve an existing Resource.
      Specified by:
      resolve in class Resource
      Parameters:
      subUriPath - the encoded subUriPath
      Returns:
      a Resource representing the requested subUriPath, which may not exist, or null if the resource cannot exist.
    • newResource

      protected Resource newResource(Path path, URI uri)
      Internal override for creating a new PathResource. Used by MountedPathResource (eg)
    • isDirectory

      public boolean isDirectory()
      Description copied from class: Resource
      Return true if resource represents a directory of potential resources.
      Specified by:
      isDirectory in class Resource
      Returns:
      true if the represented resource is a container/directory.
    • isReadable

      public boolean isReadable()
      Description copied from class: Resource
      True if the resource is readable.
      Specified by:
      isReadable in class Resource
      Returns:
      true if the represented resource exists, and can read from.
    • lastModified

      public Instant lastModified()
      Description copied from class: Resource
      The time the resource was last modified.
      Overrides:
      lastModified in class Resource
      Returns:
      the last modified time instant, or Instant.EPOCH if unable to obtain last modified.
    • length

      public long length()
      Description copied from class: Resource
      Length of the resource.
      Overrides:
      length in class Resource
      Returns:
      the length of the resource in bytes, or -1L if unable to provide a size (such as a directory resource).
    • toUri

      protected URI toUri(Path path)
      Ensure Path to URI is sane when it returns a directory reference.

      This is different than Path.toUri() in that not all FileSystems seem to put the trailing slash on a directory reference in the URI.

      Parameters:
      path - the path to convert to URI
      Returns:
      the appropriate URI for the path
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object