com.android.providers.downloads.ui.network.SslHttpClient.java Source code

Java tutorial

Introduction

Here is the source code for com.android.providers.downloads.ui.network.SslHttpClient.java

Source

/**
 * Copyright 2013 Ognyan Bankov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.providers.downloads.ui.network;

import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

import java.io.InputStream;
import java.security.GeneralSecurityException;

public class SslHttpClient extends DefaultHttpClient {
    private static final int HTTP_DEFAULT_PORT = 80;

    private static final String HTTP_SCHEME = "http";

    private static final int HTTP_DEFAULT_HTTPS_PORT = 443;

    private static final String HTTP_SSL_SCHEME = "https";

    private InputStream mKeyStore;

    private String mKeyStorePassword;

    private int mHttpsPort;

    private HttpParams mParams;

    public SslHttpClient(InputStream keyStore, String keyStorePassword) {
        mKeyStore = keyStore;
        mKeyStorePassword = keyStorePassword;
        mHttpsPort = HTTP_DEFAULT_HTTPS_PORT;
    }

    public SslHttpClient(InputStream keyStore, String keyStorePassword, int httpPort) {
        mKeyStore = keyStore;
        mKeyStorePassword = keyStorePassword;
        mHttpsPort = httpPort;
    }

    public SslHttpClient(ClientConnectionManager conman, InputStream keyStore, String keyStorePassword) {
        super(conman, null);
        mKeyStore = keyStore;
        mKeyStorePassword = keyStorePassword;
    }

    public SslHttpClient(final ClientConnectionManager conman, final HttpParams params, InputStream keyStore,
            String keyStorePassword) {
        super(conman, checkForInvalidParams(params));
        this.mParams = params;
        this.mKeyStore = keyStore;
        this.mKeyStorePassword = keyStorePassword;
    }

    public SslHttpClient(final HttpParams params, InputStream keyStore, String keyStorePassword) {
        super(null, checkForInvalidParams(params));
        this.mParams = params;
        this.mKeyStore = keyStore;
        this.mKeyStorePassword = keyStorePassword;
    }

    // we check intentionally for an old parameter
    private static HttpParams checkForInvalidParams(HttpParams params) {
        String className = (String) params.getParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME);
        if (className != null) {
            throw new IllegalArgumentException(
                    "Don't try to pass ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME parameter. We use our own connection manager factory anyway...");
        }

        return params;
    }

    @Override
    protected ClientConnectionManager createClientConnectionManager() {
        SchemeRegistry registry = new SchemeRegistry();

        PlainSocketFactory pfs = PlainSocketFactory.getSocketFactory();
        Scheme s = new Scheme(HTTP_SCHEME, pfs, HTTP_DEFAULT_PORT);
        registry.register(s);

        ClientConnectionManager ret;
        try {
            registry.register(
                    new Scheme(HTTP_SSL_SCHEME, new SslSocketFactory(mKeyStore, mKeyStorePassword), mHttpsPort));
            if (mParams == null) {
                mParams = new BasicHttpParams();
            }
            ret = new ThreadSafeClientConnManager(mParams, registry);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }

        return ret;
    }

    public void setHttpsPort(int httpsPort) {
        mHttpsPort = httpsPort;
    }
}