RestClient.java
7.84 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright © 2016-2018 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.client.tools;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.support.HttpRequestWrapper;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.security.DeviceCredentials;
import org.thingsboard.server.common.data.security.DeviceCredentialsType;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* @author Andrew Shvayka
*/
@RequiredArgsConstructor
public class RestClient implements ClientHttpRequestInterceptor {
private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization";
private final RestTemplate restTemplate = new RestTemplate();
private String token;
private final String baseURL;
public void login(String username, String password) {
Map<String, String> loginRequest = new HashMap<>();
loginRequest.put("username", username);
loginRequest.put("password", password);
ResponseEntity<JsonNode> tokenInfo = restTemplate.postForEntity(baseURL + "/api/auth/login", loginRequest, JsonNode.class);
this.token = tokenInfo.getBody().get("token").asText();
restTemplate.setInterceptors(Collections.singletonList(this));
}
public Optional<Device> findDevice(String name) {
Map<String, String> params = new HashMap<String, String>();
params.put("deviceName", name);
try {
ResponseEntity<Device> deviceEntity = restTemplate.getForEntity(baseURL + "/api/tenant/devices?deviceName={deviceName}", Device.class, params);
return Optional.of(deviceEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Customer> findCustomer(String title) {
Map<String, String> params = new HashMap<String, String>();
params.put("customerTitle", title);
try {
ResponseEntity<Customer> customerEntity = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, params);
return Optional.of(customerEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Optional<Asset> findAsset(String name) {
Map<String, String> params = new HashMap<String, String>();
params.put("assetName", name);
try {
ResponseEntity<Asset> assetEntity = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, params);
return Optional.of(assetEntity.getBody());
} catch (HttpClientErrorException exception) {
if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
return Optional.empty();
} else {
throw exception;
}
}
}
public Customer createCustomer(String title) {
Customer customer = new Customer();
customer.setTitle(title);
return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
}
public Device createDevice(String name, String type) {
Device device = new Device();
device.setName(name);
device.setType(type);
return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
}
public void deleteDevice(DeviceId deviceId) {
restTemplate.delete(baseURL + "/api/device/" + deviceId.getId().toString());
}
public DeviceCredentials updateDeviceCredentials(DeviceId deviceId, String token) {
DeviceCredentials deviceCredentials = getCredentials(deviceId);
deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
deviceCredentials.setCredentialsId(token);
return saveDeviceCredentials(deviceCredentials);
}
public DeviceCredentials saveDeviceCredentials(DeviceCredentials deviceCredentials) {
return restTemplate.postForEntity(baseURL + "/api/device/credentials", deviceCredentials, DeviceCredentials.class).getBody();
}
public Asset createAsset(String name, String type) {
Asset asset = new Asset();
asset.setName(name);
asset.setType(type);
return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
}
public Alarm createAlarm(Alarm alarm) {
return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
}
public Device assignDevice(CustomerId customerId, DeviceId deviceId) {
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class,
customerId.toString(), deviceId.toString()).getBody();
}
public Asset assignAsset(CustomerId customerId, AssetId assetId) {
return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", null, Asset.class,
customerId.toString(), assetId.toString()).getBody();
}
public EntityRelation makeRelation(String relationType, EntityId idFrom, EntityId idTo) {
EntityRelation relation = new EntityRelation();
relation.setFrom(idFrom);
relation.setTo(idTo);
relation.setType(relationType);
return restTemplate.postForEntity(baseURL + "/api/relation", relation, EntityRelation.class).getBody();
}
public DeviceCredentials getCredentials(DeviceId id) {
return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
public String getToken() {
return token;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
HttpRequest wrapper = new HttpRequestWrapper(request);
wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
return execution.execute(wrapper, bytes);
}
}