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  public class Http4FileSystem extends AbstractFileSystem {
37  
38      /**
39       * Internal base URI of this file system.
40       */
41      private final URI internalBaseURI;
42  
43      /**
44       * Internal <code>HttpClient</code> instance of this file system.
45       */
46      private final HttpClient httpClient;
47  
48      /**
49       * Internal <code>HttpClientContext</code> instance of this file system.
50       */
51      private final HttpClientContext httpClientContext;
52  
53      /**
54       * Construct <code>Http4FileSystem</code>.
55       *
56       * @param rootName root base name
57       * @param fileSystemOptions file system options
58       * @param httpClient {@link HttpClient} instance
59       * @param httpClientContext {@link HttpClientContext} instance
60       */
61      protected Http4FileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions, final HttpClient httpClient,
62              final HttpClientContext httpClientContext) {
63          super(rootName, null, fileSystemOptions);
64  
65          final String rootURI = getRootURI();
66          final int offset = rootURI.indexOf(':');
67          final char lastCharOfScheme = (offset > 0) ? rootURI.charAt(offset - 1) : 0;
68  
69          // if scheme is 'http*s' or 'HTTP*S', then the internal base URI should be 'https'. 'http' otherwise.
70          if (lastCharOfScheme == 's' || lastCharOfScheme == 'S') {
71              this.internalBaseURI = URI.create("https" + rootURI.substring(offset));
72          } else {
73              this.internalBaseURI = URI.create("http" + rootURI.substring(offset));
74          }
75  
76          this.httpClient = httpClient;
77          this.httpClientContext = httpClientContext;
78      }
79  
80      @Override
81      protected FileObject createFile(final AbstractFileName name) throws Exception {
82          return new Http4FileObject<>(name, this);
83      }
84  
85      @Override
86      protected void addCapabilities(final Collection<Capability> caps) {
87          caps.addAll(Http4FileProvider.capabilities);
88      }
89  
90      @Override
91      protected void doCloseCommunicationLink() {
92          if (httpClient instanceof CloseableHttpClient) {
93              try {
94                  ((CloseableHttpClient) httpClient).close();
95              } catch (final IOException e) {
96                  throw new RuntimeException("Error closing HttpClient", e);
97              }
98          }
99      }
100 
101     /**
102      * Return the internal {@link HttpClient} instance.
103      *
104      * @return the internal {@link HttpClient} instance
105      */
106     protected HttpClient getHttpClient() {
107         return httpClient;
108     }
109 
110     /**
111      * Return the internal {@link HttpClientContext} instance.
112      *
113      * @return the internal {@link HttpClientContext} instance
114      */
115     protected HttpClientContext getHttpClientContext() {
116         return httpClientContext;
117     }
118 
119     /**
120      * Return the internal base <code>URI</code> instance.
121      *
122      * @return the internal base <code>URI</code> instance
123      */
124     protected URI getInternalBaseURI() {
125         return internalBaseURI;
126     }
127 }