2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.resource;
21 import java.io.Closeable;
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;
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;
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;
46 /* ------------------------------------------------------------ */
48 * Abstract resource class.
50 * This class provides a resource abstraction, where a resource may be
51 * a file, a URL or an entry in a jar file.
54 public abstract class Resource implements ResourceFactory, Closeable
56 private static final Logger LOG = Log.getLogger(Resource.class);
57 public static boolean __defaultUseCaches = true;
58 volatile Object _associate;
60 /* ------------------------------------------------------------ */
62 * Change the default setting for url connection caches.
63 * Subsequent URLConnections will use this default.
66 public static void setDefaultUseCaches (boolean useCaches)
68 __defaultUseCaches=useCaches;
71 /* ------------------------------------------------------------ */
72 public static boolean getDefaultUseCaches ()
74 return __defaultUseCaches;
77 /* ------------------------------------------------------------ */
78 /** Construct a resource from a uri.
80 * @return A Resource object.
81 * @throws MalformedURLException Problem accessing URI
83 public static Resource newResource(URI uri)
84 throws MalformedURLException
86 return newResource(uri.toURL());
89 /* ------------------------------------------------------------ */
90 /** Construct a resource from a url.
92 * @return A Resource object.
94 public static Resource newResource(URL url)
96 return newResource(url, __defaultUseCaches);
99 /* ------------------------------------------------------------ */
101 * Construct a resource from a url.
102 * @param url the url for which to make the resource
103 * @param useCaches true enables URLConnection caching if applicable to the type of resource
106 static Resource newResource(URL url, boolean useCaches)
111 String url_string=url.toExternalForm();
112 if( url_string.startsWith( "file:"))
116 FileResource fileResource= new FileResource(url);
121 LOG.warn(e.toString());
122 LOG.debug(Log.EXCEPTION,e);
123 return new BadResource(url,e.toString());
126 else if( url_string.startsWith( "jar:file:"))
128 return new JarFileResource(url, useCaches);
130 else if( url_string.startsWith( "jar:"))
132 return new JarResource(url, useCaches);
135 return new URLResource(url,null,useCaches);
140 /* ------------------------------------------------------------ */
141 /** Construct a resource from a string.
142 * @param resource A URL or filename.
143 * @throws MalformedURLException Problem accessing URI
144 * @return A Resource object.
146 public static Resource newResource(String resource)
147 throws MalformedURLException
149 return newResource(resource, __defaultUseCaches);
152 /* ------------------------------------------------------------ */
153 /** Construct a resource from a string.
154 * @param resource A URL or filename.
155 * @param useCaches controls URLConnection caching
156 * @return A Resource object.
157 * @throws MalformedURLException Problem accessing URI
159 public static Resource newResource(String resource, boolean useCaches)
160 throws MalformedURLException
165 // Try to format as a URL?
166 url = new URL(resource);
168 catch(MalformedURLException e)
170 if(!resource.startsWith("ftp:") &&
171 !resource.startsWith("file:") &&
172 !resource.startsWith("jar:"))
177 if (resource.startsWith("./"))
178 resource=resource.substring(2);
180 File file=new File(resource).getCanonicalFile();
181 return new FileResource(file);
185 LOG.debug(Log.EXCEPTION,e2);
191 LOG.warn("Bad Resource: "+resource);
196 return newResource(url);
199 /* ------------------------------------------------------------ */
200 public static Resource newResource(File file)
202 return new FileResource(file);
205 /* ------------------------------------------------------------ */
206 /** Construct a system resource from a string.
207 * The resource is tried as classloader resource before being
208 * treated as a normal resource.
209 * @param resource Resource as string representation
210 * @return The new Resource
211 * @throws IOException Problem accessing resource.
213 public static Resource newSystemResource(String resource)
217 // Try to format as a URL?
218 ClassLoader loader=Thread.currentThread().getContextClassLoader();
223 url = loader.getResource(resource);
224 if (url == null && resource.startsWith("/"))
225 url = loader.getResource(resource.substring(1));
227 catch (IllegalArgumentException e)
229 // Catches scenario where a bad Windows path like "C:\dev" is
230 // improperly escaped, which various downstream classloaders
231 // tend to have a problem with
237 loader=Resource.class.getClassLoader();
240 url=loader.getResource(resource);
241 if (url==null && resource.startsWith("/"))
242 url=loader.getResource(resource.substring(1));
248 url=ClassLoader.getSystemResource(resource);
249 if (url==null && resource.startsWith("/"))
250 url=ClassLoader.getSystemResource(resource.substring(1));
256 return newResource(url);
259 /* ------------------------------------------------------------ */
260 /** Find a classpath resource.
262 public static Resource newClassPathResource(String resource)
264 return newClassPathResource(resource,true,false);
267 /* ------------------------------------------------------------ */
268 /** Find a classpath resource.
269 * The {@link java.lang.Class#getResource(String)} method is used to lookup the resource. If it is not
270 * found, then the {@link Loader#getResource(Class, String)} method is used.
271 * If it is still not found, then {@link ClassLoader#getSystemResource(String)} is used.
272 * Unlike {@link ClassLoader#getSystemResource(String)} this method does not check for normal resources.
273 * @param name The relative name of the resource
274 * @param useCaches True if URL caches are to be used.
275 * @param checkParents True if forced searching of parent Classloaders is performed to work around
276 * loaders with inverted priorities
277 * @return Resource or null
279 public static Resource newClassPathResource(String name,boolean useCaches,boolean checkParents)
281 URL url=Resource.class.getResource(name);
284 url=Loader.getResource(Resource.class,name);
287 return newResource(url,useCaches);
290 /* ------------------------------------------------------------ */
291 public static boolean isContainedIn (Resource r, Resource containingResource) throws MalformedURLException
293 return r.isContainedIn(containingResource);
296 /* ------------------------------------------------------------ */
298 protected void finalize()
303 /* ------------------------------------------------------------ */
304 public abstract boolean isContainedIn (Resource r) throws MalformedURLException;
307 /* ------------------------------------------------------------ */
308 /** Release any temporary resources held by the resource.
309 * @deprecated use {@link #close()}
311 public final void release()
316 /* ------------------------------------------------------------ */
317 /** Release any temporary resources held by the resource.
320 public abstract void close();
322 /* ------------------------------------------------------------ */
324 * Returns true if the respresened resource exists.
326 public abstract boolean exists();
329 /* ------------------------------------------------------------ */
331 * Returns true if the respresenetd resource is a container/directory.
332 * If the resource is not a file, resources ending with "/" are
333 * considered directories.
335 public abstract boolean isDirectory();
337 /* ------------------------------------------------------------ */
339 * Returns the last modified time
341 public abstract long lastModified();
344 /* ------------------------------------------------------------ */
346 * Return the length of the resource
348 public abstract long length();
351 /* ------------------------------------------------------------ */
353 * Returns an URL representing the given resource
355 public abstract URL getURL();
357 /* ------------------------------------------------------------ */
359 * Returns an URI representing the given resource
365 return getURL().toURI();
369 throw new RuntimeException(e);
374 /* ------------------------------------------------------------ */
376 * Returns an File representing the given resource or NULL if this
379 public abstract File getFile()
383 /* ------------------------------------------------------------ */
385 * Returns the name of the resource
387 public abstract String getName();
390 /* ------------------------------------------------------------ */
392 * Returns an input stream to the resource
394 public abstract InputStream getInputStream()
395 throws java.io.IOException;
397 /* ------------------------------------------------------------ */
399 * Returns an readable bytechannel to the resource or null if one is not available.
401 public abstract ReadableByteChannel getReadableByteChannel()
402 throws java.io.IOException;
404 /* ------------------------------------------------------------ */
406 * Deletes the given resource
408 public abstract boolean delete()
409 throws SecurityException;
411 /* ------------------------------------------------------------ */
413 * Rename the given resource
415 public abstract boolean renameTo( Resource dest)
416 throws SecurityException;
418 /* ------------------------------------------------------------ */
420 * Returns a list of resource names contained in the given resource
421 * The resource names are not URL encoded.
423 public abstract String[] list();
425 /* ------------------------------------------------------------ */
427 * Returns the resource contained inside the current resource with the
429 * @param path The path segment to add, which is not encoded
431 public abstract Resource addPath(String path)
432 throws IOException,MalformedURLException;
434 /* ------------------------------------------------------------ */
435 /** Get a resource from within this resource.
437 * This method is essentially an alias for {@link #addPath(String)}, but without checked exceptions.
438 * This method satisfied the {@link ResourceFactory} interface.
439 * @see org.eclipse.jetty.util.resource.ResourceFactory#getResource(java.lang.String)
442 public Resource getResource(String path)
446 return addPath(path);
455 /* ------------------------------------------------------------ */
459 public String encode(String uri)
464 /* ------------------------------------------------------------ */
465 public Object getAssociate()
470 /* ------------------------------------------------------------ */
471 public void setAssociate(Object o)
476 /* ------------------------------------------------------------ */
478 * @return The canonical Alias of this resource or null if none.
480 public URI getAlias()
485 /* ------------------------------------------------------------ */
486 /** Get the resource list as a HTML directory listing.
487 * @param base The base URL
488 * @param parent True if the parent directory should be included
489 * @return String of HTML
491 public String getListHTML(String base,boolean parent)
494 base=URIUtil.canonicalPath(base);
495 if (base==null || !isDirectory())
498 String[] ls = list();
503 String decodedBase = URIUtil.decodePath(base);
504 String title = "Directory: "+deTag(decodedBase);
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>");
510 buf.append("</TITLE></HEAD><BODY>\n<H1>");
512 buf.append("</H1>\n<TABLE BORDER=0>\n");
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");
521 String encodedBase = hrefEncodeURI(base);
523 DateFormat dfmt=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
525 for (int i=0 ; i< ls.length ; i++)
527 Resource item = addPath(ls[i]);
529 buf.append("\n<TR><TD><A HREF=\"");
530 String path=URIUtil.addPaths(encodedBase,URIUtil.encodePath(ls[i]));
534 if (item.isDirectory() && !path.endsWith("/"))
535 buf.append(URIUtil.SLASH);
537 // URIUtil.encodePath(buf,path);
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>");
547 buf.append("</TABLE>\n");
548 buf.append("</BODY></HTML>\n");
550 return buf.toString();
554 * Encode any characters that could break the URI string in an HREF.
555 * Such as <a href="/path/to;<script>Window.alert("XSS"+'%20'+"here");</script>">Link</a>
557 * The above example would parse incorrectly on various browsers as the "<" or '"' characters
558 * would end the href attribute value string prematurely.
560 * @param raw the raw text to encode.
561 * @return the defanged text.
563 private static String hrefEncodeURI(String raw)
565 StringBuffer buf = null;
568 for (int i=0;i<raw.length();i++)
570 char c=raw.charAt(i);
577 buf=new StringBuffer(raw.length()<<1);
584 for (int i=0;i<raw.length();i++)
586 char c=raw.charAt(i);
607 return buf.toString();
610 private static String deTag(String raw)
612 return StringUtil.replace( StringUtil.replace(raw,"<","<"), ">", ">");
615 /* ------------------------------------------------------------ */
618 * @param start First byte to write
619 * @param count Bytes to write or -1 for all of them.
621 public void writeTo(OutputStream out,long start,long count)
624 try (InputStream in = getInputStream())
630 IO.copy(in,out,count);
634 /* ------------------------------------------------------------ */
635 public void copyTo(File destination)
638 if (destination.exists())
639 throw new IllegalArgumentException(destination+" exists");
640 try (OutputStream out = new FileOutputStream(destination))
646 /* ------------------------------------------------------------ */
647 public String getWeakETag()
651 StringBuilder b = new StringBuilder(32);
654 String name=getName();
655 int length=name.length();
657 for (int i=0; i<length;i++)
658 lhash=31*lhash+name.charAt(i);
660 B64Code.encode(lastModified()^lhash,b);
661 B64Code.encode(length()^lhash,b);
665 catch (IOException e)
667 throw new RuntimeException(e);
671 /* ------------------------------------------------------------ */
672 public Collection<Resource> getAllResources()
676 ArrayList<Resource> deep=new ArrayList<>();
678 String[] list=list();
683 Resource r=addPath(i);
685 deep.addAll(r.getAllResources());
695 throw new IllegalStateException(e);
699 /* ------------------------------------------------------------ */
700 /** Generate a properly encoded URL from a {@link File} instance.
701 * @param file Target file.
702 * @return URL of the target file.
703 * @throws MalformedURLException
705 public static URL toURL(File file) throws MalformedURLException
707 return file.toURI().toURL();