package com.worklight.wlclient;

import android.content.Context;
import android.net.SSLCertificateSocketFactory;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.worklight.common.Logger;
import com.worklight.common.WLConfig;
import java.security.KeyStore;
import java.util.Iterator;
import java.util.List;
import javax.net.ssl.TrustManagerFactory;
import org.apache.http.client.params.HttpClientParams;
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.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.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class HttpClientManager
{
  private static final int SOCKET_OPERATION_TIMEOUT = 60000;
  private static HttpClientManager instance;
  private static Logger logger = Logger.getInstance(HttpClientManager.class.getName());
  private DefaultHttpClient httpClient;
  private HttpContext httpContext;
  private String webViewUserAgent;

  private HttpClientManager(Context paramContext)
  {
    BasicHttpParams localBasicHttpParams = new BasicHttpParams();
    HttpConnectionParams.setStaleCheckingEnabled(localBasicHttpParams, false);
    HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 60000);
    HttpConnectionParams.setSoTimeout(localBasicHttpParams, 60000);
    HttpConnectionParams.setSocketBufferSize(localBasicHttpParams, 8192);
    HttpClientParams.setRedirecting(localBasicHttpParams, false);
    HttpClientParams.setAuthenticating(localBasicHttpParams, false);
    SchemeRegistry localSchemeRegistry = new SchemeRegistry();
    String str = WLConfig.getInstance().getProtocol();
    int i = Integer.valueOf(WLConfig.getInstance().getPort()).intValue();
    if ((str.equalsIgnoreCase("http")) || (str.equalsIgnoreCase("https")))
    {
      localSchemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), i));
      try
      {
        localSchemeRegistry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(60000, null), i));
        this.httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(localBasicHttpParams, localSchemeRegistry), localBasicHttpParams);
        this.httpClient.setCookieStore(new WLPersistentCookieStore(paramContext));
        this.httpContext = new BasicHttpContext();
        this.httpContext.setAttribute("http.cookie-store", this.httpClient.getCookieStore());
        this.webViewUserAgent = new WebView(paramContext).getSettings().getUserAgentString();
        if (!this.webViewUserAgent.contains("Worklight"))
          this.webViewUserAgent = (this.webViewUserAgent + "/Worklight/" + WLConfig.getInstance().getPlatformVersion());
        return;
      }
      catch (NoClassDefFoundError localNoClassDefFoundError)
      {
        while (true)
          logger.error("Error while registering the https schema", localNoClassDefFoundError);
      }
    }
    throw new RuntimeException("HttpClientFactory: Can't create HttpClient with protocol " + str);
  }

  public static void createInstance(Context paramContext)
  {
    try
    {
      if (instance == null)
        instance = new HttpClientManager(paramContext);
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  private static KeyStore getAndroidCATrustStore()
  {
    try
    {
      KeyStore localKeyStore = KeyStore.getInstance("AndroidCAStore");
      TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).init(localKeyStore);
      return localKeyStore;
    }
    catch (Throwable localThrowable)
    {
    }
    return null;
  }

  static HttpClientManager getInstance()
  {
    if (instance == null)
      throw new IllegalStateException("HttpClientManager should be created first (before calling getInstance");
    return instance;
  }

  private static void replaceSocketFactory(SSLSocketFactory paramSSLSocketFactory)
  {
    SchemeRegistry localSchemeRegistry = instance.httpClient.getConnectionManager().getSchemeRegistry();
    Iterator localIterator = localSchemeRegistry.getSchemeNames().iterator();
    while (localIterator.hasNext())
      if (((String)localIterator.next()).equalsIgnoreCase("https"))
      {
        Scheme localScheme = localSchemeRegistry.getScheme("https");
        localSchemeRegistry.unregister("https");
        localSchemeRegistry.register(new Scheme("https", paramSSLSocketFactory, localScheme.getDefaultPort()));
      }
  }

  public static boolean setSSLSocketFactory(KeyStore paramKeyStore, char[] paramArrayOfChar)
  {
    boolean bool = true;
    try
    {
      KeyStore localKeyStore = getAndroidCATrustStore();
      if (localKeyStore == null)
      {
        if (1 == 0)
          return bool;
      }
      else
      {
        SSLSocketFactory localSSLSocketFactory = new SSLSocketFactory(paramKeyStore, new String(paramArrayOfChar), localKeyStore);
        if ((instance != null) && (instance.httpClient != null))
          replaceSocketFactory(localSSLSocketFactory);
        return true;
      }
    }
    catch (Exception localException)
    {
      if (1 != 0)
        bool = false;
      return bool;
    }
    return false;
  }

  public DefaultHttpClient getHttpClient()
  {
    return this.httpClient;
  }

  public HttpContext getHttpContext()
  {
    return this.httpContext;
  }

  public String getWebViewUserAgent()
  {
    return this.webViewUserAgent;
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.wlclient.HttpClientManager
 * JD-Core Version:    0.6.2
 */