e.g. Calendar Search Help
You must enter a value before pressing Search
spring

Class: org.springframework.web.context.support.ServletContextResource   ©

 OK to copy?
001 /*
002  * Copyright 2002-2004 the original author or authors.
003  * 
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  * 
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  * 
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 
017 package org.springframework.web.context.support;
018 
019 import java.io.File;
020 import java.io.FileNotFoundException;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.net.URL;
024 
025 import javax.servlet.ServletContext;
026 
027 import org.springframework.core.io.AbstractResource;
028 import org.springframework.core.io.Resource;
029 import org.springframework.util.StringUtils;
030 import org.springframework.util.Assert;
031 import org.springframework.web.util.WebUtils;
032 
033 /**
034  * Resource implementation for ServletContext resources,
035  * interpreting relative paths within the web application root.
036  *
037  * <p>Always supports stream access, but only allows java.io.File
038  * access when the web application archive is expanded.
039  * Always supports resolution as URL.
040  *
041  * @author Juergen Hoeller
042  * @since 28.12.2003
043  * @see javax.servlet.ServletContext#getResourceAsStream
044  * @see javax.servlet.ServletContext#getRealPath
045  */
046 public class ServletContextResource extends AbstractResource {
047 
048     private final ServletContext servletContext;
049 
050     private final String path;
051 
052     /**
053      * Create a new ServletContextResource.
054      * <p>The Servlet spec requires that resource paths start with a slash,
055      * even if many containers accept paths without leading slash too.
056      * Consequently, the given path will be prepended with a slash if it
057      * doesn't already start with one.
058      * @param servletContext the ServletContext to load from
059      * @param path the path of the resource
060      */
061     public ServletContextResource(ServletContext servletContext, String path) {
062         this.servletContext = servletContext;
063         Assert.notNull(path, "path is required");
064         if (!path.startsWith("/")) {
065             path = "/" + path;
066         }
067         this.path = path;
068     }
069 
070     /**
071      * Return the ServletContext for this resource. Only for internal use.
072      */
073     protected ServletContext getServletContext() {
074         return servletContext;
075     }
076 
077     /**
078      * Return the path for this resource. Only for internal use.
079      */
080     protected String getPath() {
081         return path;
082     }
083 
084     /**
085      * This implementation delegates to ServletContext.getResourceAsStream,
086      * but throws a FileNotFoundException if not found.
087      * @see javax.servlet.ServletContext#getResourceAsStream
088      */
089     public InputStream getInputStream() throws IOException {
090         InputStream is = this.servletContext.getResourceAsStream(this.path);
091         if (is == null) {
092             throw new FileNotFoundException("Could not open " + getDescription());
093         }
094         return is;
095     }
096 
097     public URL getURL() throws IOException {
098         URL url = this.servletContext.getResource(this.path);
099         if (url == null) {
100             throw new FileNotFoundException(
101                     getDescription() + " cannot be resolved to URL because it does not exist");
102         }
103         return url;
104     }
105 
106     /**
107      * This implementation delegates to ServletContext.getRealPath,
108      * but throws a FileNotFoundException if not found or not resolvable.
109      * @see javax.servlet.ServletContext#getRealPath
110      */
111     public File getFile() throws IOException {
112         String realPath = WebUtils.getRealPath(this.servletContext, this.path);
113         return new File(realPath);
114     }
115 
116     public Resource createRelative(String relativePath) throws IOException {
117         String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
118         return new ServletContextResource(this.servletContext, pathToUse);
119     }
120 
121     public String getFilename() {
Rate122         return StringUtils.getFilename(this.path);
123     }
124 
125     public String getDescription() {
126         return "ServletContext resource [" + this.path + "]";
127     }
128 
129     public boolean equals(Object obj) {
130         if (obj == this) {
131             return true;
132         }
133         if (obj instanceof ServletContextResource) {
134             ServletContextResource otherRes = (ServletContextResource) obj;
135             return (this.servletContext.equals(otherRes.servletContext) && this.path.equals(otherRes.path));
136         }
137         return false;
138     }
139 
140     public int hashCode() {
141         return this.path.hashCode();
142     }
143 
144 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
122 0% of 0