Skip to content

Commit

Permalink
use autozone
Browse files Browse the repository at this point in the history
  • Loading branch information
longbai committed Sep 21, 2016
1 parent 0b37731 commit efd6c32
Show file tree
Hide file tree
Showing 31 changed files with 610 additions and 773 deletions.
9 changes: 9 additions & 0 deletions .xmake/cache/history
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

{
["__version"] = "XMake v2.0.5"
, ["cmdlines"] =
{
"xmake"
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ apply plugin: 'checkstyle'


def versionName() {
String config = 'src/main/java/com/qiniu/common/Config.java'
String config = 'src/main/java/com/qiniu/common/Constants.java'
String fileContents = new File(config).text
Matcher myMatcher = fileContents =~ /VERSION = "(.+)";/
String version = myMatcher[0][1]
Expand Down
154 changes: 154 additions & 0 deletions src/main/java/com/qiniu/common/AutoZone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.qiniu.common;

import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.util.Json;
import com.qiniu.util.UrlSafeBase64;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by bailong on 16/9/15.
*/
final class AutoZone extends Zone {
static AutoZone instance = new AutoZone();
private final String ucServer;
private Map<ZoneIndex, ZoneInfo> zones = new ConcurrentHashMap<>();
private Client client;

AutoZone() {
this("https://uc.qbox.me");
}

AutoZone(String ucServer) {
this.ucServer = ucServer;
client = new Client();
}

private UCRet getZoneJson(ZoneIndex index) throws QiniuException {
String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;

Response r = client.get(address);
return r.jsonToObject(UCRet.class);
}

// only for test public
ZoneInfo zoneInfo(String ak, String bucket) throws QiniuException {
ZoneIndex index = new ZoneIndex(ak, bucket);
ZoneInfo info = zones.get(index);
if (info == null) {
UCRet ret = getZoneJson(index);
try {
info = ZoneInfo.buildFromUcRet(ret);
} catch (Exception e) {
e.printStackTrace();
}

if (info != null) {
zones.put(index, info);
}
}
return info;
}

// only for test public
ZoneInfo queryByToken(String token) {
try {
// http://developer.qiniu.com/article/developer/security/upload-token.html
// http://developer.qiniu.com/article/developer/security/put-policy.html
String[] strings = token.split(":");
String ak = strings[0];
String policy = new String(UrlSafeBase64.decode(strings[2]), Constants.UTF_8);
String bkt = Json.decode(policy).get("scope").toString().split(":")[0];
return zoneInfo(ak, bkt);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public String upHost(String token) {
ZoneInfo info = queryByToken(token);
if (info == null) {
return "";
}
return info.upHost;
}

public String upHostBackup(String token) {
ZoneInfo info = queryByToken(token);
if (info == null) {
return "";
}
return info.upBackup;
}

public String upIpBackup(String token) {
ZoneInfo info = queryByToken(token);
if (info == null) {
return "";
}
return info.upIp;
}

public String upHostHttps(String token) {
ZoneInfo info = queryByToken(token);
if (info == null) {
return "";
}
return info.upHttps;
}

static class ZoneInfo {
final String ioHost;
final String upHost;
final String upIp;
final String upBackup;
final String upHttps;

private ZoneInfo(String ioHost, String upHost, String upIp, String upBackup, String upHttps) {
this.ioHost = ioHost;
this.upHost = upHost;
this.upIp = upIp;
this.upBackup = upBackup;
this.upHttps = upHttps;
}

static ZoneInfo buildFromUcRet(UCRet ret) {
String ioHost = ret.http.get("io").get(0);
List<String> up = ret.http.get("up");
String upHost = up.get(0);
String upBackup = up.get(1);
String upIp = up.get(2).split(" ")[2].split("//")[1];
String upHttps = ret.https.get("up").get(0);

return new ZoneInfo(ioHost, upHost, upIp, upBackup, upHttps);
}
}

private static class ZoneIndex {
private final String accessKey;
private final String bucket;

ZoneIndex(String accessKey, String bucket) {
this.accessKey = accessKey;
this.bucket = bucket;
}

public int hashCode() {
return accessKey.hashCode() * 37 + bucket.hashCode();
}

public boolean equals(Object obj) {
return obj == this || !(obj == null || !(obj instanceof ZoneIndex))
&& ((ZoneIndex) obj).accessKey.equals(accessKey) && ((ZoneIndex) obj).bucket.equals(bucket);
}
}

private class UCRet {
Map<String, List<String>> http;
Map<String, List<String>> https;
}
}
88 changes: 0 additions & 88 deletions src/main/java/com/qiniu/common/Config.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/com/qiniu/common/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.qiniu.common;

import java.nio.charset.Charset;

/**
* Created by bailong on 16/9/14.
*/
public interface Constants {
String VERSION = "8.0.0";
int BLOCK_SIZE = 4 * 1024 * 1024;
Charset UTF_8 = Charset.forName("UTF-8");
/**
* 连接超时时间 单位秒(默认10s)
*/
public int CONNECT_TIMEOUT = 10;
/**
* 写超时时间 单位秒(默认 0 , 不超时)
*/
public int WRITE_TIMEOUT = 0;
/**
* 回复超时时间 单位秒(默认30s)
*/
public int RESPONSE_TIMEOUT = 30;
}

72 changes: 72 additions & 0 deletions src/main/java/com/qiniu/common/FixedZone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.qiniu.common;

/**
* Created by bailong on 16/9/15.
*/
public final class FixedZone extends Zone {
/**
* 默认上传服务器
*/
private final String upHost;
/**
* 备用上传服务器,当默认服务器网络链接失败时使用
*/
private final String upHostBackup;

private final String upHostHttps;

private final String upHttpIp;

private final String rsHost;

private final String rsfHost;

private final String ioHost;

private final String apiHost;


public FixedZone(String upHost, String upHostBackup, String upHttpIp,
String rsHost, String rsfHost, String ioHost, String upHostHttps, String apiHost) {
this.upHost = upHost;
this.upHostBackup = upHostBackup;
this.upHttpIp = upHttpIp;
this.rsHost = rsHost;
this.rsfHost = rsfHost;
this.ioHost = ioHost;
this.upHostHttps = upHostHttps;
this.apiHost = apiHost;
}

public String upHost(String token) {
return upHost;
}

public String upHostBackup(String token) {
return upHostBackup;
}

public String upIpBackup(String token) {
return upHttpIp;
}

public String upHostHttps(String token) {
return upHostHttps;
}

public String rsHost(String ak, String bucket) {
return rsHost;
}

public String rsfHost(String ak, String bucket) {
return rsfHost;
}

public String ioHost(String ak, String bucket) {
return ioHost;
}

public String apiHost(String ak, String bucket) {
return apiHost;
}
}
Loading

0 comments on commit efd6c32

Please sign in to comment.