Skip to content

Commit 4494a37

Browse files
committed
Merge branch 'master' of github.com:qiniu/java-sdk into hash_record_file_name
merge from master
2 parents f62b787 + 8860efb commit 4494a37

File tree

18 files changed

+833
-151
lines changed

18 files changed

+833
-151
lines changed

src/main/java/com/qiniu/cdn/CdnManager.java

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
import com.qiniu.util.Auth;
88
import com.qiniu.util.Json;
99
import com.qiniu.util.StringMap;
10+
import com.qiniu.util.StringUtils;
1011

12+
import java.io.UnsupportedEncodingException;
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
import java.net.URLEncoder;
16+
import java.security.NoSuchAlgorithmException;
17+
import java.util.Map;
18+
import java.util.ArrayList;
1119
import java.util.HashMap;
20+
import java.util.List;
1221

1322
/**
1423
* Created by bailong on 16/9/21.
@@ -29,10 +38,21 @@ private CdnManager(Auth auth, String server) {
2938
Constants.CONNECT_TIMEOUT, Constants.RESPONSE_TIMEOUT, Constants.WRITE_TIMEOUT);
3039
}
3140

41+
/**
42+
* 刷新链接列表,每次最多不可以超过100条链接
43+
*
44+
* @link http://developer.qiniu.com/article/fusion/api/refresh.html
45+
*/
3246
public Response refreshUrls(String[] urls) throws QiniuException {
3347
return refreshUrlsAndDirs(urls, null);
3448
}
3549

50+
/**
51+
* 刷新目录列表,每次最多不可以超过10个目录
52+
* 刷新目录需要额外开通权限,可以联系七牛技术支持处理
53+
*
54+
* @link http://developer.qiniu.com/article/fusion/api/refresh.html
55+
*/
3656
public Response refreshDirs(String[] dirs) throws QiniuException {
3757
return refreshUrlsAndDirs(null, dirs);
3858
}
@@ -50,4 +70,119 @@ public Response refreshUrlsAndDirs(String[] urls, String[] dirs) throws QiniuExc
5070
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
5171
return client.post(url, body, headers, Client.JsonMime);
5272
}
73+
74+
/**
75+
* 预取文件链接,每次最多不可以超过100条
76+
*
77+
* @link http://developer.qiniu.com/article/fusion/api/prefetch.html
78+
*/
79+
public Response prefetchUrls(String[] urls) throws QiniuException {
80+
HashMap<String, String[]> req = new HashMap<>();
81+
req.put("urls", urls);
82+
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
83+
String url = server + "/v2/tune/prefetch";
84+
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
85+
return client.post(url, body, headers, Client.JsonMime);
86+
}
87+
88+
/**
89+
* 获取域名访问带宽数据
90+
*
91+
* @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
92+
*/
93+
public Response getBandwidthData(String[] domains, String startDate, String endDate,
94+
String granularity) throws QiniuException {
95+
HashMap<String, String> req = new HashMap<>();
96+
req.put("domains", StringUtils.join(domains, ";"));
97+
req.put("startDate", startDate);
98+
req.put("endDate", endDate);
99+
req.put("granularity", granularity);
100+
101+
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
102+
String url = server + "/v2/tune/bandwidth";
103+
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
104+
return client.post(url, body, headers, Client.JsonMime);
105+
}
106+
107+
/**
108+
* 获取域名访问流量数据
109+
*
110+
* @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
111+
*/
112+
public Response getFluxData(String[] domains, String startDate, String endDate,
113+
String granularity) throws QiniuException {
114+
HashMap<String, String> req = new HashMap<>();
115+
req.put("domains", StringUtils.join(domains, ";"));
116+
req.put("startDate", startDate);
117+
req.put("endDate", endDate);
118+
req.put("granularity", granularity);
119+
120+
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
121+
String url = server + "/v2/tune/flux";
122+
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
123+
return client.post(url, body, headers, Client.JsonMime);
124+
}
125+
126+
/**
127+
* 获取CDN域名访问日志的下载链接
128+
*
129+
* @link http://developer.qiniu.com/article/fusion/api/log.html
130+
*/
131+
public Response getCdnLogList(String[] domains, String logDate) throws QiniuException {
132+
HashMap<String, String> req = new HashMap<>();
133+
req.put("domains", StringUtils.join(domains, ";"));
134+
req.put("day", logDate);
135+
136+
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
137+
String url = server + "/v2/tune/log/list";
138+
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
139+
return client.post(url, body, headers, Client.JsonMime);
140+
}
141+
142+
/**
143+
* 构建标准的基于时间戳的防盗链
144+
*
145+
* @param host 自定义域名,例如 http://img.abc.com
146+
* @param fileName 待访问的原始文件名,必须是utf8编码,不需要进行urlencode
147+
* @param queryStringMap 业务自身的查询参数,必须是utf8编码,不需要进行urlencode
148+
* @param encryptKey 时间戳防盗链的签名密钥,从七牛后台获取
149+
* @param deadline 链接的有效期时间戳,是以秒为单位的Unix时间戳
150+
* @return signedUrl 最终的带时间戳防盗链的url
151+
*/
152+
public static String createTimestampAntiLeechUrl(
153+
String host, String fileName, final StringMap queryStringMap, String encryptKey, long deadline)
154+
throws UnsupportedEncodingException, MalformedURLException, NoSuchAlgorithmException {
155+
String urlToSign;
156+
if (queryStringMap != null && queryStringMap.size() > 0) {
157+
List<String> queryStrings = new ArrayList<String>();
158+
for (Map.Entry<String, Object> entry : queryStringMap.map().entrySet()) {
159+
StringBuilder queryStringBuilder = new StringBuilder();
160+
queryStringBuilder.append(URLEncoder.encode(entry.getKey(), "utf-8"));
161+
queryStringBuilder.append("=");
162+
queryStringBuilder.append(URLEncoder.encode(entry.getValue().toString(), "utf-8"));
163+
queryStrings.add(queryStringBuilder.toString());
164+
}
165+
urlToSign = String.format("%s/%s?%s", host, URLEncoder.encode(fileName, "utf-8"),
166+
StringUtils.join(queryStrings, "&"));
167+
} else {
168+
urlToSign = String.format("%s/%s", host, URLEncoder.encode(fileName, "utf-8"));
169+
}
170+
171+
URL urlObj = new URL(urlToSign);
172+
String path = urlObj.getPath();
173+
174+
String expireHex = Long.toHexString(deadline);
175+
176+
String toSignStr = String.format("%s%s%s", encryptKey, path, expireHex);
177+
String signedStr = StringUtils.md5Lower(toSignStr);
178+
179+
String signedUrl;
180+
if (urlObj.getQuery() != null) {
181+
signedUrl = String.format("%s&sign=%s&t=%s", urlToSign, signedStr, expireHex);
182+
} else {
183+
signedUrl = String.format("%s?sign=%s&t=%s", urlToSign, signedStr, expireHex);
184+
}
185+
186+
return signedUrl;
187+
}
53188
}

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

