org.springframework.web.context.support
Class ServletContextResource

java.lang.Object
  extended by org.springframework.core.io.AbstractResource
      extended by org.springframework.web.context.support.ServletContextResource
All Implemented Interfaces:
InputStreamSource, Resource

public class ServletContextResource
extends AbstractResource

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.

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
ServletContextResource(ServletContext servletContext, String path)
          Create a new ServletContextResource.
 
Method Summary
 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.
 String getDescription()
          This implementation returns a description that includes the ServletContext resource location.
 File getFile()
          This implementation delegates to ServletContext.getRealPath, but throws 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.
 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.
 
Methods inherited from class org.springframework.core.io.AbstractResource
exists, isOpen, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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 from
path - the path of the resource
Method Detail

getServletContext

public final ServletContext getServletContext()
Return the ServletContext for this resource.


getPath

public final String getPath()
Return the path for this resource.


getInputStream

public InputStream getInputStream()
                           throws IOException
This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if no resource found.

Throws:
IOException - if the stream could not be opened
See Also:
ServletContext.getResourceAsStream(String)

getURL

public URL getURL()
           throws IOException
This implementation delegates to ServletContext.getResource, but throws a FileNotFoundException if no resource found.

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
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 delegates to ServletContext.getRealPath, but throws a FileNotFoundException if not found or not resolvable.

Specified by:
getFile in interface Resource
Overrides:
getFile in class AbstractResource
Throws:
IOException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system
See Also:
ServletContext.getRealPath(String)

createRelative

public Resource createRelative(String relativePath)
                        throws IOException
This implementation creates a ServletContextResource, applying the given path relative to the path of the underlying file of this resource descriptor.

Specified by:
createRelative in interface Resource
Overrides:
createRelative in class AbstractResource
Parameters:
relativePath - the relative path (relative to this resource)
Returns:
the resource handle for the relative resource
Throws:
IOException - if the relative resource cannot be determined
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:
getFilename in interface Resource
Overrides:
getFilename in class AbstractResource
See Also:
StringUtils.getFilename(String)

getDescription

public String getDescription()
This implementation returns a description that includes the ServletContext resource location.

Specified by:
getDescription in interface Resource
Specified by:
getDescription in class AbstractResource
See Also:
Resource.getDescription(), AbstractResource.toString(), AbstractResource.equals(Object), AbstractResource.hashCode()

equals

public boolean equals(Object obj)
This implementation compares the underlying ServletContext resource locations.

Overrides:
equals in class AbstractResource
See Also:
AbstractResource.getDescription()

hashCode

public int hashCode()
This implementation returns the hash code of the underlying ServletContext resource location.

Overrides:
hashCode in class AbstractResource
See Also:
AbstractResource.getDescription()


Copyright (c) 2002-2007 The Spring Framework Project.