View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * http4 file system.
35   *
36   * @since 2.3
37   */
38  public class Http4FileSystem extends AbstractFileSystem {
39  
40      /**
41       * Internal base URI of this file system.
42       */
43      private final URI internalBaseURI;
44  
45      /**
46       * Internal {@code HttpClient} instance of this file system.
47       */
48      private final HttpClient httpClient;
49  
50      /**
51       * Internal {@code HttpClientContext} instance of this file system.
52       */
53      private final HttpClientContext httpClientContext;
54  
55      /**
56       * Construct {@code Http4FileSystem}.
57       *
58       * @param rootName root base name
59       * @param fileSystemOptions file system options
60       * @param httpClient {@link HttpClient} instance
61       * @param httpClientContext {@link HttpClientContext} instance
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          // if scheme is 'http*s' or 'HTTP*S', then the internal base URI should be 'https'. 'http' otherwise.
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      * Return the internal {@link HttpClient} instance.
105      *
106      * @return the internal {@link HttpClient} instance
107      */
108     protected HttpClient getHttpClient() {
109         return httpClient;
110     }
111 
112     /**
113      * Return the internal {@link HttpClientContext} instance.
114      *
115      * @return the internal {@link HttpClientContext} instance
116      */
117     protected HttpClientContext getHttpClientContext() {
118         return httpClientContext;
119     }
120 
121     /**
122      * Return the internal base {@code URI} instance.
123      *
124      * @return the internal base {@code URI} instance
125      */
126     protected URI getInternalBaseURI() {
127         return internalBaseURI;
128     }
129 }