Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions src/main/java/com/qiniu/cdn/CdnManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import com.qiniu.util.StringMap;
import com.qiniu.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;

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

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

/**
* 刷新目录列表,每次最多不可以超过10个目录
* 刷新目录需要额外开通权限,可以联系七牛技术支持处理
*
* @link http://developer.qiniu.com/article/fusion/api/refresh.html
*/
public Response refreshDirs(String[] dirs) throws QiniuException {
return refreshUrlsAndDirs(null, dirs);
}
Expand All @@ -50,4 +67,111 @@ public Response refreshUrlsAndDirs(String[] urls, String[] dirs) throws QiniuExc
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

/**
* 预取文件链接,每次最多不可以超过100条
*
* @link http://developer.qiniu.com/article/fusion/api/prefetch.html
*/
public Response prefetchUrls(String[] urls) throws QiniuException {
HashMap<String, String[]> req = new HashMap<>();
req.put("urls", urls);
byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
String url = server + "/v2/tune/prefetch";
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

/**
* 获取域名访问带宽数据
*
* @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
*/
public Response getBandwidthData(String[] domains, String startDate, String endDate,
String granularity) throws QiniuException {
HashMap<String, String> req = new HashMap<>();
req.put("domains", StringUtils.join(domains, ";"));
req.put("startDate", startDate);
req.put("endDate", endDate);
req.put("granularity", granularity);

byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
String url = server + "/v2/tune/bandwidth";
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

/**
* 获取域名访问流量数据
*
* @link http://developer.qiniu.com/article/fusion/api/traffic-bandwidth.html
*/
public Response getFluxData(String[] domains, String startDate, String endDate,
String granularity) throws QiniuException {
HashMap<String, String> req = new HashMap<>();
req.put("domains", StringUtils.join(domains, ";"));
req.put("startDate", startDate);
req.put("endDate", endDate);
req.put("granularity", granularity);

byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
String url = server + "/v2/tune/flux";
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

/**
* 获取CDN域名访问日志的下载链接
*
* @link http://developer.qiniu.com/article/fusion/api/log.html
*/
public Response getCdnLogList(String[] domains, String logDate) throws QiniuException {
HashMap<String, String> req = new HashMap<>();
req.put("domains", StringUtils.join(domains, ";"));
req.put("day", logDate);

byte[] body = Json.encode(req).getBytes(Constants.UTF_8);
String url = server + "/v2/tune/log/list";
StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime);
return client.post(url, body, headers, Client.JsonMime);
}

/**
* 构建标准的基于时间戳的防盗链
*
* @param host 自定义域名,例如 http://img.abc.com
* @param fileName 待访问的原始文件名,必须是utf8编码,不需要进行urlencode
* @param queryString 业务自身的查询参数,必须是utf8编码,不需要进行urlencode
* @param encryptKey 时间戳防盗链的签名密钥,从七牛后台获取
* @param deadline 链接的有效期时间戳,是以秒为单位的Unix时间戳
* @return signedUrl 最终的带时间戳防盗链的url
*/
public static String createStandardAntileechUrlBasedOnTimestamp(
String host, String fileName, String queryString, String encryptKey, long deadline)
throws UnsupportedEncodingException, MalformedURLException, NoSuchAlgorithmException {
String urlToSign;
if (queryString.trim().length() != 0) {
urlToSign = String.format("%s/%s?%s", host, URLEncoder.encode(fileName, "utf-8"),
URLEncoder.encode(queryString, "utf-8"));
} else {
urlToSign = String.format("%s/%s", host, URLEncoder.encode(fileName, "utf-8"));
}

URL urlObj = new URL(urlToSign);
String path = urlObj.getPath();

String expireHex = Long.toHexString(deadline);

String toSignStr = String.format("%s%s%s", encryptKey, path, expireHex);
String signedStr = StringUtils.md5Lower(toSignStr);

String signedUrl;
if (urlObj.getQuery() != null) {
signedUrl = String.format("%s&sign=%s&t=%s", urlToSign, signedStr, expireHex);
} else {
signedUrl = String.format("%s?sign=%s&t=%s", urlToSign, signedStr, expireHex);
}

return signedUrl;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/qiniu/util/StringUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.qiniu.util;

import com.qiniu.common.Constants;
import qiniu.happydns.util.Hex;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;

/**
Expand Down Expand Up @@ -133,5 +137,12 @@ public static byte[] utf8Bytes(String data) {
public static String utf8String(byte[] data) {
return new String(data, Constants.UTF_8);
}

public static String md5Lower(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(src.getBytes("utf-8"));
byte[] md5Bytes = digest.digest();
return Hex.encodeHexString(md5Bytes);
}
}

98 changes: 98 additions & 0 deletions src/test/java/com/qiniu/CdnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,104 @@ public void testRefresh() {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testPrefetch() {
CdnManager c = new CdnManager(TestConfig.testAuth);
Response r = null;
try {
r = c.prefetchUrls(new String[]{"http://javasdk.qiniudn.com/gopher.jpg"});
Assert.assertEquals(200, r.statusCode);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testGetBandwidth() {
CdnManager c = new CdnManager(TestConfig.testAuth);
Response r = null;
String[] domains = {TestConfig.domain};
String startDate = "2017-01-01";
String endDate = "2017-01-06";
String granularity = "day";
try {
r = c.getBandwidthData(domains, startDate, endDate, granularity);
Assert.assertEquals(200, r.statusCode);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testGetFlux() {
CdnManager c = new CdnManager(TestConfig.testAuth);
Response r = null;
String[] domains = {TestConfig.domain};
String startDate = "2017-01-01";
String endDate = "2017-01-06";
String granularity = "day";
try {
r = c.getFluxData(domains, startDate, endDate, granularity);
Assert.assertEquals(200, r.statusCode);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testGetCdnLogList() {
CdnManager c = new CdnManager(TestConfig.testAuth);
Response r = null;
String[] domains = {TestConfig.domain};
String logDate = "2017-01-01";

try {
r = c.getCdnLogList(domains, logDate);
Assert.assertEquals(200, r.statusCode);
} catch (QiniuException e) {
e.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateAntileechUrlBasedOnTimestampSimple() {
String host = "http://img.abc.com";
String fileName = "2017/01/07/测试.png";
String queryString = "";
long deadline = System.currentTimeMillis() / 1000 + 3600;
String encryptKey = "";
String signedUrl;
try {
signedUrl = CdnManager.createStandardAntileechUrlBasedOnTimestamp(host, fileName,
queryString, encryptKey, deadline);
System.out.println(signedUrl);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}

@Test
public void testCreateAntileechUrlBasedOnTimestampWithQueryString() {
String host = "http://video.abc.com";
String fileName = "测试.mp4";
String queryString = "name=七牛&year=2017";
long deadline = System.currentTimeMillis() / 1000 + 3600;
String encryptKey = "";
String signedUrl;
try {
signedUrl = CdnManager.createStandardAntileechUrlBasedOnTimestamp(host, fileName,
queryString, encryptKey, deadline);
System.out.println(signedUrl);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail();
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/qiniu/streaming/StreamingTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qiniu.streaming;

import com.qiniu.TestConfig;
import com.qiniu.common.QiniuException;
import com.qiniu.streaming.model.ActivityRecords;
import com.qiniu.streaming.model.StreamAttribute;
Expand All @@ -14,7 +15,7 @@
* Created by bailong on 16/9/22.
*/
public class StreamingTest {
private Auth auth = Auth.create(System.getenv("ak"), System.getenv("sk"));
private Auth auth = TestConfig.testAuth;
private String hub = "pilisdktest";
private String streamKeyPrefix = "pilijava" + System.currentTimeMillis();
private StreamingManager manager = new StreamingManager(auth, hub);
Expand Down