001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.http4;
018
019import java.io.Closeable;
020import java.net.URI;
021import java.util.Collection;
022
023import org.apache.commons.io.function.Uncheck;
024import org.apache.commons.vfs2.Capability;
025import org.apache.commons.vfs2.FileName;
026import org.apache.commons.vfs2.FileObject;
027import org.apache.commons.vfs2.FileSystemOptions;
028import org.apache.commons.vfs2.provider.AbstractFileName;
029import org.apache.commons.vfs2.provider.AbstractFileSystem;
030import org.apache.http.client.HttpClient;
031import org.apache.http.client.protocol.HttpClientContext;
032
033/**
034 * http4 file system.
035 *
036 * @since 2.3
037 * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
038 */
039@Deprecated
040public class Http4FileSystem extends AbstractFileSystem {
041
042    /**
043     * Internal base URI of this file system.
044     */
045    private final URI internalBaseURI;
046
047    /**
048     * Internal {@code HttpClient} instance of this file system.
049     */
050    private final HttpClient httpClient;
051
052    /**
053     * Internal {@code HttpClientContext} instance of this file system.
054     */
055    private final HttpClientContext httpClientContext;
056
057    /**
058     * Constructs a new instance.
059     *
060     * @param rootName root base name
061     * @param fileSystemOptions file system options
062     * @param httpClient {@link HttpClient} instance
063     * @param httpClientContext {@link HttpClientContext} instance
064     */
065    protected Http4FileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions, final HttpClient httpClient,
066            final HttpClientContext httpClientContext) {
067        super(rootName, null, fileSystemOptions);
068
069        final String rootURI = getRootURI();
070        final int offset = rootURI.indexOf(':');
071        final char lastCharOfScheme = offset > 0 ? rootURI.charAt(offset - 1) : 0;
072
073        // if scheme is 'http*s' or 'HTTP*S', then the internal base URI should be 'https'. 'http' otherwise.
074        final String scheme  = lastCharOfScheme == 's' || lastCharOfScheme == 'S' ? "https" : "http";
075        this.internalBaseURI = URI.create(scheme + rootURI.substring(offset));
076        this.httpClient = httpClient;
077        this.httpClientContext = httpClientContext;
078    }
079
080    @Override
081    protected void addCapabilities(final Collection<Capability> caps) {
082        caps.addAll(Http4FileProvider.CAPABILITIES);
083    }
084
085    @Override
086    protected FileObject createFile(final AbstractFileName name) throws Exception {
087        return new Http4FileObject<>(name, this);
088    }
089
090    @Override
091    protected void doCloseCommunicationLink() {
092        if (httpClient instanceof Closeable) {
093            // TODO "Error closing HttpClient" Commons IO
094            // Uncheck.run(() -> ((Closeable) httpClient).close(), () -> "Error closing HttpClient");
095            Uncheck.run(() -> ((Closeable) httpClient).close());
096        }
097    }
098
099    /**
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}