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.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.vfs2.Capability;
25 import org.apache.commons.vfs2.FileName;
26 import org.apache.commons.vfs2.FileSystem;
27 import org.apache.commons.vfs2.FileSystemConfigBuilder;
28 import org.apache.commons.vfs2.FileSystemException;
29 import org.apache.commons.vfs2.FileSystemOptions;
30 import org.apache.commons.vfs2.UserAuthenticationData;
31 import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
32 import org.apache.commons.vfs2.provider.GenericFileName;
33 import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
34
35
36
37
38 public class HttpFileProvider extends AbstractOriginatingFileProvider {
39
40
41 public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
42 UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
43
44 static final Collection<Capability> CAPABILITIES = Collections
45 .unmodifiableCollection(Arrays.asList(Capability.GET_TYPE, Capability.READ_CONTENT, Capability.URI, Capability.GET_LAST_MODIFIED,
46 Capability.ATTRIBUTES, Capability.RANDOM_ACCESS_READ, Capability.DIRECTORY_READ_CONTENT));
47
48
49
50
51 public HttpFileProvider() {
52 setFileNameParser(HttpFileNameParser.getInstance());
53 }
54
55
56
57
58 @Override
59 protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
60 throws FileSystemException {
61
62 final GenericFileName/../../org/apache/commons/vfs2/provider/GenericFileName.html#GenericFileName">GenericFileName rootName = (GenericFileName) name;
63
64 UserAuthenticationData authData = null;
65 HttpClient httpClient;
66 try {
67 authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
68
69 final String fileScheme = rootName.getScheme();
70 final char lastChar = fileScheme.charAt(fileScheme.length() - 1);
71 final String internalScheme = (lastChar == 's' || lastChar == 'S') ? "https" : "http";
72
73 httpClient = HttpClientFactory.createConnection(internalScheme, rootName.getHostName(),
74 rootName.getPort(),
75 UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
76 UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
77 UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
78 UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
79 fileSystemOptions);
80 } finally {
81 UserAuthenticatorUtils.cleanup(authData);
82 }
83
84 return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
85 }
86
87 @Override
88 public Collection<Capability> getCapabilities() {
89 return CAPABILITIES;
90 }
91
92 @Override
93 public FileSystemConfigBuilder getConfigBuilder() {
94 return HttpFileSystemConfigBuilder.getInstance();
95 }
96 }