1 package org.sapia.resource; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 import java.net.URI ; 7 import java.net.URL ; 8 9 16 public class UrlResource implements Resource { 17 static final long UNDEFINED = -1; 18 private URL _url; 19 20 public UrlResource(URL url) { 21 _url = url; 22 } 23 24 public long lastModified() { 25 return UNDEFINED; 26 } 27 28 public InputStream getInputStream() throws IOException { 29 try{ 30 return _url.openStream(); 31 }catch(FileNotFoundException e){ 32 throw new ResourceNotFoundException(e.getMessage()); 33 } 34 } 35 36 public String getURI() { 37 return _url.toExternalForm(); 38 } 39 40 public String toString() { 41 return _url.toExternalForm(); 42 } 43 44 public Resource getRelative(String uri) throws IOException { 45 URI u = Utils.toURIObject(uri); 46 if(u.isAbsolute()){ 47 throw new IOException ("URI is absolute: " + uri + "; must be relative"); 48 } 49 if(_url != null){ 50 return new UrlResource(new URL (Utils.getRelativePath(_url.toExternalForm(), uri, true))); 51 } 52 else{ 53 return new UrlResource(new URL (uri)); 54 } 55 } 56 57 } 58 | Popular Tags |