Class ServletContextResource
- java.lang.Object
- org.springframework.core.io.AbstractResource
- org.springframework.core.io.AbstractFileResolvingResource
- org.springframework.web.context.support.ServletContextResource
- All Implemented Interfaces:
ContextResource,InputStreamSource,Resource
public class ServletContextResource extends AbstractFileResolvingResource implements ContextResource
Resourceimplementation forServletContextresources, interpreting relative paths within the web application root directory.Always supports stream access and URL access, but only allows
java.io.Fileaccess when the web application archive is expanded.- Since:
- 28.12.2003
- Author:
- Juergen Hoeller
- See Also:
ServletContext.getResourceAsStream(java.lang.String),ServletContext.getResource(java.lang.String),ServletContext.getRealPath(java.lang.String)
Constructor Summary
Constructors Constructor Description ServletContextResource(ServletContext servletContext, String path)Create a new ServletContextResource.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ResourcecreateRelative(String relativePath)This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.booleanequals(Object obj)This implementation compares the underlying ServletContext resource locations.booleanexists()This implementation checksServletContext.getResource.StringgetDescription()This implementation returns a description that includes the ServletContext resource location.FilegetFile()This implementation resolves "file:" URLs or alternatively delegates toServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.StringgetFilename()This implementation returns the name of the file that this ServletContext resource refers to.InputStreamgetInputStream()This implementation delegates toServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.StringgetPath()Return the path for this resource.StringgetPathWithinContext()Return the path within the enclosing 'context'.ServletContextgetServletContext()Return the ServletContext for this resource.URLgetURL()This implementation delegates toServletContext.getResource, but throws a FileNotFoundException if no resource found.inthashCode()This implementation returns the hash code of the underlying ServletContext resource location.booleanisReadable()This implementation delegates toServletContext.getResourceAsStream, which returnsnullin case of a non-readable resource (e.g.Methods inherited from class org.springframework.core.io.AbstractFileResolvingResource
contentLength, customizeConnection, customizeConnection, getFile, getFileForLastModifiedCheck, lastModified
Methods inherited from class org.springframework.core.io.AbstractResource
getURI, isOpen, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.core.io.Resource
contentLength, getURI, isOpen, lastModified
Constructor Detail
ServletContextResource
public ServletContextResource(ServletContext servletContext, String path)
Create a new ServletContextResource.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.
- Parameters:
servletContext- the ServletContext to load frompath- the path of the resource
Method Detail
getServletContext
public final ServletContext getServletContext()
Return the ServletContext for this resource.
exists
public boolean exists()
This implementation checksServletContext.getResource.- Specified by:
existsin interfaceResource- Overrides:
existsin classAbstractFileResolvingResource- See Also:
ServletContext.getResource(String)
isReadable
public boolean isReadable()
This implementation delegates toServletContext.getResourceAsStream, which returnsnullin case of a non-readable resource (e.g. a directory).- Specified by:
isReadablein interfaceResource- Overrides:
isReadablein classAbstractFileResolvingResource- See Also:
ServletContext.getResourceAsStream(String)
getInputStream
public InputStream getInputStream() throws IOException
This implementation delegates toServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.- Specified by:
getInputStreamin interfaceInputStreamSource- Returns:
- the input stream for the underlying resource (must not be
null) - Throws:
FileNotFoundException- if the underlying resource doesn't existIOException- if the content stream could not be opened- See Also:
ServletContext.getResourceAsStream(String)
getURL
public URL getURL() throws IOException
This implementation delegates toServletContext.getResource, but throws a FileNotFoundException if no resource found.- Specified by:
getURLin interfaceResource- Overrides:
getURLin classAbstractResource- Throws:
IOException- if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor- See Also:
ServletContext.getResource(String)
getFile
public File getFile() throws IOException
This implementation resolves "file:" URLs or alternatively delegates toServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.- Specified by:
getFilein interfaceResource- Overrides:
getFilein classAbstractFileResolvingResource- Throws:
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 failures- See Also:
ServletContext.getResource(String),ServletContext.getRealPath(String)
createRelative
public 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.- Specified by:
createRelativein interfaceResource- Overrides:
createRelativein classAbstractResource- Parameters:
relativePath- the relative path (relative to this resource)- Returns:
- the resource handle for the relative resource
- See Also:
StringUtils.applyRelativePath(String, String)
getFilename
public String getFilename()
This implementation returns the name of the file that this ServletContext resource refers to.- Specified by:
getFilenamein interfaceResource- Overrides:
getFilenamein classAbstractResource- See Also:
StringUtils.getFilename(String)
getDescription
public String getDescription()
This implementation returns a description that includes the ServletContext resource location.- Specified by:
getDescriptionin interfaceResource- See Also:
Object.toString()
getPathWithinContext
public String getPathWithinContext()
Description copied from interface:ContextResourceReturn the path within the enclosing 'context'.This is typically path relative to a context-specific root directory, e.g. a ServletContext root or a PortletContext root.
- Specified by:
getPathWithinContextin interfaceContextResource
equals
public boolean equals(Object obj)
This implementation compares the underlying ServletContext resource locations.- Overrides:
equalsin classAbstractResource- See Also:
Resource.getDescription()
hashCode
public int hashCode()
This implementation returns the hash code of the underlying ServletContext resource location.- Overrides:
hashCodein classAbstractResource- See Also:
Resource.getDescription()