Skip to content

Commit

Permalink
Merge pull request #109 from qiniu/develop
Browse files Browse the repository at this point in the history
Release 6.1.3
  • Loading branch information
longbai committed Apr 28, 2014
2 parents d79d0d2 + c961af0 commit 68864b5
Show file tree
Hide file tree
Showing 25 changed files with 1,431 additions and 78 deletions.
3 changes: 1 addition & 2 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
<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">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
Expand Down
3 changes: 0 additions & 3 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.6
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
## CHANGE LOG

### v6.1.3

2014-04-28 [#109](https://github.com/qiniu/java-sdk/pull/109)

- [#101] [#102] 用户自定义参数
- [#103] 分片上传,断点续传
- [#106] bugfix: 普通流上传,key为空对象导致错误
- [#107] bugfix: 指明为utf8字符集
- [#108] 限定上传文件类型

### v6.1.2

2014-2-10 [#98](https://github.com/qiniu/java-sdk/pull/98)
Expand All @@ -13,7 +23,7 @@
- bugfix: PutExtra.mimeType 不生效问题
- PutPolicy 补充字段

### v6.0.7
### v6.0.7

2013-11-7 [#85](https://github.com/qiniu/java-sdk/pull/85)

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Java SDK
title: Java SDK | 七牛云存储
---

# Java SDK 使用指南
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
20 changes: 20 additions & 0 deletions src/main/java/com/qiniu/api/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* side only.
*/
public class Config {
public static final String CHARSET = "utf-8";

public static String USER_AGENT="qiniu java-sdk v6.0.0";

Expand All @@ -25,5 +26,24 @@ public class Config {
public static String UP_HOST = "http://up.qbox.me";

public static String RSF_HOST = "http://rsf.qbox.me";

/**
* HTTP连接超时的时间毫秒(ms)
* Determines the timeout in milliseconds until a connection is established.
* A timeout value of zero is interpreted as an infinite timeout.
*
* Please note this parameter can only be applied to connections that
* are bound to a particular local address.
*/
public static int CONNECTION_TIMEOUT = 30 * 1000;
/**
* 读取response超时的时间毫秒(ms)
* Defines the socket timeout (<code>SO_TIMEOUT</code>) in milliseconds,
* which is the timeout for waiting for data or, put differently,
* a maximum period inactivity between two consecutive data packets).
* A timeout value of zero is interpreted as an infinite timeout.
* @see java.net.SocketOptions#SO_TIMEOUT
*/
public static int SO_TIMEOUT = 30 * 1000;

}
43 changes: 34 additions & 9 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,12 +39,19 @@ 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!")));
}
requestEntity.addPart("crc32", new StringBody(extra.crc32 + ""));
}
}

if (extra.params != null) {
for (Map.Entry<String, String> xvar : extra.params.entrySet()) {
requestEntity.addPart(xvar.getKey(), new StringBody(xvar.getValue(), Charset.forName(Config.CHARSET)));
}
}
} catch (Exception e) {
e.printStackTrace();
return new PutRet(new CallRet(400, e));
Expand All @@ -64,17 +72,27 @@ private static FileBody buildFileBody(File file,PutExtra extra){

private static void setKey(MultipartEntity requestEntity, String key) throws UnsupportedEncodingException{
if(key != null){
requestEntity.addPart("key", new StringBody(key,Charset.forName("utf-8")));
requestEntity.addPart("key", new StringBody(key,Charset.forName(Config.CHARSET)));
}
}

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(Config.CHARSET)));
}
}

private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra) {
private static PutRet putStream(String uptoken, String key, InputStream reader,PutExtra extra, String fileName) {
MultipartEntity requestEntity = new MultipartEntity();
try {
requestEntity.addPart("token", new StringBody(uptoken));
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, key);
AbstractContentBody inputBody = buildInputStreamBody(reader, extra, fileName != null ? fileName : "null");
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 All @@ -91,19 +109,26 @@ private static PutRet putStream(String uptoken, String key, InputStream reader,P
return new PutRet(ret);
}

private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String key){
private static InputStreamBody buildInputStreamBody(InputStream reader,PutExtra extra, String fileName){
if(extra.mimeType != null){
return new InputStreamBody(reader, extra.mimeType, key);
return new InputStreamBody(reader, extra.mimeType, fileName);
}else{
return new InputStreamBody(reader, key);
return new InputStreamBody(reader, fileName);
}
}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra){
return putStream(uptoken,key,reader,extra, null);
}

public static PutRet put(String uptoken,String key,InputStream reader,PutExtra extra, String fileName){
return putStream(uptoken,key,reader,extra, fileName);
}


public static PutRet Put(String uptoken,String key,InputStream reader,PutExtra extra)
{
PutRet ret = putStream(uptoken,key,reader,extra);
return ret;
return put(uptoken,key,reader,extra);
}


Expand Down
27 changes: 23 additions & 4 deletions src/main/java/com/qiniu/api/net/EncodeUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qiniu.api.net;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -10,6 +11,8 @@
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import com.qiniu.api.config.Config;

/**
* URLEncoding is the alternate base64 encoding defined in RFC 4648. It is
* typically used in URLs and file names.
Expand Down Expand Up @@ -38,7 +41,7 @@ public static byte[] urlsafeEncodeBytes(byte[] src) {
}

public static byte[] urlsafeBase64Decode(String encoded){
byte[] rawbs = encoded.getBytes();
byte[] rawbs = toByte(encoded);
for(int i=0;i<rawbs.length;i++){
if(rawbs[i] == '_'){
rawbs[i] = '/';
Expand All @@ -50,11 +53,11 @@ public static byte[] urlsafeBase64Decode(String encoded){
}

public static String urlsafeEncodeString(byte[] src) {
return new String(urlsafeEncodeBytes(src));
return toString(urlsafeEncodeBytes(src));
}

public static String urlsafeEncode(String text) {
return new String(urlsafeEncodeBytes(text.getBytes()));
return toString(urlsafeEncodeBytes(toByte(text)));
}

// replace '/' with '_', '+" with '-'
Expand Down Expand Up @@ -84,8 +87,24 @@ public static String encodeParams(Object params) {
list.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
return URLEncodedUtils.format(list, "UTF-8");
return URLEncodedUtils.format(list, Config.CHARSET);
}
return null;
}

public static byte[] toByte(String s){
try {
return s.getBytes(Config.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

public static String toString(byte[] bs){
try {
return new String(bs, Config.CHARSET);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/qiniu/api/net/Http.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.CoreConnectionPNames;

import com.qiniu.api.config.Config;

/**
* The class Http provides a default HttpConnectionPool implementation. Anyway,
Expand Down Expand Up @@ -54,6 +57,10 @@ private static HttpClient makeDefaultClient() {
HttpHost localhost = new HttpHost("locahost", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);

return new DefaultHttpClient(cm);
HttpClient client = new DefaultHttpClient(cm);
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Config.CONNECTION_TIMEOUT);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, Config.SO_TIMEOUT);

return client;
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/qiniu/api/resumableio/ChunkUploadCallRet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.qiniu.api.resumableio;

import org.json.JSONException;
import org.json.JSONObject;

import com.qiniu.api.net.CallRet;

public class ChunkUploadCallRet extends CallRet {
protected String ctx;
protected String checksum;
protected int offset;
protected String host;
protected long crc32;

public ChunkUploadCallRet(CallRet ret) {
super(ret);
doUnmarshal();
}

public ChunkUploadCallRet(int statusCode, String response) {
super(statusCode, response);
doUnmarshal();
}

public ChunkUploadCallRet(int statusCode, Exception e) {
super(statusCode, e);
}

private void doUnmarshal() {
if (this.exception != null || this.response == null
|| !this.response.trim().startsWith("{")) {
return;
}
try {
unmarshal();
} catch (Exception e) {
e.printStackTrace();
if (this.exception == null) {
this.exception = e;
}
}

}

protected void unmarshal() throws JSONException{
JSONObject jsonObject = new JSONObject(this.response);
ctx = jsonObject.optString("ctx", null);
checksum = jsonObject.optString("checksum", null);
offset = jsonObject.optInt("offset", 0);
host = jsonObject.optString("host", null);
crc32 = jsonObject.optLong("crc32", 0);
}

public String getCtx() {
return ctx;
}

public String getChecksum() {
return checksum;
}

public long getOffset() {
return offset;
}

public String getHost() {
return host;
}

public long getCrc32() {
return crc32;
}

}
Loading

0 comments on commit 68864b5

Please sign in to comment.