/**
 * 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.github.volley_examples.toolbox;

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

import khandroid.ext.apache.http.client.params.ClientPNames;
import khandroid.ext.apache.http.conn.ClientConnectionManager;
import khandroid.ext.apache.http.conn.scheme.PlainSocketFactory;
import khandroid.ext.apache.http.conn.scheme.Scheme;
import khandroid.ext.apache.http.conn.scheme.SchemeRegistry;
import khandroid.ext.apache.http.impl.client.DefaultHttpClient;
import khandroid.ext.apache.http.impl.conn.PoolingClientConnectionManager;
import khandroid.ext.apache.http.params.HttpParams;


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;


    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);
        mKeyStore = keyStore;
        mKeyStorePassword = keyStorePassword;
    }


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


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


    @SuppressWarnings("deprecation")
    // 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, HTTP_DEFAULT_PORT, pfs);
        registry.register(s);

        PoolingClientConnectionManager ret;
        try {
            registry.register(new Scheme(HTTP_SSL_SCHEME, mHttpsPort, new SslSocketFactory(mKeyStore, mKeyStorePassword)));
            ret = new PoolingClientConnectionManager(registry);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }

        return ret;
    }


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

@@ -1521,25 +1521,74 @@
 am mKeyStore
-;
+=null;%0A    private InputStream mTrustKeyStore;%0A   
 %0A    private
@@ -2599,32 +2599,40 @@
 Password;%0A    %7D%0A
+    %0A  %0A
 %0A%0A    public Ssl
@@ -2856,24 +2856,331 @@
 word;%0A    %7D%0A
+    %0A      //Construct for mutual auth%0A    public SslHttpClient(InputStream trustKeyStore, String keyStorePassword, int httpPort, InputStream keyStore) %7B%0A        mTrustKeyStore = trustKeyStore;%0A        mKeyStore = keyStore;%0A        mKeyStorePassword = keyStorePassword;%0A        mHttpsPort = httpPort;%0A    %7D%0A
 %0A%0A    @Suppr
@@ -4178,16 +4178,21 @@
 actory(m
+Trust
 KeyStore
@@ -4210,16 +4210,26 @@
 Password
+,mKeyStore
 )));%0A   

Update SslHttpClient.java