1 /*
2 * Copyright 2002-2008 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
17 package org.springframework.web.context.support;
18
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25
26 import javax.servlet.ServletContext;
27
28 import org.springframework.core.io.AbstractResource;
29 import org.springframework.core.io.ContextResource;
30 import org.springframework.core.io.Resource;
31 import org.springframework.util.Assert;
32 import org.springframework.util.StringUtils;
33 import org.springframework.web.util.WebUtils;
34
35 /**
36 * {@link org.springframework.core.io.Resource} implementation for
37 * {@link javax.servlet.ServletContext} resources, interpreting
38 * relative paths within the web application root directory.
39 *
40 * <p>Always supports stream access and URL access, but only allows
41 * <code>java.io.File</code> access when the web application archive
42 * is expanded.
43 *
44 * @author Juergen Hoeller
45 * @since 28.12.2003
46 * @see javax.servlet.ServletContext#getResourceAsStream
47 * @see javax.servlet.ServletContext#getResource
48 * @see javax.servlet.ServletContext#getRealPath
49 */
50 public class ServletContextResource extends AbstractResource implements ContextResource {
51
52 private final ServletContext servletContext;
53
54 private final String path;
55
56
57 /**
58 * Create a new ServletContextResource.
59 * <p>The Servlet spec requires that resource paths start with a slash,
60 * even if many containers accept paths without leading slash too.
61 * Consequently, the given path will be prepended with a slash if it
62 * doesn't already start with one.
63 * @param servletContext the ServletContext to load from
64 * @param path the path of the resource
65 */
66 public ServletContextResource(ServletContext servletContext, String path) {
67 // check ServletContext
68 Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");
69 this.servletContext = servletContext;
70
71 // check path
72 Assert.notNull(path, "Path is required");
73 String pathToUse = StringUtils.cleanPath(path);
74 if (!pathToUse.startsWith("/")) {
75 pathToUse = "/" + pathToUse;
76 }
77 this.path = pathToUse;
78 }
79
80 /**
81 * Return the ServletContext for this resource.
82 */
83 public final ServletContext getServletContext() {
84 return this.servletContext;
85 }
86
87 /**
88 * Return the path for this resource.
89 */
90 public final String getPath() {
91 return this.path;
92 }
93
94
95 /**
96 * This implementation checks <code>ServletContext.getResource</code>.
97 * @see javax.servlet.ServletContext#getResource(String)
98 */
99 public boolean exists() {
100 try {
101 URL url = this.servletContext.getResource(this.path);
102 return (url != null);
103 }
104 catch (MalformedURLException ex) {
105 return false;
106 }
107 }
108
109 /**
110 * This implementation delegates to <code>ServletContext.getResourceAsStream</code>,
111 * but throws a FileNotFoundException if no resource found.
112 * @see javax.servlet.ServletContext#getResourceAsStream(String)
113 */
114 public InputStream getInputStream() throws IOException {
115 InputStream is = this.servletContext.getResourceAsStream(this.path);
116 if (is == null) {
117 throw new FileNotFoundException("Could not open " + getDescription());
118 }
119 return is;
120 }
121
122 /**
123 * This implementation delegates to <code>ServletContext.getResource</code>,
124 * but throws a FileNotFoundException if no resource found.
125 * @see javax.servlet.ServletContext#getResource(String)
126 */
127 public URL getURL() throws IOException {
128 URL url = this.servletContext.getResource(this.path);
129 if (url == null) {
130 throw new FileNotFoundException(
131 getDescription() + " cannot be resolved to URL because it does not exist");
132 }
133 return url;
134 }
135
136 /**
137 * This implementation delegates to <code>ServletContext.getRealPath</code>,
138 * but throws a FileNotFoundException if not found or not resolvable.
139 * @see javax.servlet.ServletContext#getRealPath(String)
140 */
141 public File getFile() throws IOException {
142 String realPath = WebUtils.getRealPath(this.servletContext, this.path);
143 return new File(realPath);
144 }
145
146 /**
147 * This implementation creates a ServletContextResource, applying the given path
148 * relative to the path of the underlying file of this resource descriptor.
149 * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
150 */
151 public Resource createRelative(String relativePath) {
152 String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
153 return new ServletContextResource(this.servletContext, pathToUse);
154 }
155
156 /**
157 * This implementation returns the name of the file that this ServletContext
158 * resource refers to.
159 * @see org.springframework.util.StringUtils#getFilename(String)
160 */
161 public String getFilename() {
162 return StringUtils.getFilename(this.path);
163 }
164
165 /**
166 * This implementation returns a description that includes the ServletContext
167 * resource location.
168 */
169 public String getDescription() {
170 return "ServletContext resource [" + this.path + "]";
171 }
172
173 public String getPathWithinContext() {
174 return this.path;
175 }
176
177
178 /**
179 * This implementation compares the underlying ServletContext resource locations.
180 */
181 public boolean equals(Object obj) {
182 if (obj == this) {
183 return true;
184 }
185 if (obj instanceof ServletContextResource) {
186 ServletContextResource otherRes = (ServletContextResource) obj;
187 return (this.servletContext.equals(otherRes.servletContext) && this.path.equals(otherRes.path));
188 }
189 return false;
190 }
191
192 /**
193 * This implementation returns the hash code of the underlying
194 * ServletContext resource location.
195 */
196 public int hashCode() {
197 return this.path.hashCode();
198 }
199
200 }