Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom field name #102

Merged
merged 5 commits into from
Apr 3, 2014
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JRE 6 应该去掉

<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/qiniu/api/auth/digest/Mac.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public Mac(String accessKey, String secretKey) {
* @throws AuthException
*/
public String sign(byte[] data) throws AuthException {
System.out.println("data : " + new String(data));
javax.crypto.Mac mac = null;
try {
mac = javax.crypto.Mac.getInstance("HmacSHA1");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/qiniu/api/io/IoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;

Expand Down Expand Up @@ -38,6 +39,7 @@ private static PutRet put(String uptoken, String key, File file,
AbstractContentBody fileBody = buildFileBody(file, extra);
requestEntity.addPart("file", fileBody);
setKey(requestEntity, key);
setParam(requestEntity, extra.params);
if (extra.checkCrc != NO_CRC32) {
if (extra.crc32 == 0) {
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
Expand Down Expand Up @@ -68,13 +70,23 @@ private static void setKey(MultipartEntity requestEntity, String key) throws Uns
}
}

private static void setParam(MultipartEntity requestEntity, Map<String, String> params) throws UnsupportedEncodingException{
if(params == null){
return;
}
for(String name : params.keySet()){
requestEntity.addPart(name, new StringBody(params.get(name),Charset.forName("utf-8")));
}
}

private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
MultipartEntity requestEntity = new MultipartEntity();
try {
requestEntity.addPart("token", new StringBody(uptoken));
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
requestEntity.addPart("file", inputBody);
setKey(requestEntity, key);
setParam(requestEntity, extra.params);
if (extra.checkCrc != NO_CRC32) {
if (extra.crc32 == 0) {
return new PutRet(new CallRet(400, new Exception("no crc32 specified!")));
Expand Down
40 changes: 39 additions & 1 deletion src/test/java/com/qiniu/testing/IOTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.qiniu.testing;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import junit.framework.TestCase;

Expand Down Expand Up @@ -44,23 +48,57 @@ public void setUp() {

// just upload an image in testdata.
public void testPut() throws Exception {
String uptoken = new PutPolicy(bucketName).token(mac);
// must start with "x:"
String xname = "x:test_name";
String xvalue = "test_value";
String name = "nonxtest_name";
String value = "nonxtest_value";

PutPolicy putPolicy = new PutPolicy(bucketName);
putPolicy.returnBody = "{\"hash\":\"$(etag)\",\"key\":\"$(key)\",\"fsize\":\"$(fsize)\",\""+xname+"\":\"$("+xname+")\",\""+name+"\":\"$("+name+")\"}";
String uptoken = putPolicy.token(mac);

String dir = System.getProperty("user.dir");
String localFile = dir + "/testdata/" + "logo.png";

PutExtra extra = new PutExtra();
Map<String, String> params = new HashMap<String,String>();
params.put(xname, xvalue);
params.put(name, value);
extra.params = params;

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

JSONObject jsonObject = new JSONObject(ret.response);
assertEquals(xvalue, getJsonValue(jsonObject, xname));
assertEquals(null, getJsonValue(jsonObject, name));

//test stream upload
{
String str="Hello,Qiniu";
ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes());
ret = IoApi.Put(uptoken, key2, stream, extra);
assertTrue(ret.ok());
assertTrue(expectedHash2.equals(ret.getHash()));

jsonObject = new JSONObject(ret.response);
assertEquals(xvalue, getJsonValue(jsonObject, xname));
assertEquals(null, getJsonValue(jsonObject, name));
}
}

private String getJsonValue(JSONObject jsonObject, String name){
try{
String value = jsonObject.getString(name);
// 针对使用returnBody情况
if("null".equalsIgnoreCase(value)){
return null;
}
return value;
}catch(Exception e){
return null;
}
}

Expand Down