From af7c811d0d3811aee3440fc5b6b33821a8c7cd90 Mon Sep 17 00:00:00 2001 From: Posi-Paka Date: Mon, 25 Dec 2017 14:59:53 -0800 Subject: [PATCH 1/3] Removal of Joda-Time library. --- application/pom.xml | 4 ---- .../security/model/token/JwtTokenFactory.java | 19 ++++++++++--------- pom.xml | 8 +------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/application/pom.xml b/application/pom.xml index c7d3240ef4..e5a4a4e614 100644 --- a/application/pom.xml +++ b/application/pom.xml @@ -125,10 +125,6 @@ io.jsonwebtoken jjwt - - joda-time - joda-time - org.apache.velocity velocity diff --git a/application/src/main/java/org/thingsboard/server/service/security/model/token/JwtTokenFactory.java b/application/src/main/java/org/thingsboard/server/service/security/model/token/JwtTokenFactory.java index 20de6d8e73..7e5fba5477 100644 --- a/application/src/main/java/org/thingsboard/server/service/security/model/token/JwtTokenFactory.java +++ b/application/src/main/java/org/thingsboard/server/service/security/model/token/JwtTokenFactory.java @@ -20,7 +20,6 @@ import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.apache.commons.lang3.StringUtils; -import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.thingsboard.server.common.data.id.CustomerId; @@ -31,7 +30,9 @@ import org.thingsboard.server.config.JwtSettings; import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.UserPrincipal; -import java.util.Arrays; +import java.time.ZonedDateTime; +import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @@ -81,13 +82,13 @@ public class JwtTokenFactory { claims.put(CUSTOMER_ID, securityUser.getCustomerId().getId().toString()); } - DateTime currentTime = new DateTime(); + ZonedDateTime currentTime = ZonedDateTime.now(); String token = Jwts.builder() .setClaims(claims) .setIssuer(settings.getTokenIssuer()) - .setIssuedAt(currentTime.toDate()) - .setExpiration(currentTime.plusSeconds(settings.getTokenExpirationTime()).toDate()) + .setIssuedAt(Date.from(currentTime.toInstant())) + .setExpiration(Date.from(currentTime.plusSeconds(settings.getTokenExpirationTime()).toInstant())) .signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()) .compact(); @@ -129,11 +130,11 @@ public class JwtTokenFactory { throw new IllegalArgumentException("Cannot create JWT Token without username/email"); } - DateTime currentTime = new DateTime(); + ZonedDateTime currentTime = ZonedDateTime.now(); UserPrincipal principal = securityUser.getUserPrincipal(); Claims claims = Jwts.claims().setSubject(principal.getValue()); - claims.put(SCOPES, Arrays.asList(Authority.REFRESH_TOKEN.name())); + claims.put(SCOPES, Collections.singletonList(Authority.REFRESH_TOKEN.name())); claims.put(USER_ID, securityUser.getId().getId().toString()); claims.put(IS_PUBLIC, principal.getType() == UserPrincipal.Type.PUBLIC_ID); @@ -141,8 +142,8 @@ public class JwtTokenFactory { .setClaims(claims) .setIssuer(settings.getTokenIssuer()) .setId(UUID.randomUUID().toString()) - .setIssuedAt(currentTime.toDate()) - .setExpiration(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toDate()) + .setIssuedAt(Date.from(currentTime.toInstant())) + .setExpiration(Date.from(currentTime.plusSeconds(settings.getRefreshTokenExpTime()).toInstant())) .signWith(SignatureAlgorithm.HS512, settings.getTokenSigningKey()) .compact(); diff --git a/pom.xml b/pom.xml index e45fc3ea28..4b77abbd98 100755 --- a/pom.xml +++ b/pom.xml @@ -33,8 +33,7 @@ 4.3.4.RELEASE 4.2.0.RELEASE 0.7.0 - 2.4 - 2.2.0 + 2.2.0 4.12 1.7.7 1.2.3 @@ -483,11 +482,6 @@ jjwt ${jjwt.version} - - joda-time - joda-time - ${joda-time.version} - org.apache.velocity velocity From f67a192e2bdb5eb99a91cb4425b4c301653cb71c Mon Sep 17 00:00:00 2001 From: Dima Landiak Date: Fri, 29 Dec 2017 10:42:56 +0200 Subject: [PATCH 2/3] rest client update --- .../server/controller/CustomerController.java | 16 ++++++++++++++-- .../server/dao/customer/CustomerService.java | 8 ++++++-- .../dao/customer/CustomerServiceImpl.java | 8 ++++++++ .../thingsboard/client/tools/RestClient.java | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java index c54973f0bc..d87ea5cfe0 100644 --- a/application/src/main/java/org/thingsboard/server/controller/CustomerController.java +++ b/application/src/main/java/org/thingsboard/server/controller/CustomerController.java @@ -82,7 +82,7 @@ public class CustomerController extends BaseController { @PreAuthorize("hasAuthority('TENANT_ADMIN')") @RequestMapping(value = "/customer", method = RequestMethod.POST) - @ResponseBody + @ResponseBody public Customer saveCustomer(@RequestBody Customer customer) throws ThingsboardException { try { customer.setTenantId(getCurrentUser().getTenantId()); @@ -107,7 +107,7 @@ public class CustomerController extends BaseController { } @PreAuthorize("hasAuthority('TENANT_ADMIN')") - @RequestMapping(value = "/customers", params = { "limit" }, method = RequestMethod.GET) + @RequestMapping(value = "/customers", params = {"limit"}, method = RequestMethod.GET) @ResponseBody public TextPageData getCustomers(@RequestParam int limit, @RequestParam(required = false) String textSearch, @@ -122,4 +122,16 @@ public class CustomerController extends BaseController { } } + @PreAuthorize("hasAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/tenant/customers", params = {"customerTitle"}, method = RequestMethod.GET) + @ResponseBody + public Customer getTenantCustomer( + @RequestParam String customerTitle) throws ThingsboardException { + try { + TenantId tenantId = getCurrentUser().getTenantId(); + return checkNotNull(customerService.findCustomerByTenantIdAndTitle(tenantId, customerTitle)); + } catch (Exception e) { + throw handleException(e); + } + } } diff --git a/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java b/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java index 4600d9f048..1d1abda11f 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerService.java @@ -22,20 +22,24 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.page.TextPageData; import org.thingsboard.server.common.data.page.TextPageLink; +import java.util.Optional; + public interface CustomerService { Customer findCustomerById(CustomerId customerId); + Optional findCustomerByTenantIdAndTitle(TenantId tenantId, String title); + ListenableFuture findCustomerByIdAsync(CustomerId customerId); Customer saveCustomer(Customer customer); - + void deleteCustomer(CustomerId customerId); Customer findOrCreatePublicCustomer(TenantId tenantId); TextPageData findCustomersByTenantId(TenantId tenantId, TextPageLink pageLink); - + void deleteCustomersByTenantId(TenantId tenantId); } diff --git a/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java index e8ec21f3c0..f76d654bf9 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java +++ b/dao/src/main/java/org/thingsboard/server/dao/customer/CustomerServiceImpl.java @@ -52,6 +52,7 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom private static final String PUBLIC_CUSTOMER_TITLE = "Public"; public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId "; + public static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; @Autowired private CustomerDao customerDao; @@ -78,6 +79,13 @@ public class CustomerServiceImpl extends AbstractEntityService implements Custom return customerDao.findById(customerId.getId()); } + @Override + public Optional findCustomerByTenantIdAndTitle(TenantId tenantId, String title) { + log.trace("Executing findCustomerByTenantIdAndTitle [{}] [{}]", tenantId, title); + validateId(tenantId, INCORRECT_TENANT_ID + tenantId); + return customerDao.findCustomersByTenantIdAndTitle(tenantId.getId(), title); + } + @Override public ListenableFuture findCustomerByIdAsync(CustomerId customerId) { log.trace("Executing findCustomerByIdAsync [{}]", customerId); 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 8ef51258ed..e2a8e066f1 100644 --- a/tools/src/main/java/org/thingsboard/client/tools/RestClient.java +++ b/tools/src/main/java/org/thingsboard/client/tools/RestClient.java @@ -77,6 +77,21 @@ public class RestClient implements ClientHttpRequestInterceptor { } } + 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; + } + } + } + public Customer createCustomer(String title) { Customer customer = new Customer(); customer.setTitle(title); @@ -123,6 +138,10 @@ public class RestClient implements ClientHttpRequestInterceptor { return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody(); } + public Customer getCustomerByTitle(String title) { + return restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle=" + title, Customer.class).getBody(); + } + public RestTemplate getRestTemplate() { return restTemplate; } From 0aba2fcfe6dac3f0c5c6ffa386db5d8fddc3a5f3 Mon Sep 17 00:00:00 2001 From: Dima Landiak Date: Fri, 29 Dec 2017 11:07:43 +0200 Subject: [PATCH 3/3] fix rest client --- .../thingsboard/client/tools/RestClient.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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 e2a8e066f1..0baf1db508 100644 --- a/tools/src/main/java/org/thingsboard/client/tools/RestClient.java +++ b/tools/src/main/java/org/thingsboard/client/tools/RestClient.java @@ -77,6 +77,21 @@ public class RestClient implements ClientHttpRequestInterceptor { } } + 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; + } + } + } + public Optional findAsset(String name) { Map params = new HashMap(); params.put("assetName", name); @@ -138,10 +153,6 @@ public class RestClient implements ClientHttpRequestInterceptor { return restTemplate.getForEntity(baseURL + "/api/device/" + id.getId().toString() + "/credentials", DeviceCredentials.class).getBody(); } - public Customer getCustomerByTitle(String title) { - return restTemplate.getForEntity(baseURL + "/api/tenant/customers?customerTitle=" + title, Customer.class).getBody(); - } - public RestTemplate getRestTemplate() { return restTemplate; }