package com.yxcheng.token.utils;



import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

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

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
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.conn.ssl.SSLSocketFactory;
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.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class HttpConnectionManager
{
    private static HttpParams httpParams;
    
    private static ClientConnectionManager connectionManager;
    
    /** 
     * �?大连接数 
     */
    public final static int MAX_TOTAL_CONNECTIONS = 500;
    
    /** 
     * 获取连接的最大等待时�?
     */
    public final static int WAIT_TIMEOUT = 300000;
    
    /** 
     * 每个路由�?大连接数
     */
    public final static int MAX_ROUTE_CONNECTIONS = 400;
    
    /** 
     * 连接超时时间 
     */
    public final static int CONNECT_TIMEOUT = 15000;
    
    /** 
     * 读取超时时间 
     */
    public final static int READ_TIMEOUT = 15000;
    
    static
    {
        httpParams = new BasicHttpParams();
        // 设置�?大连接数  
        ConnManagerParams.setMaxTotalConnections(httpParams,
                MAX_TOTAL_CONNECTIONS);
        // 设置获取连接的最大等待时�?  
        ConnManagerParams.setTimeout(httpParams, WAIT_TIMEOUT);
        // 设置每个路由�?大连接数  
        ConnPerRouteBean connPerRoute = new ConnPerRouteBean(
                MAX_ROUTE_CONNECTIONS);
        ConnManagerParams.setMaxConnectionsPerRoute(httpParams, connPerRoute);
        // 设置连接超时时间  
        HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT);
        // 设置读取超时时间  
        HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);
        
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http",
                PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https",
                SSLSocketFactory.getSocketFactory(), 443));
        
        connectionManager = new ThreadSafeClientConnManager(httpParams,
                registry);
    }
    
    public static HttpClient getHttpClient()
    {
        return new DefaultHttpClient(connectionManager, httpParams);
    }
    
    public static HttpClient httpsTrustClient()
    {
        HttpClient base = getHttpClient();
        try
        {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager()
            {

                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException
                {
                    // TODO Auto-generated method stub
                    
                }
                

                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException
                {
                    // TODO Auto-generated method stub
                    
                }
                

                public X509Certificate[] getAcceptedIssuers()
                {
                    // TODO Auto-generated method stub
                    return null;
                }
                
            };
            ctx.init(null, new TrustManager[] {tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            ClientConnectionManager ccm = base.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            //设置要使用的端口，默认是443
            sr.register(new Scheme("https", ssf, 443));
            return new DefaultHttpClient(ccm, base.getParams());
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
    
    
}
