Skip to content

Commit 348fd7b

Browse files
authored
Merge pull request #420 from qiniu/developer
Developer
2 parents 8b04e47 + 3264fd7 commit 348fd7b

File tree

17 files changed

+1097
-0
lines changed

17 files changed

+1097
-0
lines changed

examples/LinkingDemo.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import com.qiniu.common.QiniuException;
2+
import com.qiniu.linking.*;
3+
import com.qiniu.linking.model.*;
4+
import com.qiniu.util.Auth;
5+
import sun.security.tools.jarsigner.TimestampedSigner;
6+
7+
import java.security.Timestamp;
8+
import java.util.Date;
9+
10+
11+
public class LinkingDemo {
12+
13+
final static String testAk = "ak";
14+
final static String testSk = "sk";
15+
final static String testAppid = "appid";
16+
final static String testHost = "http://linking.qiniuapi.com";
17+
final static String testDeviceName1 = "test1";
18+
final static String testDeviceName2 = "test2";
19+
public static void main(String[] args) {
20+
21+
Auth auth = Auth.create(testAk,testSk);
22+
LinkingDeviceManager deviceManager = new LinkingDeviceManager(auth,testHost);
23+
try{
24+
//创建设备
25+
deviceManager.createDevice(testAppid,testDeviceName1);
26+
} catch (QiniuException e){
27+
System.out.println(e.error());
28+
}
29+
30+
try{
31+
//添加dak
32+
DeviceKey[] keys = deviceManager.addDeviceKey(testAppid,testDeviceName1);
33+
System.out.println(keys[0].getAccessKey());
34+
System.out.println(keys[0].getSecretKey());
35+
}catch (QiniuException e){
36+
System.out.println(e.error());
37+
}
38+
39+
try{
40+
//查询设备
41+
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
42+
if (keys.length==1){
43+
throw new QiniuException(new Exception(),"expect one length");
44+
}
45+
//删除设备
46+
deviceManager.deleteDeviceKey(testAppid,testDeviceName1,keys[0].getAccessKey());
47+
keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
48+
if (keys.length==0){
49+
throw new QiniuException(new Exception(),"expect zero length");
50+
}
51+
}catch (QiniuException e){
52+
System.out.println(e.error());
53+
}
54+
55+
try{
56+
//列出设备
57+
DeviceListing deviceslist = deviceManager.listDevice(testAppid,"","", 1,false);
58+
System.out.println(deviceslist.items.length);
59+
}catch (QiniuException e){
60+
System.out.println(e.error());
61+
}
62+
63+
64+
65+
try{
66+
//修改设备字段
67+
PatchOperation[] operations={new PatchOperation("replace","segmentExpireDays",9)};
68+
Device device= deviceManager.updateDevice(testAppid,testDeviceName1,operations);
69+
System.out.println(device.getSegmentExpireDays());
70+
}catch (QiniuException e){
71+
System.out.println(e.error());
72+
}
73+
74+
75+
try{
76+
//查询设备在线历史记录
77+
DeviceHistoryListing history= deviceManager.listDeviceHistory(testAppid,testDeviceName1,
78+
0,(new Date().getTime())/1000,"",0);
79+
}catch (QiniuException e){
80+
System.out.println(e.error());
81+
}
82+
83+
84+
85+
try{
86+
//删除设备信息
87+
deviceManager.deleteDevice(testAppid,testDeviceName1);
88+
} catch (QiniuException e){
89+
System.out.println(e.error());
90+
}
91+
92+
try{
93+
deviceManager.createDevice(testAppid,testDeviceName1);
94+
deviceManager.createDevice(testAppid,testDeviceName2);
95+
96+
//添加dak
97+
deviceManager.addDeviceKey(testAppid,testDeviceName1);
98+
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
99+
String dak = keys[0].getAccessKey();
100+
101+
//移动dak
102+
deviceManager.cloneDeviceKey(testAppid,testDeviceName1,testDeviceName2,true, false,dak);
103+
Device device = deviceManager.getDeviceByAccessKey(dak);
104+
device.getDeviceName();
105+
106+
deviceManager.deleteDeviceKey(testAppid,testDeviceName2,dak);
107+
108+
String token;
109+
//生成具有所有功能的token
110+
String[] actions = new String[]{Auth.DTOKEN_ACTION_STATUS,Auth.DTOKEN_ACTION_VOD,Auth.DTOKEN_ACTION_TUTK};
111+
token = auth.generateLinkingDeviceTokenWithExpires(testAppid,testDeviceName1,1000,actions);
112+
//生成视频相关功能的token
113+
token = auth.generateLinkingDeviceVodTokenWithExpires(testAppid,testDeviceName1,1000,actions);
114+
//生成获取设备状态的token
115+
token = auth.generateLinkingDeviceStatusTokenWithExpires(testAppid,testDeviceName1,1000,actions);
116+
117+
}catch (QiniuException e){
118+
System.out.println(e.error());
119+
}finally {
120+
try{
121+
deviceManager.deleteDevice(testAppid,testDeviceName1);
122+
}catch (Exception ignored){
123+
}
124+
try{
125+
deviceManager.deleteDevice(testAppid,testDeviceName2);
126+
}catch (Exception ignored){
127+
}
128+
}
129+
130+
131+
}
132+
}