Lines changed: 103 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.qiniu.http.Client;
44
import com.qiniu.http.Response;
5-
import com.qiniu.util.Json;
6-
import com.qiniu.util.UrlSafeBase64;
75

86
import java.util.List;
97
import java.util.Map;
@@ -54,77 +52,143 @@ ZoneInfo zoneInfo(String ak, String bucket) throws QiniuException {
5452
}
5553

5654
// only for test public
57-
ZoneInfo queryByToken(String token) {
55+
ZoneInfo queryByToken(ZoneReqInfo ab) {
5856
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);
57+
return zoneInfo(ab.ak, ab.bucket);
6658
} catch (Exception e) {
6759
e.printStackTrace();
6860
}
6961
return null;
7062
}
7163

72-
public String upHost(String token) {
73-
ZoneInfo info = queryByToken(token);
64+
@Override
65+
public String getUpHttp(ZoneReqInfo ab) {
66+
ZoneInfo info = queryByToken(ab);
7467
if (info == null) {
7568
return "";
7669
}
77-
return info.upHost;
70+
return info.upHttp;
7871
}
7972

80-
public String upHostBackup(String token) {
81-
ZoneInfo info = queryByToken(token);
73+
@Override
74+
public String getUpBackupHttp(ZoneReqInfo ab) {
75+
ZoneInfo info = queryByToken(ab);
8276
if (info == null) {
8377
return "";
8478
}
85-
return info.upBackup;
79+
return info.upBackupHttp;
8680
}
8781

88-
public String upIpBackup(String token) {
89-
ZoneInfo info = queryByToken(token);
82+
@Override
83+
public String getUpIpHttp(ZoneReqInfo ab) {
84+
ZoneInfo info = queryByToken(ab);
9085
if (info == null) {
9186
return "";
9287
}
93-
return info.upIp;
88+
return info.upIpHttp;
9489
}
9590

96-
public String upHostHttps(String token) {
97-
ZoneInfo info = queryByToken(token);
91+
@Override
92+
public String getIovipHttp(ZoneReqInfo ab) {
93+
ZoneInfo info = queryByToken(ab);
94+
if (info == null) {
95+
return "";
96+
}
97+
return info.iovipHttp;
98+
}
99+
100+
101+
@Override
102+
public String getUpHttps(ZoneReqInfo ab) {
103+
ZoneInfo info = queryByToken(ab);
98104
if (info == null) {
99105
return "";
100106
}
101107
return info.upHttps;
102108
}
103109

110+
@Override
111+
public String getUpBackupHttps(ZoneReqInfo ab) {
112+
ZoneInfo info = queryByToken(ab);
113+
if (info == null) {
114+
return "";
115+
}
116+
return info.upBackupHttps;
117+
}
118+
119+
@Override
120+
public String getUpIpHttps(ZoneReqInfo ab) {
121+
ZoneInfo info = queryByToken(ab);
122+
if (info == null) {
123+
return "";
124+
}
125+
return info.upIpHttps;
126+
}
127+
128+
@Override
129+
public String getIovipHttps(ZoneReqInfo ab) {
130+
ZoneInfo info = queryByToken(ab);
131+
if (info == null) {
132+
return "";
133+
}
134+
return info.iovipHttps;
135+
}
136+
137+
138+
104139
static class ZoneInfo {
105-
final String ioHost;
106-
final String upHost;
107-
final String upIp;
108-
final String upBackup;
109-
final String upHttps;
140+
final String upHttp;
141+
final String upBackupHttp;
142+
final String upIpHttp;
143+
final String iovipHttp;
110144

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;
145+
final String upHttps;
146+
final String upBackupHttps;
147+
final String upIpHttps;
148+
final String iovipHttps;
149+
150+
private ZoneInfo(String upHttp, String upBackupHttp, String upIpHttp, String iovipHttp,
151+
String upHttps, String upBackupHttps, String upIpHttps, String iovipHttps) {
152+
this.upHttp = upHttp;
153+
this.upBackupHttp = upBackupHttp;
154+
this.upIpHttp = upIpHttp;
155+
this.iovipHttp = iovipHttp;
116156
this.upHttps = upHttps;
157+
this.upBackupHttps = upBackupHttps;
158+
this.upIpHttps = upIpHttps;
159+
this.iovipHttps = iovipHttps;
117160
}
118161

162+
/*
163+
* {"ttl":86400,
164+
* "http":
165+
* {
166+
* "io":["http://iovip.qbox.me"],
167+
* "up":["http://up.qiniu.com","http://upload.qiniu.com",
168+
* "-H up.qiniu.com http://183.136.139.16"]
169+
* },
170+
* "https":{"io":["https://iovip.qbox.me"],"up":["https://up.qbox.me"]}}
171+
* */
119172
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);
173+
List<String> upsHttp = ret.http.get("up");
174+
String upHttp = upsHttp.get(0);
175+
String upBackupHttp = upsHttp.get(1);
176+
String upIpHttp = upsHttp.get(2).split(" ")[2].split("//")[1];
177+
String ioHttp = ret.http.get("io").get(0);
178+
179+
List<String> upsHttps = ret.https.get("up");
180+
String upHttps = upsHttps.get(0);
181+
String upBackupHttps = upHttps;
182+
String upIpHttps = "";
183+
if (upsHttps.size() > 1) {
184+
upBackupHttps = upsHttps.get(1);
185+
}
186+
if (upsHttps.size() > 2) {
187+
upIpHttps = upsHttps.get(2).split(" ")[2].split("//")[1];
188+
}
189+
String ioHttps = ret.https.get("io").get(0);
190+
191+
return new ZoneInfo(upHttp, upBackupHttp, upIpHttp, ioHttp, upHttps, upBackupHttps, upIpHttps, ioHttps);
128192
}
129193
}
130194

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public QiniuException(Response response) {
1818
}
1919

2020
public QiniuException(Exception e) {
21+
this(e, null);
22+
}
23+
24+
public QiniuException(Exception e, String msg) {
2125
super(e);
2226
this.response = null;
27+
this.error = msg;
2328
}
2429

2530
public String url() {

0 commit comments

Comments
 (0)