1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util.resource;
20
21 import java.io.Closeable;
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.net.MalformedURLException;
28 import java.net.URI;
29 import java.net.URL;
30 import java.nio.channels.ReadableByteChannel;
31 import java.text.DateFormat;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Date;
36
37 import org.eclipse.jetty.util.B64Code;
38 import org.eclipse.jetty.util.IO;
39 import org.eclipse.jetty.util.Loader;
40 import org.eclipse.jetty.util.StringUtil;
41 import org.eclipse.jetty.util.URIUtil;
42 import org.eclipse.jetty.util.log.Log;
43 import org.eclipse.jetty.util.log.Logger;
44
45
46
47
48
49
50
51
52
53
54 public abstract class Resource implements ResourceFactory, Closeable
55 {
56 private static final Logger LOG = Log.getLogger(Resource.class);
57 public static boolean __defaultUseCaches = true;
58 volatile Object _associate;
59
60
61
62
63
64
65
66 public static void setDefaultUseCaches (boolean useCaches)
67 {
68 __defaultUseCaches=useCaches;
69 }
70
71
72 public static boolean getDefaultUseCaches ()
73 {
74 return __defaultUseCaches;
75 }
76
77
78
79
80
81
82
83 public static Resource newResource(URI uri)
84 throws MalformedURLException
85 {
86 return newResource(uri.toURL());
87 }
88
89
90
91
92
93
94 public static Resource newResource(URL url)
95 {
96 return newResource(url, __defaultUseCaches);
97 }
98
99
100
101
102
103
104
105
106 static Resource newResource(URL url, boolean useCaches)
107 {
108 if (url==null)
109 return null;
110
111 String url_string=url.toExternalForm();
112 if( url_string.startsWith( "file:"))
113 {
114 try
115 {
116 FileResource fileResource= new FileResource(url);
117 return fileResource;
118 }
119 catch(Exception e)
120 {
121 LOG.warn(e.toString());
122 LOG.debug(Log.EXCEPTION,e);
123 return new BadResource(url,e.toString());
124 }
125 }
126 else if( url_string.startsWith( "jar:file:"))
127 {
128 return new JarFileResource(url, useCaches);
129 }
130 else if( url_string.startsWith( "jar:"))
131 {
132 return new JarResource(url, useCaches);
133 }
134
135 return new URLResource(url,null,useCaches);
136 }
137
138
139
140
141
142
143
144
145
146 public static Resource newResource(String resource)
147 throws MalformedURLException
148 {
149 return newResource(resource, __defaultUseCaches);
150 }
151
152
153
154
155
156
157
158
159 public static Resource newResource(String resource, boolean useCaches)
160 throws MalformedURLException
161 {
162 URL url=null;
163 try
164 {
165
166 url = new URL(resource);
167 }
168 catch(MalformedURLException e)
169 {
170 if(!resource.startsWith("ftp:") &&
171 !resource.startsWith("file:") &&
172 !resource.startsWith("jar:"))
173 {
174 try
175 {
176
177 if (resource.startsWith("./"))
178 resource=resource.substring(2);
179
180 File file=new File(resource).getCanonicalFile();
181 return new FileResource(file);
182 }
183 catch(Exception e2)
184 {
185 LOG.debug(Log.EXCEPTION,e2);
186 throw e;
187 }
188 }
189 else
190 {
191 LOG.warn("Bad Resource: "+resource);
192 throw e;
193 }
194 }
195
196 return newResource(url);
197 }
198
199
200 public static Resource newResource(File file)
201 {
202 return new FileResource(file);
203 }
204
205
206
207
208
209
210
211
212
213 public static Resource newSystemResource(String resource)
214 throws IOException
215 {
216 URL url=null;
217
218 ClassLoader loader=Thread.currentThread().getContextClassLoader();
219 if (loader!=null)
220 {
221 try
222 {
223 url = loader.getResource(resource);
224 if (url == null && resource.startsWith("/"))
225 url = loader.getResource(resource.substring(1));
226 }
227 catch (IllegalArgumentException e)
228 {
229
230
231
232 url = null;
233 }
234 }
235 if (url==null)
236 {
237 loader=Resource.class.getClassLoader();
238 if (loader!=null)
239 {
240 url=loader.getResource(resource);
241 if (url==null && resource.startsWith("/"))
242 url=loader.getResource(resource.substring(1));
243 }
244 }
245
246 if (url==null)
247 {
248 url=ClassLoader.getSystemResource(resource);
249 if (url==null && resource.startsWith("/"))
250 url=ClassLoader.getSystemResource(resource.substring(1));
251 }
252
253 if (url==null)
254 return null;
255
256 return newResource(url);
257 }
258
259
260
261
262 public static Resource newClassPathResource(String resource)
263 {
264 return newClassPathResource(resource,true,false);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public static Resource newClassPathResource(String name,boolean useCaches,boolean checkParents)
280 {
281 URL url=Resource.class.getResource(name);
282
283 if (url==null)
284 url=Loader.getResource(Resource.class,name);
285 if (url==null)
286 return null;
287 return newResource(url,useCaches);
288 }
289
290
291 public static boolean isContainedIn (Resource r, Resource containingResource) throws MalformedURLException
292 {
293 return r.isContainedIn(containingResource);
294 }
295
296
297 @Override
298 protected void finalize()
299 {
300 close();
301 }
302
303
304 public abstract boolean isContainedIn (Resource r) throws MalformedURLException;
305
306
307
308
309
310
311 public final void release()
312 {
313 close();
314 }
315
316
317
318
319 @Override
320 public abstract void close();
321
322
323
324
325
326 public abstract boolean exists();
327
328
329
330
331
332
333
334
335 public abstract boolean isDirectory();
336
337
338
339
340
341 public abstract long lastModified();
342
343
344
345
346
347
348 public abstract long length();
349
350
351
352
353
354
355 public abstract URL getURL();
356
357
358
359
360
361 public URI getURI()
362 {
363 try
364 {
365 return getURL().toURI();
366 }
367 catch(Exception e)
368 {
369 throw new RuntimeException(e);
370 }
371 }
372
373
374
375
376
377
378
379 public abstract File getFile()
380 throws IOException;
381
382
383
384
385
386
387 public abstract String getName();
388
389
390
391
392
393
394 public abstract InputStream getInputStream()
395 throws java.io.IOException;
396
397
398
399
400
401 public abstract ReadableByteChannel getReadableByteChannel()
402 throws java.io.IOException;
403
404
405
406
407
408 public abstract boolean delete()
409 throws SecurityException;
410
411
412
413
414
415 public abstract boolean renameTo( Resource dest)
416 throws SecurityException;
417
418
419
420
421
422
423 public abstract String[] list();
424
425
426
427
428
429
430
431 public abstract Resource addPath(String path)
432 throws IOException,MalformedURLException;
433
434
435
436
437
438
439
440
441 @Override
442 public Resource getResource(String path)
443 {
444 try
445 {
446 return addPath(path);
447 }
448 catch(Exception e)
449 {
450 LOG.debug(e);
451 return null;
452 }
453 }
454
455
456
457
458
459 public String encode(String uri)
460 {
461 return null;
462 }
463
464
465 public Object getAssociate()
466 {
467 return _associate;
468 }
469
470
471 public void setAssociate(Object o)
472 {
473 _associate=o;
474 }
475
476
477
478
479
480 public URI getAlias()
481 {
482 return null;
483 }
484
485
486
487
488
489
490
491 public String getListHTML(String base,boolean parent)
492 throws IOException
493 {
494 base=URIUtil.canonicalPath(base);
495 if (base==null || !isDirectory())
496 return null;
497
498 String[] ls = list();
499 if (ls==null)
500 return null;
501 Arrays.sort(ls);
502
503 String decodedBase = URIUtil.decodePath(base);
504 String title = "Directory: "+deTag(decodedBase);
505
506 StringBuilder buf=new StringBuilder(4096);
507 buf.append("<HTML><HEAD>");
508 buf.append("<LINK HREF=\"").append("jetty-dir.css").append("\" REL=\"stylesheet\" TYPE=\"text/css\"/><TITLE>");
509 buf.append(title);
510 buf.append("</TITLE></HEAD><BODY>\n<H1>");
511 buf.append(title);
512 buf.append("</H1>\n<TABLE BORDER=0>\n");
513
514 if (parent)
515 {
516 buf.append("<TR><TD><A HREF=\"");
517 buf.append(URIUtil.addPaths(base,"../"));
518 buf.append("\">Parent Directory</A></TD><TD></TD><TD></TD></TR>\n");
519 }
520
521 String encodedBase = hrefEncodeURI(base);
522
523 DateFormat dfmt=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
524 DateFormat.MEDIUM);
525 for (int i=0 ; i< ls.length ; i++)
526 {
527 Resource item = addPath(ls[i]);
528
529 buf.append("\n<TR><TD><A HREF=\"");
530 String path=URIUtil.addPaths(encodedBase,URIUtil.encodePath(ls[i]));
531
532 buf.append(path);
533
534 if (item.isDirectory() && !path.endsWith("/"))
535 buf.append(URIUtil.SLASH);
536
537
538 buf.append("\">");
539 buf.append(deTag(ls[i]));
540 buf.append(" ");
541 buf.append("</A></TD><TD ALIGN=right>");
542 buf.append(item.length());
543 buf.append(" bytes </TD><TD>");
544 buf.append(dfmt.format(new Date(item.lastModified())));
545 buf.append("</TD></TR>");
546 }
547 buf.append("</TABLE>\n");
548 buf.append("</BODY></HTML>\n");
549
550 return buf.toString();
551 }
552
553
554
555
556
557
558
559
560
561
562
563 private static String hrefEncodeURI(String raw)
564 {
565 StringBuffer buf = null;
566
567 loop:
568 for (int i=0;i<raw.length();i++)
569 {
570 char c=raw.charAt(i);
571 switch(c)
572 {
573 case '\'':
574 case '"':
575 case '<':
576 case '>':
577 buf=new StringBuffer(raw.length()<<1);
578 break loop;
579 }
580 }
581 if (buf==null)
582 return raw;
583
584 for (int i=0;i<raw.length();i++)
585 {
586 char c=raw.charAt(i);
587 switch(c)
588 {
589 case '"':
590 buf.append("%22");
591 continue;
592 case '\'':
593 buf.append("%27");
594 continue;
595 case '<':
596 buf.append("%3C");
597 continue;
598 case '>':
599 buf.append("%3E");
600 continue;
601 default:
602 buf.append(c);
603 continue;
604 }
605 }
606
607 return buf.toString();
608 }
609
610 private static String deTag(String raw)
611 {
612 return StringUtil.replace( StringUtil.replace(raw,"<","<"), ">", ">");
613 }
614
615
616
617
618
619
620
621 public void writeTo(OutputStream out,long start,long count)
622 throws IOException
623 {
624 try (InputStream in = getInputStream())
625 {
626 in.skip(start);
627 if (count<0)
628 IO.copy(in,out);
629 else
630 IO.copy(in,out,count);
631 }
632 }
633
634
635 public void copyTo(File destination)
636 throws IOException
637 {
638 if (destination.exists())
639 throw new IllegalArgumentException(destination+" exists");
640 try (OutputStream out = new FileOutputStream(destination))
641 {
642 writeTo(out,0,-1);
643 }
644 }
645
646
647 public String getWeakETag()
648 {
649 try
650 {
651 StringBuilder b = new StringBuilder(32);
652 b.append("W/\"");
653
654 String name=getName();
655 int length=name.length();
656 long lhash=0;
657 for (int i=0; i<length;i++)
658 lhash=31*lhash+name.charAt(i);
659
660 B64Code.encode(lastModified()^lhash,b);
661 B64Code.encode(length()^lhash,b);
662 b.append('"');
663 return b.toString();
664 }
665 catch (IOException e)
666 {
667 throw new RuntimeException(e);
668 }
669 }
670
671
672 public Collection<Resource> getAllResources()
673 {
674 try
675 {
676 ArrayList<Resource> deep=new ArrayList<>();
677 {
678 String[] list=list();
679 if (list!=null)
680 {
681 for (String i:list)
682 {
683 Resource r=addPath(i);
684 if (r.isDirectory())
685 deep.addAll(r.getAllResources());
686 else
687 deep.add(r);
688 }
689 }
690 }
691 return deep;
692 }
693 catch(Exception e)
694 {
695 throw new IllegalStateException(e);
696 }
697 }
698
699
700
701
702
703
704
705 public static URL toURL(File file) throws MalformedURLException
706 {
707 return file.toURI().toURL();
708 }
709 }