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
132 changes: 132 additions & 0 deletions examples/LinkingDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import com.qiniu.common.QiniuException;
import com.qiniu.linking.*;
import com.qiniu.linking.model.*;
import com.qiniu.util.Auth;
import sun.security.tools.jarsigner.TimestampedSigner;

import java.security.Timestamp;
import java.util.Date;


public class LinkingDemo {

final static String testAk = "ak";
final static String testSk = "sk";
final static String testAppid = "appid";
final static String testHost = "http://linking.qiniuapi.com";
final static String testDeviceName1 = "test1";
final static String testDeviceName2 = "test2";
public static void main(String[] args) {

Auth auth = Auth.create(testAk,testSk);
LinkingDeviceManager deviceManager = new LinkingDeviceManager(auth,testHost);
try{
//创建设备
deviceManager.createDevice(testAppid,testDeviceName1);
} catch (QiniuException e){
System.out.println(e.error());
}

try{
//添加dak
DeviceKey[] keys = deviceManager.addDeviceKey(testAppid,testDeviceName1);
System.out.println(keys[0].getAccessKey());
System.out.println(keys[0].getSecretKey());
}catch (QiniuException e){
System.out.println(e.error());
}

try{
//查询设备
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
if (keys.length==1){
throw new QiniuException(new Exception(),"expect one length");
}
//删除设备
deviceManager.deleteDeviceKey(testAppid,testDeviceName1,keys[0].getAccessKey());
keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
if (keys.length==0){
throw new QiniuException(new Exception(),"expect zero length");
}
}catch (QiniuException e){
System.out.println(e.error());
}

try{
//列出设备
DeviceListing deviceslist = deviceManager.listDevice(testAppid,"","", 1,false);
System.out.println(deviceslist.items.length);
}catch (QiniuException e){
System.out.println(e.error());
}



try{
//修改设备字段
PatchOperation[] operations={new PatchOperation("replace","segmentExpireDays",9)};
Device device= deviceManager.updateDevice(testAppid,testDeviceName1,operations);
System.out.println(device.getSegmentExpireDays());
}catch (QiniuException e){
System.out.println(e.error());
}


try{
//查询设备在线历史记录
DeviceHistoryListing history= deviceManager.listDeviceHistory(testAppid,testDeviceName1,
0,(new Date().getTime())/1000,"",0);
}catch (QiniuException e){
System.out.println(e.error());
}



try{
//删除设备信息
deviceManager.deleteDevice(testAppid,testDeviceName1);
} catch (QiniuException e){
System.out.println(e.error());
}

try{
deviceManager.createDevice(testAppid,testDeviceName1);
deviceManager.createDevice(testAppid,testDeviceName2);

//添加dak
deviceManager.addDeviceKey(testAppid,testDeviceName1);
DeviceKey[] keys = deviceManager.queryDeviceKey(testAppid,testDeviceName1);
String dak = keys[0].getAccessKey();

//移动dak
deviceManager.cloneDeviceKey(testAppid,testDeviceName1,testDeviceName2,true, false,dak);
Device device = deviceManager.getDeviceByAccessKey(dak);
device.getDeviceName();

deviceManager.deleteDeviceKey(testAppid,testDeviceName2,dak);

String token;
//生成具有所有功能的token
String[] actions = new String[]{Auth.DTOKEN_ACTION_STATUS,Auth.DTOKEN_ACTION_VOD,Auth.DTOKEN_ACTION_TUTK};
token = auth.generateLinkingDeviceTokenWithExpires(testAppid,testDeviceName1,1000,actions);
//生成视频相关功能的token
token = auth.generateLinkingDeviceVodTokenWithExpires(testAppid,testDeviceName1,1000,actions);
//生成获取设备状态的token
token = auth.generateLinkingDeviceStatusTokenWithExpires(testAppid,testDeviceName1,1000,actions);

}catch (QiniuException e){
System.out.println(e.error());
}finally {
try{
deviceManager.deleteDevice(testAppid,testDeviceName1);
}catch (Exception ignored){
}
try{
deviceManager.deleteDevice(testAppid,testDeviceName2);
}catch (Exception ignored){
}
}


}
}
48 changes: 48 additions & 0 deletions src/main/java/com/qiniu/http/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,54 @@ public void accept(String key, Object value) {
return send(requestBuilder, headers);
}

