1
2
3
4
5
6
7
8
9
10
11
12
13 package org.eclipse.jetty.util.resource;
14
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.net.MalformedURLException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25 import java.net.URLConnection;
26 import java.security.Permission;
27
28 import org.eclipse.jetty.util.IO;
29 import org.eclipse.jetty.util.URIUtil;
30 import org.eclipse.jetty.util.log.Log;
31
32
33
34
35
36
37
38
39
40
41
42
43 public class FileResource extends URLResource
44 {
45 private static boolean __checkAliases = true;
46
47
48 private File _file;
49 private transient URL _alias=null;
50 private transient boolean _aliasChecked=false;
51
52
53
54
55
56 public static void setCheckAliases(boolean checkAliases)
57 {
58 __checkAliases=checkAliases;
59 }
60
61
62
63
64
65 public static boolean getCheckAliases()
66 {
67 return __checkAliases;
68 }
69
70
71 public FileResource(URL url)
72 throws IOException, URISyntaxException
73 {
74 super(url,null);
75
76 try
77 {
78
79 _file =new File(new URI(url.toString()));
80 }
81 catch (Exception e)
82 {
83 Log.ignore(e);
84 try
85 {
86
87
88 String file_url="file:"+URIUtil.encodePath(url.toString().substring(5));
89 URI uri = new URI(file_url);
90 if (uri.getAuthority()==null)
91 _file = new File(uri);
92 else
93 _file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile()));
94 }
95 catch (Exception e2)
96 {
97 Log.ignore(e2);
98
99
100 checkConnection();
101 Permission perm = _connection.getPermission();
102 _file = new File(perm==null?url.getFile():perm.getName());
103 }
104 }
105 if (_file.isDirectory())
106 {
107 if (!_urlString.endsWith("/"))
108 _urlString=_urlString+"/";
109 }
110 else
111 {
112 if (_urlString.endsWith("/"))
113 _urlString=_urlString.substring(0,_urlString.length()-1);
114 }
115
116 }
117
118
119 FileResource(URL url, URLConnection connection, File file)
120 {
121 super(url,connection);
122 _file=file;
123 if (_file.isDirectory() && !_urlString.endsWith("/"))
124 _urlString=_urlString+"/";
125 }
126
127
128 @Override
129 public Resource addPath(String path)
130 throws IOException,MalformedURLException
131 {
132 URLResource r=null;
133 String url=null;
134
135 path = org.eclipse.jetty.util.URIUtil.canonicalPath(path);
136
137 if ("/".equals(path))
138 return this;
139 else if (!isDirectory())
140 {
141 r=(FileResource)super.addPath(path);
142 url=r._urlString;
143 }
144 else
145 {
146 if (path==null)
147 throw new MalformedURLException();
148
149
150 String rel=path;
151 if (path.startsWith("/"))
152 rel = path.substring(1);
153
154 url=URIUtil.addPaths(_urlString,URIUtil.encodePath(rel));
155 r=(URLResource)Resource.newResource(url);
156 }
157
158 String encoded=URIUtil.encodePath(path);
159 int expected=r.toString().length()-encoded.length();
160 int index = r._urlString.lastIndexOf(encoded, expected);
161
162 if (expected!=index && ((expected-1)!=index || path.endsWith("/") || !r.isDirectory()))
163 {
164 if (!(r instanceof BadResource))
165 {
166 ((FileResource)r)._alias=new URL(url);
167 ((FileResource)r)._aliasChecked=true;
168 }
169 }
170 return r;
171 }
172
173
174
175 @Override
176 public URL getAlias()
177 {
178 if (__checkAliases && !_aliasChecked)
179 {
180 try
181 {
182 String abs=_file.getAbsolutePath();
183 String can=_file.getCanonicalPath();
184
185 if (abs.length()!=can.length() || !abs.equals(can))
186 _alias=new File(can).toURI().toURL();
187
188 _aliasChecked=true;
189
190 if (_alias!=null && Log.isDebugEnabled())
191 {
192 Log.debug("ALIAS abs="+abs);
193 Log.debug("ALIAS can="+can);
194 }
195 }
196 catch(Exception e)
197 {
198 Log.warn(Log.EXCEPTION,e);
199 return getURL();
200 }
201 }
202 return _alias;
203 }
204
205
206
207
208
209 @Override
210 public boolean exists()
211 {
212 return _file.exists();
213 }
214
215
216
217
218
219 @Override
220 public long lastModified()
221 {
222 return _file.lastModified();
223 }
224
225
226
227
228
229 @Override
230 public boolean isDirectory()
231 {
232 return _file.isDirectory();
233 }
234
235
236
237
238
239 @Override
240 public long length()
241 {
242 return _file.length();
243 }
244
245
246
247
248
249
250 @Override
251 public String getName()
252 {
253 return _file.getAbsolutePath();
254 }
255
256
257
258
259
260
261 @Override
262 public File getFile()
263 {
264 return _file;
265 }
266
267
268
269
270
271 @Override
272 public InputStream getInputStream() throws IOException
273 {
274 return new FileInputStream(_file);
275 }
276
277
278
279
280
281 @Override
282 public OutputStream getOutputStream()
283 throws java.io.IOException, SecurityException
284 {
285 return new FileOutputStream(_file);
286 }
287
288
289
290
291
292 @Override
293 public boolean delete()
294 throws SecurityException
295 {
296 return _file.delete();
297 }
298
299
300
301
302
303 @Override
304 public boolean renameTo( Resource dest)
305 throws SecurityException
306 {
307 if( dest instanceof FileResource)
308 return _file.renameTo( ((FileResource)dest)._file);
309 else
310 return false;
311 }
312
313
314
315
316
317 @Override
318 public String[] list()
319 {
320 String[] list =_file.list();
321 if (list==null)
322 return null;
323 for (int i=list.length;i-->0;)
324 {
325 if (new File(_file,list[i]).isDirectory() &&
326 !list[i].endsWith("/"))
327 list[i]+="/";
328 }
329 return list;
330 }
331
332
333
334
335
336
337
338 @Override
339 public String encode(String uri)
340 {
341 return uri;
342 }
343
344
345
346
347
348
349 @Override
350 public boolean equals( Object o)
351 {
352 if (this == o)
353 return true;
354
355 if (null == o || ! (o instanceof FileResource))
356 return false;
357
358 FileResource f=(FileResource)o;
359 return f._file == _file || (null != _file && _file.equals(f._file));
360 }
361
362
363
364
365
366 @Override
367 public int hashCode()
368 {
369 return null == _file ? super.hashCode() : _file.hashCode();
370 }
371
372
373 @Override
374 public void copyTo(File destination)
375 throws IOException
376 {
377 if (isDirectory())
378 {
379 IO.copyDir(getFile(),destination);
380 }
381 else
382 {
383 if (destination.exists())
384 throw new IllegalArgumentException(destination+" exists");
385 IO.copy(getFile(),destination);
386 }
387 }
388 }