/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 org.wso2.carbon.apimgt.gateway.handlers.security.thrift;


import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;


import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class ThriftAuthClient {


    private THttpClient client = null;

    public String getSessionId(String userName, String password) throws AuthenticationException {
        String sessionId = null;
        try {

            TProtocol protocol = new TCompactProtocol(client);
            AuthenticatorService.Client authClient = new AuthenticatorService.Client(protocol);
            client.open();
            sessionId = authClient.authenticate(userName, password);
            client.close();

        } catch (TTransportException e) {
            throw new AuthenticationException("Error in authenticating with thrift client..", e);
        } catch (TException e) {
            throw new AuthenticationException("Error in authenticating with thrift client..", e);
        } catch (AuthenticationException e) {
            throw new AuthenticationException("Error in authenticating with thrift client..", e);
        } finally {
            if (client != null) {
                client.close();
            }
        }
        return sessionId;
    }

    public ThriftAuthClient(String serverIP, String remoteServerPort, String webContextRoot)
            throws AuthenticationException {

        try {
            TrustManager easyTrustManager = new X509TrustManager() {
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s){
                }

                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] x509Certificates,
                        String s){
                }

                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };

            //skip host name verification
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{easyTrustManager}, null);
            SSLSocketFactory sf = new SSLSocketFactory(sslContext);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            //REGISTERS SCHEMES FOR BOTH HTTP AND HTTPS
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("https", sf,Integer.parseInt(remoteServerPort)));


            PoolingClientConnectionManager manager = new PoolingClientConnectionManager (registry);
            HttpClient httpClient = new DefaultHttpClient(manager);

            //If the webContextRoot is null or /
            if(webContextRoot == null || "/".equals(webContextRoot)){
                //Assign it an empty value since it is part of the thriftServiceURL.
                webContextRoot = "";
            }
            String thriftServiceURL = "https://" + serverIP + ':' + remoteServerPort +
                                      webContextRoot + '/' + "thriftAuthenticator";
            client = new THttpClient(thriftServiceURL, httpClient);


        } catch (TTransportException e) {
            throw new AuthenticationException("Error in creating thrift authentication client..", e);
        } catch (Exception e) {
            throw new AuthenticationException("Error in creating thrift authentication client..", e);
        }
    }
}

@@ -930,271 +930,8 @@
 nt;%0A
-import org.apache.http.conn.scheme.Scheme;%0Aimport org.apache.http.conn.scheme.SchemeRegistry;%0Aimport org.apache.http.conn.ssl.SSLSocketFactory;%0Aimport org.apache.http.impl.client.DefaultHttpClient;%0Aimport org.apache.http.impl.conn.PoolingClientConnectionManager;%0A
 impo
@@ -1168,116 +1168,107 @@
 on;%0A
-%0A%0A
 import 
-javax.net.ssl.SSLContext;%0Aimport javax.net.ssl.TrustManager;%0Aimport javax.net.ssl.X509TrustManager;
+org.wso2.carbon.apimgt.impl.APIConstants;%0Aimport org.wso2.carbon.apimgt.impl.utils.APIUtil;%0A
 %0A%0Apu
@@ -2496,1085 +2496,54 @@
     
-TrustManager easyTrustManager = new X509TrustManager() %7B%0A                public void checkClientTrusted(%0A                        java.security.cert.X509Certificate%5B%5D x509Certificates,%0A                        String s)%7B%0A                %7D%0A%0A                public void checkServerTrusted(%0A                        java.security.cert.X509Certificate%5B%5D x509Certificates,%0A                        String s)%7B%0A                %7D%0A%0A                public java.security.cert.X509Certificate%5B%5D getAcceptedIssuers() %7B%0A                    return null;%0A                %7D%0A            %7D;%0A%0A            //skip host name verification%0A            SSLContext sslContext = SSLContext.getInstance(%22TLS%22);%0A            sslContext.init(null, new TrustManager%5B%5D%7BeasyTrustManager%7D, null);%0A            SSLSocketFactory sf = new SSLSocketFactory(sslContext);%0A            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);%0A%0A            //REGISTERS SCHEMES FOR BOTH HTTP AND HTTPS%0A            SchemeRegistry registry = new SchemeRegistry();%0A            registry.register(new Scheme(%22https%22, sf,
+HttpClient httpClient = APIUtil.getHttpClient(
 Inte
@@ -2576,13 +2576,9 @@
 ort)
-));%0A%0A
+,
 %0A   
@@ -2590,161 +2590,43 @@
     
-PoolingClientConnectionManager manager = new PoolingClientConnectionManager (registry);%0A            HttpClient httpClient = new DefaultHttpClient(manager
+        APIConstants.HTTPS_PROTOCOL
 );%0A%0A

Add hostname verifier check for thriftclient