Skip to content

Commit efd6c32

Browse files
committed
use autozone
1 parent 0b37731 commit efd6c32

31 files changed

+610
-773
lines changed

.xmake/cache/history

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
{
3+
["__version"] = "XMake v2.0.5"
4+
, ["cmdlines"] =
5+
{
6+
"xmake"
7+
}
8+
9+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ apply plugin: 'checkstyle'
3434

3535

3636
def versionName() {
37-
String config = 'src/main/java/com/qiniu/common/Config.java'
37+
String config = 'src/main/java/com/qiniu/common/Constants.java'
3838
String fileContents = new File(config).text
3939
Matcher myMatcher = fileContents =~ /VERSION = "(.+)";/
4040
String version = myMatcher[0][1]
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package com.qiniu.common;
2+
3+
import com.qiniu.http.Client;
4+
import com.qiniu.http.Response;
5+
import com.qiniu.util.Json;
6+
import com.qiniu.util.UrlSafeBase64;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
12+
/**
13+
* Created by bailong on 16/9/15.
14+
*/
15+
final class AutoZone extends Zone {
16+
static AutoZone instance = new AutoZone();
17+
private final String ucServer;
18+
private Map<ZoneIndex, ZoneInfo> zones = new ConcurrentHashMap<>();
19+
private Client client;
20+
21+
AutoZone() {
22+
this("https://uc.qbox.me");
23+
}
24+
25+
AutoZone(String ucServer) {
26+
this.ucServer = ucServer;
27+
client = new Client();
28+
}
29+
30+
private UCRet getZoneJson(ZoneIndex index) throws QiniuException {
31+
String address = ucServer + "/v1/query?ak=" + index.accessKey + "&bucket=" + index.bucket;
32+
33+
Response r = client.get(address);
34+
return r.jsonToObject(UCRet.class);
35+
}
36+
37+
// only for test public
38+
ZoneInfo zoneInfo(String ak, String bucket) throws QiniuException {
39+
ZoneIndex index = new ZoneIndex(ak, bucket);
40+
ZoneInfo info = zones.get(index);
41+
if (info == null) {
42+
UCRet ret = getZoneJson(index);
43+
try {
44+
info = ZoneInfo.buildFromUcRet(ret);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
48+
49+
if (info != null) {
50+
zones.put(index, info);
51+
}
52+
}
53+
return info;
54+
}
55+
56+
// only for test public
57+
ZoneInfo queryByToken(String token) {
58+
try {
59+
// http://developer.qiniu.com/article/developer/security/upload-token.html
60+
// http://developer.qiniu.com/article/developer/security/put-policy.html
61+
String[] strings = token.split(":");
62+
String ak = strings[0];
63+
String policy = new String(UrlSafeBase64.decode(strings[2]), Constants.UTF_8);
64+
String bkt = Json.decode(policy).get("scope").toString().split(":")[0];
65+
return zoneInfo(ak, bkt);
66+
} catch (Exception e) {
67+
e.printStackTrace();
68+
}
69+
return null;
70+
}
71+
72+
public String upHost(String token) {
73+
ZoneInfo info = queryByToken(token);
74+
if (info == null) {
75+
return "";
76+
}
77+
return info.upHost;
78+
}
79+
80+
public String upHostBackup(String token) {
81+
ZoneInfo info = queryByToken(token);
82+
if (info == null) {
83+
return "";
84+
}
85+
return info.upBackup;
86+
}
87+
88+
public String upIpBackup(String token) {
89+
ZoneInfo info = queryByToken(token);
90+
if (info == null) {
91+
return "";
92+
}
93+
return info.upIp;
94+
}
95+
96+
public String upHostHttps(String token) {
97+
ZoneInfo info = queryByToken(token);
98+
if (info == null) {
99+
return "";
100+
}
101+
return info.upHttps;
102+
}
103+
104+
static class ZoneInfo {
105+
final String ioHost;
106+
final String upHost;
107+
final String upIp;
108+
final String upBackup;
109+
final String upHttps;
110+
111+
private ZoneInfo(String ioHost, String upHost, String upIp, String upBackup, String upHttps) {
112+
this.ioHost = ioHost;
113+
this.upHost = upHost;
114+
this.upIp = upIp;
115+
this.upBackup = upBackup;
116+
this.upHttps = upHttps;
117+
}
118+
119+
static ZoneInfo buildFromUcRet(UCRet ret) {
120+
String ioHost = ret.http.get("io").get(0);
121+
List<String> up = ret.http.get("up");
122+
String upHost = up.get(0);
123+
String upBackup = up.get(1);
124+
String upIp = up.get(2).split(" ")[2].split("//")[1];
125+
String upHttps = ret.https.get("up").get(0);
126+
127+
return new ZoneInfo(ioHost, upHost, upIp, upBackup, upHttps);
128+
}
129+
}
130+
131+
private static class ZoneIndex {
132+
private final String accessKey;
133+
private final String bucket;
134+
135+
ZoneIndex(String accessKey, String bucket) {
136+
this.accessKey = accessKey;
137+
this.bucket = bucket;
138+
}
139+
140+
public int hashCode() {
141+
return accessKey.hashCode() * 37 + bucket.hashCode();
142+
}
143+
144+
public boolean equals(Object obj) {
145+
return obj == this || !(obj == null || !(obj instanceof ZoneIndex))
146+
&& ((ZoneIndex) obj).accessKey.equals(accessKey) && ((ZoneIndex) obj).bucket.equals(bucket);
147+
}
148+
}
149+
150+
private class UCRet {
151+
Map<String, List<String>> http;
152+
Map<String, List<String>> https;
153+
}
154+
}

src/main/java/com/qiniu/common/Config.java

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.qiniu.common;
2+
3+
import java.nio.charset.Charset;
4+
5+
/**
6+
* Created by bailong on 16/9/14.
7+
*/
8+
public interface Constants {
9+
String VERSION = "8.0.0";
10+
int BLOCK_SIZE = 4 * 1024 * 1024;
11+
Charset UTF_8 = Charset.forName("UTF-8");
12+
/**
13+
* 连接超时时间 单位秒(默认10s)
14+
*/
15+
public int CONNECT_TIMEOUT = 10;
16+
/**
17+
* 写超时时间 单位秒(默认 0 , 不超时)
18+
*/
19+
public int WRITE_TIMEOUT = 0;
20+
/**
21+
* 回复超时时间 单位秒(默认30s)
22+
*/
23+
public int RESPONSE_TIMEOUT = 30;
24+
}
25+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.qiniu.common;
2+
3+
/**
4+
* Created by bailong on 16/9/15.
5+
*/
6+
public final class FixedZone extends Zone {
7+
/**
8+
* 默认上传服务器
9+
*/
10+
private final String upHost;
11+
/**
12+
* 备用上传服务器,当默认服务器网络链接失败时使用
13+
*/
14+
private final String upHostBackup;
15+
16+
private final String upHostHttps;
17+
18+
private final String upHttpIp;
19+
20+
private final String rsHost;
21+
22+
private final String rsfHost;
23+
24+
private final String ioHost;
25+
26+
private final String apiHost;
27+
28+
29+
public FixedZone(String upHost, String upHostBackup, String upHttpIp,
30+
String rsHost, String rsfHost, String ioHost, String upHostHttps, String apiHost) {
31+
this.upHost = upHost;
32+
this.upHostBackup = upHostBackup;
33+
this.upHttpIp = upHttpIp;
34+
this.rsHost = rsHost;
35+
this.rsfHost = rsfHost;
36+
this.ioHost = ioHost;
37+
this.upHostHttps = upHostHttps;
38+
this.apiHost = apiHost;
39+
}
40+
41+
public String upHost(String token) {
42+
return upHost;
43+
}
44+
45+
public String upHostBackup(String token) {
46+
return upHostBackup;
47+
}
48+
49+
public String upIpBackup(String token) {
50+
return upHttpIp;
51+
}
52+
53+
public String upHostHttps(String token) {
54+
return upHostHttps;
55+
}
56+
57+
public String rsHost(String ak, String bucket) {
58+
return rsHost;
59+
}
60+
61+
public String rsfHost(String ak, String bucket) {
62+
return rsfHost;
63+
}
64+
65+
public String ioHost(String ak, String bucket) {
66+
return ioHost;
67+
}
68+
69+
public String apiHost(String ak, String bucket) {
70+
return apiHost;
71+
}
72+
}

0 commit comments

Comments
 (0)