1
1
package com .qiniu .qbox .auth ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .HashMap ;
3
5
import java .util .List ;
6
+ import java .util .Map .Entry ;
4
7
5
8
import org .apache .commons .codec .binary .Base64 ;
6
9
import org .apache .http .HttpResponse ;
9
12
import org .apache .http .client .HttpClient ;
10
13
import org .apache .http .client .entity .UrlEncodedFormEntity ;
11
14
import org .apache .http .client .methods .HttpPost ;
15
+ import org .apache .http .client .utils .URLEncodedUtils ;
16
+ import org .apache .http .entity .AbstractHttpEntity ;
12
17
import org .apache .http .entity .ByteArrayEntity ;
13
18
import org .apache .http .entity .StringEntity ;
14
19
import org .apache .http .impl .client .DefaultHttpClient ;
20
+ import org .apache .http .message .BasicNameValuePair ;
15
21
import org .apache .http .util .EntityUtils ;
16
22
17
23
public abstract class Client {
@@ -53,14 +59,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
53
59
}
54
60
}
55
61
56
- public CallRet callWithBinary (String url , String contentType , byte [] body , long bodyLength ) {
57
-
58
- ByteArrayEntity entity = new ByteArrayEntity (body );
59
-
60
- if (contentType == null || contentType .length () == 0 ) {
61
- contentType = "application/octet-stream" ;
62
- }
63
- entity .setContentType (contentType );
62
+ public CallRet callWithBinary (String url , AbstractHttpEntity entity ) {
64
63
65
64
HttpPost postMethod = new HttpPost (url );
66
65
@@ -80,6 +79,18 @@ public CallRet callWithBinary(String url, String contentType, byte[] body, long
80
79
}
81
80
}
82
81
82
+ public CallRet callWithBinary (String url , String contentType , byte [] body , long bodyLength ) {
83
+
84
+ ByteArrayEntity entity = new ByteArrayEntity (body );
85
+
86
+ if (contentType == null || contentType .isEmpty ()) {
87
+ contentType = "application/octet-stream" ;
88
+ }
89
+ entity .setContentType (contentType );
90
+
91
+ return callWithBinary (url , entity );
92
+ }
93
+
83
94
private CallRet handleResult (HttpResponse response ) {
84
95
85
96
if (response == null || response .getStatusLine () == null ) {
@@ -100,19 +111,6 @@ private CallRet handleResult(HttpResponse response) {
100
111
return new CallRet (statusCode , responseBody );
101
112
}
102
113
103
- private static byte [] encodeBase64Ex (byte [] src ) {
104
- byte [] b64 = Base64 .encodeBase64 (src ); // urlsafe version is not supported in version 1.4 or lower.
105
-
106
- for (int i = 0 ; i < b64 .length ; i ++) {
107
- if (b64 [i ] == '/' ) {
108
- b64 [i ] = '_' ;
109
- } else if (b64 [i ] == '+' ) {
110
- b64 [i ] = '-' ;
111
- }
112
- }
113
- return b64 ;
114
- }
115
-
116
114
public static byte [] urlsafeEncodeBytes (byte [] src ) {
117
115
if (src .length % 3 == 0 ) {
118
116
return encodeBase64Ex (src );//, false, true);
@@ -140,4 +138,33 @@ public static String urlsafeEncodeString(byte[] src) {
140
138
public static String urlsafeEncode (String text ) {
141
139
return new String (urlsafeEncodeBytes (text .getBytes ()));
142
140
}
141
+
142
+ private static byte [] encodeBase64Ex (byte [] src ) {
143
+ byte [] b64 = Base64 .encodeBase64 (src ); // urlsafe version is not supported in version 1.4 or lower.
144
+
145
+ for (int i = 0 ; i < b64 .length ; i ++) {
146
+ if (b64 [i ] == '/' ) {
147
+ b64 [i ] = '_' ;
148
+ } else if (b64 [i ] == '+' ) {
149
+ b64 [i ] = '-' ;
150
+ }
151
+ }
152
+ return b64 ;
153
+ }
154
+
155
+ @ SuppressWarnings ("unchecked" )
156
+ public static String encodeParams (Object params1 ) {
157
+ if (params1 instanceof String ) {
158
+ return (String )params1 ;
159
+ }
160
+ if (params1 instanceof HashMap <?, ?>) {
161
+ HashMap <String , String > params = (HashMap <String , String >)params1 ;
162
+ ArrayList <NameValuePair > list = new ArrayList <NameValuePair >();
163
+ for (Entry <String , String > entry : params .entrySet ()) {
164
+ list .add (new BasicNameValuePair (entry .getKey (), entry .getValue ()));
165
+ }
166
+ return URLEncodedUtils .format (list , "UTF-8" );
167
+ }
168
+ return null ;
169
+ }
143
170
}
0 commit comments