KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > ServletContextResource


1 /*
2  * Copyright 2002-2005 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 JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import javax.servlet.ServletContext JavaDoc;
26
27 import org.springframework.core.io.AbstractResource;
28 import org.springframework.core.io.Resource;
29 import org.springframework.util.Assert;
30 import org.springframework.util.StringUtils;
31 import org.springframework.web.util.WebUtils;
32
33 /**
34  * Resource implementation for ServletContext resources,
35  * interpreting relative paths within the web application root directory.
36  *
37  * <p>Always supports stream access and URL access, but only allows
38  * <code>java.io.File</code> access when the web application archive
39  * is expanded.
40  *
41  * @author Juergen Hoeller
42  * @since 28.12.2003
43  * @see javax.servlet.ServletContext#getResourceAsStream
44  * @see javax.servlet.ServletContext#getResource
45  * @see javax.servlet.ServletContext#getRealPath
46  */

47 public class ServletContextResource extends AbstractResource {
48
49     private final ServletContext JavaDoc servletContext;
50
51     private final String JavaDoc path;
52
53
54     /**
55      * Create a new ServletContextResource.
56      * <p>The Servlet spec requires that resource paths start with a slash,
57      * even if many containers accept paths without leading slash too.
58      * Consequently, the given path will be prepended with a slash if it
59      * doesn't already start with one.
60      * @param servletContext the ServletContext to load from
61      * @param path the path of the resource
62      */

63     public ServletContextResource(ServletContext JavaDoc servletContext, String JavaDoc path) {
64         // check ServletContext
65
Assert.notNull(servletContext, "Cannot resolve ServletContextResource without ServletContext");
66         this.servletContext = servletContext;
67
68         // check path
69
Assert.notNull(path, "path is required");
70         if (!path.startsWith("/")) {
71             path = "/" + path;
72         }
73         this.path = StringUtils.cleanPath(path);
74     }
75
76     /**
77      * Return the ServletContext for this resource.
78      */

79     public final ServletContext JavaDoc getServletContext() {
80         return servletContext;
81     }
82
83     /**
84      * Return the path for this resource.
85      */

86     public final String JavaDoc getPath() {
87         return path;
88     }
89
90
91     /**
92      * This implementation delegates to <code>ServletContext.getResourceAsStream</code>,
93      * but throws a FileNotFoundException if no resource found.
94      * @see javax.servlet.ServletContext#getResourceAsStream(String)
95      */

96     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
97         InputStream JavaDoc is = this.servletContext.getResourceAsStream(this.path);
98         if (is == null) {
99             throw new FileNotFoundException JavaDoc("Could not open " + getDescription());
100         }
101         return is;
102     }
103
104     /**
105      * This implementation delegates to <code>ServletContext.getResource</code>,
106      * but throws a FileNotFoundException if no resource found.
107      * @see javax.servlet.ServletContext#getResource(String)
108      */

109     public URL JavaDoc getURL() throws IOException JavaDoc {
110         URL JavaDoc url = this.servletContext.getResource(this.path);
111         if (url == null) {
112             throw new FileNotFoundException JavaDoc(
113                     getDescription() + " cannot be resolved to URL because it does not exist");
114         }
115         return url;
116     }
117
118     /**
119      * This implementation delegates to <code>ServletContext.getRealPath</code>,
120      * but throws a FileNotFoundException if not found or not resolvable.
121      * @see javax.servlet.ServletContext#getRealPath(String)
122      */

123     public File JavaDoc getFile() throws IOException JavaDoc {
124         String JavaDoc realPath = WebUtils.getRealPath(this.servletContext, this.path);
125         return new File JavaDoc(realPath);
126     }
127
128     /**
129      * This implementation creates a ServletContextResource, applying the given path
130      * relative to the path of the underlying file of this resource descriptor.
131      * @see org.springframework.util.StringUtils#applyRelativePath(String, String)
132      */

133     public Resource createRelative(String JavaDoc relativePath) throws IOException JavaDoc {
134         String JavaDoc pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
135         return new ServletContextResource(this.servletContext, pathToUse);
136     }
137
138     /**
139      * This implementation returns the name of the file that this ServletContext
140      * resource refers to.
141      * @see org.springframework.util.StringUtils#getFilename(String)
142      */

143     public String JavaDoc getFilename() {
144         return StringUtils.getFilename(this.path);
145     }
146
147     /**
148      * This implementation returns a description that includes the ServletContext
149      * resource location.
150      */

151     public String JavaDoc getDescription() {
152         return "ServletContext resource [" + this.path + "]";
153     }
154
155
156     /**
157      * This implementation compares the underlying ServletContext resource locations.
158      */

159     public boolean equals(Object JavaDoc obj) {
160         if (obj == this) {
161             return true;
162         }
163         if (obj instanceof ServletContextResource) {
164             ServletContextResource otherRes = (ServletContextResource) obj;
165             return (this.servletContext.equals(otherRes.servletContext) && this.path.equals(otherRes.path));
166         }
167         return false;
168     }
169
170     /**
171      * This implementation returns the hash code of the underlying
172      * ServletContext resource location.
173      */

174     public int hashCode() {
175         return this.path.hashCode();
176     }
177
178 }
179
Popular Tags