1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.core.io;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.HttpURLConnection;
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URL;
26 import java.net.URLConnection;
27
28 import org.springframework.util.Assert;
29 import org.springframework.util.ResourceUtils;
30 import org.springframework.util.StringUtils;
31
32
33
34
35
36
37
38
39
40
41 public class UrlResource extends AbstractFileResolvingResource {
42
43
44
45
46 private final URL url;
47
48
49
50
51 private final URL cleanedUrl;
52
53
54
55
56 private final URI uri;
57
58
59
60
61
62
63 public UrlResource(URL url) {
64 Assert.notNull(url, "URL must not be null");
65 this.url = url;
66 this.cleanedUrl = getCleanedUrl(this.url, url.toString());
67 this.uri = null;
68 }
69
70
71
72
73
74
75 public UrlResource(URI uri) throws MalformedURLException {
76 Assert.notNull(uri, "URI must not be null");
77 this.url = uri.toURL();
78 this.cleanedUrl = getCleanedUrl(this.url, uri.toString());
79 this.uri = uri;
80 }
81
82
83
84
85
86
87 public UrlResource(String path) throws MalformedURLException {
88 Assert.notNull(path, "Path must not be null");
89 this.url = new URL(path);
90 this.cleanedUrl = getCleanedUrl(this.url, path);
91 this.uri = null;
92 }
93
94
95
96
97
98
99
100
101 private URL getCleanedUrl(URL originalUrl, String originalPath) {
102 try {
103 return new URL(StringUtils.cleanPath(originalPath));
104 }
105 catch (MalformedURLException ex) {
106
107
108 return originalUrl;
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121 public InputStream getInputStream() throws IOException {
122 URLConnection con = this.url.openConnection();
123 ResourceUtils.useCachesIfNecessary(con);
124 try {
125 return con.getInputStream();
126 }
127 catch (IOException ex) {
128
129 if (con instanceof HttpURLConnection) {
130 ((HttpURLConnection) con).disconnect();
131 }
132 throw ex;
133 }
134 }
135
136
137
138
139 @Override
140 public URL getURL() throws IOException {
141 return this.url;
142 }
143
144
145
146
147
148 @Override
149 public URI getURI() throws IOException {
150 if (this.uri != null) {
151 return this.uri;
152 }
153 else {
154 return super.getURI();
155 }
156 }
157
158
159
160
161
162
163 @Override
164 public File getFile() throws IOException {
165 if (this.uri != null) {
166 return super.getFile(this.uri);
167 }
168 else {
169 return super.getFile();
170 }
171 }
172
173
174
175
176
177
178 @Override
179 public Resource createRelative(String relativePath) throws MalformedURLException {
180 if (relativePath.startsWith("/")) {
181 relativePath = relativePath.substring(1);
182 }
183 return new UrlResource(new URL(this.url, relativePath));
184 }
185
186
187
188
189
190
191 @Override
192 public String getFilename() {
193 return new File(this.url.getFile()).getName();
194 }
195
196
197
198
199 public String getDescription() {
200 return "URL [" + this.url + "]";
201 }
202
203
204
205
206
207 @Override
208 public boolean equals(Object obj) {
209 return (obj == this ||
210 (obj instanceof UrlResource && this.cleanedUrl.equals(((UrlResource) obj).cleanedUrl)));
211 }
212
213
214
215
216 @Override
217 public int hashCode() {
218 return this.cleanedUrl.hashCode();
219 }
220
221 }