Skip to content

Commit b6b93fd

Browse files
authored
Merge pull request #231 from qiniu/v8
use autozone
2 parents 0b37731 + d53f3f5 commit b6b93fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1365
-801
lines changed

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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.qiniu.cdn;
2+
3+
import com.qiniu.common.Constants;
4+
import com.qiniu.common.QiniuException;
5+
import com.qiniu.http.Client;
6+
import com.qiniu.http.Response;
7+
import com.qiniu.util.Auth;
8+
import com.qiniu.util.Json;
9+
import com.qiniu.util.StringMap;
10+
11+
import java.util.HashMap;
12+
13+
/**
14+
* Created by bailong on 16/9/21.
15+
*/
16+
public final class CdnManager {
17+
private final Auth auth;
18+
private final String server;
19+
private final Client client;
20+
21+
public CdnManager(Auth auth) {
22+
this(auth, "http://fusion.qiniuapi.com");
23+
}
24+
25+
private CdnManager(Auth auth, String server) {
26+
this.auth = auth;
27+
this.server = server;
28+
this.client = new Client(null, false, null,
29+
Constants.CONNECT_TIMEOUT, Constants.RESPONSE_TIMEOUT, Constants.WRITE_TIMEOUT);
30+
}
31+
32+
public Response refreshUrls(String[] urls) throws QiniuException {
33+
return refreshUrlsAndDirs(urls, null);
34+
}
35+
36+
public Response refreshDirs(String[] dirs) throws QiniuException {
37+
return refreshUrlsAndDirs(null, dirs);
38+
}
39+
40+
public Response refreshUrlsAndDirs(String[] urls, String[] dirs) throws QiniuException {
41+
HashMap<String, String[]> req = new HashMap<>();
42+
if (urls != null) {
43+
req.put("urls", urls);
44+
}
45+
if (dirs != null) {
46+
req.put("dirs", dirs);
47+
}
48+
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
49+
String url = server + "/v2/tune/refresh";
50+
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
51+
return client.post(url, body, headers, Client.JsonMime);
52+
}
53+
}
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 final class Constants {
9+
/**
10+
* 版本号
11+
*/
12+
public static final String VERSION = "8.0.0";
13+
/**
14+
* 块大小,不能改变
15+
*/
16+
public static final int BLOCK_SIZE = 4 * 1024 * 1024;
17+
/**
18+
* 所有都是UTF-8编码
19+
*/
20+
public static final Charset UTF_8 = Charset.forName("UTF-8");
21+
/**
22+
* 连接超时时间 单位秒(默认10s)
23+
*/
24+
public static final int CONNECT_TIMEOUT = 10;
25+
/**
26+
* 写超时时间 单位秒(默认 0 , 不超时)
27+
*/
28+
public static final int WRITE_TIMEOUT = 0;
29+
/**
30+
* 回复超时时间 单位秒(默认30s)
31+
*/
32+
public static final int RESPONSE_TIMEOUT = 30;
33+
34+
private Constants() {
35+
}
36+
}
37+

0 commit comments

Comments
 (0)