1 package org.mortbay.util; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.io.Serializable ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLConnection ; 25 import java.text.DateFormat ; 26 import java.util.Arrays ; 27 import java.util.Date ; 28 29 import org.apache.commons.logging.Log; 30 import org.mortbay.log.LogFactory; 31 32 33 34 40 public abstract class Resource implements Serializable 41 { 42 private static Log log = LogFactory.getLog(Resource.class); 43 44 Object _associate; 45 46 47 51 public static Resource newResource(URL url) 52 throws IOException 53 { 54 if (url==null) 55 return null; 56 57 String urls=url.toExternalForm(); 58 if( urls.startsWith( "file:")) 59 { 60 try 61 { 62 FileResource fileResource= new FileResource(url); 63 return fileResource; 64 } 65 catch(Exception e) 66 { 67 log.debug(LogSupport.EXCEPTION,e); 68 return new BadResource(url,e.toString()); 69 } 70 } 71 else if( urls.startsWith( "jar:file:")) 72 { 73 return new JarFileResource(url); 74 } 75 else if( urls.startsWith( "jar:")) 76 { 77 return new JarResource(url); 78 } 79 80 return new URLResource(url,null); 81 } 82 83 84 88 public static Resource newResource(String resource) 89 throws MalformedURLException , IOException 90 { 91 URL url=null; 92 try 93 { 94 url = new URL (resource); 96 } 97 catch(MalformedURLException e) 98 { 99 if(!resource.startsWith("ftp:") && 100 !resource.startsWith("file:") && 101 !resource.startsWith("jar:")) 102 { 103 try 104 { 105 if (resource.startsWith("./")) 107 resource=resource.substring(2); 108 109 File file=new File (resource).getCanonicalFile(); 110 url=file.toURI().toURL(); 111 112 URLConnection connection=url.openConnection(); 113 FileResource fileResource= new FileResource(url,connection,file); 114 return fileResource; 115 } 116 catch(Exception e2) 117 { 118 log.debug(LogSupport.EXCEPTION,e2); 119 throw e; 120 } 121 } 122 else 123 { 124 log.warn("Bad Resource: "+resource); 125 throw e; 126 } 127 } 128 129 String nurl=url.toString(); 130 if (nurl.length()>0 && 131 nurl.charAt(nurl.length()-1)!= 132 resource.charAt(resource.length()-1)) 133 { 134 if ((nurl.charAt(nurl.length()-1)!='/' || 135 nurl.charAt(nurl.length()-2)!=resource.charAt(resource.length()-1)) 136 && 137 (resource.charAt(resource.length()-1)!='/' || 138 resource.charAt(resource.length()-2)!=nurl.charAt(nurl.length()-1) 139 )) 140 { 141 return new BadResource(url,"Trailing special characters stripped by URL in "+resource); 142 } 143 } 144 return newResource(url); 145 } 146 147 148 152 public static Resource newSystemResource(String resource) 153 throws IOException 154 { 155 URL url=null; 156 ClassLoader 158 loader=Thread.currentThread().getContextClassLoader(); 159 if (loader!=null) 160 { 161 url=loader.getResource(resource); 162 if (url==null && resource.startsWith("/")) 163 url=loader.getResource(resource.substring(1)); 164 } 165 if (url==null) 166 { 167 loader=Resource.class.getClassLoader(); 168 if (loader!=null) 169 { 170 url=loader.getResource(resource); 171 if (url==null && resource.startsWith("/")) 172 url=loader.getResource(resource.substring(1)); 173 } 174 } 175 176 if (url==null) 177 { 178 url=ClassLoader.getSystemResource(resource); 179 if (url==null && resource.startsWith("/")) 180 url=loader.getResource(resource.substring(1)); 181 } 182 183 if (url==null) 184 return null; 185 186 return newResource(url); 187 } 188 189 190 protected void finalize() 191 { 192 release(); 193 } 194 195 196 198 public abstract void release(); 199 200 201 202 205 public abstract boolean exists(); 206 207 208 209 214 public abstract boolean isDirectory(); 215 216 217 220 public abstract long lastModified(); 221 222 223 224 227 public abstract long length(); 228 229 230 231 234 public abstract URL getURL(); 235 236 237 238 242 public abstract File getFile() 243 throws IOException ; 244 245 246 247 250 public abstract String getName(); 251 252 253 254 257 public abstract InputStream getInputStream() 258 throws java.io.IOException ; 259 260 261 264 public abstract OutputStream getOutputStream() 265 throws java.io.IOException , SecurityException ; 266 267 268 271 public abstract boolean delete() 272 throws SecurityException ; 273 274 275 278 public abstract boolean renameTo( Resource dest) 279 throws SecurityException ; 280 281 282 286 public abstract String [] list(); 287 288 289 295 public abstract Resource addPath(String path) 296 throws IOException ,MalformedURLException ; 297 298 299 300 305 public String encode(String uri) 306 { 307 return URI.encodePath(uri); 308 } 309 310 311 312 public Object getAssociate() 313 { 314 return _associate; 315 } 316 317 318 public void setAssociate(Object o) 319 { 320 _associate=o; 321 } 322 323 324 327 public URL getAlias() 328 { 329 return null; 330 } 331 332 333 334 public CachedResource cache() 335 throws IOException 336 { 337 return new CachedResource(this); 338 } 339 340 341 346 public String getListHTML(String base, 347 boolean parent) 348 throws IOException 349 { 350 if (!isDirectory()) 351 return null; 352 353 354 String [] ls = list(); 355 if (ls==null) 356 return null; 357 Arrays.sort(ls); 358 359 String title = "Directory: "+URI.decodePath(base); 360 title=StringUtil.replace(StringUtil.replace(title,"<","<"),">",">"); 361 StringBuffer buf=new StringBuffer (4096); 362 buf.append("<HTML><HEAD><TITLE>"); 363 buf.append(title); 364 buf.append("</TITLE></HEAD><BODY>\n<H1>"); 365 buf.append(title); 366 buf.append("</H1><TABLE BORDER=0>"); 367 368 if (parent) 369 { 370 buf.append("<TR><TD><A HREF="); 371 buf.append(URI.encodePath(URI.addPaths(base,"../"))); 372 buf.append(">Parent Directory</A></TD><TD></TD><TD></TD></TR>\n"); 373 } 374 375 DateFormat dfmt=DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 376 DateFormat.MEDIUM); 377 for (int i=0 ; i< ls.length ; i++) 378 { 379 String encoded=URI.encodePath(ls[i]); 380 Resource item = addPath(encoded); 381 382 buf.append("<TR><TD><A HREF=\""); 383 384 String path=URI.addPaths(base,encoded); 385 386 if (item.isDirectory() && !path.endsWith("/")) 387 path=URI.addPaths(path,"/"); 388 buf.append(path); 389 buf.append("\">"); 390 buf.append(StringUtil.replace(StringUtil.replace(ls[i],"<","<"),">",">")); 391 buf.append(" "); 392 buf.append("</TD><TD ALIGN=right>"); 393 buf.append(item.length()); 394 buf.append(" bytes </TD><TD>"); 395 buf.append(dfmt.format(new Date (item.lastModified()))); 396 buf.append("</TD></TR>\n"); 397 } 398 buf.append("</TABLE>\n"); 399 buf.append("</BODY></HTML>\n"); 400 401 return buf.toString(); 402 } 403 404 405 410 public void writeTo(OutputStream out,long start,long count) 411 throws IOException 412 { 413 InputStream in = getInputStream(); 414 try 415 { 416 in.skip(start); 417 if (count<0) 418 IO.copy(in,out); 419 else 420 IO.copy(in,out,(int)count); 421 } 422 finally 423 { 424 in.close(); 425 } 426 } 427 } 428 | Popular Tags |