Skip to content

Commit 61da8ad

Browse files
committed
Merge pull request #102 from simon-liubin/feature/custom_field_name
Feature/custom field name
2 parents 7fc94be + 0f18b52 commit 61da8ad

File tree

5 files changed

+51
-7
lines changed

5 files changed

+51
-7
lines changed

.classpath

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
<attribute name="maven.pomderived" value="true"/>
1313
</attributes>
1414
</classpathentry>
15-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
16-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
1716
<attributes>
1817
<attribute name="maven.pomderived" value="true"/>
1918
</attributes>

.settings/org.eclipse.jdt.core.prefs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
eclipse.preferences.version=1
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3-
org.eclipse.jdt.core.compiler.compliance=1.6
42
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5-
org.eclipse.jdt.core.compiler.source=1.6

src/main/java/com/qiniu/api/auth/digest/Mac.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public Mac(String accessKey, String secretKey) {
2929
* @throws AuthException
3030
*/
3131
public String sign(byte[] data) throws AuthException {
32-
System.out.println("data : " + new String(data));
3332
javax.crypto.Mac mac = null;
3433
try {
3534
mac = javax.crypto.Mac.getInstance("HmacSHA1");

src/main/java/com/qiniu/api/io/IoApi.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private static PutRet put(String uptoken, String key, File file,
3939
AbstractContentBody fileBody = buildFileBody(file, extra);
4040
requestEntity.addPart("file", fileBody);
4141
setKey(requestEntity, key);
42+
setParam(requestEntity, extra.params);
4243
if (extra.checkCrc != NO_CRC32) {
4344
if (extra.crc32 == 0) {
4445
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
@@ -75,13 +76,23 @@ private static void setKey(MultipartEntity requestEntity, String key) throws Uns
7576
}
7677
}
7778

79+
private static void setParam(MultipartEntity requestEntity, Map<String, String> params) throws UnsupportedEncodingException{
80+
if(params == null){
81+
return;
82+
}
83+
for(String name : params.keySet()){
84+
requestEntity.addPart(name, new StringBody(params.get(name),Charset.forName("utf-8")));
85+
}
86+
}
87+
7888
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
7989
MultipartEntity requestEntity = new MultipartEntity();
8090
try {
8191
requestEntity.addPart("token", new StringBody(uptoken));
8292
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
8393
requestEntity.addPart("file", inputBody);
8494
setKey(requestEntity, key);
95+
setParam(requestEntity, extra.params);
8596
if (extra.checkCrc != NO_CRC32) {
8697
if (extra.crc32 == 0) {
8798
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));

src/test/java/com/qiniu/testing/IOTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.qiniu.testing;
22

33
import java.io.*;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
import org.json.JSONObject;
48

59
import junit.framework.TestCase;
610

@@ -44,23 +48,57 @@ public void setUp() {
4448

4549
// just upload an image in testdata.
4650
public void testPut() throws Exception {
47-
String uptoken = new PutPolicy(bucketName).token(mac);
51+
// must start with "x:"
52+
String xname = "x:test_name";
53+
String xvalue = "test_value";
54+
String name = "nonxtest_name";
55+
String value = "nonxtest_value";
56+
57+
PutPolicy putPolicy = new PutPolicy(bucketName);
58+
putPolicy.returnBody = "{\"hash\":\"$(etag)\",\"key\":\"$(key)\",\"fsize\":\"$(fsize)\",\""+xname+"\":\"$("+xname+")\",\""+name+"\":\"$("+name+")\"}";
59+
String uptoken = putPolicy.token(mac);
60+
4861
String dir = System.getProperty("user.dir");
4962
String localFile = dir + "/testdata/" + "logo.png";
5063

5164
PutExtra extra = new PutExtra();
65+
Map<String, String> params = new HashMap<String,String>();
66+
params.put(xname, xvalue);
67+
params.put(name, value);
68+
extra.params = params;
5269

5370
PutRet ret = IoApi.putFile(uptoken, key, localFile, extra);
5471
assertTrue(ret.ok());
5572
assertTrue(expectedHash.equals(ret.getHash()));
5673

74+
JSONObject jsonObject = new JSONObject(ret.response);
75+
assertEquals(xvalue, getJsonValue(jsonObject, xname));
76+
assertEquals(null, getJsonValue(jsonObject, name));
77+
5778
//test stream upload
5879
{
5980
String str="Hello,Qiniu";
6081
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
6182
ret = IoApi.Put(uptoken, key2, stream, extra);
6283
assertTrue(ret.ok());
6384
assertTrue(expectedHash2.equals(ret.getHash()));
85+
86+
jsonObject = new JSONObject(ret.response);
87+
assertEquals(xvalue, getJsonValue(jsonObject, xname));
88+
assertEquals(null, getJsonValue(jsonObject, name));
89+
}
90+
}
91+
92+
private String getJsonValue(JSONObject jsonObject, String name){
93+
try{
94+
String value = jsonObject.getString(name);
95+
// 针对使用returnBody情况
96+
if("null".equalsIgnoreCase(value)){
97+
return null;
98+
}
99+
return value;
100+
}catch(Exception e){
101+
return null;
64102
}
65103
}
66104

0 commit comments

Comments
 (0)