public Response patch(String url, byte[] body, StringMap headers) throws QiniuException {
return patch(url, body, headers, DefaultMime);
}

public Response patch(String url, String body, StringMap headers) throws QiniuException {
return patch(url, StringUtils.utf8Bytes(body), headers, DefaultMime);
}

public Response patch(String url, StringMap params, StringMap headers) throws QiniuException {
final FormBody.Builder f = new FormBody.Builder();
params.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
f.add(key, value.toString());
}
});
return patch(url, f.build(), headers);
}

public Response patch(String url, byte[] body, StringMap headers, String contentType) throws QiniuException {
RequestBody rbody;
if (body != null && body.length > 0) {
MediaType t = MediaType.parse(contentType);
rbody = RequestBody.create(t, body);
} else {
rbody = RequestBody.create(null, new byte[0]);
}
return patch(url, rbody, headers);
}

public Response patch(String url, byte[] body, int offset, int size,
StringMap headers, String contentType) throws QiniuException {
RequestBody rbody;
if (body != null && body.length > 0) {
MediaType t = MediaType.parse(contentType);
rbody = create(t, body, offset, size);
} else {
rbody = RequestBody.create(null, new byte[0]);
}
return patch(url, rbody, headers);
}

private Response patch(String url, RequestBody body, StringMap headers) throws QiniuException {
Request.Builder requestBuilder = new Request.Builder().url(url).patch(body);
return send(requestBuilder, headers);
}


public Response send(final Request.Builder requestBuilder, StringMap headers) throws QiniuException {
if (headers != null) {
headers.forEach(new StringMap.Consumer() {
Expand Down
207 changes: 207 additions & 0 deletions src/main/java/com/qiniu/linking/LinkingDeviceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package com.qiniu.linking;

import com.qiniu.common.Constants;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
import com.qiniu.linking.model.*;
import com.qiniu.util.*;

public class LinkingDeviceManager {


private final Auth auth;
private final String host;
private final Client client;


public LinkingDeviceManager(Auth auth) {
this(auth, "http://linking.qiniuapi.com");
}

public LinkingDeviceManager(Auth auth, String host) {
this(auth, host, new Client());
}

public LinkingDeviceManager(Auth auth, String host, Client client) {
this.auth = auth;
this.host = host;
this.client = client;
}

public void createDevice(String appid, String deviceName) throws QiniuException {
StringMap params = new StringMap().put("device", deviceName);
String url = String.format("%s/v1/apps/%s/devices", host, appid);
Response res = post(url, params);
res.close();
}

public void deleteDevice(String appid, String deviceName) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
StringMap headers = auth.authorizationV2(url, "DELETE", null, null);
Response res = client.delete(url, headers);
if (!res.isOK()) {
throw new QiniuException(res);
}
res.close();
}

public DeviceListing listDevice(String appid, String prefix,
String marker, int limit, boolean online) throws QiniuException {
StringMap map = new StringMap().putNotEmpty("marker", marker)
.putNotEmpty("prefix", prefix).putWhen("limit", limit, limit > 0)
.putWhen("online", online, online);
String queryString = map.formString();
if (map.size()>0){
queryString="?"+queryString;
}
String url = String.format("%s/v1/apps/%s/devices%s", host, appid, queryString);
Response res = get(url);
DeviceListing ret = res.jsonToObject(DeviceListing.class);
res.close();
return ret;
}

private Response get(String url) throws QiniuException {
StringMap headers = auth.authorizationV2(url, "GET", null, null);
Response res = client.get(url, headers);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}

public Device getDevice(String appid, String deviceName) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
Response res = get(url);
Device ret = res.jsonToObject(Device.class);
res.close();
return ret;
}

public Device getDeviceByAccessKey(String deviceAccessKey) throws QiniuException {
String url = String.format("%s/v1/keys/%s", host, deviceAccessKey);
Response res = get(url);
Device ret = res.jsonToObject(Device.class);
res.close();
return ret;
}

public Device updateDevice(String appid, String deviceName, PatchOperation[] operations) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
StringMap params = new StringMap().put("operations", operations);
String url = String.format("%s/v1/apps/%s/devices/%s", host, appid, encodedDeviceName);
byte[] body = Json.encode(params).getBytes(Constants.UTF_8);
StringMap headers = auth.authorizationV2(url, "PATCH", body, Client.JsonMime);
Response res = client.patch(url, body, headers, Client.JsonMime);
if (!res.isOK()) {
throw new QiniuException(res);
}
Device ret = res.jsonToObject(Device.class);
res.close();
return ret;

}

public DeviceHistoryListing listDeviceHistory(String appid, String deviceName,
long start, long end, String marker, int limit) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
StringMap map = new StringMap().putNotEmpty("marker", marker).
put("start", start).put("end", end).putWhen("limit", limit, limit > 0);
String queryString = map.formString();
String url = String.format("%s/v1/apps/%s/devices/%s/historyactivity?%s",
host, appid, encodedDeviceName, queryString);
Response res = get(url);
DeviceHistoryListing ret = res.jsonToObject(DeviceHistoryListing.class);
res.close();
return ret;

}

private class DeviceKeyRet {
DeviceKey[] keys;
}

public DeviceKey[] addDeviceKey(String appid, String deviceName) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName);
Response res = post(url, null);
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
res.close();
if (ret != null) {
return ret.keys;
}
return null;
}

