020ea372
Hugiee.Liu
fea: 测试接口添加
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
package com.objecteye.handle.viid;
import com.objecteye.config.Gat1400Properties;
import com.objecteye.content.VIIDApi;
import com.objecteye.pojo.viid.common.KeepaliveRequestObject;
import com.objecteye.pojo.viid.common.RegisterObject;
import com.objecteye.pojo.viid.common.RegisterRequestObject;
import com.objecteye.pojo.viid.common.UnRegisterRequestObject;
import com.objecteye.utils.DigestUtils;
import com.objecteye.utils.JSONUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* @author: liuhaoyu
* @date: 2023/6/28
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class CommonHandle {
private final RestTemplate restTemplate;
private final Gat1400Properties gat1400Properties;
private static final String HTTP = "http://";
/**
* 注册
*
* @param deviceId 注册目标设备
* @return 注册结果
*/
public boolean register(String deviceId) {
// 请求参数设置
RegisterRequestObject registerRequestObject = new RegisterRequestObject();
RegisterObject registerObject = new RegisterObject();
registerObject.setDeviceId(deviceId);
registerRequestObject.setRegisterObject(registerObject);
return twoStepCheck(VIIDApi.REGISTER, deviceId, JSONUtils.toJsonString(registerRequestObject));
}
/**
* 保活
*
* @param deviceId 心跳目标设备
* @return 发送结果
*/
public boolean keepalive(String deviceId) {
String keepaliveUrl = getAddress() + VIIDApi.KEEPALIVE;
// 请求参数设置
KeepaliveRequestObject keepaliveRequestObject = new KeepaliveRequestObject();
RegisterObject keepaliveObject = new RegisterObject();
keepaliveObject.setDeviceId(deviceId);
keepaliveRequestObject.setKeepaliveObject(keepaliveObject);
if (log.isDebugEnabled()) {
log.debug("保活请求 url:{} ,参数:{}", keepaliveUrl, keepaliveRequestObject);
}
HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtils.toJsonString(keepaliveRequestObject),
buildRequestHttpHeader(deviceId));
// 请求执行
ResponseEntity<String> responseEntity = postForEntity(keepaliveUrl, httpEntity);
return org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value();
}
/**
* 注销
*
* @param deviceId 注销目标设备
* @return 注销结果
*/
public boolean unregister(String deviceId) {
// 请求参数设置
UnRegisterRequestObject unRegisterRequestObject = new UnRegisterRequestObject();
RegisterObject unRegisterObject = new RegisterObject();
unRegisterObject.setDeviceId(deviceId);
unRegisterRequestObject.setUnRegisterObject(unRegisterObject);
return twoStepCheck(VIIDApi.UNREGISTER, deviceId, JSONUtils.toJsonString(unRegisterRequestObject));
}
private String getAddress() {
return HTTP + gat1400Properties.getServerAddress() + ":" + gat1400Properties.getPort();
}
private HttpHeaders buildRequestHttpHeader(String deviceId) {
// 请求头设置
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;charset=UTF-8");
headers.set("User-Identify", deviceId);
headers.setConnection("keepalive");
return headers;
}
private ResponseEntity<String> postForEntity(String url, HttpEntity<String> httpEntity) {
return restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
}
private boolean twoStepCheck(String uri, String deviceId, String requestJson) {
String url = getAddress() + uri;
// 请求头设置
HttpHeaders headers = buildRequestHttpHeader(deviceId);
// 第一次请求
ResponseEntity<String> responseEntity = postForEntity(url, new HttpEntity<>(requestJson, headers));
if (org.apache.http.HttpStatus.SC_UNAUTHORIZED == responseEntity.getStatusCode().value()) {
headers.set("Authorization", getAuthorization(responseEntity, url));
// 第二次请求
responseEntity = postForEntity(url, new HttpEntity<>(requestJson, headers));
return org.apache.http.HttpStatus.SC_OK == responseEntity.getStatusCode().value();
}
return false;
}
private String getAuthorization(ResponseEntity<String> responseEntity, String url) {
HttpHeaders responseEntityHeaders = responseEntity.getHeaders();
String authenticate = responseEntityHeaders.get("WWW-Authenticate").get(0);
String[] children = authenticate.split(",");
String realm = null, qop = null, nonce = null, opaque = null, method = "POST";
for (String item : children) {
String[] itemEntry = item.split("=");
if ("Digest realm".equals(itemEntry[0])) {
realm = itemEntry[1].replaceAll("\"", "");
} else if ("qop".equals(itemEntry[0])) {
qop = itemEntry[1].replaceAll("\"", "");
} else if ("nonce".equals(itemEntry[0])) {
nonce = itemEntry[1].replaceAll("\"", "");
}
}
String nc = "00000001";
String cnonce = DigestUtils.generateSalt2(8);
String response = DigestUtils.getResponse(gat1400Properties.getAccessKey(), realm, gat1400Properties.getSecurityKey(),
nonce, nc, cnonce, qop, method, url);
return DigestUtils.getAuthorization(gat1400Properties.getAccessKey(), realm, nonce,
url, qop, nc, cnonce, response, opaque);
}
}
|