Skip to content

Commit 5c22cc4

Browse files
committed
pili integrate done
1 parent 414c087 commit 5c22cc4

File tree

16 files changed

+672
-11
lines changed

16 files changed

+672
-11
lines changed
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+
}

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

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

3+
import com.qiniu.http.Error;
34
import com.qiniu.http.Response;
45

56
import java.io.IOException;
67

78

89
public final class QiniuException extends IOException {
910
public final Response response;
11+
private String error;
1012

1113

1214
public QiniuException(Response response) {
@@ -25,4 +27,21 @@ public String url() {
2527
public int code() {
2628
return response == null ? -1 : response.statusCode;
2729
}
30+
31+
public String error() {
32+
if (error != null) {
33+
return error;
34+
}
35+
if (response == null || response.statusCode / 100 == 2 || !response.isJson()) {
36+
return null;
37+
}
38+
Error e = null;
39+
try {
40+
e = response.jsonToObject(Error.class);
41+
} catch (QiniuException e1) {
42+
e1.printStackTrace();
43+
}
44+
error = e == null ? "" : e.error;
45+
return error;
46+
}
2847
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.qiniu.http;
2+
3+
/**
4+
* Created by bailong on 16/9/23.
5+
*/
6+
public final class Error {
7+
public String error;
8+
}

src/main/java/com/qiniu/storage/Configuration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* Created by bailong on 16/9/21.
1010
*/
11-
public final class Configuration {
11+
public final class Configuration implements Cloneable {
1212

1313
/**
1414
* 使用的Zone
@@ -56,7 +56,12 @@ public Configuration(Zone zone) {
5656
}
5757

5858
public Configuration clone() {
59-
return this;
59+
try {
60+
return (Configuration) super.clone();
61+
} catch (CloneNotSupportedException e) {
62+
e.printStackTrace();
63+
}
64+
return null;
6065
}
6166

6267
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
package com.qiniu.streaming;
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.streaming.model.ActivityRecords;
8+
import com.qiniu.streaming.model.StreamAttribute;
9+
import com.qiniu.streaming.model.StreamListing;
10+
import com.qiniu.streaming.model.StreamStatus;
11+
import com.qiniu.util.Auth;
12+
import com.qiniu.util.StringMap;
13+
import com.qiniu.util.StringUtils;
14+
import com.qiniu.util.UrlSafeBase64;
15+
16+
import java.util.Iterator;
17+
18+
/**
19+
* Created by bailong on 16/9/22.
20+
*/
21+
public final class StreamingManager {
22+
private final String apiServer;
23+
private final String hub;
24+
private final Client client;
25+
private final Auth auth;
26+
27+
public StreamingManager(Auth auth, String hub) {
28+
this(auth, hub, "http://pili.qiniuapi.com");
29+
}
30+
31+
StreamingManager(Auth auth, String hub, String server) {
32+
apiServer = server;
33+
this.hub = hub;
34+
this.auth = auth;
35+
client = new Client(null, false, null,
36+
Constants.CONNECT_TIMEOUT, Constants.RESPONSE_TIMEOUT, Constants.WRITE_TIMEOUT);
37+
}
38+
39+
public void create(String key) throws QiniuException {
40+
String path = "";
41+
String body = String.format("{\"key\":\"%s\"}", key);
42+
post(path, body, null);
43+
}
44+
45+
public StreamAttribute attribute(String key) throws QiniuException {
46+
String path = encodeKey(key);
47+
return get(path, StreamAttribute.class);
48+
}
49+
50+
/**
51+
* 根据前缀获取流列表的迭代器
52+
*
53+
* @param live 是否在推流
54+
* @param prefix 文件名前缀
55+
* @return Stream迭代器
56+
*/
57+
public ListIterator createStreamListIterator(boolean live, String prefix) {
58+
return new ListIterator(live, prefix);
59+
}
60+
61+
public StreamListing listStreams(boolean live, String prefix, String marker) throws QiniuException {
62+
StringMap map = new StringMap();
63+
if (live) {
64+
map.put("liveonly", live);
65+
}
66+
if (!StringUtils.isNullOrEmpty(prefix)) {
67+
map.put("prefix", prefix);
68+
}
69+
if (!StringUtils.isNullOrEmpty(marker)) {
70+
map.put("marker", marker);
71+
}
72+
String path = "";
73+
74+
if (map.size() != 0) {
75+
path += "?" + map.formString();
76+
}
77+
return get(path, StreamListing.class);
78+
}
79+
80+
public void disableTill(String key, long epoch) throws QiniuException {
81+
String path = encodeKey(key) + "/disabled";
82+
String body = String.format("{\"disabledTill\":%d}", epoch);
83+
post(path, body, null);
84+
}
85+
86+
public void enable(String key) throws QiniuException {
87+
disableTill(key, 0);
88+
}
89+
90+
public StreamStatus status(String key) throws QiniuException {
91+
String path = encodeKey(key) + "/live";
92+
return get(path, StreamStatus.class);
93+
}
94+
95+
public String saveAs(String key, String fileName) throws QiniuException {
96+
return saveAs(key, fileName, 0, 0);
97+
}
98+
99+
public String saveAs(String key, String fileName, long start, long end) throws QiniuException {
100+
String path = encodeKey(key) + "/saveas";
101+
String body;
102+
if (fileName == null) {
103+
body = String.format("{\"start\": %d,\"end\": %d}", start, end);
104+
} else {
105+
body = String.format("{\"fname\": %s,\"start\": %d,\"end\": %d}", fileName, start, end);
106+
}
107+
SaveRet r = post(path, body, SaveRet.class);
108+
return r.fname;
109+
}
110+
111+
public ActivityRecords history(String key, long start, long end) throws QiniuException {
112+
if (start <= 0 || end < 0 || (start >= end && end != 0)) {
113+
throw new QiniuException(new IllegalArgumentException("bad argument" + start + "," + end));
114+
}
115+
String path = encodeKey(key) + "/historyactivity?" + start;
116+
if (end != 0) {
117+
path += "&end=" + end;
118+
}
119+
return get(path, ActivityRecords.class);
120+
}
121+
122+
private String encodeKey(String key) {
123+
return "/" + UrlSafeBase64.encodeToString(key);
124+
}
125+
126+
private <T> T get(String path, Class<T> classOfT) throws QiniuException {
127+
String url = apiServer + "/v2/hubs/" + hub + "/streams" + path;
128+
StringMap headers = auth.authorizationV2(url);
129+
Response r = client.get(url, headers);
130+
if (classOfT != null) {
131+
return r.jsonToObject(classOfT);
132+
}
133+
return null;
134+
}
135+
136+
private <T> T post(String path, String body, Class<T> classOfT) throws QiniuException {
137+
String url = apiServer + "/v2/hubs/" + hub + "/streams" + path;
138+
byte[] b = body.getBytes();
139+
StringMap headers = auth.authorizationV2(url, "POST", b, Client.JsonMime);
140+
Response r = client.post(url, b, headers, Client.JsonMime);
141+
if (classOfT != null) {
142+
return r.jsonToObject(classOfT);
143+
}
144+
return null;
145+
}
146+
147+
private static class SaveRet {
148+
public String fname;
149+
}
150+
151+
/**
152+
* 获取文件列表迭代器
153+
*/
154+
public class ListIterator implements Iterator<String[]> {
155+
private final boolean live;
156+
private String marker = null;
157+
private String prefix;
158+
private QiniuException exception = null;
159+
160+
public ListIterator(boolean live, String prefix) {
161+
this.live = live;
162+
this.prefix = prefix;
163+
}
164+
165+
public QiniuException exception() {
166+
return exception;
167+
}
168+
169+
@Override
170+
public boolean hasNext() {
171+
return exception == null && !"".equals(marker);
172+
}
173+
174+
@Override
175+
public String[] next() {
176+
try {
177+
StreamListing l = listStreams(live, prefix, marker);
178+
this.marker = l.marker == null ? "" : l.marker;
179+
return l.keys();
180+
} catch (QiniuException e) {
181+
this.exception = e;
182+
return null;
183+
}
184+
}
185+
186+
@Override
187+
public void remove() {
188+
throw new UnsupportedOperationException("remove");
189+
}
190+
}
191+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.qiniu.streaming;
2+
3+
import com.qiniu.util.Auth;
4+
5+
import java.util.Date;
6+
7+
/**
8+
* Created by bailong on 16/9/22.
9+
*/
10+
public final class UrlFactory {
11+
12+
private final String hub;
13+
private final Auth auth;
14+
private final String rtmpPubDomain;
15+
private final String rtmpPlayDomain;
16+
private final String hlsDomain;
17+
private final String hdlDomain;
18+
private final String snapDomain;
19+
20+
public UrlFactory(String hub, Auth auth, String rtmpPubDomain, String rtmpPlayDomain) {
21+
this(hub, auth, rtmpPubDomain, rtmpPlayDomain, null, null, null);
22+
}
23+
24+
public UrlFactory(String hub, Auth auth, String rtmpPubDomain,
25+
String rtmpPlayDomain, String hlsDomain, String hdlDomain, String snapDomain) {
26+
this.hub = hub;
27+
this.auth = auth;
28+
this.rtmpPubDomain = rtmpPubDomain;
29+
this.rtmpPlayDomain = rtmpPlayDomain;
30+
this.hlsDomain = hlsDomain;
31+
this.hdlDomain = hdlDomain;
32+
this.snapDomain = snapDomain;
33+
}
34+
35+
public String rtmpPublishUrl(String streamKey) {
36+
return String.format("rtmp://%s/%s/%s", rtmpPubDomain, hub, streamKey);
37+
}
38+
39+
public String rtmpPublishUrl(String streamKey, int expireAfterSeconds) {
40+
long expire = new Date().getTime() + expireAfterSeconds;
41+
String path = String.format("/%s/%s?e=%d", hub, streamKey, expire);
42+
String token;
43+
try {
44+
token = auth.sign(path);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
return null;
48+
}
49+
return String.format("rtmp://%s%s&token=%s", rtmpPubDomain, path, token);
50+
}
51+
52+
/*
53+
RTMPPlayURL generates RTMP play URL
54+
*/
55+
public String rtmpPlayUrl(String streamKey) {
56+
return String.format("rtmp://%s/%s/%s", rtmpPlayDomain, hub, streamKey);
57+
}
58+
59+
/*
60+
HLSPlayURL generates HLS play URL
61+
*/
62+
public String hlsPlayUrl(String streamKey) {
63+
return String.format("http://%s/%s/%s.m3u8", hlsDomain, hub, streamKey);
64+
}
65+
66+
/*
67+
HDLPlayURL generates HDL play URL
68+
*/
69+
public String hdlPlayUrl(String streamKey) {
70+
return String.format("http://%s/%s/%s.flv", hdlDomain, hub, streamKey);
71+
}
72+
73+
/*
74+
SnapshotPlayURL generates snapshot URL
75+
*/
76+
public String snapshotUrl(String streamKey) {
77+
return String.format("http://%s/%s/%s.jpg", snapDomain, hub, streamKey);
78+
}
79+
}

0 commit comments

Comments
 (0)