public DeviceKey[] queryDeviceKey(String appid, String deviceName) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
String url = String.format("%s/v1/apps/%s/devices/%s/keys", host, appid, encodedDeviceName);
Response res = get(url);
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
res.close();
if (ret != null) {
return ret.keys;
}
return null;
}

public void deleteDeviceKey(String appid, String deviceName, String accessKey) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s", host, appid, encodedDeviceName, accessKey);
StringMap headers = auth.authorizationV2(url, "DELETE", null, null);
Response res = client.delete(url, headers);
if (!res.isOK()) {
throw new QiniuException(res);
}
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
res.close();
}

public void updateDeviceKeyState(String appid, String deviceName,
String accessKey, int state) throws QiniuException {
String encodedDeviceName = UrlSafeBase64.encodeToString(deviceName);
StringMap params = new StringMap().put("state", state);
String url = String.format("%s/v1/apps/%s/devices/%s/keys/%s/state", host, appid, encodedDeviceName, accessKey);
Response res = post(url, params);
res.close();
}

public DeviceKey[] cloneDeviceKey(String appid, String fromDeviceName,
String toDeviceName, boolean cleanSelfKeys,
boolean deleteDevice, String deviceAccessKey) throws QiniuException {
String encodedFromDeviceName = UrlSafeBase64.encodeToString(fromDeviceName);
String encodedToDeviceName = UrlSafeBase64.encodeToString(toDeviceName);
String url = String.format("%s/v1/apps/%s/devices/%s/keys/clone", host, appid, encodedToDeviceName);
StringMap params = new StringMap().put("fromDevice", fromDeviceName).put("cleanSelfKeys", cleanSelfKeys).
put("deleteDevice", deleteDevice).put("deviceAccessKey", deviceAccessKey);

Response res = post(url, params);
DeviceKeyRet ret = res.jsonToObject(DeviceKeyRet.class);
res.close();
if (ret != null) {
return ret.keys;

}
return null;
}

private Response post(String url, StringMap params) throws QiniuException {
byte[] body;
String contentType = null;
if (params == null) {
body = null;
} else {
contentType = Client.JsonMime;
body = Json.encode(params).getBytes(Constants.UTF_8);
}
StringMap headers = auth.authorizationV2(url, "POST", body, contentType);
Response res = client.post(url, body, headers, Client.JsonMime);
if (!res.isOK()) {
throw new QiniuException(res);
}
return res;
}
}
Loading