1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.HttpURLConnection;
22
23 import org.apache.commons.httpclient.Header;
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.HttpMethod;
26 import org.apache.commons.httpclient.URIException;
27 import org.apache.commons.httpclient.methods.GetMethod;
28 import org.apache.commons.httpclient.methods.HeadMethod;
29 import org.apache.commons.httpclient.util.DateUtil;
30 import org.apache.commons.httpclient.util.URIUtil;
31 import org.apache.commons.vfs2.FileContentInfoFactory;
32 import org.apache.commons.vfs2.FileNotFoundException;
33 import org.apache.commons.vfs2.FileSystemException;
34 import org.apache.commons.vfs2.FileSystemOptions;
35 import org.apache.commons.vfs2.FileType;
36 import org.apache.commons.vfs2.RandomAccessContent;
37 import org.apache.commons.vfs2.provider.AbstractFileName;
38 import org.apache.commons.vfs2.provider.AbstractFileObject;
39 import org.apache.commons.vfs2.provider.URLFileName;
40 import org.apache.commons.vfs2.util.MonitorInputStream;
41 import org.apache.commons.vfs2.util.RandomAccessMode;
42
43
44
45
46
47
48
49
50
51
52 @Deprecated
53 public class HttpFileObject<FS extends HttpFileSystem> extends AbstractFileObject<FS> {
54
55
56
57
58 static class HttpInputStream extends MonitorInputStream {
59 private final GetMethod method;
60
61 HttpInputStream(final GetMethod method) throws IOException {
62 super(method.getResponseBodyAsStream());
63 this.method = method;
64 }
65
66 HttpInputStream(final GetMethod method, final int bufferSize) throws IOException {
67 super(method.getResponseBodyAsStream(), bufferSize);
68 this.method = method;
69 }
70
71
72
73
74 @Override
75 protected void onClose() throws IOException {
76 method.releaseConnection();
77 }
78 }
79
80 private final String urlCharset;
81 private final String userAgent;
82 private final boolean followRedirect;
83 private HeadMethod method;
84
85
86
87
88
89
90
91 protected HttpFileObject(final AbstractFileName fileName, final FS fileSystem) {
92 this(fileName, fileSystem, HttpFileSystemConfigBuilder.getInstance());
93 }
94
95
96
97
98
99
100
101
102 protected HttpFileObject(final AbstractFileName fileName, final FS fileSystem,
103 final HttpFileSystemConfigBuilder builder) {
104 super(fileName, fileSystem);
105 final FileSystemOptions fileSystemOptions = fileSystem.getFileSystemOptions();
106 urlCharset = builder.getUrlCharset(fileSystemOptions);
107 userAgent = builder.getUserAgent(fileSystemOptions);
108 followRedirect = builder.getFollowRedirect(fileSystemOptions);
109 }
110
111
112
113
114 @Override
115 protected void doDetach() throws Exception {
116 synchronized (getFileSystem()) {
117 method = null;
118 }
119 }
120
121
122
123
124 @Override
125 protected long doGetContentSize() throws Exception {
126 final Header header = getHeadMethod().getResponseHeader("content-length");
127 if (header == null) {
128
129 return 0;
130 }
131 return Long.parseLong(header.getValue());
132 }
133
134
135
136
137
138
139
140
141
142
143
144 @Override
145 protected InputStream doGetInputStream(final int bufferSize) throws Exception {
146 final GetMethod getMethod = new GetMethod();
147 setupMethod(getMethod);
148 final int status = getAbstractFileSystem().getClient().executeMethod(getMethod);
149 if (status == HttpURLConnection.HTTP_NOT_FOUND) {
150 throw new FileNotFoundException(getName());
151 }
152 if (status != HttpURLConnection.HTTP_OK) {
153 throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status));
154 }
155
156 return new HttpInputStream(getMethod, bufferSize);
157 }
158
159
160
161
162
163
164
165 @Override
166 protected long doGetLastModifiedTime() throws Exception {
167 final Header header = getHeadMethod().getResponseHeader("last-modified");
168 FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName());
169 return DateUtil.parseDate(header.getValue()).getTime();
170 }
171
172 @Override
173 protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
174 return new HttpRandomAccessContent<>(this, mode);
175 }
176
177
178
179
180
181 @Override
182 protected FileType doGetType() throws Exception {
183
184 final int status = this.getHeadMethod().getStatusCode();
185 if (status == HttpURLConnection.HTTP_OK
186 || status == HttpURLConnection.HTTP_BAD_METHOD ) {
187 return FileType.FILE;
188 }
189 if (status == HttpURLConnection.HTTP_NOT_FOUND || status == HttpURLConnection.HTTP_GONE) {
190 return FileType.IMAGINARY;
191 }
192 throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status));
193 }
194
195 @Override
196 protected boolean doIsWriteable() throws Exception {
197 return false;
198 }
199
200
201
202
203
204
205 @Override
206 protected String[] doListChildren() throws Exception {
207 throw new UnsupportedOperationException("Not implemented.");
208 }
209
210
211
212
213
214
215
216
217 protected String encodePath(final String unescaped) throws URIException {
218 return URIUtil.encodePath(unescaped);
219 }
220
221
222
223
224
225
226 @Override
227 protected FileContentInfoFactory getFileContentInfoFactory() {
228 return new HttpFileContentInfoFactory();
229 }
230
231
232
233
234
235
236 protected boolean getFollowRedirect() {
237 return followRedirect;
238 }
239
240
241
242
243
244
245
246 HeadMethod getHeadMethod() throws IOException {
247
248 synchronized (getFileSystem()) {
249 if (method != null) {
250 return method;
251 }
252 method = new HeadMethod();
253 try {
254 setupMethod(method);
255 final HttpClient client = getAbstractFileSystem().getClient();
256 client.executeMethod(method);
257 } finally {
258 method.releaseConnection();
259 }
260 return method;
261 }
262 }
263
264
265
266
267
268
269 protected String getUrlCharset() {
270 return urlCharset;
271 }
272
273
274
275
276
277
278 protected String getUserAgent() {
279 return userAgent;
280 }
281
282
283
284
285
286
287
288
289
290 protected void setupMethod(final HttpMethod method) throws FileSystemException, URIException {
291 final String pathEncoded = ((URLFileName) getName()).getPathQueryEncoded(this.getUrlCharset());
292 method.setPath(pathEncoded);
293 method.setFollowRedirects(this.getFollowRedirect());
294 method.setRequestHeader("User-Agent", this.getUserAgent());
295 }
296
297
298
299
300
301
302
303
304
305
306 }