public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource
Resource
implementation for
ServletContext
resources, interpreting
relative paths within the web application root directory.
Always supports stream access and URL access, but only allows
java.io.File
access when the web application archive
is expanded.
ServletContext.getResourceAsStream(java.lang.String)
,
ServletContext.getResource(java.lang.String)
,
ServletContext.getRealPath(java.lang.String)
Constructor and Description |
---|
ServletContextResource(ServletContext servletContext,
String path)
Create a new ServletContextResource.
|
Modifier and Type | Method and Description |
---|---|
Resource |
createRelative(String relativePath)
This implementation creates a ServletContextResource, applying the given path
relative to the path of the underlying file of this resource descriptor.
|
boolean |
equals(Object obj)
This implementation compares the underlying ServletContext resource locations.
|
boolean |
exists()
This implementation checks
ServletContext.getResource . |
String |
getDescription()
This implementation returns a description that includes the ServletContext
resource location.
|
File |
getFile()
This implementation resolves "file:" URLs or alternatively delegates to
ServletContext.getRealPath , throwing a FileNotFoundException
if not found or not resolvable. |
String |
getFilename()
This implementation returns the name of the file that this ServletContext
resource refers to.
|
InputStream |
getInputStream()
This implementation delegates to
ServletContext.getResourceAsStream ,
but throws a FileNotFoundException if no resource found. |
String |
getPath()
Return the path for this resource.
|
String |
getPathWithinContext()
Return the path within the enclosing 'context'.
|
ServletContext |
getServletContext()
Return the ServletContext for this resource.
|
URL |
getURL()
This implementation delegates to
ServletContext.getResource ,
but throws a FileNotFoundException if no resource found. |
int |
hashCode()
This implementation returns the hash code of the underlying
ServletContext resource location.
|
boolean |
isFile()
This implementation always returns
false . |
boolean |
isReadable()
This implementation delegates to
ServletContext.getResourceAsStream ,
which returns null in case of a non-readable resource (e.g. |
contentLength, customizeConnection, customizeConnection, getFile, getFileForLastModifiedCheck, isFile, lastModified, readableChannel
getURI, isOpen, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
contentLength, getURI, isOpen, lastModified, readableChannel
public ServletContextResource(ServletContext servletContext, String path)
The Servlet spec requires that resource paths start with a slash, even if many containers accept paths without leading slash too. Consequently, the given path will be prepended with a slash if it doesn't already start with one.
servletContext
- the ServletContext to load frompath
- the path of the resourcepublic final ServletContext getServletContext()
public final String getPath()
public boolean exists()
ServletContext.getResource
.exists
in interface Resource
exists
in class AbstractFileResolvingResource
ServletContext.getResource(String)
public boolean isReadable()
ServletContext.getResourceAsStream
,
which returns null
in case of a non-readable resource (e.g. a directory).isReadable
in interface Resource
isReadable
in class AbstractFileResolvingResource
ServletContext.getResourceAsStream(String)
public boolean isFile()
AbstractResource
false
.isFile
in interface Resource
isFile
in class AbstractFileResolvingResource
Resource.getFile()
public InputStream getInputStream() throws IOException
ServletContext.getResourceAsStream
,
but throws a FileNotFoundException if no resource found.getInputStream
in interface InputStreamSource
null
)FileNotFoundException
- if the underlying resource doesn't existIOException
- if the content stream could not be openedServletContext.getResourceAsStream(String)
public URL getURL() throws IOException
ServletContext.getResource
,
but throws a FileNotFoundException if no resource found.getURL
in interface Resource
getURL
in class AbstractResource
IOException
- if the resource cannot be resolved as URL,
i.e. if the resource is not available as descriptorServletContext.getResource(String)
public File getFile() throws IOException
ServletContext.getRealPath
, throwing a FileNotFoundException
if not found or not resolvable.getFile
in interface Resource
getFile
in class AbstractFileResolvingResource
FileNotFoundException
- if the resource cannot be resolved as
absolute file path, i.e. if the resource is not available in a file systemIOException
- in case of general resolution/reading failuresServletContext.getResource(String)
,
ServletContext.getRealPath(String)
public Resource createRelative(String relativePath)
createRelative
in interface Resource
createRelative
in class AbstractResource
relativePath
- the relative path (relative to this resource)StringUtils.applyRelativePath(String, String)
@Nullable public String getFilename()
getFilename
in interface Resource
getFilename
in class AbstractResource
StringUtils.getFilename(String)
public String getDescription()
getDescription
in interface Resource
Object.toString()
public String getPathWithinContext()
ContextResource
This is typically path relative to a context-specific root directory, e.g. a ServletContext root or a PortletContext root.
getPathWithinContext
in interface ContextResource
public boolean equals(Object obj)
equals
in class AbstractResource
Resource.getDescription()
public int hashCode()
hashCode
in class AbstractResource
Resource.getDescription()