Skip to content

Commit d7dced9

Browse files
committed
获得正确的 auth [ci skip]
2 parents 7ed9d8a + b5e1f35 commit d7dced9

File tree

4 files changed

+235
-1
lines changed

4 files changed

+235
-1
lines changed

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

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
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;
1117
import java.util.HashMap;
1218

1319
/**
@@ -29,10 +35,21 @@ private CdnManager(Auth auth, String server) {
2935
Constants.CONNECT_TIMEOUT, Constants.RESPONSE_TIMEOUT, Constants.WRITE_TIMEOUT);
3036
}
3137

38+
/**
39+
* 刷新链接列表,每次最多不可以超过100条链接
40+
*
41+
* @link http://developer.qiniu.com/article/fusion/api/refresh.html
42+
*/
3243
public Response refreshUrls(String[] urls) throws QiniuException {
3344
return refreshUrlsAndDirs(urls, null);
3445
}
3546

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

src/main/java/com/qiniu/util/StringUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.qiniu.util;
22

33
import com.qiniu.common.Constants;
4+
import qiniu.happydns.util.Hex;
45

6+
import java.io.UnsupportedEncodingException;
7+
import java.security.MessageDigest;
8+
import java.security.NoSuchAlgorithmException;
59
import java.util.Collection;
610

711
/**
@@ -133,5 +137,12 @@ public static byte[] utf8Bytes(String data) {
133137
public static String utf8String(byte[] data) {
134138
return new String(data, Constants.UTF_8);
135139
}
140+
141+
public static String md5Lower(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
142+
MessageDigest digest = MessageDigest.getInstance("MD5");
143+
digest.update(src.getBytes("utf-8"));
144+
byte[] md5Bytes = digest.digest();
145+
return Hex.encodeHexString(md5Bytes);
146+
}
136147
}
137148

src/test/java/com/qiniu/CdnTest.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,104 @@ public void testRefresh() {
2121
e.printStackTrace();
2222
Assert.fail();
2323
}
24+
}
25+
26+
@Test
27+
public void testPrefetch() {
28+
CdnManager c = new CdnManager(TestConfig.testAuth);
29+
Response r = null;
30+
try {
31+
r = c.prefetchUrls(new String[]{"http://javasdk.qiniudn.com/gopher.jpg"});
32+
Assert.assertEquals(200, r.statusCode);
33+
} catch (QiniuException e) {
34+
e.printStackTrace();
35+
Assert.fail();
36+
}
37+
}
38+
39+
@Test
40+
public void testGetBandwidth() {
41+
CdnManager c = new CdnManager(TestConfig.testAuth);
42+
Response r = null;
43+
String[] domains = {TestConfig.domain};
44+
String startDate = "2017-01-01";
45+
String endDate = "2017-01-06";
46+
String granularity = "day";
47+
try {
48+
r = c.getBandwidthData(domains, startDate, endDate, granularity);
49+
Assert.assertEquals(200, r.statusCode);
50+
} catch (QiniuException e) {
51+
e.printStackTrace();
52+
Assert.fail();
53+
}
54+
}
55+
56+
@Test
57+
public void testGetFlux() {
58+
CdnManager c = new CdnManager(TestConfig.testAuth);
59+
Response r = null;
60+
String[] domains = {TestConfig.domain};
61+
String startDate = "2017-01-01";
62+
String endDate = "2017-01-06";
63+
String granularity = "day";
64+
try {
65+
r = c.getFluxData(domains, startDate, endDate, granularity);
66+
Assert.assertEquals(200, r.statusCode);
67+
} catch (QiniuException e) {
68+
e.printStackTrace();
69+
Assert.fail();
70+
}
71+
}
72+
73+
@Test
74+
public void testGetCdnLogList() {
75+
CdnManager c = new CdnManager(TestConfig.testAuth);
76+
Response r = null;
77+
String[] domains = {TestConfig.domain};
78+
String logDate = "2017-01-01";
79+
80+
try {
81+
r = c.getCdnLogList(domains, logDate);
82+
Assert.assertEquals(200, r.statusCode);
83+
} catch (QiniuException e) {
84+
e.printStackTrace();
85+
Assert.fail();
86+
}
87+
}
88+
89+
@Test
90+
public void testCreateAntileechUrlBasedOnTimestampSimple() {
91+
String host = "http://img.abc.com";
92+
String fileName = "2017/01/07/测试.png";
93+
String queryString = "";
94+
long deadline = System.currentTimeMillis() / 1000 + 3600;
95+
String encryptKey = "";
96+
String signedUrl;
97+
try {
98+
signedUrl = CdnManager.createStandardAntileechUrlBasedOnTimestamp(host, fileName,
99+
queryString, encryptKey, deadline);
100+
System.out.println(signedUrl);
101+
} catch (Exception ex) {
102+
ex.printStackTrace();
103+
Assert.fail();
104+
}
105+
}
24106

107+
@Test
108+
public void testCreateAntileechUrlBasedOnTimestampWithQueryString() {
109+
String host = "http://video.abc.com";
110+
String fileName = "测试.mp4";
111+
String queryString = "name=七牛&year=2017";
112+
long deadline = System.currentTimeMillis() / 1000 + 3600;
113+
String encryptKey = "";
114+
String signedUrl;
115+
try {
116+
signedUrl = CdnManager.createStandardAntileechUrlBasedOnTimestamp(host, fileName,
117+
queryString, encryptKey, deadline);
118+
System.out.println(signedUrl);
119+
} catch (Exception ex) {
120+
ex.printStackTrace();
121+
Assert.fail();
122+
}
25123
}
26124
}

src/test/java/com/qiniu/streaming/StreamingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import static org.junit.Assert.*;
1313

1414
/**
15-
* Created by bailong on 16/9/22.
15+
* Created by bailong on 16/9/22
1616
*/
1717
public class StreamingTest {
1818
private Auth auth = null;
@@ -24,6 +24,7 @@ public class StreamingTest {
2424
auth = TestConfig.testAuth;
2525
}
2626
}
27+
2728
private String hub = "pilisdktest";
2829
private String streamKeyPrefix = "pilijava" + System.currentTimeMillis();
2930
private StreamingManager manager = new StreamingManager(auth, hub);

0 commit comments

Comments
 (0)