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