1 16 package org.apache.commons.vfs.provider.url; 17 18 import org.apache.commons.httpclient.URIException; 19 import org.apache.commons.vfs.FileName; 20 import org.apache.commons.vfs.FileObject; 21 import org.apache.commons.vfs.FileSystemException; 22 import org.apache.commons.vfs.FileType; 23 import org.apache.commons.vfs.provider.AbstractFileObject; 24 import org.apache.commons.vfs.provider.URLFileName; 25 26 import java.io.FileNotFoundException ; 27 import java.io.InputStream ; 28 import java.net.HttpURLConnection ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 33 40 public class UrlFileObject 41 extends AbstractFileObject 42 implements FileObject 43 { 44 private URL url; 45 46 protected UrlFileObject(final UrlFileSystem fs, 47 final FileName fileName) 48 { 49 super(fileName, fs); 50 } 51 52 57 protected void doAttach() throws Exception 58 { 59 if (url == null) 60 { 61 url = createURL(getName()); 63 } 64 } 65 66 protected URL createURL(final FileName name) throws MalformedURLException , FileSystemException, URIException 67 { 68 if (name instanceof URLFileName) 69 { 70 URLFileName urlName = (URLFileName) getName(); 71 72 return new URL (urlName.getURIEncoded(null)); 74 } 75 return new URL (getName().getURI()); 76 } 77 78 81 protected FileType doGetType() throws Exception 82 { 83 try 84 { 85 final URLConnection conn = url.openConnection(); 87 final InputStream in = conn.getInputStream(); 88 try 89 { 90 if (conn instanceof HttpURLConnection ) 91 { 92 final int status = ((HttpURLConnection ) conn).getResponseCode(); 93 if (HttpURLConnection.HTTP_OK != status) 95 { 96 return FileType.IMAGINARY; 97 } 98 } 99 100 return FileType.FILE; 101 } 102 finally 103 { 104 in.close(); 105 } 106 } 107 catch (final FileNotFoundException e) 108 { 109 return FileType.IMAGINARY; 110 } 111 } 112 113 116 protected long doGetContentSize() throws Exception 117 { 118 final URLConnection conn = url.openConnection(); 119 final InputStream in = conn.getInputStream(); 120 try 121 { 122 return conn.getContentLength(); 123 } 124 finally 125 { 126 in.close(); 127 } 128 } 129 130 133 protected long doGetLastModifiedTime() 134 throws Exception 135 { 136 final URLConnection conn = url.openConnection(); 137 final InputStream in = conn.getInputStream(); 138 try 139 { 140 return conn.getLastModified(); 141 } 142 finally 143 { 144 in.close(); 145 } 146 } 147 148 151 protected String [] doListChildren() throws Exception 152 { 153 throw new FileSystemException("Not implemented."); 154 } 155 156 159 protected InputStream doGetInputStream() throws Exception 160 { 161 return url.openStream(); 162 } 163 } 164 | Popular Tags |