77import com .qiniu .util .Auth ;
88import com .qiniu .util .Json ;
99import 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 ;
1119import 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}
0 commit comments