1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.context.support;
  17. import java.io.File;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.URL;
  22. import javax.servlet.ServletContext;
  23. import org.springframework.core.io.AbstractResource;
  24. /**
  25. * Resource implementation for ServletContext resources,
  26. * interpreting relative paths within the web application root.
  27. *
  28. * <p>Always supports stream access, but only allows java.io.File
  29. * access when the web application archive is expanded.
  30. * Always supports resolution as URL.
  31. *
  32. * @author Juergen Hoeller
  33. * @since 28.12.2003
  34. * @see javax.servlet.ServletContext#getResourceAsStream
  35. * @see javax.servlet.ServletContext#getRealPath
  36. */
  37. public class ServletContextResource extends AbstractResource {
  38. private final ServletContext servletContext;
  39. private final String path;
  40. /**
  41. * Create a new ServletContextResource.
  42. * <p>The Servlet spec requires that resource paths start with a slash,
  43. * even if many containers accept paths without leading slash too.
  44. * Consequently, the given path will be prepended with a slash if it
  45. * doesn't already start with one.
  46. * @param servletContext the ServletContext to load from
  47. * @param path the path of the resource
  48. */
  49. public ServletContextResource(ServletContext servletContext, String path) {
  50. this.servletContext = servletContext;
  51. if (path != null && !path.startsWith("/")) {
  52. path = "/" + path;
  53. }
  54. this.path = path;
  55. }
  56. /**
  57. * This implementation delegates to ServletContext.getResourceAsStream,
  58. * but throws a FileNotFoundException if not found.
  59. * @see javax.servlet.ServletContext#getResourceAsStream
  60. */
  61. public InputStream getInputStream() throws IOException {
  62. InputStream is = this.servletContext.getResourceAsStream(this.path);
  63. if (is == null) {
  64. throw new FileNotFoundException("Could not open " + getDescription());
  65. }
  66. return is;
  67. }
  68. public URL getURL() throws IOException {
  69. URL url = this.servletContext.getResource(this.path);
  70. if (url == null) {
  71. throw new FileNotFoundException(getDescription() + " cannot be resolved to URL " +
  72. "because it does not exist");
  73. }
  74. return url;
  75. }
  76. /**
  77. * This implementation delegates to ServletContext.getRealPath,
  78. * but throws a FileNotFoundException if not found or not resolvable.
  79. * @see javax.servlet.ServletContext#getRealPath
  80. */
  81. public File getFile() throws IOException {
  82. String realPath = this.servletContext.getRealPath(this.path);
  83. if (realPath == null) {
  84. throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path - " +
  85. "web application archive not expanded?");
  86. }
  87. return new File(realPath);
  88. }
  89. public String getDescription() {
  90. return "resource [" + this.path + "] of ServletContext";
  91. }
  92. }