package com.qiniu.auth;

import android.os.AsyncTask;
import com.qiniu.conf.Conf;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
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.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class Client {
	
	protected HttpClient mClient;
	
	public Client(HttpClient client) {
		mClient = client;
	}

	public void call(String url, CallRet ret) {
		HttpPost httppost = new HttpPost(url);
		execute(httppost, ret);
	}

	public void call(String url, HttpEntity entity, CallRet ret) {
		call(url, entity.getContentType().getValue(), entity, ret);
	}

	public void call(String url, String contentType, HttpEntity entity, CallRet ret) {
		HttpPost httppost = new HttpPost(url);
		httppost.setEntity(entity);

		if (contentType != null) {
			httppost.setHeader("Content-Type", contentType);
		}
		execute(httppost, ret);
	}

	protected void execute(HttpPost httpPost, CallRet ret) {
		new ClientExecuter().execute(httpPost, ret);
	}

	protected HttpResponse roundtrip(HttpPost httpPost) throws IOException {
		httpPost.setHeader("User-Agent", Conf.USER_AGENT);
		return mClient.execute(httpPost);
	}

	class ClientExecuter extends AsyncTask<Object, Object, Object> {
		HttpPost httpPost;
		CallRet ret;
		
		@Override
		protected Object doInBackground(Object... objects) {
			httpPost = (HttpPost) objects[0];
			ret = (CallRet) objects[1];
			String errMsg = "";

			HttpResponse resp;
			try {
				resp = roundtrip(httpPost);
			} catch (IOException e) {
				e.printStackTrace();
				return e;
			}

			if (resp.getHeaders("X-Log").length > 0) {
				errMsg = resp.getHeaders("X-Log")[0].getValue();
			}

			int statusCode = resp.getStatusLine().getStatusCode();


			byte[] data = new byte[0];
			try {
				data = EntityUtils.toByteArray(resp.getEntity());
			} catch (IOException e) {
				e.printStackTrace();
			}

			if (statusCode / 100 != 2) {
				errMsg += new String(data);
				return new Exception(errMsg);
			}

			return data;
		}

		@Override
		protected void onPostExecute(Object o) {
			if (o instanceof Exception) {
				ret.onFailure((Exception) o);
				return;
			}
			ret.onSuccess((byte[]) o);
		}
	};

	public static Client defaultClient() {
		return new Client(getMultithreadClient());
	}

	public static HttpClient getMultithreadClient() {
		HttpParams params = new BasicHttpParams();
		SchemeRegistry registry = new SchemeRegistry();
		registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
		ClientConnectionManager cm = new ThreadSafeClientConnManager(params, registry);
		HttpClient client = new DefaultHttpClient(cm, params);
		return client;
	}
}

@@ -1878,29 +1878,15 @@
 %0A%09%09%09
-S
 tr
-ing errMsg = %22%22;%0A%0A
+y %7B%0A%09
 %09%09%09H
@@ -1901,27 +1901,8 @@
 nse 
-resp;%0A%09%09%09try %7B%0A%09%09%09%09
 resp
@@ -1932,184 +1932,67 @@
 %0A%09%09%09
-%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09return e;%0A%09%09%09%7D%0A%0A%09%09%09if (resp.getHeaders(%22X-Log%22).length %3E 0) %7B%0A%09%09%09%09errMsg = resp.getHeaders(%22X-Log%22)%5B0%5D.getValue();%0A%09%09%09%7D%0A%0A
+%09byte%5B%5D data = EntityUtils.toByteArray(resp.getEntity());%0A%09
 %09%09%09i
@@ -2049,282 +2049,181 @@
 ();%0A
-%0A%0A%09%09%09byte%5B%5D data = new byte%5B0%5D;%0A%09%09%09try %7B%0A%09%09%09%09data = EntityUtils.toByteArray(resp.getEntity());%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%0A%09%09%09if (statusCode / 100 != 2) %7B%0A%09%09%09%09errMsg += new String(data);%0A%09%09%09%09return new Exception(errMsg);%0A%09%09%09%7D%0A%0A%09%09%09return data;
+%09%09%09%09if (statusCode / 100 != 2) %7B%0A%09%09%09%09%09return new Exception(new String(data));%0A%09%09%09%09%7D%0A%09%09%09%09return data;%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%09return e;%0A%09%09%09%7D
 %0A%09%09%7D

fix client response