/**
 * 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=null;
    private InputStream mTrustKeyStore;
   
    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;
    }
    
      //Construct for mutual auth
    public SslHttpClient(InputStream trustKeyStore, String keyStorePassword, int httpPort, InputStream keyStore) {
        mTrustKeyStore = trustKeyStore;
        mKeyStore = keyStore;
        mKeyStorePassword = keyStorePassword;
        mHttpsPort = httpPort;
    }


    @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(mTrustKeyStore, mKeyStorePassword,mKeyStore)));
            ret = new PoolingClientConnectionManager(registry);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }

        return ret;
    }


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

@@ -1517,24 +1517,29 @@
 Stream m
+Trust
 KeyStore
 =null;%0A 
@@ -1534,13 +1534,8 @@
 tore
-=null
 ;%0A  
@@ -1549,37 +1549,32 @@
 te InputStream m
-Trust
 KeyStore;%0A   %0A  
@@ -1567,19 +1567,16 @@
 yStore;%0A
-   
 %0A    pri
@@ -1711,32 +1711,37 @@
 ord) %7B%0A        m
+Trust
 KeyStore = keySt
@@ -1874,33 +1874,307 @@
 ent(InputStream 
-k
+TrustKeyStore, String keyStorePassword, int httpPort, InputStream keyStore) %7B%0A        mTrustKeyStore = TrustKeyStore;%0A        mKeyStore = keyStore;%0A        mKeyStorePassword = keyStorePassword;%0A        mHttpsPort = httpPort;%0A    %7D%0A%0A    public SslHttpClient(InputStream TrustK
 eyStore, String 
@@ -2208,32 +2208,37 @@
 ort) %7B%0A        m
+Trust
 KeyStore = keySt
@@ -2224,33 +2224,38 @@
 TrustKeyStore = 
-k
+TrustK
 eyStore;%0A       
@@ -2316,32 +2316,57 @@
 ort = httpPort;%0A
+        mKeyStore =null;%0A
     %7D%0A%0A%0A    publ
@@ -2511,32 +2511,37 @@
 nman);%0A        m
+Trust
 KeyStore = keySt
@@ -2654,32 +2654,45 @@
 Manager conman,%0A
+             
             fina
@@ -2716,32 +2716,45 @@
 ms,%0A            
+       
+      
 InputStream keyS
@@ -2751,32 +2751,45 @@
 tream keyStore,%0A
+             
             Stri
@@ -2871,32 +2871,37 @@
 ;%0A        this.m
+Trust
 KeyStore = keySt
@@ -2962,23 +2962,16 @@
 ;%0A    %7D%0A
-    %0A  
 %0A%0A%0A    p
@@ -3122,32 +3122,37 @@
 ;%0A        this.m
+Trust
 KeyStore = keySt
@@ -3217,315 +3217,8 @@
   %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  
@@ -4132,16 +4132,20 @@
   try %7B%0A
+%0A%0A%0A%0A
         
@@ -4271,24 +4271,26 @@
 eyStore)));%0A
+%0A%0A
             

Update SslHttpClient.java