Skip to content

Commit f294f07

Browse files
committed
Merge pull request qiniu#14 from hughlv/master
Fixed the encodeParams missing issue.
2 parents a99c434 + 3674668 commit f294f07

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/main/java/com/qiniu/qbox/auth/Client.java

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.qiniu.qbox.auth;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
35
import java.util.List;
6+
import java.util.Map.Entry;
47

58
import org.apache.commons.codec.binary.Base64;
69
import org.apache.http.HttpResponse;
@@ -9,9 +12,12 @@
912
import org.apache.http.client.HttpClient;
1013
import org.apache.http.client.entity.UrlEncodedFormEntity;
1114
import org.apache.http.client.methods.HttpPost;
15+
import org.apache.http.client.utils.URLEncodedUtils;
16+
import org.apache.http.entity.AbstractHttpEntity;
1217
import org.apache.http.entity.ByteArrayEntity;
1318
import org.apache.http.entity.StringEntity;
1419
import org.apache.http.impl.client.DefaultHttpClient;
20+
import org.apache.http.message.BasicNameValuePair;
1521
import org.apache.http.util.EntityUtils;
1622

1723
public abstract class Client {
@@ -53,14 +59,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
5359
}
5460
}
5561

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) {
6463

6564
HttpPost postMethod = new HttpPost(url);
6665

@@ -80,6 +79,18 @@ public CallRet callWithBinary(String url, String contentType, byte[] body, long
8079
}
8180
}
8281

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+
8394
private CallRet handleResult(HttpResponse response) {
8495

8596
if (response == null || response.getStatusLine() == null) {
@@ -100,19 +111,6 @@ private CallRet handleResult(HttpResponse response) {
100111
return new CallRet(statusCode, responseBody);
101112
}
102113

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-
116114
public static byte[] urlsafeEncodeBytes(byte[] src) {
117115
if (src.length % 3 == 0) {
118116
return encodeBase64Ex(src);//, false, true);
@@ -140,4 +138,33 @@ public static String urlsafeEncodeString(byte[] src) {
140138
public static String urlsafeEncode(String text) {
141139
return new String(urlsafeEncodeBytes(text.getBytes()));
142140
}
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+
}
143170
}

0 commit comments

Comments
 (0)