// Path: src/com/andrewshu/android/reddit/RedditIsFunApplication.java
// public class RedditIsFunApplication extends Application {
// 	private static RedditIsFunApplication application;
// 	
// 	public RedditIsFunApplication(){
// 		application = this;
// 	}
// 	
// 	public static RedditIsFunApplication getApplication(){
// 		return application;
// 	}
// }


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
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.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
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.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import com.andrewshu.android.reddit.R;
import com.andrewshu.android.reddit.RedditIsFunApplication;
package com.andrewshu.android.reddit.common;





public class RedditIsFunHttpClientFactory {
	
	private static final String TAG = "RedditIsFunHttpClientFactory";
	
	private static final DefaultHttpClient mGzipHttpClient = createGzipHttpClient();
	private static final CookieStore mCookieStore = mGzipHttpClient.getCookieStore();

	// Default connection and socket timeout of 60 seconds.  Tweak to taste.
	private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;

	static DefaultHttpClient createGzipHttpClient() {
		HttpParams params = new BasicHttpParams();
		params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		
		DefaultHttpClient httpclient = new DefaultHttpClient(params) {
		    @Override
		    protected ClientConnectionManager createClientConnectionManager() {
		        SchemeRegistry registry = new SchemeRegistry();
		        registry.register(
		                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		        registry.register(
		        		new Scheme("https", getHttpsSocketFactory(), 443));
		        HttpParams params = getParams();
				HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
				HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
				HttpConnectionParams.setSocketBufferSize(params, 8192);
		        return new ThreadSafeClientConnManager(params, registry);
		    }
		    
		    /** Gets an HTTPS socket factory with SSL Session Caching if such support is available, otherwise falls back to a non-caching factory
		     * @return
		     */
		    protected SocketFactory getHttpsSocketFactory(){
				try {
					Class<?> sslSessionCacheClass = Class.forName("android.net.SSLSessionCache");
// Path: src/com/andrewshu/android/reddit/RedditIsFunApplication.java
// public class RedditIsFunApplication extends Application {
// 	private static RedditIsFunApplication application;
// 	
// 	public RedditIsFunApplication(){
// 		application = this;
// 	}
// 	
// 	public static RedditIsFunApplication getApplication(){
// 		return application;
// 	}
// }


// Path: src/com/andrewshu/android/reddit/common/RedditIsFunHttpClientFactory.java
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.zip.GZIPInputStream;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpVersion;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
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.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.HttpEntityWrapper;
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.CoreProtocolPNames;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;
import com.andrewshu.android.reddit.R;
import com.andrewshu.android.reddit.RedditIsFunApplication;

package com.andrewshu.android.reddit.common;





public class RedditIsFunHttpClientFactory {
	
	private static final String TAG = "RedditIsFunHttpClientFactory";
	
	private static final DefaultHttpClient mGzipHttpClient = createGzipHttpClient();
	private static final CookieStore mCookieStore = mGzipHttpClient.getCookieStore();

	// Default connection and socket timeout of 60 seconds.  Tweak to taste.
	private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;

	static DefaultHttpClient createGzipHttpClient() {
		HttpParams params = new BasicHttpParams();
		params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
		
		DefaultHttpClient httpclient = new DefaultHttpClient(params) {
		    @Override
		    protected ClientConnectionManager createClientConnectionManager() {
		        SchemeRegistry registry = new SchemeRegistry();
		        registry.register(
		                new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		        registry.register(
		        		new Scheme("https", getHttpsSocketFactory(), 443));
		        HttpParams params = getParams();
				HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
				HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
				HttpConnectionParams.setSocketBufferSize(params, 8192);
		        return new ThreadSafeClientConnManager(params, registry);
		    }
		    
		    /** Gets an HTTPS socket factory with SSL Session Caching if such support is available, otherwise falls back to a non-caching factory
		     * @return
		     */
		    protected SocketFactory getHttpsSocketFactory(){
				try {
					Class<?> sslSessionCacheClass = Class.forName("android.net.SSLSessionCache");
			    	Object sslSessionCache = sslSessionCacheClass.getConstructor(Context.class).newInstance(RedditIsFunApplication.getApplication());