1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.url;
18
19 import java.io.FileNotFoundException;
20 import java.io.InputStream;
21 import java.net.HttpURLConnection;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.net.URLConnection;
25
26 import org.apache.commons.httpclient.URIException;
27 import org.apache.commons.vfs2.FileName;
28 import org.apache.commons.vfs2.FileSystemException;
29 import org.apache.commons.vfs2.FileType;
30 import org.apache.commons.vfs2.provider.AbstractFileName;
31 import org.apache.commons.vfs2.provider.AbstractFileObject;
32 import org.apache.commons.vfs2.provider.URLFileName;
33
34
35
36
37
38
39
40
41
42
43 public class UrlFileObject extends AbstractFileObject<UrlFileSystem> {
44 private URL url;
45
46 protected UrlFileObject(final UrlFileSystem fs, final AbstractFileName fileName) {
47 super(fileName, fs);
48 }
49
50
51
52
53
54 @Override
55 protected void doAttach() throws Exception {
56 if (url == null) {
57
58 url = createURL(getName());
59 }
60 }
61
62 protected URL createURL(final FileName name) throws MalformedURLException, FileSystemException, URIException {
63 if (name instanceof URLFileName) {
64 final URLFileName../../../../org/apache/commons/vfs2/provider/URLFileName.html#URLFileName">URLFileName urlName = (URLFileName) getName();
65
66
67 return new URL(urlName.getURIEncoded(null));
68 }
69 return new URL(getName().getURI());
70 }
71
72
73
74
75 @Override
76 protected FileType doGetType() throws Exception {
77 try {
78
79 final URLConnection conn = url.openConnection();
80 try (InputStream in = conn.getInputStream()) {
81 if (conn instanceof HttpURLConnection) {
82 final int status = ((HttpURLConnection) conn).getResponseCode();
83
84 if (HttpURLConnection.HTTP_OK != status) {
85 return FileType.IMAGINARY;
86 }
87 }
88
89 return FileType.FILE;
90 }
91 } catch (final FileNotFoundException e) {
92 return FileType.IMAGINARY;
93 }
94 }
95
96
97
98
99 @Override
100 protected long doGetContentSize() throws Exception {
101 final URLConnection conn = url.openConnection();
102 try (InputStream in = conn.getInputStream()) {
103 return conn.getContentLength();
104 }
105 }
106
107
108
109
110 @Override
111 protected long doGetLastModifiedTime() throws Exception {
112 final URLConnection conn = url.openConnection();
113 try (InputStream in = conn.getInputStream()) {
114 return conn.getLastModified();
115 }
116 }
117
118
119
120
121 @Override
122 protected String[] doListChildren() throws Exception {
123 throw new FileSystemException("Not implemented.");
124 }
125
126
127
128
129 @Override
130 protected InputStream doGetInputStream(final int bufferSize) throws Exception {
131 return url.openStream();
132 }
133 }