11package  com .qiniu .auth ;
22
33import  android .os .AsyncTask ;
4- import  com .qiniu .conf .Conf ;
5- import  com .qiniu .utils .ICancel ;
4+ 
65import  org .apache .http .Header ;
76import  org .apache .http .HttpEntity ;
87import  org .apache .http .HttpResponse ;
2423import  java .io .IOException ;
2524import  java .util .concurrent .TimeUnit ;
2625
26+ 
27+ import  com .qiniu .conf .Conf ;
28+ import  com .qiniu .utils .ICancel ;
29+ import  com .qiniu .utils .QiniuException ;
30+ 
2731public  class  Client  {
2832
2933	protected  HttpClient  mClient ;
@@ -44,10 +48,8 @@ public static ClientExecutor get(String url, CallRet ret) {
4448
4549	public  ClientExecutor  call (ClientExecutor  client , String  url , HttpEntity  entity , CallRet  ret ) {
4650		Header  header  = entity .getContentType ();
47- 		String  contentType  = "application/octet-stream" ;
48- 		if  (header  != null ) {
49- 			contentType  = header .getValue ();
50- 		}
51+ 		String  contentType  = header  == null  ? "application/octet-stream"  :  header .getValue ();
52+ 
5153		return  call (client , url , contentType , entity , ret );
5254	}
5355
@@ -76,7 +78,7 @@ protected ClientExecutor execute(ClientExecutor client, HttpRequestBase httpRequ
7678	}
7779
7880	protected  HttpResponse  roundtrip (HttpRequestBase  httpRequest ) throws  IOException  {
79- 		httpRequest .setHeader ("User-Agent" , Conf .USER_AGENT );
81+ 		httpRequest .setHeader ("User-Agent" , Conf .getUserAgent () );
8082		return  mClient .execute (httpRequest );
8183	}
8284
@@ -91,33 +93,49 @@ public void setup(HttpRequestBase httpRequest, CallRet ret) {
9193		public  void  upload (long  current , long  total ) {
9294			publishProgress (current , total );
9395		}
94- 		 
96+ 
9597		@ Override 
9698		protected  Object  doInBackground (Object ... objects ) {
9799			try  {
98100				HttpResponse  resp  = roundtrip (mHttpRequest );
99101				int  statusCode  = resp .getStatusLine ().getStatusCode ();
100- 				String  xl  = resp .getFirstHeader ("X-Log" ).getValue ();
102+ 				String  phrase  = resp .getStatusLine ().getReasonPhrase ();
103+ 
104+ 				Header  h  = resp .getFirstHeader ("X-Log" );
105+ 				String  xl  = h  == null  ? "" :h .getValue ();
106+ 
107+ 				h  = resp .getFirstHeader ("X-Reqid" );
108+ 				String  reqId  = h  == null  ? "" :h .getValue ();
101109
102- 				if  (statusCode  == 401 ) return  new  Exception ("unauthorized!" ); // android 2.3 will not response 
103- 				if  (xl .contains ("invalid BlockCtx" )) return  new  Exception (xl );
110+ 				if  (statusCode  == 401 ) {
111+ 					return  new  QiniuException (401 , reqId , phrase ); // android 2.3 will not response 
112+ 				}
104113
105114				byte [] data  = EntityUtils .toByteArray (resp .getEntity ());
106- 				if  (statusCode  / 100  == 2 ) return  data ;
107- 				if  (data .length  > 0 ) return  new  Exception (new  String (data ));
108- 				if  (xl .length () > 0 ) return  new  Exception (xl );
109- 				return  new  Exception (resp .getStatusLine ().getStatusCode () + ":"  + resp .getStatusLine ().getReasonPhrase ());
115+ 				if  (statusCode  / 100  == 2 ) {
116+ 					return  data ;
117+ 				}
118+ 
119+ 				if  (data .length  > 0 ) {
120+ 					return  new  QiniuException (statusCode , reqId , new  String (data ));
121+ 				}
122+ 				if  (xl .length () > 0 ) {
123+ 					return  new  QiniuException (statusCode , reqId , xl );
124+ 				}
125+ 				return  new  QiniuException (statusCode , reqId , phrase );
110126			} catch  (IOException  e ) {
111127				e .printStackTrace ();
112- 				return  e ;
128+ 				return  new   QiniuException ( QiniuException . IO ,  "net IOException" ,  e ) ;
113129			}
114130		}
115131
116132		@ Override 
117133		protected  void  onProgressUpdate (Object ... values ) {
118- 			if  (failed ) return ;
119- 			if  (values .length  == 1  && values [0 ] instanceof  Exception ) {
120- 				mRet .onFailure ((Exception ) values [0 ]);
134+ 			if  (failed ){
135+ 				return ;
136+ 			}
137+ 			if  (values .length  == 1  && values [0 ] instanceof  QiniuException ) {
138+ 				mRet .onFailure ((QiniuException ) values [0 ]);
121139				failed  = true ;
122140				return ;
123141			}
@@ -126,15 +144,17 @@ protected void onProgressUpdate(Object... values) {
126144
127145		@ Override 
128146		protected  void  onPostExecute (Object  o ) {
129- 			if  (failed ) return ;
130- 			if  (o  instanceof  Exception ) {
131- 				mRet .onFailure ((Exception ) o );
147+ 			if  (failed ) {
148+ 				return ;
149+ 			}
150+ 			if  (o  instanceof  QiniuException ) {
151+ 				mRet .onFailure ((QiniuException ) o );
132152				return ;
133153			}
134154			mRet .onSuccess ((byte []) o );
135155		}
136156
137- 		public  void  onFailure (Exception  ex ) {
157+ 		public  void  onFailure (QiniuException  ex ) {
138158			publishProgress (ex );
139159			cancel (true );
140160		}
0 commit comments