src/main/java/com/qiniu/http/Client.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,54 @@ public void accept(String key, Object value) {
236236
return send(requestBuilder, headers);
237237
}
238238

239+
public Response patch(String url, byte[] body, StringMap headers) throws QiniuException {
240+
return patch(url, body, headers, DefaultMime);
241+
}
242+
243+
public Response patch(String url, String body, StringMap headers) throws QiniuException {
244+
return patch(url, StringUtils.utf8Bytes(body), headers, DefaultMime);
245+
}
246+
247+
public Response patch(String url, StringMap params, StringMap headers) throws QiniuException {
248+
final FormBody.Builder f = new FormBody.Builder();
249+
params.forEach(new StringMap.Consumer() {
250+
@Override
251+
public void accept(String key, Object value) {
252+
f.add(key, value.toString());
253+
}
254+
});
255+
return patch(url, f.build(), headers);
256+
}
257+
258+
public Response patch(String url, byte[] body, StringMap headers, String contentType) throws QiniuException {
259+
RequestBody rbody;
260+
if (body != null && body.length > 0) {
261+
MediaType t = MediaType.parse(contentType);
262+
rbody = RequestBody.create(t, body);
263+
} else {
264+
rbody = RequestBody.create(null, new byte[0]);
265+
}
266+
return patch(url, rbody, headers);
267+
}
268+
269+
public Response patch(String url, byte[] body, int offset, int size,
270+
StringMap headers, String contentType) throws QiniuException {
271+
RequestBody rbody;
272+
if (body != null && body.length > 0) {
273+
MediaType t = MediaType.parse(contentType);
274+
rbody = create(t, body, offset, size);
275+
} else {
276+
rbody = RequestBody.create(null, new byte[0]);
277+
}
278+
return patch(url, rbody, headers);
279+
}
280+
281+
private Response patch(String url, RequestBody body, StringMap headers) throws QiniuException {
282+
Request.Builder requestBuilder = new Request.Builder().url(url).patch(body);
283+
return send(requestBuilder, headers);
284+
}
285+
286+
239287
public Response send(final Request.Builder requestBuilder, StringMap headers) throws QiniuException {
240288
if (headers != null) {
241289
headers.forEach(new StringMap.Consumer() {
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.qiniu.linking;
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.linking.model.*;
8+
import com.qiniu.util.*;
9+
10+
public class LinkingDeviceManager {
11+
12+
13+
private final Auth auth;
14+
private final String host;
15+
private final Client client;
16+
17+
18+
public LinkingDeviceManager(Auth auth) {
19+
this(auth, "http://linking.qiniuapi.com");
20+
}
21+
22+
public LinkingDeviceManager(Auth auth, String host) {
23+
this(auth, host, new Client());
24+
}
25+
26+
public LinkingDeviceManager(Auth auth, String host, Client client) {
27+
this.auth = auth;
28+
this.host = host;
29+
this.client = client;
30+
}
31+
32+
public void createDevice(String appid, String deviceName) throws QiniuException {
33+
StringMap params = new StringMap().put("device", deviceName);
34+
String url = String.format("%s/v1/apps/%s/devices", host, appid);
35+
Response res = post(url, params);
36+
res.close();
37+
}
38+
39+
public void deleteDevice(String appid, String deviceName) throws QiniuException {
40+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
41+
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
42+
StringMap headers = auth.authorizationV2(url, "DELETE", null, null);
43+
Response res = client.delete(url, headers);
44+
if (!res.isOK()) {
45+
throw new QiniuException(res);
46+
}
47+
res.close();
48+
}
49+
50+
public DeviceListing listDevice(String appid, String prefix,
51+
String marker, int limit, boolean online) throws QiniuException {
52+
StringMap map = new StringMap().putNotEmpty("marker", marker)
53+
.putNotEmpty("prefix", prefix).putWhen("limit", limit, limit > 0)
54+
.putWhen("online", online, online);
55+
String queryString = map.formString();
56+
if (map.size()>0){
57+
queryString="?"+queryString;
58+
}
59+
String url = String.format("%s/v1/apps/%s/devices%s", host, appid, queryString);
60+
Response res = get(url);
61+
DeviceListing ret = res.jsonToObject(DeviceListing.class);
62+
res.close();
63+
return ret;
64+
}
65+
66+
private Response get(String url) throws QiniuException {
67+
StringMap headers = auth.authorizationV2(url, "GET", null, null);
68+
Response res = client.get(url, headers);
69+
if (!res.isOK()) {
70+
throw new QiniuException(res);
71+
}
72+
return res;
73+
}
74+
75+
public Device getDevice(String appid, String deviceName) throws QiniuException {
76+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
77+
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
78+
Response res = get(url);
79+
Device ret = res.jsonToObject(Device.class);
80+
res.close();
81+
return ret;
82+
}
83+
84+
public Device getDeviceByAccessKey(String deviceAccessKey) throws QiniuException {
85+
String url = String.format("%s/v1/keys/%s", host, deviceAccessKey);
86+
Response res = get(url);
87+
Device ret = res.jsonToObject(Device.class);
88+
res.close();
89+
return ret;
90+
}
91+
92+
public Device updateDevice(String appid, String deviceName, PatchOperation[] operations) throws QiniuException {
93+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
94+
StringMap params = new StringMap().put("operations", operations);
95+
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
96+
byte[] body = Json.encode(params).getBytes(Constants.UTF_8);
97+
StringMap headers = auth.authorizationV2(url, "PATCH", body, Client.JsonMime);
98+
Response res = client.patch(url, body, headers, Client.JsonMime);
99+
if (!res.isOK()) {
100+
throw new QiniuException(res);
101+
}
102+
Device ret = res.jsonToObject(Device.class);
103+
res.close();
104+
return ret;
105+
106+
}
107+
108+
public DeviceHistoryListing listDeviceHistory(String appid, String deviceName,
109+
long start, long end, String marker, int limit) throws QiniuException {
110+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
111+
StringMap map = new StringMap().putNotEmpty("marker", marker).
112+
put("start", start).put("end", end).putWhen("limit", limit, limit > 0);
113+
String queryString = map.formString();
114+
String url = String.format("%s/v1/apps/%s/devices/%s/historyactivity?%s",
115+
host, appid, encodedDeviceName, queryString);
116+
Response res = get(url);
117+
DeviceHistoryListing ret = res.jsonToObject(DeviceHistoryListing.class);
118+
res.close();
119+
return ret;
120+
121+
}
122+
123+
private class DeviceKeyRet {
124+
DeviceKey[] keys;
125+
}
126+
127+
public DeviceKey[] addDeviceKey(String appid, String deviceName) throws QiniuException {
128+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
129+
String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName);
130+
Response res = post(url, null);
131+
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
132+
res.close();
133+
if (ret != null) {
134+
return ret.keys;
135+
}
136+
return null;
137+
}
138+
139+
public DeviceKey[] queryDeviceKey(String appid, String deviceName) throws QiniuException {
140+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
141+
String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName);
142+
Response res = get(url);
143+
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
144+
res.close();
145+
if (ret != null) {
146+
return ret.keys;
147+
}
148+
return null;
149+
}
150+
151+
public void deleteDeviceKey(String appid, String deviceName, String accessKey) throws QiniuException {
152+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
153+
String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s", host, appid, encodedDeviceName, accessKey);
154+
StringMap headers = auth.authorizationV2(url, "DELETE", null, null);
155+
Response res = client.delete(url, headers);
156+
if (!res.isOK()) {
157+
throw new QiniuException(res);
158+
}
159+
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
160+
res.close();
161+
}
162+
163+
public void updateDeviceKeyState(String appid, String deviceName,
164+
String accessKey, int state) throws QiniuException {
165+
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
166+
StringMap params = new StringMap().put("state", state);
167+
String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s/state", host, appid, encodedDeviceName, accessKey);
168+
Response res = post(url, params);
169+
res.close();
170+
}
171+
172+
public DeviceKey[] cloneDeviceKey(String appid, String fromDeviceName,
173+
String toDeviceName, boolean cleanSelfKeys,
174+
boolean deleteDevice, String deviceAccessKey) throws QiniuException {
175+
String encodedFromDeviceName = UrlSafeBase64.encodeToString(fromDeviceName);
176+
String encodedToDeviceName = UrlSafeBase64.encodeToString(toDeviceName);
177+
String url = String.format("%s/v1/apps/%s/devices/%s/keys/clone", host, appid, encodedToDeviceName);
178+
StringMap params = new StringMap().put("fromDevice", fromDeviceName).put("cleanSelfKeys", cleanSelfKeys).
179+
put("deleteDevice", deleteDevice).put("deviceAccessKey", deviceAccessKey);
180+
181+
Response res = post(url, params);
182+
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
183+
res.close();
184+
if (ret != null) {
185+
return ret.keys;
186+
187+
}
188+
return null;
189+
}
190+
191+
private Response post(String url, StringMap params) throws QiniuException {
192+
byte[] body;
193+
String contentType = null;
194+
if (params == null) {
195+
body = null;
196+
} else {
197+
contentType = Client.JsonMime;
198+
body = Json.encode(params).getBytes(Constants.UTF_8);
199+
}
200+
StringMap headers = auth.authorizationV2(url, "POST", body, contentType);
201+
Response res = client.post(url, body, headers, Client.JsonMime);
202+
if (!res.isOK()) {
203+
throw new QiniuException(res);
204+
}
205+
return res;
206+
}
207+
}

0 commit comments

Comments
 (0)