Top Description Fields Constructors Methods
org.eclipse.jetty.util.resource

public Class PathResource

extends Resource
Class Inheritance
Known Direct Subclasses
org.eclipse.jetty.util.resource.GraalIssue5720PathResource, org.eclipse.jetty.util.resource.MountedPathResource
Imports
java.io.IOException, java.net.URI, java.nio.file.DirectoryIteratorException, .Files, .LinkOption, .Path, .Paths, java.nio.file.attribute.FileTime, java.time.Instant, java.util.ArrayList, .List, .Objects, java.util.stream.Collectors, .Stream, org.eclipse.jetty.util.Index, .URIUtil, org.slf4j.Logger, .LoggerFactory

Java NIO Path Resource.

Field Summary

Modifier and TypeField and Description
private Boolean
private static final Logger
private final Path
private Path
public static Index<String>
SUPPORTED_SCHEMES

Deprecated for removal since 12.0.4. Using ResourceFactoryInternals.isSupported() instead.
private final URI

Constructor Summary

AccessConstructor and Description
pack-priv
PathResource(URI
the URI to build this PathResource from.
uri
)

Construct a new PathResource from a URI object.

pack-priv
PathResource(URI uri, boolean bypassAllowedSchemeCheck)

pack-priv
pack-priv
PathResource(Path
the Path object
path
,
URI
the as-requested URI for the resource
uri
,
boolean
true to bypass the allowed schemes check
bypassAllowedSchemeCheck
)

Create a PathResource.

Method Summary

Modifier and TypeMethod and Description
public boolean
contains(Resource
the resource
other
)

Overrides org.eclipse.jetty.util.resource.Resource.contains.

Return true if this resource deeply contains the other Resource.
public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public boolean
public String
getFileName()

Implements abstract org.eclipse.jetty.util.resource.Resource.getFileName.

The file name of the resource.

public String
getName()

Implements abstract org.eclipse.jetty.util.resource.Resource.getName.

The full name of the resource.
public Path
getPath()

Implements abstract org.eclipse.jetty.util.resource.Resource.getPath.

Return the Path corresponding to this resource.
public Path
public URI
getRealURI()

Overrides org.eclipse.jetty.util.resource.Resource.getRealURI.

The real URI of the resource.

public URI
getURI()

Implements abstract org.eclipse.jetty.util.resource.Resource.getURI.

URI representing the resource.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean
public boolean
isDirectory()

Implements abstract org.eclipse.jetty.util.resource.Resource.isDirectory.

Return true if resource represents a directory of potential resources.
public boolean
isReadable()

Implements abstract org.eclipse.jetty.util.resource.Resource.isReadable.

True if the resource is readable.
public static boolean
isSameName(Path pathA, Path pathB)

Test if the paths are the same name.

public Instant
lastModified()

Overrides org.eclipse.jetty.util.resource.Resource.lastModified.

The time the resource was last modified.
public long
length()

Overrides org.eclipse.jetty.util.resource.Resource.length.

Length of the resource.
public List<Resource>
list()

Overrides org.eclipse.jetty.util.resource.Resource.list.

List of contents of a directory Resource.

protected Resource
newResource(Path path, URI uri)

Internal override for creating a new PathResource.

public Resource
resolve(String
the encoded subUriPath
subUriPath
)

Implements abstract org.eclipse.jetty.util.resource.Resource.resolve.

Resolve an existing Resource.
private void
resolveAlias()

Perform a check of the original Path and as-requested URI to determine if this resource is an alias to another name/location.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
protected URI

Returns:

the appropriate URI for the path
toUri
(Path
the path to convert to URI
path
)

Ensure Path to URI is sane when it returns a directory reference.

Inherited from org.eclipse.jetty.util.resource.Resource:
copyTodumpensureDirExistsgetAllResourcesgetPathToisContainedIniteratornewInputStreamnewReadableByteChannelresolveDifferentFileSystem

Field Detail

aliasback to summary
private Boolean alias
LOGback to summary
private static final Logger LOG

Hides org.eclipse.jetty.util.resource.Resource.LOG.

pathback to summary
private final Path path
realPathback to summary
private Path realPath
SUPPORTED_SCHEMESback to summary
public static Index<String> SUPPORTED_SCHEMES

Deprecated

for removal since 12.0.4.

Using ResourceFactoryInternals.isSupported() instead.

Annotations
@Deprecated
since:12.0.4
forRemoval:true
uriback to summary
private final URI uri

Constructor Detail

PathResourceback to summary
pack-priv PathResource(URI uri)

Construct a new PathResource from a URI object.

Must be an absolute URI using the file scheme.

Parameters
uri:URI

the URI to build this PathResource from.

PathResourceback to summary
pack-priv PathResource(URI uri, boolean bypassAllowedSchemeCheck)
PathResourceback to summary
pack-priv PathResource(Path path)
PathResourceback to summary
pack-priv PathResource(Path path, URI uri, boolean bypassAllowedSchemeCheck)

Create a PathResource.

Parameters
path:Path

the Path object

uri:URI

the as-requested URI for the resource

bypassAllowedSchemeCheck:boolean

true to bypass the allowed schemes check

Method Detail

containsback to summary
public boolean contains(Resource other)

Overrides org.eclipse.jetty.util.resource.Resource.contains.

Doc from org.eclipse.jetty.util.resource.Resource.contains.

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.

Parameters
other:Resource

the resource

Returns:boolean

true if this Resource is deeply contains the other Resource, false otherwise

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
existsback to summary
public boolean exists()

Overrides org.eclipse.jetty.util.resource.Resource.exists.

Doc from org.eclipse.jetty.util.resource.Resource.exists.

