//
// Visionline Web Api Connector Example Java
// V1.1
// 2017-06-22
//
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
class Program {
private static String visionline = ""; //Example:https://127.0.0.1
private static String endpointId = ""; //Example JILLY44
private static String roomNumber = "101";
public static void main(String[] args) throws Exception {
callSession();
}
static void callSession() throws Exception{
if(visionline.length()==0 || endpointId.length()==0){
System.out.println("Check required values for visionline url and endpointId...");
return;
}
System.out.println("Getting sessionId & accessKey...");
String sessionJson = "{\"username\":\"sym\",\"password\":\"sym\"}";
URL url = new URL(visionline+"/api/v1/sessions");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("Date", getCurrentTimeStamp());
conn.setRequestProperty("Content-MD5", md5(sessionJson));
conn.setDoOutput(true);
TrustModifier.relaxHostChecking(conn); // Skip invalid SSL check
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(sessionJson);
writer.flush();
writer.close();
os.close();
BufferedReader br;
if (conn.getResponseCode() == 201) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
System.out.println("ERR:");
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
StringBuilder output = new StringBuilder();
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
output.append(inputLine);
}
conn.disconnect();
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(output.toString()).getAsJsonObject();
if (o.get("id").toString().length()>0 && o.get("accessKey").toString().length()>0){
System.out.println("sessionId:"+ o.get("id").getAsString());
System.out.println("accessKey:"+ o.get("accessKey").getAsString());
callCreateKey(o.get("id").getAsString(),o.get("accessKey").getAsString());
}else{
System.out.println("Error creating valid session");
}
}
static void callCreateKey(String sessionId, String accessKey) throws Exception{
boolean error = false;
String keyJson = "{\"doorOperations\":[{\"doors\":[\""+ roomNumber +"\"],\"operation\":\"guest\"}],\"label\":\"%ROOMRANGE%:%UUID%:%CARDNUM%\",\"endPointID\":\""+ endpointId +"\",\"description\":\"Hotel California\",\"expireTime\":\""+ getKeyExpireTime() +"\",\"format\":\"rfid48\"}";
System.out.println("Creating key...");
String requestTimestamp = getCurrentTimeStamp();
String toSign = new StringBuilder()
.append("POST\n")
.append(md5(keyJson)+"\n")
.append("application/json;charset=utf-8\n")
.append(requestTimestamp+"\n")
.append("/api/v1/cards?action=mobileAccess&override=true")
.toString();
String singned = sha1(toSign,accessKey);
URL url = new URL(visionline+"/api/v1/cards?action=mobileAccess&override=true");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
conn.setRequestProperty("Date", requestTimestamp);
conn.setRequestProperty("Content-MD5", md5(keyJson));
conn.setRequestProperty("Authorization", String.format("AWS %s:%s", sessionId, singned));
conn.setDoOutput(true);
TrustModifier.relaxHostChecking(conn); // Skip invalid SSL check
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(keyJson);
writer.flush();
writer.close();
os.close();
BufferedReader br;
if (conn.getResponseCode() == 201) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
} else {
error = true;
System.out.println("ERR:");
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
}
StringBuilder output = new StringBuilder();
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
output.append(inputLine);
}
conn.disconnect();
if(!error){
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(output.toString()).getAsJsonObject();
System.out.println("credentialId:"+ o.get("credentialID"));
System.out.println("Key created!");
callDeleteSession(sessionId,accessKey);
}
else{
System.out.println("Error creating key...");
}
}
static void callDeleteSession(String sessionId, String accessKey) throws Exception{
System.out.println("Deleting session...");
String requestTimestamp = getCurrentTimeStamp();
String toSign = new StringBuilder()
.append("DELETE\n")
.append("\n")
.append("\n")
.append(requestTimestamp+"\n")
.append("/api/v1/sessions/"+sessionId)
.toString();
String singned = sha1(toSign,accessKey);
URL url = new URL(visionline+"/api/v1/sessions/"+sessionId);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("DELETE");
conn.setRequestProperty("Date", requestTimestamp);
conn.setRequestProperty("Authorization", String.format("AWS %s:%s", sessionId, singned));
conn.setDoOutput(true);
TrustModifier.relaxHostChecking(conn); // Skip invalid SSL check
BufferedReader br;
if (conn.getResponseCode() == 201) {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
System.out.println("Session deleted!");
} else {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
System.out.println("ERR:"+br);
}
conn.disconnect();
}
static private String md5(String str) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(str.getBytes("UTF-8"));
return Base64.encodeBase64String(md.digest());
}
public static String sha1(String data, String key) throws Exception {
Mac sha1_HMAC = Mac.getInstance("HmacSHA1");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA1");
sha1_HMAC.init(secret_key);
return Base64.encodeBase64String(sha1_HMAC.doFinal(data.getBytes()));
}
public static String getCurrentTimeStamp() {
SimpleDateFormat sdfDate = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss zzz",Locale.US);
sdfDate.setTimeZone(TimeZone.getTimeZone("GMT"));
Date now = new Date();
String strDate = sdfDate.format(now);
return strDate;
}
public static String getKeyExpireTime() {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyyMMdd'T'HHmm");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
String strDate = sdfDate.format(cal.getTime());
return strDate;
}
public static void printRequestHeaders(HttpURLConnection conn){
for (String
没有合适的资源?快使用搜索试试~ 我知道了~
VL_Example_Java.zip_bookumj_java_zip
共17个文件
jar:11个
class:4个
zip:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 22 浏览量
2022-09-23
04:10:15
上传
评论
收藏 2.83MB ZIP 举报
温馨提示
visionline demo code
资源推荐
资源详情
资源评论
收起资源包目录
VL_Example_Java.zip (17个子文件)
VL_Example_Java
bin
Program$TrustModifier$AlwaysTrustManager.class 1KB
Program$TrustModifier.class 2KB
Program.class 9KB
Program$TrustModifier$TrustingHostnameVerifier.class 802B
src
Program.java 10KB
lib
httpcore-4.4.4.jar 319KB
httpmime-4.5.2.jar 40KB
httpclient-win-4.5.2.jar 17KB
gson-2.7.jar 227KB
httpclient-4.5.2.jar 719KB
httpclient-cache-4.5.2.jar 155KB
jna-4.1.0.jar 893KB
jna-platform-4.1.0.jar 1.4MB
commons-codec-1.9.jar 258KB
commons-logging-1.2.jar 60KB
fluent-hc-4.5.2.jar 31KB
vl-java-maven.zip 5KB
共 17 条
- 1
资源评论
Kinonoyomeo
- 粉丝: 77
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 智能卡管理工具 - 野火2019专业版软件
- Android平台上的百度地图定位演示案例
- 全国 31省市场分割指数2004-2022年
- 华夏ERP基于SpringBoot框架和SaaS模式立志为中小企业提供开源好用的ERP软件
- Android光学字符识别(OCR)工具或库
- 基于C语言+STM32实现的智能自平衡小车+源码解析+超声波避障+超声波跟随+蓝牙遥控等功能+硬件资料+仿真(高分优秀项目)
- C# 屏幕录制 AForge.NET Framework-2.2.5
- 全国31省-环境污染综合指数(2008-2022年).zip
- 校园小情书微信小程序源码 - 社区小程序前后端开源 - 校园表白墙交友小程序
- 基于java的进销存管理系统软件源码+课设文档资料.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功