1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http5;
18
19 import java.io.Closeable;
20 import java.net.URI;
21 import java.util.Collection;
22
23 import org.apache.commons.io.function.Uncheck;
24 import org.apache.commons.vfs2.Capability;
25 import org.apache.commons.vfs2.FileName;
26 import org.apache.commons.vfs2.FileObject;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.provider.AbstractFileName;
29 import org.apache.commons.vfs2.provider.AbstractFileSystem;
30 import org.apache.hc.client5.http.classic.HttpClient;
31 import org.apache.hc.client5.http.protocol.HttpClientContext;
32
33
34
35
36
37
38 public class Http5FileSystem extends AbstractFileSystem {
39
40
41
42
43 private final URI internalBaseURI;
44
45
46
47
48 private final HttpClient httpClient;
49
50
51
52
53 private final HttpClientContext httpClientContext;
54
55
56
57
58
59
60
61
62
63 protected Http5FileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions, final HttpClient httpClient,
64 final HttpClientContext httpClientContext) {
65 super(rootName, null, fileSystemOptions);
66
67 final String rootURI = getRootURI();
68 final int offset = rootURI.indexOf(':');
69 final char lastCharOfScheme = offset > 0 ? rootURI.charAt(offset - 1) : 0;
70
71
72 final String scheme = lastCharOfScheme == 's' || lastCharOfScheme == 'S' ? "https" : "http";
73 internalBaseURI = URI.create(scheme + rootURI.substring(offset));
74 this.httpClient = httpClient;
75 this.httpClientContext = httpClientContext;
76 }
77
78 @Override
79 protected void addCapabilities(final Collection<Capability> caps) {
80 caps.addAll(Http5FileProvider.CAPABILITIES);
81 }
82
83 @Override
84 protected FileObject createFile(final AbstractFileName name) throws Exception {
85 return new Http5FileObject<>(name, this);
86 }
87
88 @Override
89 protected void doCloseCommunicationLink() {
90 if (httpClient instanceof Closeable) {
91
92
93 Uncheck.run(() -> ((Closeable) httpClient).close());
94 }
95 }
96
97
98
99
100
101
102 protected HttpClient getHttpClient() {
103 return httpClient;
104 }
105
106
107
108
109
110
111 protected HttpClientContext getHttpClientContext() {
112 return httpClientContext;
113 }
114
115
116
117
118
119
120 protected URI getInternalBaseURI() {
121 return internalBaseURI;
122 }
123 }