org.springframework.web.context.support
Class ServletContextResource

java.lang.Object
  extended byorg.springframework.core.io.AbstractResource
      extended byorg.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.

Always supports stream access, but only allows java.io.File access when the web application archive is expanded. Always supports resolution as URL.

Since:
28.12.2003
Author:
Juergen Hoeller
See Also:
ServletContext.getResourceAsStream(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 throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.
 boolean equals(Object obj)
          This implementation compares description strings.
 String getDescription()
          Return a description for this resource, to be used for error output when working with the resource.
 File getFile()
          This implementation delegates to ServletContext.getRealPath, but throws a FileNotFoundException if not found or not resolvable.
 String getFilename()
          This implementation always throws IllegalStateException, assuming that the resource does not carry a filename.
 InputStream getInputStream()
          This implementation delegates to ServletContext.getResourceAsStream, but throws a FileNotFoundException if not found.
protected  String getPath()
          Return the path for this resource.
protected  ServletContext getServletContext()
          Return the ServletContext for this resource.
 URL getURL()
          This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.
 int hashCode()
          This implementation returns the description's hash code.
 
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

protected ServletContext getServletContext()
Return the ServletContext for this resource. Only for internal use.


getPath

protected String getPath()
Return the path for this resource. Only for internal use.


getInputStream

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

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

getURL

public URL getURL()
           throws IOException
Description copied from class: AbstractResource
This implementation throws a FileNotFoundException, assuming that the resource cannot be resolved to a URL.

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
Throws:
IOException

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
See Also:
ServletContext.getRealPath(java.lang.String)

createRelative

public Resource createRelative(String relativePath)
                        throws IOException
Description copied from class: AbstractResource
This implementation throws a FileNotFoundException, assuming that relative resources cannot be created for this resource.

Specified by:
createRelative in interface Resource
Overrides:
createRelative in class AbstractResource
Throws:
IOException

getFilename

public String getFilename()
Description copied from class: AbstractResource
This implementation always throws IllegalStateException, assuming that the resource does not carry a filename.

Specified by:
getFilename in interface Resource
Overrides:
getFilename in class AbstractResource

getDescription

public String getDescription()
Description copied from interface: Resource
Return a description for this resource, to be used for error output when working with the resource.

Implementations are also encouraged to return this value from their toString method.

See Also:
Object.toString()

equals

public boolean equals(Object obj)
Description copied from class: AbstractResource
This implementation compares description strings.

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

hashCode

public int hashCode()
Description copied from class: AbstractResource
This implementation returns the description's hash code.

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


Copyright (C) 2003-2004 The Spring Framework Project.