public class UrlResource extends AbstractFileResolvingResource
Resource
implementation for java.net.URL
locators.
Supports resolution as a URL
and also as a File
in
case of the "file:"
protocol.URL
Modifier and Type | Field and Description |
---|---|
private java.net.URL |
cleanedUrl
Cleaned URL (with normalized path), used for comparisons.
|
private java.net.URI |
uri
Original URI, if available; used for URI and File access.
|
private java.net.URL |
url
Original URL, used for actual access.
|
Constructor and Description |
---|
UrlResource(java.lang.String path)
Create a new
UrlResource based on a URL path. |
UrlResource(java.lang.String protocol,
java.lang.String location)
Create a new
UrlResource based on a URI specification. |
UrlResource(java.lang.String protocol,
java.lang.String location,
java.lang.String fragment)
Create a new
UrlResource based on a URI specification. |
UrlResource(java.net.URI uri)
Create a new
UrlResource based on the given URI object. |
UrlResource(java.net.URL url)
Create a new
UrlResource based on the given URL object. |
Modifier and Type | Method and Description |
---|---|
Resource |
createRelative(java.lang.String relativePath)
This implementation creates a
UrlResource , applying the given path
relative to the path of the underlying URL of this resource descriptor. |
boolean |
equals(java.lang.Object obj)
This implementation compares the underlying URL references.
|
private java.net.URL |
getCleanedUrl(java.net.URL originalUrl,
java.lang.String originalPath)
Determine a cleaned URL for the given original URL.
|
java.lang.String |
getDescription()
This implementation returns a description that includes the URL.
|
java.io.File |
getFile()
This implementation returns a File reference for the underlying URL/URI,
provided that it refers to a file in the file system.
|
java.lang.String |
getFilename()
This implementation returns the name of the file that this URL refers to.
|
java.io.InputStream |
getInputStream()
This implementation opens an InputStream for the given URL.
|
java.net.URI |
getURI()
This implementation returns the underlying URI directly,
if possible.
|
java.net.URL |
getURL()
This implementation returns the underlying URL reference.
|
int |
hashCode()
This implementation returns the hash code of the underlying URL reference.
|
contentLength, customizeConnection, customizeConnection, exists, getFile, getFileForLastModifiedCheck, isReadable, lastModified
isOpen, toString
private final java.net.URI uri
private final java.net.URL url
private final java.net.URL cleanedUrl
public UrlResource(java.net.URI uri) throws java.net.MalformedURLException
UrlResource
based on the given URI object.uri
- a URIjava.net.MalformedURLException
- if the given URL path is not validpublic UrlResource(java.net.URL url)
UrlResource
based on the given URL object.url
- a URLpublic UrlResource(java.lang.String path) throws java.net.MalformedURLException
UrlResource
based on a URL path.
Note: The given path needs to be pre-encoded if necessary.
path
- a URL pathjava.net.MalformedURLException
- if the given URL path is not validURL.URL(String)
public UrlResource(java.lang.String protocol, java.lang.String location) throws java.net.MalformedURLException
UrlResource
based on a URI specification.
The given parts will automatically get encoded if necessary.
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon);
also known as "scheme"location
- the location (e.g. the file path within that protocol);
also known as "scheme-specific part"java.net.MalformedURLException
- if the given URL specification is not validURI.URI(String, String, String)
public UrlResource(java.lang.String protocol, java.lang.String location, java.lang.String fragment) throws java.net.MalformedURLException
UrlResource
based on a URI specification.
The given parts will automatically get encoded if necessary.
protocol
- the URL protocol to use (e.g. "jar" or "file" - without colon);
also known as "scheme"location
- the location (e.g. the file path within that protocol);
also known as "scheme-specific part"fragment
- the fragment within that location (e.g. anchor on an HTML page,
as following after a "#" separator)java.net.MalformedURLException
- if the given URL specification is not validURI.URI(String, String, String)
private java.net.URL getCleanedUrl(java.net.URL originalUrl, java.lang.String originalPath)
originalUrl
- the original URLoriginalPath
- the original URL pathStringUtils.cleanPath(java.lang.String)
public java.io.InputStream getInputStream() throws java.io.IOException
It sets the useCaches
flag to false
,
mainly to avoid jar file locking on Windows.
null
)java.io.IOException
- if the stream could not be openedURL.openConnection()
,
URLConnection.setUseCaches(boolean)
,
URLConnection.getInputStream()
public java.net.URL getURL() throws java.io.IOException
getURL
in interface Resource
getURL
in class AbstractResource
java.io.IOException
- if the resource cannot be resolved as URL,
i.e. if the resource is not available as descriptorpublic java.net.URI getURI() throws java.io.IOException
getURI
in interface Resource
getURI
in class AbstractResource
java.io.IOException
- if the resource cannot be resolved as URI,
i.e. if the resource is not available as descriptorpublic java.io.File getFile() throws java.io.IOException
getFile
in interface Resource
getFile
in class AbstractFileResolvingResource
java.io.IOException
- if the resource cannot be resolved as absolute
file path, i.e. if the resource is not available in a file systemResourceUtils.getFile(java.net.URL, String)
public Resource createRelative(java.lang.String relativePath) throws java.net.MalformedURLException
UrlResource
, applying the given path
relative to the path of the underlying URL of this resource descriptor.createRelative
in interface Resource
createRelative
in class AbstractResource
relativePath
- the relative path (relative to this resource)java.net.MalformedURLException
URL.URL(java.net.URL, String)
public java.lang.String getFilename()
getFilename
in interface Resource
getFilename
in class AbstractResource
URL.getFile()
,
File.getName()
public java.lang.String getDescription()
Object.toString()
public boolean equals(java.lang.Object obj)
equals
in class AbstractResource
Resource.getDescription()
public int hashCode()
hashCode
in class AbstractResource
Resource.getDescription()