From 2d27b444a5d24ed9f9fcb3b66579501e93e9b0cd Mon Sep 17 00:00:00 2001 From: Andrew Shvayka Date: Thu, 2 Feb 2017 12:04:11 +0200 Subject: [PATCH] Rest API call to get device by tenant id and name --- .../server/controller/DeviceController.java | 13 +++++++++++++ .../thingsboard/client/tools/RestClient.java | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/application/src/main/java/org/thingsboard/server/controller/DeviceController.java b/application/src/main/java/org/thingsboard/server/controller/DeviceController.java index ff06ce7bf4..57433214ce 100644 --- a/application/src/main/java/org/thingsboard/server/controller/DeviceController.java +++ b/application/src/main/java/org/thingsboard/server/controller/DeviceController.java @@ -156,6 +156,19 @@ public class DeviceController extends BaseController { } } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/tenant/devices", params = {"deviceName"}, method = RequestMethod.GET) + @ResponseBody + public Device getTenantDevice( + @RequestParam String deviceName) throws ThingsboardException { + try { + TenantId tenantId = getCurrentUser().getTenantId(); + return checkNotNull(deviceService.findDeviceByTenantIdAndName(tenantId, deviceName)); + } catch (Exception e) { + throw handleException(e); + } + } + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')") @RequestMapping(value = "/customer/{customerId}/devices", params = {"limit"}, method = RequestMethod.GET) @ResponseBody diff --git a/tools/src/main/java/org/thingsboard/client/tools/RestClient.java b/tools/src/main/java/org/thingsboard/client/tools/RestClient.java index e3e1793f30..f742e6131e 100644 --- a/tools/src/main/java/org/thingsboard/client/tools/RestClient.java +++ b/tools/src/main/java/org/thingsboard/client/tools/RestClient.java @@ -18,11 +18,13 @@ 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.Device; import org.thingsboard.server.common.data.id.DeviceId; @@ -32,6 +34,7 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; /** * @author Andrew Shvayka @@ -52,6 +55,21 @@ public class RestClient implements ClientHttpRequestInterceptor { restTemplate.setInterceptors(Collections.singletonList(this)); } + 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; + } + } + } + public Device createDevice(String name) { Device device = new Device(); device.setName(name);