All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.schlichtherle.truezip.fs.http.HttpDriver Maven / Gradle / Ivy

/*
 * Copyright 2011 Schlichtherle IT Services
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package de.schlichtherle.truezip.fs.http;

import de.schlichtherle.truezip.fs.FsController;
import de.schlichtherle.truezip.fs.FsDriver;
import de.schlichtherle.truezip.fs.FsModel;
import de.schlichtherle.truezip.socket.IOPool;
import de.schlichtherle.truezip.socket.IOPoolProvider;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import net.jcip.annotations.Immutable;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

/**
 * A file system driver for the HTTP(S) schemes.
 * 
 * @see     #newClient()
 * @author  Christian Schlichtherle
 * @version $Id$
 */
@Immutable
@DefaultAnnotation(NonNull.class)
@edu.umd.cs.findbugs.annotations.SuppressWarnings("JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS")
public class HttpDriver extends FsDriver {

    private final IOPoolProvider provider;
    private volatile @CheckForNull HttpClient client;

    public HttpDriver(final IOPoolProvider provider) {
        if (null == provider)
            throw new NullPointerException();
        this.provider = provider;
    }

    final IOPool getPool() {
        return provider.get();
    }

    /**
     * Returns the cached http client obtained by calling {@link #newClient()}.
     * 
     * @return The cached http client obtained by calling {@link #newClient()}.
     */
    protected final HttpClient getClient() {
        final HttpClient client = this.client;
        return null != client ? client : (this.client = newClient());
    }

    /**
     * Returns a new http client.
     * 

* The implementation in the class {@link HttpDriver} simply returns * {@code new DefaultHttpClient(new ThreadSafeClientConnManager())}. * If you need special configuration, e.g. for authentication or caching, * then you should override this method. * * @return A new http client. */ protected HttpClient newClient() { return new DefaultHttpClient(new ThreadSafeClientConnManager()); } /** * Executes the HEAD request method for the given URI. * Equivalent to {@code getClient().execute(entry.newHead())}. */ protected HttpResponse executeHead(HttpEntry entry) throws IOException { // This version could be better when using a CachingHttpDriver: /*HttpUriRequest request = entry.newGet(); HttpResponse response = getClient().execute(request); request.abort(); return response;*/ return getClient().execute(entry.newHead()); } /** * Executes the GET request method for the given URI. * Equivalent to {@code getClient().execute(entry.newGet())}. */ protected HttpResponse executeGet(HttpEntry entry) throws IOException { return getClient().execute(entry.newGet()); } @Override public FsController newController(FsModel model, @CheckForNull FsController parent) { assert null == model.getParent() ? null == parent : model.getParent().equals(parent.getModel()); if (null != parent) throw new IllegalArgumentException(); return new HttpController(this, model); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy