Skip to content

Commit

Permalink
Merge pull request #200 from longbai/okhttp3
Browse files Browse the repository at this point in the history
up to okhttp3
  • Loading branch information
longbai committed Apr 27, 2016
2 parents 1144aca + 098b530 commit b7bc4dd
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sudo: false
language: java

jdk:
- openjdk6
# - openjdk6
- oraclejdk7
- oraclejdk8

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Changelog

## 7.1.0 (2016-04-27)
### 增加
* 升级到okhttp3, 升级次版本号。

## 7.0.9 (2016-04-22)
### 增加
* 强制copy或者move
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.0.0, 7.0.99]</version>
<version>[7.0.0, 7.1.99]</version>
</dependency>
```
或者 Gradle:
```groovy
compile 'com.qiniu:qiniu-java-sdk:7.0.+'
compile 'com.qiniu:qiniu-java-sdk:7.1.+'
```
jdk 6.0 不能直接使用mvn上的okhttp, 需要另外下载,[代码][2], [okhttp.jar][3], [okio.jar][4]
7.0.x 版本的jdk 6.0 不能直接使用mvn上的okhttp, 需要另外下载,[代码][2], [okhttp.jar][3], [okio.jar][4]
7.1.x 版本 jdk6.0 支持 后面会做处理,暂时只支持7及以上。

## 运行环境

| Qiniu SDK版本 | Java 版本 |
|:--------------------:|:---------------------------:|
| 7.x | 6+ |
| 7.1.x | 7+ |
| 7.0.x | 6+ |
| 6.x | 6+ |

## 使用方法
Expand Down
31 changes: 18 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import java.util.regex.Matcher

apply plugin: 'java'

sourceCompatibility = 1.6
targetCompatibility = 1.6
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
[compileJava,compileTestJava,javadoc]*.options*.encoding = 'UTF-8'

Expand All @@ -12,19 +12,24 @@ repositories {
}

dependencies {
String version = System.getProperty("java.version")
println("JDK: " + version)
int c = version.compareTo("1.7")
if (c < 0) {
compile fileTree(dir: 'libs', include: '*.jar')
} else {
compile group:'com.squareup.okhttp', name:'okhttp', version:'2.7.1'
}
compile group:'com.google.code.gson', name:'gson', version:'2.3.1'
testCompile group: 'junit', name: 'junit', version: '4.11'
// String version = System.getProperty("java.version")
// println("JDK: " + version)
// int c = version.compareTo("1.7")
// if (c < 0) {
// compile fileTree(dir: 'libs', include: '*.jar')
// } else {
compile group:'com.squareup.okhttp3', name:'okhttp', version:'3.2.0'
// }
compile group:'com.google.code.gson', name:'gson', version:'2.6.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
println("JDK : " + version)
}


task getHomeDir << {
println gradle.gradleHomeDir
}

apply plugin: 'checkstyle'


Expand Down Expand Up @@ -56,4 +61,4 @@ task gen_eclipse(dependsOn:[
'cleanEclipseProject', 'cleanEclipseClasspath',
'eclipseProject', 'eclipseClasspath'])
eclipseProject.mustRunAfter cleanEclipseProject
eclipseClasspath.mustRunAfter cleanEclipseClasspath
eclipseClasspath.mustRunAfter cleanEclipseClasspath
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip
2 changes: 1 addition & 1 deletion src/main/java/com/qiniu/common/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public final class Config {

public static final String VERSION = "7.0.9";
public static final String VERSION = "7.1.0";
/**
* 断点上传时的分块大小(默认的分块大小, 不允许改变)
*/
Expand Down
48 changes: 27 additions & 21 deletions src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.qiniu.common.QiniuException;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import com.squareup.okhttp.*;
import okhttp3.*;
import okio.BufferedSink;

import java.io.File;
Expand All @@ -25,25 +25,31 @@ public Client() {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(64);
dispatcher.setMaxRequestsPerHost(16);
ConnectionPool connectionPool = new ConnectionPool(32, 5 * 60 * 1000);
httpClient = new OkHttpClient();
httpClient.setDispatcher(dispatcher);
httpClient.setConnectionPool(connectionPool);
httpClient.networkInterceptors().add(new Interceptor() {
ConnectionPool connectionPool = new ConnectionPool(32, 5, TimeUnit.MINUTES);
OkHttpClient.Builder builder = new OkHttpClient.Builder();

builder.dispatcher(dispatcher);
builder.connectionPool(connectionPool);
builder.addNetworkInterceptor(new Interceptor() {
@Override
public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();

com.squareup.okhttp.Response response = chain.proceed(request);
okhttp3.Response response = chain.proceed(request);
IpTag tag = (IpTag) request.tag();
String ip = chain.connection().getSocket().getRemoteSocketAddress().toString();
tag.ip = ip;
try {
tag.ip = chain.connection().socket().getRemoteSocketAddress().toString();
} catch (Exception e) {
e.printStackTrace();
tag.ip = "";
}
return response;
}
});
httpClient.setConnectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
httpClient.setReadTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
httpClient.setWriteTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);
builder.connectTimeout(Config.CONNECT_TIMEOUT, TimeUnit.SECONDS);
builder.readTimeout(Config.RESPONSE_TIMEOUT, TimeUnit.SECONDS);
builder.writeTimeout(Config.WRITE_TIMEOUT, TimeUnit.SECONDS);
httpClient = builder.build();
}

private static String userAgent() {
Expand Down Expand Up @@ -94,7 +100,7 @@ public Response post(String url, String body, StringMap headers) throws QiniuExc
}

public Response post(String url, StringMap params, StringMap headers) throws QiniuException {
final FormEncodingBuilder f = new FormEncodingBuilder();
final FormBody.Builder f = new FormBody.Builder();
params.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
Expand Down Expand Up @@ -160,7 +166,7 @@ private Response multipartPost(String url,
String fileName,
RequestBody file,
StringMap headers) throws QiniuException {
final MultipartBuilder mb = new MultipartBuilder();
final MultipartBody.Builder mb = new MultipartBody.Builder();
mb.addFormDataPart(name, fileName, file);

fields.forEach(new StringMap.Consumer() {
Expand All @@ -169,7 +175,7 @@ public void accept(String key, Object value) {
mb.addFormDataPart(key, value.toString());
}
});
mb.type(MediaType.parse("multipart/form-data"));
mb.setType(MediaType.parse("multipart/form-data"));
RequestBody body = mb.build();
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
return send(requestBuilder, headers);
Expand All @@ -187,7 +193,7 @@ public void accept(String key, Object value) {

requestBuilder.header("User-Agent", userAgent());
long start = System.currentTimeMillis();
com.squareup.okhttp.Response res = null;
okhttp3.Response res = null;
Response r;
double duration = (System.currentTimeMillis() - start) / 1000.0;
IpTag tag = new IpTag();
Expand Down Expand Up @@ -220,14 +226,14 @@ public void accept(String key, Object value) {
IpTag tag = new IpTag();
httpClient.newCall(requestBuilder.tag(tag).build()).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
public void onFailure(Call call, IOException e) {
e.printStackTrace();
long duration = (System.currentTimeMillis() - start) / 1000;
cb.complete(Response.createError(null, "", duration, e.getMessage()));
}

@Override
public void onResponse(com.squareup.okhttp.Response response) throws IOException {
public void onResponse(Call call, okhttp3.Response response) throws IOException {
long duration = (System.currentTimeMillis() - start) / 1000;
cb.complete(Response.create(response, "", duration));
}
Expand Down Expand Up @@ -279,7 +285,7 @@ private void asyncMultipartPost(String url,
RequestBody file,
StringMap headers,
AsyncCallback cb) {
final MultipartBuilder mb = new MultipartBuilder();
final MultipartBody.Builder mb = new MultipartBody.Builder();
mb.addFormDataPart(name, fileName, file);

fields.forEach(new StringMap.Consumer() {
Expand All @@ -288,7 +294,7 @@ public void accept(String key, Object value) {
mb.addFormDataPart(key, value.toString());
}
});
mb.type(MediaType.parse("multipart/form-data"));
mb.setType(MediaType.parse("multipart/form-data"));
RequestBody body = mb.build();
Request.Builder requestBuilder = new Request.Builder().url(url).post(body);
asyncSend(requestBuilder, headers, cb);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/qiniu/http/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;
import com.squareup.okhttp.MediaType;
import okhttp3.MediaType;

import java.io.IOException;
import java.util.Locale;
Expand Down Expand Up @@ -48,9 +48,9 @@ public final class Response {
public final String address;

private byte[] body;
private com.squareup.okhttp.Response response;
private okhttp3.Response response;

private Response(com.squareup.okhttp.Response response, int statusCode, String reqId, String xlog, String xvia,
private Response(okhttp3.Response response, int statusCode, String reqId, String xlog, String xvia,
String address, double duration, String error, byte[] body) {
this.response = response;
this.statusCode = statusCode;
Expand All @@ -63,7 +63,7 @@ private Response(com.squareup.okhttp.Response response, int statusCode, String r
this.body = body;
}

static Response create(com.squareup.okhttp.Response response, String address, double duration) {
static Response create(okhttp3.Response response, String address, double duration) {
String error = null;
int code = response.code();
String reqId = null;
Expand All @@ -88,7 +88,7 @@ static Response create(com.squareup.okhttp.Response response, String address, do
address, duration, error, body);
}

static Response createError(com.squareup.okhttp.Response response, String address, double duration, String error) {
static Response createError(okhttp3.Response response, String address, double duration, String error) {
if (response == null) {
return new Response(null, -1, "", "", "", "", duration, error, null);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ static Response createError(com.squareup.okhttp.Response response, String addres
}


private static String via(com.squareup.okhttp.Response response) {
private static String via(okhttp3.Response response) {
String via;
if (!(via = response.header("X-Via", "")).equals("")) {
return via;
Expand All @@ -132,7 +132,7 @@ private static String via(com.squareup.okhttp.Response response) {
return via;
}

private static String ctype(com.squareup.okhttp.Response response) {
private static String ctype(okhttp3.Response response) {
MediaType mediaType = response.body().contentType();
if (mediaType == null) {
return "";
Expand Down Expand Up @@ -207,7 +207,7 @@ public boolean isJson() {
}

public String url() {
return response.request().urlString();
return response.request().url().toString();
}

public static class ErrorBody {
Expand Down
17 changes: 8 additions & 9 deletions src/test/java/com/qiniu/storage/FormUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ public void testSizeMin2() {
}
}

class MyRet {
public String hash;
public String key;
public String fsize;
public String fname;
public String mimeType;
}

// @Test
public void testFormLargeSize() {
Config.PUT_THRESHOLD = 25 * 1024 * 1024;
Expand All @@ -277,7 +269,6 @@ public void testFormLargeSize() {

}


// @Test
public void testFormLargeSize2() {
Config.PUT_THRESHOLD = 25 * 1024 * 1024;
Expand Down Expand Up @@ -308,4 +299,12 @@ public void testFormLargeSize2() {

}

class MyRet {
public String hash;
public String key;
public String fsize;
public String fname;
public String mimeType;
}

}
48 changes: 24 additions & 24 deletions src/test/java/com/qiniu/storage/RecordUploadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,6 @@ public void test25M1k() throws Throwable {
template(1024 * 25 + 1);
}

class Up implements Callable<Response> {
private final UploadManager uploadManager;
private final File file;
private final String key;
private final String token;

public Up(UploadManager uploadManager, File file, String key, String token) {
this.uploadManager = uploadManager;
this.file = file;
this.key = key;
this.token = token;
}

@Override
public Response call() throws Exception {
Response res = uploadManager.put(file, key, token);
System.out.println("up: " + res);
System.out.println("up: " + res.bodyString());
isDone = true;
response = res;
return res;
}
}

@Test
public void testLastModify() throws IOException {
File f = File.createTempFile("qiniutest", "b");
Expand Down Expand Up @@ -240,4 +216,28 @@ public void testLastModify() throws IOException {
long m4 = recoderFile.lastModified();
assertTrue(m4 > m1);
}

class Up implements Callable<Response> {
private final UploadManager uploadManager;
private final File file;
private final String key;
private final String token;

public Up(UploadManager uploadManager, File file, String key, String token) {
this.uploadManager = uploadManager;
this.file = file;
this.key = key;
this.token = token;
}

@Override
public Response call() throws Exception {
Response res = uploadManager.put(file, key, token);
System.out.println("up: " + res);
System.out.println("up: " + res.bodyString());
isDone = true;
response = res;
return res;
}
}
}
Loading

0 comments on commit b7bc4dd

Please sign in to comment.