package com.xero.api;

import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
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.HttpParams;

import com.google.api.client.http.apache.ApacheHttpTransport;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

public class PartnerHttpClient {

	private ApacheHttpTransport transport = null;
	private Config c = Config.getInstance();

	public PartnerHttpClient() {

		InputStream clientCertPKCS12Stream = null;
		try {
			clientCertPKCS12Stream = new FileInputStream(c.getPathToEntrust());
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String password = c.getEntrustPassword();

		KeyStore entrustStore = null;

		try {
			entrustStore = KeyStore.getInstance("PKCS12");
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			try {
				entrustStore.load(clientCertPKCS12Stream, password.toCharArray());
			} catch (CertificateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		

		HttpClient client = new DefaultHttpClient();
		ClientConnectionManager mgr = client.getConnectionManager();
		if (!(mgr instanceof ThreadSafeClientConnManager)) {
			HttpParams params = client.getParams();
			client = new DefaultHttpClient(
					new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);
		}
		SchemeRegistry schemeRegistry = client.getConnectionManager().getSchemeRegistry();

		SSLContext sslContext = null;
		try {
			sslContext = SSLContext.getInstance("TLS");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		KeyManagerFactory kmFactory = null;
		try {
			kmFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			kmFactory.init(entrustStore, password == null ? null : password.toCharArray());
		} catch (UnrecoverableKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (KeyStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			sslContext.init(kmFactory.getKeyManagers(), null, null);
		} catch (KeyManagementException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);

		Scheme partnerHttps = new Scheme("https", sslSocketFactory, 443);
		schemeRegistry.register(partnerHttps);

		transport = new ApacheHttpTransport(client);
	}

	public ApacheHttpTransport getPartnerHttpClient(){
		return transport;
	}

}

@@ -1101,2472 +1101,55 @@
 ) %7B%0A
-%0A
 %09%09
-InputStream clientCertPKCS12Stream = null;%0A%09%09try %7B%0A%09%09%09clientCertPKCS12Stream = new FileInputStream(c.getPathToEntrust());%0A%09%09%7D catch (FileNotFoundException e1) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e1.printStackTrace();%0A%09%09%7D%0A%09%09String password = c.getEntrustPassword();%0A%0A%09%09KeyStore entrustStore = null;%0A%0A%09%09try %7B%0A%09%09%09entrustStore = KeyStore.getInstance(%22PKCS12%22);%0A%09%09%7D catch (KeyStoreException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09try %7B%0A%09%09%09%09entrustStore.load(clientCertPKCS12Stream, password.toCharArray());%0A%09%09%09%7D catch (CertificateException e) %7B%0A%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%09%09%0A%0A%09%09HttpClient client = new DefaultHttpClient();%0A%09%09ClientConnectionManager mgr = client.getConnectionManager();%0A%09%09if (!(mgr instanceof ThreadSafeClientConnManager)) %7B%0A%09%09%09HttpParams params = client.getParams();%0A%09%09%09client = new DefaultHttpClient(%0A%09%09%09%09%09new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);%0A%09%09%7D%0A%09%09SchemeRegistry schemeRegistry = client.getConnectionManager().getSchemeRegistry();%0A%0A%09%09SSLContext sslContext = null;%0A%09%09try %7B%0A%09%09%09sslContext = SSLContext.getInstance(%22TLS%22);%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%0A%09%09KeyManagerFactory kmFactory = null;%0A%09%09try %7B%0A%09%09%09kmFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09kmFactory.init(entrustStore, password == null ? null : password.toCharArray());%0A%09%09%7D catch (UnrecoverableKeyException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (KeyStoreException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D catch (NoSuchAlgorithmException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09sslContext.init(kmFactory.getKeyManagers(), null, null);%0A%09%09%7D catch (KeyManagementException e) %7B%0A%09%09%09// TODO Auto-generated catch block%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%0A%09%09SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext);%0A%0A%09%09Scheme partnerHttps = new Scheme(%22https%22, sslSocketFactory, 443);%0A%09%09schemeRegistry.register(partnerHttps
+%0A%09%09HttpClient client = new DefaultHttpClient(
 );%0A%0A

Remove entrust from Partner Http Client