1 16 package org.apache.commons.vfs.provider.http; 17 18 import org.apache.commons.httpclient.Header; 19 import org.apache.commons.httpclient.HttpClient; 20 import org.apache.commons.httpclient.HttpMethod; 21 import org.apache.commons.httpclient.URIException; 22 import org.apache.commons.httpclient.methods.GetMethod; 23 import org.apache.commons.httpclient.methods.HeadMethod; 24 import org.apache.commons.httpclient.util.DateParser; 25 import org.apache.commons.httpclient.util.URIUtil; 26 import org.apache.commons.vfs.FileContentInfoFactory; 27 import org.apache.commons.vfs.FileName; 28 import org.apache.commons.vfs.FileSystemException; 29 import org.apache.commons.vfs.FileType; 30 import org.apache.commons.vfs.RandomAccessContent; 31 import org.apache.commons.vfs.provider.AbstractFileObject; 32 import org.apache.commons.vfs.provider.URLFileName; 33 import org.apache.commons.vfs.util.MonitorInputStream; 34 import org.apache.commons.vfs.util.RandomAccessMode; 35 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.net.HttpURLConnection ; 39 40 46 public class HttpFileObject 47 extends AbstractFileObject 48 { 49 private final HttpFileSystem fileSystem; 50 private final String urlCharset; 51 private HeadMethod method; 52 53 protected HttpFileObject(final FileName name, 54 final HttpFileSystem fileSystem) 55 { 56 super(name, fileSystem); 57 this.fileSystem = fileSystem; 58 urlCharset = HttpFileSystemConfigBuilder.getInstance().getUrlCharset(getFileSystem().getFileSystemOptions()); 59 } 60 61 64 protected void doDetach() 65 throws Exception 66 { 67 method = null; 68 } 69 70 74 protected FileType doGetType() 75 throws Exception 76 { 77 method = new HeadMethod(); 79 setupMethod(method); 80 final HttpClient client = fileSystem.getClient(); 81 final int status = client.executeMethod(method); 82 method.releaseConnection(); 83 if (status == HttpURLConnection.HTTP_OK) 84 { 85 return FileType.FILE; 86 } 87 else if (status == HttpURLConnection.HTTP_NOT_FOUND 88 || status == HttpURLConnection.HTTP_GONE) 89 { 90 return FileType.IMAGINARY; 91 } 92 else 93 { 94 throw new FileSystemException("vfs.provider.http/head.error", getName()); 95 } 96 } 97 98 101 protected String [] doListChildren() 102 throws Exception 103 { 104 throw new Exception ("Not implemented."); 105 } 106 107 110 protected long doGetContentSize() 111 throws Exception 112 { 113 final Header header = method.getResponseHeader("content-length"); 114 if (header == null) 115 { 116 return 0; 118 } 119 return Integer.parseInt(header.getValue()); 120 } 121 122 127 protected long doGetLastModifiedTime() 128 throws Exception 129 { 130 final Header header = method.getResponseHeader("last-modified"); 131 if (header == null) 132 { 133 throw new FileSystemException("vfs.provider.http/last-modified.error", getName()); 134 } 135 return DateParser.parseDate(header.getValue()).getTime(); 136 } 137 138 147 protected InputStream doGetInputStream() 148 throws Exception 149 { 150 final GetMethod getMethod = new GetMethod(); 151 setupMethod(getMethod); 152 final int status = fileSystem.getClient().executeMethod(getMethod); 153 if (status != HttpURLConnection.HTTP_OK) 154 { 155 throw new FileSystemException("vfs.provider.http/get.error", getName()); 156 } 157 158 return new HttpInputStream(getMethod); 159 } 160 161 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception 162 { 163 return new HttpRandomAccesContent(this, mode); 164 } 165 166 169 void setupMethod(final HttpMethod method) throws FileSystemException, URIException 170 { 171 String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(urlCharset); 172 method.setPath(pathEncoded); 173 method.setFollowRedirects(true); 174 method.setRequestHeader("User-Agent", "Jakarta-Commons-VFS"); 175 } 176 177 protected String encodePath(final String decodedPath) throws URIException 178 { 179 String pathEncoded = URIUtil.encodePath(decodedPath); 180 return pathEncoded; 181 } 182 183 186 static class HttpInputStream 187 extends MonitorInputStream 188 { 189 private final GetMethod method; 190 191 public HttpInputStream(final GetMethod method) 192 throws IOException 193 { 194 super(method.getResponseBodyAsStream()); 195 this.method = method; 196 } 197 198 201 protected void onClose() 202 throws IOException 203 { 204 method.releaseConnection(); 205 } 206 } 207 208 209 protected FileContentInfoFactory getFileContentInfoFactory() 210 { 211 return new HttpFileContentInfoFactory(); 212 } 213 214 HeadMethod getHeadMethod() 215 { 216 return method; 217 } 218 219 238 } 239 | Popular Tags |