Equivalent to Files#exists(Path, LinkOption...) with the following parameters: getPath() and LinkOption#NOFOLLOW_LINKS.

Returns:boolean

true if the represented resource exists.

Annotations
@Override
getFileNameback to summary
public String getFileName()

Implements abstract org.eclipse.jetty.util.resource.Resource.getFileName.

Doc from org.eclipse.jetty.util.resource.Resource.getFileName.

The file name of the resource.

This is the last segment of the path.

Returns:String

the filename of the resource, or "" if there are no path segments (eg: path of "/"), or null if resource cannot determine a filename.

Annotations
@Override
getNameback to summary
public String getName()

Implements abstract org.eclipse.jetty.util.resource.Resource.getName.

Doc from org.eclipse.jetty.util.resource.Resource.getName.

The full name of the resource.

Returns:String

the full name of the resource, or null if there is no name for the resource.

Annotations
@Override
getPathback to summary
public Path getPath()

Implements abstract org.eclipse.jetty.util.resource.Resource.getPath.

Doc from org.eclipse.jetty.util.resource.Resource.getPath.

Return the Path corresponding to this resource.

Returns:Path

the path or null if there is no Path representation.

Annotations
@Override
getRealPathback to summary
public Path getRealPath()
getRealURIback to summary
public URI getRealURI()

Overrides org.eclipse.jetty.util.resource.Resource.getRealURI.

Doc from org.eclipse.jetty.util.resource.Resource.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:URI

The real URI location of this resource.

Annotations
@Override
getURIback to summary
public URI getURI()

Implements abstract org.eclipse.jetty.util.resource.Resource.getURI.

Doc from org.eclipse.jetty.util.resource.Resource.getURI.

URI representing the resource.

Returns:URI

a URI representing the given resource, or null if there is no URI representation of the resource.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isAliasback to summary
public boolean isAlias()

Overrides org.eclipse.jetty.util.resource.Resource.isAlias.

Returns:boolean

Doc from org.eclipse.jetty.util.resource.Resource.isAlias.

true if this Resource is an alias to another real Resource

Annotations
@Override
isDirectoryback to summary
public boolean isDirectory()

Implements abstract org.eclipse.jetty.util.resource.Resource.isDirectory.

Doc from org.eclipse.jetty.util.resource.Resource.isDirectory.

Return true if resource represents a directory of potential resources.

Returns:boolean

true if the represented resource is a container/directory.

Annotations
@Override
isReadableback to summary
public boolean isReadable()

Implements abstract org.eclipse.jetty.util.resource.Resource.isReadable.

Doc from org.eclipse.jetty.util.resource.Resource.isReadable.

True if the resource is readable.

Returns:boolean

true if the represented resource exists, and can read from.

Annotations
@Override
isSameNameback to summary
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)

lastModifiedback to summary
public Instant lastModified()

Overrides org.eclipse.jetty.util.resource.Resource.lastModified.

Doc from org.eclipse.jetty.util.resource.Resource.lastModified.

The time the resource was last modified.

Returns:Instant

the last modified time instant, or Instant#EPOCH if unable to obtain last modified.

Annotations
@Override
lengthback to summary
public long length()

Overrides org.eclipse.jetty.util.resource.Resource.length.

Doc from org.eclipse.jetty.util.resource.Resource.length.

Length of the resource.

Returns:long

the length of the resource in bytes, or -1L if unable to provide a size (such as a directory resource).

Annotations
@Override
listback to summary
public List<Resource> list()

Overrides org.eclipse.jetty.util.resource.Resource.list.

Doc from org.eclipse.jetty.util.resource.Resource.list.

List of contents of a directory Resource.

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

Returns:List<Resource>

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).

newResourceback to summary
protected Resource newResource(Path path, URI uri)

Internal override for creating a new PathResource. Used by MountedPathResource (eg)

resolveback to summary
public Resource resolve(String subUriPath)

Implements abstract org.eclipse.jetty.util.resource.Resource.resolve.

Doc from org.eclipse.jetty.util.resource.Resource.resolve.

Resolve an existing Resource.

Parameters
subUriPath:String

the encoded subUriPath

Returns:Resource

an existing Resource representing the requested subUriPath, or null if resource does not exist.

Annotations
@Override
resolveAliasback to summary
private void resolveAlias()

Perform a check of the original Path and as-requested URI to determine if this resource is an alias to another name/location.

Alias Check Logic
path realPath uri-as-requested uri-from-realPath alias
C:/temp/aa./foo.txt C:/temp/aa/foo.txt file:///C:/temp/aa./foo.txt file:///C:/temp/aa./foo.txt true
/tmp/foo-symlink /tmp/bar.txt file:///tmp/foo-symlink file:///tmp/bar.txt true
C:/temp/aa.txt C:/temp/AA.txt file:///C:/temp/aa.txt file:///C:/temp/AA.txt true
/tmp/bar-exists/../foo.txt /tmp/foo.txt file:///tmp/bar-exists/../foo.txt file:///tmp/foo.txt true
/tmp/doesnt-exist.txt null (does not exist) file:///tmp/doesnt-exist.txt null (does not exist) false
/tmp/doesnt-exist/../foo.txt null (intermediate does not exist) file:///tmp/doesnt-exist/../foo.txt null (intermediate does not exist) false
/var/protected/config.xml null (no permissions) file:///var/protected/config.xml null (no permission) false
/tmp/foo-symlink null (broken symlink, doesn't point to anything) file:///tmp/foo-symlink null (broken symlink, doesn't point to anything) false
C:/temp/cannot:be:referenced null (illegal filename) file:///C:/temp/cannot:be:referenced null (illegal filename) false
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
toUriback to summary
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:Path

the path to convert to URI

Returns:URI

the appropriate URI for the path