diff --git a/.travis.yml b/.travis.yml
index 6f22da9139..f635da5a55 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,9 +4,9 @@ before_install:
- export MAVEN_OPTS="-Dmaven.repo.local=$HOME/.m2/repository -Xms1024m -Xmx3072m"
- export HTTP_LOG_CONTROLLER_ERROR_STACK_TRACE=false
jdk:
- - oraclejdk8
+ - openjdk8
language: java
sudo: required
services:
- docker
-script: mvn clean verify -Ddockerfile.skip=false -DblackBoxTests.skip=false -DblackBoxTests.skipTailChildContainers=true
+script: mvn clean verify -Ddockerfile.skip=false
diff --git a/application/build.gradle b/application/build.gradle
index 1d6017c964..1ea603039a 100644
--- a/application/build.gradle
+++ b/application/build.gradle
@@ -1,5 +1,5 @@
/**
- * Copyright © 2016-2019 The Thingsboard Authors
+ * Copyright © 2016-2020 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.
diff --git a/application/pom.xml b/application/pom.xml
index 21e9b566ce..e23515f5c9 100644
--- a/application/pom.xml
+++ b/application/pom.xml
@@ -1,6 +1,6 @@
+
+
+ download-dependencies
+
+ true
+ true
+
+
@@ -162,9 +173,9 @@
${spring-boot.version}
- org.fortasoft
+ org.thingsboard
gradle-maven-plugin
- 1.0.8
+ 1.0.9
org.apache.maven.plugins
@@ -427,6 +438,12 @@
${project.version}
test
+
+ org.thingsboard
+ rest-client
+ ${project.version}
+ test
+
org.thingsboard
dao
diff --git a/rest-client/pom.xml b/rest-client/pom.xml
new file mode 100644
index 0000000000..5d931d7afd
--- /dev/null
+++ b/rest-client/pom.xml
@@ -0,0 +1,48 @@
+
+
+ 4.0.0
+
+ org.thingsboard
+ 2.5.0-SNAPSHOT
+ thingsboard
+
+ rest-client
+ jar
+
+ Thingsboard Rest Client
+ https://thingsboard.io
+
+
+ UTF-8
+ ${basedir}/..
+
+
+
+
+ org.thingsboard.common
+ data
+
+
+ org.springframework
+ spring-web
+
+
+
+
diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java
new file mode 100644
index 0000000000..7e0dbf3858
--- /dev/null
+++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java
@@ -0,0 +1,2076 @@
+/**
+ * Copyright © 2016-2020 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.rest.client;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpMethod;
+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.util.StringUtils;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.RestTemplate;
+import org.thingsboard.rest.client.utils.RestJsonConverter;
+import org.thingsboard.server.common.data.AdminSettings;
+import org.thingsboard.server.common.data.ClaimRequest;
+import org.thingsboard.server.common.data.Customer;
+import org.thingsboard.server.common.data.Dashboard;
+import org.thingsboard.server.common.data.DashboardInfo;
+import org.thingsboard.server.common.data.Device;
+import org.thingsboard.server.common.data.EntitySubtype;
+import org.thingsboard.server.common.data.EntityView;
+import org.thingsboard.server.common.data.Event;
+import org.thingsboard.server.common.data.Tenant;
+import org.thingsboard.server.common.data.UpdateMessage;
+import org.thingsboard.server.common.data.User;
+import org.thingsboard.server.common.data.alarm.Alarm;
+import org.thingsboard.server.common.data.alarm.AlarmId;
+import org.thingsboard.server.common.data.alarm.AlarmInfo;
+import org.thingsboard.server.common.data.alarm.AlarmSearchStatus;
+import org.thingsboard.server.common.data.alarm.AlarmSeverity;
+import org.thingsboard.server.common.data.alarm.AlarmStatus;
+import org.thingsboard.server.common.data.asset.Asset;
+import org.thingsboard.server.common.data.asset.AssetSearchQuery;
+import org.thingsboard.server.common.data.audit.ActionType;
+import org.thingsboard.server.common.data.audit.AuditLog;
+import org.thingsboard.server.common.data.device.DeviceSearchQuery;
+import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery;
+import org.thingsboard.server.common.data.id.AssetId;
+import org.thingsboard.server.common.data.id.CustomerId;
+import org.thingsboard.server.common.data.id.DashboardId;
+import org.thingsboard.server.common.data.id.DeviceId;
+import org.thingsboard.server.common.data.id.EntityId;
+import org.thingsboard.server.common.data.id.EntityViewId;
+import org.thingsboard.server.common.data.id.RuleChainId;
+import org.thingsboard.server.common.data.id.RuleNodeId;
+import org.thingsboard.server.common.data.id.TenantId;
+import org.thingsboard.server.common.data.id.UserId;
+import org.thingsboard.server.common.data.id.WidgetTypeId;
+import org.thingsboard.server.common.data.id.WidgetsBundleId;
+import org.thingsboard.server.common.data.kv.Aggregation;
+import org.thingsboard.server.common.data.kv.AttributeKvEntry;
+import org.thingsboard.server.common.data.kv.TsKvEntry;
+import org.thingsboard.server.common.data.page.TextPageData;
+import org.thingsboard.server.common.data.page.TextPageLink;
+import org.thingsboard.server.common.data.page.TimePageData;
+import org.thingsboard.server.common.data.page.TimePageLink;
+import org.thingsboard.server.common.data.plugin.ComponentDescriptor;
+import org.thingsboard.server.common.data.plugin.ComponentType;
+import org.thingsboard.server.common.data.relation.EntityRelation;
+import org.thingsboard.server.common.data.relation.EntityRelationInfo;
+import org.thingsboard.server.common.data.relation.EntityRelationsQuery;
+import org.thingsboard.server.common.data.relation.RelationTypeGroup;
+import org.thingsboard.server.common.data.rule.RuleChain;
+import org.thingsboard.server.common.data.rule.RuleChainMetaData;
+import org.thingsboard.server.common.data.security.DeviceCredentials;
+import org.thingsboard.server.common.data.security.DeviceCredentialsType;
+import org.thingsboard.server.common.data.security.model.SecuritySettings;
+import org.thingsboard.server.common.data.security.model.UserPasswordPolicy;
+import org.thingsboard.server.common.data.widget.WidgetType;
+import org.thingsboard.server.common.data.widget.WidgetsBundle;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.URI;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.stream.Collectors;
+
+import static org.springframework.util.StringUtils.isEmpty;
+
+/**
+ * @author Andrew Shvayka
+ */
+public class RestClient implements ClientHttpRequestInterceptor, Closeable {
+ private static final String JWT_TOKEN_HEADER_PARAM = "X-Authorization";
+ protected final RestTemplate restTemplate;
+ protected final String baseURL;
+ private String token;
+ private String refreshToken;
+ private final ObjectMapper objectMapper = new ObjectMapper();
+ private ExecutorService service = Executors.newWorkStealingPool(10);
+
+
+ protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken=";
+
+ public RestClient(String baseURL) {
+ this(new RestTemplate(), baseURL);
+ }
+
+ public RestClient(RestTemplate restTemplate, String baseURL) {
+ this.restTemplate = restTemplate;
+ this.baseURL = baseURL;
+ }
+
+ @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);
+ ClientHttpResponse response = execution.execute(wrapper, bytes);
+ if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
+ synchronized (this) {
+ restTemplate.getInterceptors().remove(this);
+ refreshToken();
+ wrapper.getHeaders().set(JWT_TOKEN_HEADER_PARAM, "Bearer " + token);
+ return execution.execute(wrapper, bytes);
+ }
+ }
+ return response;
+ }
+
+ public RestTemplate getRestTemplate() {
+ return restTemplate;
+ }
+
+ public String getToken() {
+ return token;
+ }
+
+ public String getRefreshToken() {
+ return refreshToken;
+ }
+
+ public void refreshToken() {
+ Map refreshTokenRequest = new HashMap<>();
+ refreshTokenRequest.put("refreshToken", refreshToken);
+ ResponseEntity tokenInfo = restTemplate.postForEntity(baseURL + "/api/auth/token", refreshTokenRequest, JsonNode.class);
+ setTokenInfo(tokenInfo.getBody());
+ }
+
+ public void login(String username, String password) {
+ Map loginRequest = new HashMap<>();
+ loginRequest.put("username", username);
+ loginRequest.put("password", password);
+ ResponseEntity tokenInfo = restTemplate.postForEntity(baseURL + "/api/auth/login", loginRequest, JsonNode.class);
+ setTokenInfo(tokenInfo.getBody());
+ }
+
+ private void setTokenInfo(JsonNode tokenInfo) {
+ this.token = tokenInfo.get("token").asText();
+ this.refreshToken = tokenInfo.get("refreshToken").asText();
+ restTemplate.getInterceptors().add(this);
+ }
+
+ public Optional getAdminSettings(String key) {
+ try {
+ ResponseEntity adminSettings = restTemplate.getForEntity(baseURL + "/api/admin/settings/{key}", AdminSettings.class, key);
+ return Optional.ofNullable(adminSettings.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public AdminSettings saveAdminSettings(AdminSettings adminSettings) {
+ return restTemplate.postForEntity(baseURL + "/api/admin/settings", adminSettings, AdminSettings.class).getBody();
+ }
+
+ public void sendTestMail(AdminSettings adminSettings) {
+ restTemplate.postForEntity(baseURL + "/api/admin/settings/testMail", adminSettings, AdminSettings.class);
+ }
+
+ public Optional getSecuritySettings() {
+ try {
+ ResponseEntity securitySettings = restTemplate.getForEntity(baseURL + "/api/admin/securitySettings", SecuritySettings.class);
+ return Optional.ofNullable(securitySettings.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public SecuritySettings saveSecuritySettings(SecuritySettings securitySettings) {
+ return restTemplate.postForEntity(baseURL + "/api/admin/securitySettings", securitySettings, SecuritySettings.class).getBody();
+ }
+
+ public Optional checkUpdates() {
+ try {
+ ResponseEntity updateMsg = restTemplate.getForEntity(baseURL + "/api/admin/updates", UpdateMessage.class);
+ return Optional.ofNullable(updateMsg.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getAlarmById(AlarmId alarmId) {
+ try {
+ ResponseEntity alarm = restTemplate.getForEntity(baseURL + "/api/alarm/{alarmId}", Alarm.class, alarmId.getId());
+ return Optional.ofNullable(alarm.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getAlarmInfoById(AlarmId alarmId) {
+ try {
+ ResponseEntity alarmInfo = restTemplate.getForEntity(baseURL + "/api/alarm/info/{alarmId}", AlarmInfo.class, alarmId.getId());
+ return Optional.ofNullable(alarmInfo.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Alarm saveAlarm(Alarm alarm) {
+ return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
+ }
+
+ public void deleteAlarm(AlarmId alarmId) {
+ restTemplate.delete(baseURL + "/api/alarm/{alarmId}", alarmId.getId());
+ }
+
+ public void ackAlarm(AlarmId alarmId) {
+ restTemplate.postForLocation(baseURL + "/api/alarm/{alarmId}/ack", null, alarmId.getId());
+ }
+
+ public void clearAlarm(AlarmId alarmId) {
+ restTemplate.postForLocation(baseURL + "/api/alarm/{alarmId}/clear", null, alarmId.getId());
+ }
+
+ public TimePageData getAlarms(EntityId entityId, AlarmSearchStatus searchStatus, AlarmStatus status, TimePageLink pageLink, Boolean fetchOriginator) {
+ Map params = new HashMap<>();
+ params.put("entityType", entityId.getEntityType().name());
+ params.put("entityId", entityId.getId().toString());
+ params.put("searchStatus", searchStatus.name());
+ params.put("status", status.name());
+ params.put("fetchOriginator", String.valueOf(fetchOriginator));
+ addPageLinkToParam(params, pageLink);
+
+ return restTemplate.exchange(
+ baseURL + "/api/alarm/{entityType}/{entityId}?searchStatus={searchStatus}&status={status}&fetchOriginator={fetchOriginator}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public Optional getHighestAlarmSeverity(EntityId entityId, AlarmSearchStatus searchStatus, AlarmStatus status) {
+ Map params = new HashMap<>();
+ params.put("entityType", entityId.getEntityType().name());
+ params.put("entityId", entityId.getId().toString());
+ params.put("searchStatus", searchStatus.name());
+ params.put("status", status.name());
+ try {
+ ResponseEntity alarmSeverity = restTemplate.getForEntity(baseURL + "/api/alarm/highestSeverity/{entityType}/{entityId}?searchStatus={searchStatus}&status={status}", AlarmSeverity.class, params);
+ return Optional.ofNullable(alarmSeverity.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ @Deprecated
+ public Alarm createAlarm(Alarm alarm) {
+ return restTemplate.postForEntity(baseURL + "/api/alarm", alarm, Alarm.class).getBody();
+ }
+
+ public Optional getAssetById(AssetId assetId) {
+ try {
+ ResponseEntity asset = restTemplate.getForEntity(baseURL + "/api/asset/{assetId}", Asset.class, assetId.getId());
+ return Optional.ofNullable(asset.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Asset saveAsset(Asset asset) {
+ return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
+ }
+
+ public void deleteAsset(AssetId assetId) {
+ restTemplate.delete(baseURL + "/api/asset/{assetId}", assetId.getId());
+ }
+
+ public Optional assignAssetToCustomer(CustomerId customerId, AssetId assetId) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ params.put("assetId", assetId.getId().toString());
+
+ try {
+ ResponseEntity asset = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", null, Asset.class, params);
+ return Optional.ofNullable(asset.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional unassignAssetFromCustomer(AssetId assetId) {
+ try {
+ ResponseEntity asset = restTemplate.exchange(baseURL + "/api/customer/asset/{assetId}", HttpMethod.DELETE, HttpEntity.EMPTY, Asset.class, assetId.getId());
+ return Optional.ofNullable(asset.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional assignAssetToPublicCustomer(AssetId assetId) {
+ try {
+ ResponseEntity asset = restTemplate.postForEntity(baseURL + "/api/customer/public/asset/{assetId}", null, Asset.class, assetId.getId());
+ return Optional.ofNullable(asset.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TextPageData getTenantAssets(TextPageLink pageLink, String assetType) {
+ Map params = new HashMap<>();
+ params.put("type", assetType);
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> assets = restTemplate.exchange(
+ baseURL + "/api/tenant/assets?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return assets.getBody();
+ }
+
+ public Optional getTenantAsset(String assetName) {
+ try {
+ ResponseEntity asset = restTemplate.getForEntity(baseURL + "/api/tenant/assets?assetName={assetName}", Asset.class, assetName);
+ return Optional.ofNullable(asset.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TextPageData getCustomerAssets(CustomerId customerId, TextPageLink pageLink, String assetType) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ params.put("type", assetType);
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> assets = restTemplate.exchange(
+ baseURL + "/api/customer/{customerId}/assets?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return assets.getBody();
+ }
+
+ public List getAssetsByIds(List assetIds) {
+ return restTemplate.exchange(
+ baseURL + "/api/assets?assetIds={assetIds}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ listIdsToString(assetIds))
+ .getBody();
+ }
+
+ public List findByQuery(AssetSearchQuery query) {
+ return restTemplate.exchange(
+ URI.create(baseURL + "/api/assets"),
+ HttpMethod.POST,
+ new HttpEntity<>(query),
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public List getAssetTypes() {
+ return restTemplate.exchange(URI.create(
+ baseURL + "/api/asset/types"),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ @Deprecated
+ public Optional findAsset(String name) {
+ Map params = new HashMap();
+ params.put("assetName", name);
+ try {
+ ResponseEntity 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;
+ }
+ }
+ }
+
+ @Deprecated
+ public Asset createAsset(Asset asset) {
+ return restTemplate.postForEntity(baseURL + "/api/asset", asset, Asset.class).getBody();
+ }
+
+ @Deprecated
+ 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();
+ }
+
+ @Deprecated
+ public Asset assignAsset(CustomerId customerId, AssetId assetId) {
+ return restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/asset/{assetId}", HttpEntity.EMPTY, Asset.class,
+ customerId.toString(), assetId.toString()).getBody();
+ }
+
+ public TimePageData getAuditLogsByCustomerId(CustomerId customerId, TimePageLink pageLink, List actionTypes) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ params.put("actionTypes", listEnumToString(actionTypes));
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> auditLog = restTemplate.exchange(
+ baseURL + "/api/audit/logs/customer/{customerId}?actionTypes={actionTypes}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return auditLog.getBody();
+ }
+
+ public TimePageData getAuditLogsByUserId(UserId userId, TimePageLink pageLink, List actionTypes) {
+ Map params = new HashMap<>();
+ params.put("userId", userId.getId().toString());
+ params.put("actionTypes", listEnumToString(actionTypes));
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> auditLog = restTemplate.exchange(
+ baseURL + "/api/audit/logs/user/{userId}?actionTypes={actionTypes}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return auditLog.getBody();
+ }
+
+ public TimePageData getAuditLogsByEntityId(EntityId entityId, List actionTypes, TimePageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("entityType", entityId.getEntityType().name());
+ params.put("entityId", entityId.getId().toString());
+ params.put("actionTypes", listEnumToString(actionTypes));
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> auditLog = restTemplate.exchange(
+ baseURL + "/api/audit/logs/entity/{entityType}/{entityId}?actionTypes={actionTypes}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return auditLog.getBody();
+ }
+
+ public TimePageData getAuditLogs(TimePageLink pageLink, List actionTypes) {
+ Map params = new HashMap<>();
+ params.put("actionTypes", listEnumToString(actionTypes));
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> auditLog = restTemplate.exchange(
+ baseURL + "/api/audit/logs?actionTypes={actionTypes}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return auditLog.getBody();
+ }
+
+ public String getActivateToken(UserId userId) {
+ String activationLink = getActivationLink(userId);
+ return activationLink.substring(activationLink.lastIndexOf(ACTIVATE_TOKEN_REGEX) + ACTIVATE_TOKEN_REGEX.length());
+ }
+
+ public Optional getUser() {
+ ResponseEntity user = restTemplate.getForEntity(baseURL + "/api/auth/user", User.class);
+ return Optional.ofNullable(user.getBody());
+ }
+
+ public void logout() {
+ restTemplate.postForLocation(baseURL + "/api/auth/logout", null);
+ }
+
+ public void changePassword(String currentPassword, String newPassword) {
+ ObjectNode changePasswordRequest = objectMapper.createObjectNode();
+ changePasswordRequest.put("currentPassword", currentPassword);
+ changePasswordRequest.put("newPassword", newPassword);
+ restTemplate.postForLocation(baseURL + "/api/auth/changePassword", changePasswordRequest);
+ }
+
+ public Optional getUserPasswordPolicy() {
+ try {
+ ResponseEntity userPasswordPolicy = restTemplate.getForEntity(baseURL + "/api/noauth/userPasswordPolicy", UserPasswordPolicy.class);
+ return Optional.ofNullable(userPasswordPolicy.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public ResponseEntity checkActivateToken(UserId userId) {
+ String activateToken = getActivateToken(userId);
+ return restTemplate.getForEntity(baseURL + "/api/noauth/activate?activateToken={activateToken}", String.class, activateToken);
+ }
+
+ public void requestResetPasswordByEmail(String email) {
+ ObjectNode resetPasswordByEmailRequest = objectMapper.createObjectNode();
+ resetPasswordByEmailRequest.put("email", email);
+ restTemplate.postForLocation(baseURL + "/api/noauth/resetPasswordByEmail", resetPasswordByEmailRequest);
+ }
+
+ public Optional activateUser(UserId userId, String password) {
+ ObjectNode activateRequest = objectMapper.createObjectNode();
+ activateRequest.put("activateToken", getActivateToken(userId));
+ activateRequest.put("password", password);
+ try {
+ ResponseEntity jsonNode = restTemplate.postForEntity(baseURL + "/api/noauth/activate", activateRequest, JsonNode.class);
+ return Optional.ofNullable(jsonNode.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getComponentDescriptorByClazz(String componentDescriptorClazz) {
+ try {
+ ResponseEntity componentDescriptor = restTemplate.getForEntity(baseURL + "/api/component/{componentDescriptorClazz}", ComponentDescriptor.class, componentDescriptorClazz);
+ return Optional.ofNullable(componentDescriptor.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public List getComponentDescriptorsByType(ComponentType componentType) {
+ return restTemplate.exchange(
+ baseURL + "/api/components?componentType={componentType}",
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ componentType).getBody();
+ }
+
+ public List getComponentDescriptorsByTypes(List componentTypes) {
+ return restTemplate.exchange(
+ baseURL + "/api/components?componentTypes={componentTypes}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ listEnumToString(componentTypes))
+ .getBody();
+ }
+
+ public Optional getCustomerById(CustomerId customerId) {
+ try {
+ ResponseEntity customer = restTemplate.getForEntity(baseURL + "/api/customer/{customerId}", Customer.class, customerId.getId());
+ return Optional.ofNullable(customer.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getShortCustomerInfoById(CustomerId customerId) {
+ try {
+ ResponseEntity customerInfo = restTemplate.getForEntity(baseURL + "/api/customer/{customerId}/shortInfo", JsonNode.class, customerId.getId());
+ return Optional.ofNullable(customerInfo.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public String getCustomerTitleById(CustomerId customerId) {
+ return restTemplate.getForObject(baseURL + "/api/customer/{customerId}/title", String.class, customerId.getId());
+ }
+
+ public Customer saveCustomer(Customer customer) {
+ return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
+ }
+
+ public void deleteCustomer(CustomerId customerId) {
+ restTemplate.delete(baseURL + "/api/customer/{customerId}", customerId.getId());
+ }
+
+ public TextPageData getCustomers(TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ addPageLinkToParam(params, pageLink);
+
+ ResponseEntity> customer = restTemplate.exchange(
+ baseURL + "/api/customers?" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params);
+ return customer.getBody();
+ }
+
+ public Optional getTenantCustomer(String customerTitle) {
+ try {
+ ResponseEntity customer = restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle={customerTitle}", Customer.class, customerTitle);
+ return Optional.ofNullable(customer.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ @Deprecated
+ public Optional findCustomer(String title) {
+ Map params = new HashMap<>();
+ params.put("customerTitle", title);
+ try {
+ ResponseEntity 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;
+ }
+ }
+ }
+
+ @Deprecated
+ public Customer createCustomer(Customer customer) {
+ return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
+ }
+
+ @Deprecated
+ public Customer createCustomer(String title) {
+ Customer customer = new Customer();
+ customer.setTitle(title);
+ return restTemplate.postForEntity(baseURL + "/api/customer", customer, Customer.class).getBody();
+ }
+
+ public Long getServerTime() {
+ return restTemplate.getForObject(baseURL + "/api/dashboard/serverTime", Long.class);
+ }
+
+ public Long getMaxDatapointsLimit() {
+ return restTemplate.getForObject(baseURL + "/api/dashboard/maxDatapointsLimit", Long.class);
+ }
+
+ public Optional getDashboardInfoById(DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboardInfo = restTemplate.getForEntity(baseURL + "/api/dashboard/info/{dashboardId}", DashboardInfo.class, dashboardId.getId());
+ return Optional.ofNullable(dashboardInfo.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getDashboardById(DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboard = restTemplate.getForEntity(baseURL + "/api/dashboard/{dashboardId}", Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Dashboard saveDashboard(Dashboard dashboard) {
+ return restTemplate.postForEntity(baseURL + "/api/dashboard", dashboard, Dashboard.class).getBody();
+ }
+
+ public void deleteDashboard(DashboardId dashboardId) {
+ restTemplate.delete(baseURL + "/api/dashboard/{dashboardId}", dashboardId.getId());
+ }
+
+ public Optional assignDashboardToCustomer(CustomerId customerId, DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboard = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/dashboard/{dashboardId}", null, Dashboard.class, customerId.getId(), dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional unassignDashboardFromCustomer(CustomerId customerId, DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboard = restTemplate.exchange(baseURL + "/api/customer/{customerId}/dashboard/{dashboardId}", HttpMethod.DELETE, HttpEntity.EMPTY, Dashboard.class, customerId.getId(), dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional updateDashboardCustomers(DashboardId dashboardId, List customerIds) {
+ Object[] customerIdArray = customerIds.stream().map(customerId -> customerId.getId().toString()).toArray();
+ try {
+ ResponseEntity dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers", customerIdArray, Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional addDashboardCustomers(DashboardId dashboardId, List customerIds) {
+ Object[] customerIdArray = customerIds.stream().map(customerId -> customerId.getId().toString()).toArray();
+ try {
+ ResponseEntity dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/add", customerIdArray, Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional removeDashboardCustomers(DashboardId dashboardId, List customerIds) {
+ Object[] customerIdArray = customerIds.stream().map(customerId -> customerId.getId().toString()).toArray();
+ try {
+ ResponseEntity dashboard = restTemplate.postForEntity(baseURL + "/api/dashboard/{dashboardId}/customers/remove", customerIdArray, Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional assignDashboardToPublicCustomer(DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboard = restTemplate.postForEntity(baseURL + "/api/customer/public/dashboard/{dashboardId}", null, Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional unassignDashboardFromPublicCustomer(DashboardId dashboardId) {
+ try {
+ ResponseEntity dashboard = restTemplate.exchange(baseURL + "/api/customer/public/dashboard/{dashboardId}", HttpMethod.DELETE, HttpEntity.EMPTY, Dashboard.class, dashboardId.getId());
+ return Optional.ofNullable(dashboard.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TextPageData getTenantDashboards(TenantId tenantId, TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("tenantId", tenantId.getId().toString());
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/tenant/{tenantId}/dashboards?" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public TextPageData getTenantDashboards(TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/tenant/dashboards?" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public TimePageData getCustomerDashboards(CustomerId customerId, TimePageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/customer/{customerId}/dashboards?" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ @Deprecated
+ public Dashboard createDashboard(Dashboard dashboard) {
+ return restTemplate.postForEntity(baseURL + "/api/dashboard", dashboard, Dashboard.class).getBody();
+ }
+
+ @Deprecated
+ public List findTenantDashboards() {
+ try {
+ ResponseEntity> dashboards =
+ restTemplate.exchange(baseURL + "/api/tenant/dashboards?limit=100000", HttpMethod.GET, null, new ParameterizedTypeReference>() {
+ });
+ return dashboards.getBody().getData();
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Collections.emptyList();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getDeviceById(DeviceId deviceId) {
+ try {
+ ResponseEntity device = restTemplate.getForEntity(baseURL + "/api/device/{deviceId}", Device.class, deviceId.getId());
+ return Optional.ofNullable(device.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Device saveDevice(Device device) {
+ return restTemplate.postForEntity(baseURL + "/api/device", device, Device.class).getBody();
+ }
+
+ public void deleteDevice(DeviceId deviceId) {
+ restTemplate.delete(baseURL + "/api/device/{deviceId}", deviceId.getId());
+ }
+
+ public Optional assignDeviceToCustomer(CustomerId customerId, DeviceId deviceId) {
+ try {
+ ResponseEntity device = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/device/{deviceId}", null, Device.class, customerId.getId(), deviceId.getId());
+ return Optional.ofNullable(device.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional unassignDeviceFromCustomer(DeviceId deviceId) {
+ try {
+ ResponseEntity device = restTemplate.exchange(baseURL + "/api/customer/device/{deviceId}", HttpMethod.DELETE, HttpEntity.EMPTY, Device.class, deviceId.getId());
+ return Optional.ofNullable(device.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional assignDeviceToPublicCustomer(DeviceId deviceId) {
+ try {
+ ResponseEntity device = restTemplate.postForEntity(baseURL + "/api/customer/public/device/{deviceId}", null, Device.class, deviceId.getId());
+ return Optional.ofNullable(device.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getDeviceCredentialsByDeviceId(DeviceId deviceId) {
+ try {
+ ResponseEntity deviceCredentials = restTemplate.getForEntity(baseURL + "/api/device/{deviceId}/credentials", DeviceCredentials.class, deviceId.getId());
+ return Optional.ofNullable(deviceCredentials.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public DeviceCredentials saveDeviceCredentials(DeviceCredentials deviceCredentials) {
+ return restTemplate.postForEntity(baseURL + "/api/device/credentials", deviceCredentials, DeviceCredentials.class).getBody();
+ }
+
+ public TextPageData getTenantDevices(String type, TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("type", type);
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/tenant/devices?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public Optional getTenantDevice(String deviceName) {
+ try {
+ ResponseEntity device = restTemplate.getForEntity(baseURL + "/api/tenant/devices?deviceName={deviceName}", Device.class, deviceName);
+ return Optional.ofNullable(device.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TextPageData getCustomerDevices(CustomerId customerId, String deviceType, TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ params.put("type", deviceType);
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/customer/{customerId}/devices?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET, HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public List getDevicesByIds(List deviceIds) {
+ return restTemplate.exchange(baseURL + "/api/devices?deviceIds={deviceIds}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY, new ParameterizedTypeReference>() {
+ }, listIdsToString(deviceIds)).getBody();
+ }
+
+ public List findByQuery(DeviceSearchQuery query) {
+ return restTemplate.exchange(
+ baseURL + "/api/devices",
+ HttpMethod.POST,
+ new HttpEntity<>(query),
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public List getDeviceTypes() {
+ return restTemplate.exchange(
+ baseURL + "/api/devices",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public JsonNode claimDevice(String deviceName, ClaimRequest claimRequest) {
+ return restTemplate.exchange(
+ baseURL + "/api/customer/device/{deviceName}/claim",
+ HttpMethod.POST,
+ new HttpEntity<>(claimRequest),
+ new ParameterizedTypeReference() {
+ }, deviceName).getBody();
+ }
+
+ public void reClaimDevice(String deviceName) {
+ restTemplate.delete(baseURL + "/api/customer/device/{deviceName}/claim", deviceName);
+ }
+
+ @Deprecated
+ public Device createDevice(String name, String type) {
+ Device device = new Device();
+ device.setName(name);
+ device.setType(type);
+ return doCreateDevice(device, null);
+ }
+
+ @Deprecated
+ public Device createDevice(Device device) {
+ return doCreateDevice(device, null);
+ }
+
+ @Deprecated
+ public Device createDevice(Device device, String accessToken) {
+ return doCreateDevice(device, accessToken);
+ }
+
+ @Deprecated
+ private Device doCreateDevice(Device device, String accessToken) {
+ Map params = new HashMap<>();
+ String deviceCreationUrl = "/api/device";
+ if (!StringUtils.isEmpty(accessToken)) {
+ deviceCreationUrl = deviceCreationUrl + "?accessToken={accessToken}";
+ params.put("accessToken", accessToken);
+ }
+ return restTemplate.postForEntity(baseURL + deviceCreationUrl, device, Device.class, params).getBody();
+ }
+
+ @Deprecated
+ public DeviceCredentials getCredentials(DeviceId id) {
+ return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody();
+ }
+
+ @Deprecated
+ public Optional findDevice(String name) {
+ Map params = new HashMap<>();
+ params.put("deviceName", name);
+ try {
+ ResponseEntity 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;
+ }
+ }
+ }
+
+ @Deprecated
+ public DeviceCredentials updateDeviceCredentials(DeviceId deviceId, String token) {
+ DeviceCredentials deviceCredentials = getCredentials(deviceId);
+ deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN);
+ deviceCredentials.setCredentialsId(token);
+ return saveDeviceCredentials(deviceCredentials);
+ }
+
+ @Deprecated
+ 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 void saveRelation(EntityRelation relation) {
+ restTemplate.postForLocation(baseURL + "/api/relation", relation);
+ }
+
+ public void deleteRelation(EntityId fromId, String relationType, RelationTypeGroup relationTypeGroup, EntityId toId) {
+ Map params = new HashMap<>();
+ params.put("fromId", fromId.getId().toString());
+ params.put("fromType", fromId.getEntityType().name());
+ params.put("relationType", relationType);
+ params.put("relationTypeGroup", relationTypeGroup.name());
+ params.put("toId", toId.getId().toString());
+ params.put("toType", toId.getEntityType().name());
+ restTemplate.delete(baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}", params);
+ }
+
+ public void deleteRelations(EntityId entityId) {
+ restTemplate.delete(baseURL + "/api/relations?entityId={entityId}&entityType={entityType}", entityId.getId().toString(), entityId.getEntityType().name());
+ }
+
+ public Optional getRelation(EntityId fromId, String relationType, RelationTypeGroup relationTypeGroup, EntityId toId) {
+ Map params = new HashMap<>();
+ params.put("fromId", fromId.getId().toString());
+ params.put("fromType", fromId.getEntityType().name());
+ params.put("relationType", relationType);
+ params.put("relationTypeGroup", relationTypeGroup.name());
+ params.put("toId", toId.getId().toString());
+ params.put("toType", toId.getEntityType().name());
+
+ try {
+ ResponseEntity entityRelation = restTemplate.getForEntity(
+ baseURL + "/api/relation?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}&toId={toId}&toType={toType}",
+ EntityRelation.class,
+ params);
+ return Optional.ofNullable(entityRelation.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public List findByFrom(EntityId fromId, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("fromId", fromId.getId().toString());
+ params.put("fromType", fromId.getEntityType().name());
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations?fromId={fromId}&fromType={fromType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findInfoByFrom(EntityId fromId, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("fromId", fromId.getId().toString());
+ params.put("fromType", fromId.getEntityType().name());
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations/info?fromId={fromId}&fromType={fromType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findByFrom(EntityId fromId, String relationType, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("fromId", fromId.getId().toString());
+ params.put("fromType", fromId.getEntityType().name());
+ params.put("relationType", relationType);
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations?fromId={fromId}&fromType={fromType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findByTo(EntityId toId, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("toId", toId.getId().toString());
+ params.put("toType", toId.getEntityType().name());
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations?toId={toId}&toType={toType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findInfoByTo(EntityId toId, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("toId", toId.getId().toString());
+ params.put("toType", toId.getEntityType().name());
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations?toId={toId}&toType={toType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findByTo(EntityId toId, String relationType, RelationTypeGroup relationTypeGroup) {
+ Map params = new HashMap<>();
+ params.put("toId", toId.getId().toString());
+ params.put("toType", toId.getEntityType().name());
+ params.put("relationType", relationType);
+ params.put("relationTypeGroup", relationTypeGroup.name());
+
+ return restTemplate.exchange(
+ baseURL + "/api/relations?toId={toId}&toType={toType}&relationType={relationType}&relationTypeGroup={relationTypeGroup}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public List findByQuery(EntityRelationsQuery query) {
+ return restTemplate.exchange(
+ baseURL + "/api/relations",
+ HttpMethod.POST,
+ new HttpEntity<>(query),
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public List findInfoByQuery(EntityRelationsQuery query) {
+ return restTemplate.exchange(
+ baseURL + "/api/relations",
+ HttpMethod.POST,
+ new HttpEntity<>(query),
+ new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ @Deprecated
+ 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 Optional getEntityViewById(EntityViewId entityViewId) {
+ try {
+ ResponseEntity entityView = restTemplate.getForEntity(baseURL + "/api/entityView/{entityViewId}", EntityView.class, entityViewId.getId());
+ return Optional.ofNullable(entityView.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public EntityView saveEntityView(EntityView entityView) {
+ return restTemplate.postForEntity(baseURL + "/api/entityView", entityView, EntityView.class).getBody();
+ }
+
+ public void deleteEntityView(EntityViewId entityViewId) {
+ restTemplate.delete(baseURL + "/api/entityView/{entityViewId}", entityViewId.getId());
+ }
+
+ public Optional getTenantEntityView(String entityViewName) {
+ try {
+ ResponseEntity entityView = restTemplate.getForEntity(baseURL + "/api/tenant/entityViews?entityViewName={entityViewName}", EntityView.class, entityViewName);
+ return Optional.ofNullable(entityView.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional assignEntityViewToCustomer(CustomerId customerId, EntityViewId entityViewId) {
+ try {
+ ResponseEntity entityView = restTemplate.postForEntity(baseURL + "/api/customer/{customerId}/entityView/{entityViewId}", null, EntityView.class, customerId.getId(), entityViewId.getId());
+ return Optional.ofNullable(entityView.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional unassignEntityViewFromCustomer(EntityViewId entityViewId) {
+ try {
+ ResponseEntity entityView = restTemplate.exchange(baseURL + "/api/customer/entityView/{entityViewId}", HttpMethod.DELETE, HttpEntity.EMPTY, EntityView.class, entityViewId.getId());
+ return Optional.ofNullable(entityView.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TextPageData getCustomerEntityViews(CustomerId customerId, String entityViewType, TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("customerId", customerId.getId().toString());
+ params.put("type", entityViewType);
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/customer/{customerId}/entityViews?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public TextPageData getTenantEntityViews(String entityViewType, TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("type", entityViewType);
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/tenant/entityViews?type={type}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ }, params).getBody();
+ }
+
+ public List findByQuery(EntityViewSearchQuery query) {
+ return restTemplate.exchange(baseURL + "/api/entityViews", HttpMethod.POST, new HttpEntity<>(query), new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public List getEntityViewTypes() {
+ return restTemplate.exchange(baseURL + "/api/entityView/types", HttpMethod.GET, HttpEntity.EMPTY, new ParameterizedTypeReference>() {
+ }).getBody();
+ }
+
+ public Optional assignEntityViewToPublicCustomer(EntityViewId entityViewId) {
+ try {
+ ResponseEntity entityView = restTemplate.postForEntity(baseURL + "/api/customer/public/entityView/{entityViewId}", null, EntityView.class, entityViewId.getId());
+ return Optional.ofNullable(entityView.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public TimePageData getEvents(EntityId entityId, String eventType, TenantId tenantId, TimePageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("entityType", entityId.getEntityType().name());
+ params.put("entityId", entityId.getId().toString());
+ params.put("eventType", eventType);
+ params.put("tenantId", tenantId.getId().toString());
+ addPageLinkToParam(params, pageLink);
+
+ return restTemplate.exchange(
+ baseURL + "/api/events/{entityType}/{entityId}/{eventType}?tenantId={tenantId}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public TimePageData getEvents(EntityId entityId, TenantId tenantId, TimePageLink pageLink) {
+ Map params = new HashMap<>();
+ params.put("entityType", entityId.getEntityType().name());
+ params.put("entityId", entityId.getId().toString());
+ params.put("tenantId", tenantId.getId().toString());
+ addPageLinkToParam(params, pageLink);
+
+ return restTemplate.exchange(
+ baseURL + "/api/events/{entityType}/{entityId}?tenantId={tenantId}&" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public void handleOneWayDeviceRPCRequest(DeviceId deviceId, JsonNode requestBody) {
+ restTemplate.postForLocation(baseURL + "/api/plugins/rpc/oneway/{deviceId}", requestBody, deviceId.getId());
+ }
+
+ public JsonNode handleTwoWayDeviceRPCRequest(DeviceId deviceId, JsonNode requestBody) {
+ return restTemplate.exchange(
+ baseURL + "/api/plugins/rpc/twoway/{deviceId}",
+ HttpMethod.POST,
+ new HttpEntity<>(requestBody),
+ new ParameterizedTypeReference() {
+ },
+ deviceId.getId()).getBody();
+ }
+
+ public Optional getRuleChainById(RuleChainId ruleChainId) {
+ try {
+ ResponseEntity ruleChain = restTemplate.getForEntity(baseURL + "/api/ruleChain/{ruleChainId}", RuleChain.class, ruleChainId.getId());
+ return Optional.ofNullable(ruleChain.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional getRuleChainMetaData(RuleChainId ruleChainId) {
+ try {
+ ResponseEntity ruleChainMetaData = restTemplate.getForEntity(baseURL + "/api/ruleChain/{ruleChainId}/metadata", RuleChainMetaData.class, ruleChainId.getId());
+ return Optional.ofNullable(ruleChainMetaData.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public RuleChain saveRuleChain(RuleChain ruleChain) {
+ return restTemplate.postForEntity(baseURL + "/api/ruleChain", ruleChain, RuleChain.class).getBody();
+ }
+
+ public Optional setRootRuleChain(RuleChainId ruleChainId) {
+ try {
+ ResponseEntity ruleChain = restTemplate.postForEntity(baseURL + "/api/ruleChain/{ruleChainId}/root", null, RuleChain.class, ruleChainId.getId());
+ return Optional.ofNullable(ruleChain.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public RuleChainMetaData saveRuleChainMetaData(RuleChainMetaData ruleChainMetaData) {
+ return restTemplate.postForEntity(baseURL + "/api/ruleChain/metadata", ruleChainMetaData, RuleChainMetaData.class).getBody();
+ }
+
+ public TextPageData getRuleChains(TextPageLink pageLink) {
+ Map params = new HashMap<>();
+ addPageLinkToParam(params, pageLink);
+ return restTemplate.exchange(
+ baseURL + "/api/ruleChains?" + getUrlParams(pageLink),
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ params).getBody();
+ }
+
+ public void deleteRuleChain(RuleChainId ruleChainId) {
+ restTemplate.delete(baseURL + "/api/ruleChain/{ruleChainId}", ruleChainId.getId());
+ }
+
+ public Optional getLatestRuleNodeDebugInput(RuleNodeId ruleNodeId) {
+ try {
+ ResponseEntity jsonNode = restTemplate.getForEntity(baseURL + "/api/ruleNode/{ruleNodeId}/debugIn", JsonNode.class, ruleNodeId.getId());
+ return Optional.ofNullable(jsonNode.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public Optional testScript(JsonNode inputParams) {
+ try {
+ ResponseEntity jsonNode = restTemplate.postForEntity(baseURL + "/api/ruleChain/testScript", inputParams, JsonNode.class);
+ return Optional.ofNullable(jsonNode.getBody());
+ } catch (HttpClientErrorException exception) {
+ if (exception.getStatusCode() == HttpStatus.NOT_FOUND) {
+ return Optional.empty();
+ } else {
+ throw exception;
+ }
+ }
+ }
+
+ public List getAttributeKeys(EntityId entityId) {
+ return restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ entityId.getEntityType().name(),
+ entityId.getId().toString()).getBody();
+ }
+
+ public List getAttributeKeysByScope(EntityId entityId, String scope) {
+ return restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/attributes/{scope}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ entityId.getEntityType().name(),
+ entityId.getId().toString(),
+ scope).getBody();
+ }
+
+ public List getAttributeKvEntries(EntityId entityId, List keys) {
+ List attributes = restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes?keys={keys}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ entityId.getEntityType().name(),
+ entityId.getId(),
+ listToString(keys)).getBody();
+
+ return RestJsonConverter.toAttributes(attributes);
+ }
+
+ public Future> getAttributeKvEntriesAsync(EntityId entityId, List keys) {
+ return service.submit(() -> getAttributeKvEntries(entityId, keys));
+ }
+
+ public List getAttributesByScope(EntityId entityId, String scope, List keys) {
+ List attributes = restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/attributes/{scope}?keys={keys}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ entityId.getEntityType().name(),
+ entityId.getId().toString(),
+ scope,
+ listToString(keys)).getBody();
+
+ return RestJsonConverter.toAttributes(attributes);
+ }
+
+ public List getTimeseriesKeys(EntityId entityId) {
+ return restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/keys/timeseries",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference>() {
+ },
+ entityId.getEntityType().name(),
+ entityId.getId().toString()).getBody();
+ }
+
+ public List getLatestTimeseries(EntityId entityId, List keys) {
+ return getLatestTimeseries(entityId, keys, true);
+ }
+
+ public List getLatestTimeseries(EntityId entityId, List keys, boolean useStrictDataTypes) {
+ Map> timeseries = restTemplate.exchange(
+ baseURL + "/api/plugins/telemetry/{entityType}/{entityId}/values/timeseries?keys={keys}&useStrictDataTypes={useStrictDataTypes}",
+ HttpMethod.GET,
+ HttpEntity.EMPTY,
+ new ParameterizedTypeReference