Commit 5d64a27079861d4fd0389e2ae682392fa95886fe
Merge branch 'master' of github.com:thingsboard/thingsboard
Showing
1 changed file
with
37 additions
and
23 deletions
... | ... | @@ -16,6 +16,8 @@ |
16 | 16 | package org.thingsboard.client.tools; |
17 | 17 | |
18 | 18 | import com.fasterxml.jackson.databind.JsonNode; |
19 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
20 | +import com.fasterxml.jackson.databind.node.ObjectNode; | |
19 | 21 | import lombok.RequiredArgsConstructor; |
20 | 22 | import org.springframework.core.ParameterizedTypeReference; |
21 | 23 | import org.springframework.http.HttpEntity; |
... | ... | @@ -27,6 +29,7 @@ import org.springframework.http.client.ClientHttpRequestExecution; |
27 | 29 | import org.springframework.http.client.ClientHttpRequestInterceptor; |
28 | 30 | import org.springframework.http.client.ClientHttpResponse; |
29 | 31 | import org.springframework.http.client.support.HttpRequestWrapper; |
32 | +import org.springframework.util.StringUtils; | |
30 | 33 | import org.springframework.web.client.HttpClientErrorException; |
31 | 34 | import org.springframework.web.client.RestTemplate; |
32 | 35 | import org.springframework.web.context.request.async.DeferredResult; |
... | ... | @@ -90,9 +93,11 @@ public class RestClient implements ClientHttpRequestInterceptor { |
90 | 93 | protected final String baseURL; |
91 | 94 | private String token; |
92 | 95 | private String refreshToken; |
96 | + private final ObjectMapper objectMapper = new ObjectMapper(); | |
93 | 97 | |
94 | 98 | private final static String TIME_PAGE_LINK_URL_PARAMS = "limit={limit}&startTime={startTime}&endTime={endTime}&ascOrder={ascOrder}&offset={offset}"; |
95 | 99 | private final static String TEXT_PAGE_LINK_URL_PARAMS = "limit={limit}&textSearch{textSearch}&idOffset={idOffset}&textOffset{textOffset}"; |
100 | + protected static final String ACTIVATE_TOKEN_REGEX = "/api/noauth/activate?activateToken="; | |
96 | 101 | |
97 | 102 | @Override |
98 | 103 | public ClientHttpResponse intercept(HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException { |
... | ... | @@ -110,6 +115,14 @@ public class RestClient implements ClientHttpRequestInterceptor { |
110 | 115 | return response; |
111 | 116 | } |
112 | 117 | |
118 | + public String getToken() { | |
119 | + return token; | |
120 | + } | |
121 | + | |
122 | + public String getRefreshToken() { | |
123 | + return refreshToken; | |
124 | + } | |
125 | + | |
113 | 126 | public void refreshToken() { |
114 | 127 | Map<String, String> refreshTokenRequest = new HashMap<>(); |
115 | 128 | refreshTokenRequest.put("refreshToken", refreshToken); |
... | ... | @@ -297,10 +310,6 @@ public class RestClient implements ClientHttpRequestInterceptor { |
297 | 310 | return restTemplate; |
298 | 311 | } |
299 | 312 | |
300 | - public String getToken() { | |
301 | - return token; | |
302 | - } | |
303 | - | |
304 | 313 | public Optional<AdminSettings> getAdminSettings(String key) { |
305 | 314 | try { |
306 | 315 | ResponseEntity<AdminSettings> adminSettings = restTemplate.getForEntity(baseURL + "/api/admin/settings/{key}", AdminSettings.class, key); |
... | ... | @@ -629,6 +638,11 @@ public class RestClient implements ClientHttpRequestInterceptor { |
629 | 638 | return auditLog.getBody(); |
630 | 639 | } |
631 | 640 | |
641 | + public String getActivateToken(String userId) { | |
642 | + String activationLink = getActivationLink(userId); | |
643 | + return StringUtils.delete(activationLink, baseURL + ACTIVATE_TOKEN_REGEX); | |
644 | + } | |
645 | + | |
632 | 646 | public Optional<User> getUser() { |
633 | 647 | ResponseEntity<User> user = restTemplate.getForEntity(baseURL + "/api/auth/user", User.class); |
634 | 648 | return Optional.ofNullable(user.getBody()); |
... | ... | @@ -638,7 +652,10 @@ public class RestClient implements ClientHttpRequestInterceptor { |
638 | 652 | restTemplate.exchange(URI.create(baseURL + "/api/auth/logout"), HttpMethod.POST, HttpEntity.EMPTY, Object.class); |
639 | 653 | } |
640 | 654 | |
641 | - public void changePassword(JsonNode changePasswordRequest) { | |
655 | + public void changePassword(String currentPassword, String newPassword) { | |
656 | + ObjectNode changePasswordRequest = objectMapper.createObjectNode(); | |
657 | + changePasswordRequest.put("currentPassword", currentPassword); | |
658 | + changePasswordRequest.put("newPassword", newPassword); | |
642 | 659 | restTemplate.exchange(URI.create(baseURL + "/api/auth/changePassword"), HttpMethod.POST, new HttpEntity<>(changePasswordRequest), Object.class); |
643 | 660 | } |
644 | 661 | |
... | ... | @@ -655,12 +672,13 @@ public class RestClient implements ClientHttpRequestInterceptor { |
655 | 672 | } |
656 | 673 | } |
657 | 674 | |
658 | - | |
659 | 675 | public ResponseEntity<String> checkActivateToken(String activateToken) { |
660 | 676 | return restTemplate.getForEntity(baseURL + "/api/noauth/activate?activateToken={activateToken}", String.class, activateToken); |
661 | 677 | } |
662 | 678 | |
663 | - public void requestResetPasswordByEmail(JsonNode resetPasswordByEmailRequest) { | |
679 | + public void requestResetPasswordByEmail(String email) { | |
680 | + ObjectNode resetPasswordByEmailRequest = objectMapper.createObjectNode(); | |
681 | + resetPasswordByEmailRequest.put("email", email); | |
664 | 682 | restTemplate.exchange(URI.create(baseURL + "/api/noauth/resetPasswordByEmail"), HttpMethod.POST, new HttpEntity<>(resetPasswordByEmailRequest), Object.class); |
665 | 683 | } |
666 | 684 | |
... | ... | @@ -668,7 +686,10 @@ public class RestClient implements ClientHttpRequestInterceptor { |
668 | 686 | return restTemplate.getForEntity(baseURL + "/api/noauth/resetPassword?resetToken={resetToken}", String.class, resetToken); |
669 | 687 | } |
670 | 688 | |
671 | - public Optional<JsonNode> activateUser(JsonNode activateRequest) { | |
689 | + public Optional<JsonNode> activateUser(String userId, String password) { | |
690 | + ObjectNode activateRequest = objectMapper.createObjectNode(); | |
691 | + activateRequest.put("activateToken", getActivateToken(userId)); | |
692 | + activateRequest.put("password", password); | |
672 | 693 | try { |
673 | 694 | ResponseEntity<JsonNode> jsonNode = restTemplate.postForEntity(baseURL + "/api/noauth/activate", activateRequest, JsonNode.class); |
674 | 695 | return Optional.ofNullable(jsonNode.getBody()); |
... | ... | @@ -681,7 +702,10 @@ public class RestClient implements ClientHttpRequestInterceptor { |
681 | 702 | } |
682 | 703 | } |
683 | 704 | |
684 | - public Optional<JsonNode> resetPassword(JsonNode resetPasswordRequest) { | |
705 | + public Optional<JsonNode> resetPassword(String resetToken, String resetPassword) { | |
706 | + ObjectNode resetPasswordRequest = objectMapper.createObjectNode(); | |
707 | + resetPasswordRequest.put("resetToken", resetToken); | |
708 | + resetPasswordRequest.put("resetPassword", resetPassword); | |
685 | 709 | try { |
686 | 710 | ResponseEntity<JsonNode> jsonNode = restTemplate.postForEntity(baseURL + "/api/noauth/resetPassword", resetPasswordRequest, JsonNode.class); |
687 | 711 | return Optional.ofNullable(jsonNode.getBody()); |
... | ... | @@ -696,7 +720,7 @@ public class RestClient implements ClientHttpRequestInterceptor { |
696 | 720 | |
697 | 721 | public Optional<ComponentDescriptor> getComponentDescriptorByClazz(String componentDescriptorClazz) { |
698 | 722 | try { |
699 | - ResponseEntity<ComponentDescriptor> componentDescriptor = restTemplate.getForEntity(baseURL + "/api/component/{componentDescriptorClazz}", ComponentDescriptor.class); | |
723 | + ResponseEntity<ComponentDescriptor> componentDescriptor = restTemplate.getForEntity(baseURL + "/api/component/{componentDescriptorClazz}", ComponentDescriptor.class, componentDescriptorClazz); | |
700 | 724 | return Optional.ofNullable(componentDescriptor.getBody()); |
701 | 725 | } catch (HttpClientErrorException exception) { |
702 | 726 | if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { |
... | ... | @@ -1807,31 +1831,21 @@ public class RestClient implements ClientHttpRequestInterceptor { |
1807 | 1831 | restTemplate.postForEntity(baseURL + "/api/user/sendActivationMail?email={email}", null, Object.class, email); |
1808 | 1832 | } |
1809 | 1833 | |
1810 | - public Optional<String> getActivationLink(String userId) { | |
1811 | - try { | |
1812 | - ResponseEntity<String> activationLink = restTemplate.getForEntity(baseURL + "/api/user/{userId}/activationLink", String.class, userId); | |
1813 | - return Optional.ofNullable(activationLink.getBody()); | |
1814 | - } catch (HttpClientErrorException exception) { | |
1815 | - if (exception.getStatusCode() == HttpStatus.NOT_FOUND) { | |
1816 | - return Optional.empty(); | |
1817 | - } else { | |
1818 | - throw exception; | |
1819 | - } | |
1820 | - } | |
1834 | + public String getActivationLink(String userId) { | |
1835 | + return restTemplate.getForEntity(baseURL + "/api/user/{userId}/activationLink", String.class, userId).getBody(); | |
1821 | 1836 | } |
1822 | 1837 | |
1823 | 1838 | public void deleteUser(String userId) { |
1824 | 1839 | restTemplate.delete(baseURL + "/api/user/{userId}", userId); |
1825 | 1840 | } |
1826 | 1841 | |
1827 | - // @RequestMapping(value = "/tenant/{tenantId}/users", params = {"limit"}, method = RequestMethod.GET) | |
1828 | 1842 | public TextPageData<User> getTenantAdmins(String tenantId, TextPageLink pageLink) { |
1829 | 1843 | Map<String, String> params = new HashMap<>(); |
1830 | 1844 | params.put("tenantId", tenantId); |
1831 | 1845 | addPageLinkToParam(params, pageLink); |
1832 | 1846 | |
1833 | 1847 | return restTemplate.exchange( |
1834 | - baseURL + "/tenant/{tenantId}/users?" + TEXT_PAGE_LINK_URL_PARAMS, | |
1848 | + baseURL + "/api/tenant/{tenantId}/users?" + TEXT_PAGE_LINK_URL_PARAMS, | |
1835 | 1849 | HttpMethod.GET, |
1836 | 1850 | HttpEntity.EMPTY, |
1837 | 1851 | new ParameterizedTypeReference<TextPageData<User>>() { | ... | ... |