1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http4;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import java.util.Collection;
22
23 import org.apache.commons.vfs2.Capability;
24 import org.apache.commons.vfs2.FileName;
25 import org.apache.commons.vfs2.FileObject;
26 import org.apache.commons.vfs2.FileSystemOptions;
27 import org.apache.commons.vfs2.provider.AbstractFileName;
28 import org.apache.commons.vfs2.provider.AbstractFileSystem;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.protocol.HttpClientContext;
31 import org.apache.http.impl.client.CloseableHttpClient;
32
33
34
35
36
37
38 public class Http4FileSystem 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 Http4FileSystem(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 if (lastCharOfScheme == 's' || lastCharOfScheme == 'S') {
73 this.internalBaseURI = URI.create("https" + rootURI.substring(offset));
74 } else {
75 this.internalBaseURI = URI.create("http" + rootURI.substring(offset));
76 }
77
78 this.httpClient = httpClient;
79 this.httpClientContext = httpClientContext;
80 }
81
82 @Override
83 protected void addCapabilities(final Collection<Capability> caps) {
84 caps.addAll(Http4FileProvider.CAPABILITIES);
85 }
86
87 @Override
88 protected FileObject createFile(final AbstractFileName name) throws Exception {
89 return new Http4FileObject<>(name, this);
90 }
91
92 @Override
93 protected void doCloseCommunicationLink() {
94 if (httpClient instanceof CloseableHttpClient) {
95 try {
96 ((CloseableHttpClient) httpClient).close();
97 } catch (final IOException e) {
98 throw new RuntimeException("Error closing HttpClient", e);
99 }
100 }
101 }
102
103
104
105
106
107
108 protected HttpClient getHttpClient() {
109 return httpClient;
110 }
111
112
113
114
115
116
117 protected HttpClientContext getHttpClientContext() {
118 return httpClientContext;
119 }
120
121
122
123
124
125
126 protected URI getInternalBaseURI() {
127 return internalBaseURI;
128 }
129 }