KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > UrlResource


1 /*
2  * Copyright 2002-2007 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.core.io;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLConnection JavaDoc;
25
26 import org.springframework.util.Assert;
27 import org.springframework.util.ResourceUtils;
28 import org.springframework.util.StringUtils;
29
30 /**
31  * {@link Resource} implementation for <code>java.net.URL</code> locators.
32  * Obviously supports resolution as URL, and also as File in case of
33  * the "file:" protocol.
34  *
35  * @author Juergen Hoeller
36  * @since 28.12.2003
37  * @see java.net.URL
38  */

39 public class UrlResource extends AbstractResource {
40
41     /**
42      * Original URL, used for actual access.
43      */

44     private final URL JavaDoc url;
45
46     /**
47      * Cleaned URL (with normalized path), used for comparisons.
48      */

49     private final URL JavaDoc cleanedUrl;
50
51
52     /**
53      * Create a new UrlResource.
54      * @param url a URL
55      */

56     public UrlResource(URL JavaDoc url) {
57         Assert.notNull(url, "URL must not be null");
58         this.url = url;
59         this.cleanedUrl = getCleanedUrl(this.url, url.toString());
60     }
61
62     /**
63      * Create a new UrlResource.
64      * @param path a URL path
65      * @throws MalformedURLException if the given URL path is not valid
66      */

67     public UrlResource(String JavaDoc path) throws MalformedURLException JavaDoc {
68         Assert.notNull(path, "Path must not be null");
69         this.url = new URL JavaDoc(path);
70         this.cleanedUrl = getCleanedUrl(this.url, path);
71     }
72
73     /**
74      * Determine a cleaned URL for the given original URL.
75      * @param originalUrl the original URL
76      * @param originalPath the original URL path
77      * @return the cleaned URL
78      * @see org.springframework.util.StringUtils#cleanPath
79      */

80     private URL JavaDoc getCleanedUrl(URL JavaDoc originalUrl, String JavaDoc originalPath) {
81         try {
82             return new URL JavaDoc(StringUtils.cleanPath(originalPath));
83         }
84         catch (MalformedURLException JavaDoc ex) {
85             // Cleaned URL path cannot be converted to URL
86
// -> take original URL.
87
return originalUrl;
88         }
89     }
90
91
92     /**
93      * This implementation opens an InputStream for the given URL.
94      * It sets the "UseCaches" flag to <code>false</code>,
95      * mainly to avoid jar file locking on Windows.
96      * @see java.net.URL#openConnection()
97      * @see java.net.URLConnection#setUseCaches(boolean)
98      * @see java.net.URLConnection#getInputStream()
99      */

100     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
101         URLConnection JavaDoc con = this.url.openConnection();
102         con.setUseCaches(false);
103         return con.getInputStream();
104     }
105
106     /**
107      * This implementation returns the underlying URL reference.
108      */

109     public URL JavaDoc getURL() throws IOException JavaDoc {
110         return this.url;
111     }
112
113     /**
114      * This implementation returns a File reference for the underlying URL,
115      * provided that it refers to a file in the file system.
116      * @see org.springframework.util.ResourceUtils#getFile(java.net.URL, String)
117      */

118     public File JavaDoc getFile() throws IOException JavaDoc {
119         return ResourceUtils.getFile(this.url, getDescription());
120     }
121
122     /**
123      * This implementation creates a UrlResource, applying the given path
124      * relative to the path of the underlying URL of this resource descriptor.
125      * @see java.net.URL#URL(java.net.URL, String)
126      */

127     public Resource createRelative(String JavaDoc relativePath) throws MalformedURLException JavaDoc {
128         if (relativePath.startsWith("/")) {
129             relativePath = relativePath.substring(1);
130         }
131         return new UrlResource(new URL JavaDoc(this.url, relativePath));
132     }
133
134     /**
135      * This implementation returns the name of the file that this URL refers to.
136      * @see java.net.URL#getFile()
137      * @see java.io.File#getName()
138      */

139     public String JavaDoc getFilename() {
140         return new File JavaDoc(this.url.getFile()).getName();
141     }
142
143     /**
144      * This implementation returns a description that includes the URL.
145      */

146     public String JavaDoc getDescription() {
147         return "URL [" + this.url + "]";
148     }
149
150
151     /**
152      * This implementation compares the underlying URL references.
153      */

154     public boolean equals(Object JavaDoc obj) {
155         return (obj == this ||
156             (obj instanceof UrlResource && this.cleanedUrl.equals(((UrlResource) obj).cleanedUrl)));
157     }
158
159     /**
160      * This implementation returns the hash code of the underlying URL reference.
161      */

162     public int hashCode() {
163         return this.cleanedUrl.hashCode();
164     }
165
166 }
167
Popular Tags