spring-framework / org.springframework.web.context.support / ServletContextResource

ServletContextResource

open class ServletContextResource : AbstractFileResolvingResource, ContextResource

org.springframework.core.io.Resource implementation for javax.servlet.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.

Author
Juergen Hoeller

Since
28.12.2003

See Also
javax.servlet.ServletContext#getResourceAsStreamjavax.servlet.ServletContext#getResourcejavax.servlet.ServletContext#getRealPath

Constructors

<init>

ServletContextResource(servletContext: ServletContext, path: String)

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.

Functions

createRelative

open fun createRelative(relativePath: String): Resource

This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.

equals

open fun equals(other: Any?): Boolean

This implementation compares the underlying ServletContext resource locations.

exists

open fun exists(): Boolean

This implementation checks ServletContext.getResource.

getDescription

open fun getDescription(): String

This implementation returns a description that includes the ServletContext resource location.

getFile

open fun getFile(): File

This implementation resolves "file:" URLs or alternatively delegates to ServletContext.getRealPath, throwing a FileNotFoundException if not found or not resolvable.

getFilename

open fun getFilename(): String

This implementation returns the name of the file that this ServletContext resource refers to.

getInputStream

open fun getInputStream(): InputStream

This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.

getPath

fun getPath(): String

Return the path for this resource.

getPathWithinContext

open fun getPathWithinContext(): String

getServletContext

fun getServletContext(): ServletContext

Return the ServletContext for this resource.

getURL

open fun getURL(): URL

This implementation delegates to ServletContext.getResource, but throws a FileNotFoundException if no resource found.

hashCode

open fun hashCode(): Int

This implementation returns the hash code of the underlying ServletContext resource location.

isFile

open fun isFile(): Boolean

isReadable

open fun isReadable(): Boolean

This implementation delegates to ServletContext.getResourceAsStream, which returns null in case of a non-readable resource (e.g. a directory).