From 660935e0d69a54d2cd8b4aa379f5cfbb70c90b4a Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 14 May 2020 23:37:12 +0530 Subject: [PATCH 1/3] Improve bean structure in policy UI config --- .../ui/{ConditionList.java => Condition.java} | 22 +++++---- .../mgt/common/policy/mgt/ui/Content.java | 9 ---- .../mgt/common/policy/mgt/ui/Input.java | 25 +++------- .../device/mgt/common/policy/mgt/ui/Item.java | 20 ++++---- .../device/mgt/common/policy/mgt/ui/Key.java | 46 ------------------- .../policy/mgt/ui/NotificationList.java | 37 --------------- .../device/mgt/common/policy/mgt/ui/Rule.java | 40 ++++++++++++++++ .../mgt/common/policy/mgt/ui/SubContent.java | 13 +++--- 8 files changed, 79 insertions(+), 133 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/{ConditionList.java => Condition.java} (66%) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Key.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/NotificationList.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Rule.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/ConditionList.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Condition.java similarity index 66% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/ConditionList.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Condition.java index 31015d0c3f0..a70f47039e4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/ConditionList.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Condition.java @@ -18,20 +18,24 @@ package org.wso2.carbon.device.mgt.common.policy.mgt.ui; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import java.util.List; -@XmlRootElement(name = "ConditionList") -public class ConditionList { +@XmlRootElement(name = "Condition") +public class Condition { - List conditions; + String key; + List values; @XmlElement(name = "Key") - public List getConditions() { - return conditions; - } + public String getKey() { return key; } - public void setConditions(List conditions) { - this.conditions = conditions; - } + public void setKey(String key) { this.key = key; } + + @XmlElementWrapper(name = "Values") + @XmlElement(name = "Value") + public List getValues() { return values; } + + public void setValues(List values) { this.values = values; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Content.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Content.java index 77de28d8f26..5b950b22317 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Content.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Content.java @@ -29,7 +29,6 @@ public class Content { private String key; private List items; private List subContents; - private ConditionList conditionList; @XmlAttribute(name = "key", required = true) public String getKey() { @@ -56,12 +55,4 @@ public class Content { public void setSubContents(List subContents) { this.subContents = subContents; } - @XmlElement(name = "ConditionList") - public ConditionList getConditionList() { - return conditionList; - } - - public void setConditionList(ConditionList conditionList) { - this.conditionList = conditionList; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Input.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Input.java index 664a5299b23..6aaa54a8e34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Input.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Input.java @@ -18,15 +18,16 @@ package org.wso2.carbon.device.mgt.common.policy.mgt.ui; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; @XmlRootElement(name = "Input") public class Input { private String type; private String placeholderValue; - private String regEx; - private String validationMessage; + private List rules; @XmlElement(name = "Type") public String getType() { @@ -46,21 +47,9 @@ public class Input { this.placeholderValue = placeholderValue; } - @XmlElement(name = "Regex") - public String getRegEx() { - return regEx; - } - - public void setRegEx(String regEx) { - this.regEx = regEx; - } + @XmlElementWrapper(name = "Rules") + @XmlElement(name = "Rule") + public List getRules() { return rules; } - @XmlElement(name = "ValidationMsg") - public String getValidationMessage() { - return validationMessage; - } - - public void setValidationMessage(String validationMessage) { - this.validationMessage = validationMessage; - } + public void setRules(List rules) { this.rules = rules; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Item.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Item.java index fd905ff0af3..d760d6ccc8c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Item.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Item.java @@ -18,7 +18,9 @@ package org.wso2.carbon.device.mgt.common.policy.mgt.ui; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; @XmlRootElement(name = "Item") public class Item { @@ -29,14 +31,14 @@ public class Item { private String value; private boolean isRequired; private String subTitle; - private ConditionList conditionList; + private List conditions; private Checkbox checkbox; private Select select; private Input input; private TimeSelector timeSelector; private Table table; private RadioGroup radioGroup; - private NotificationList notificationList; + private List notifications; @XmlElement(name = "Label") public String getLabel() { @@ -88,10 +90,11 @@ public class Item { public void setSubTitle(String subTitle) { this.subTitle = subTitle; } - @XmlElement(name = "ConditionList") - public ConditionList getConditionList() { return conditionList; } + @XmlElementWrapper(name = "Conditions") + @XmlElement(name = "Condition") + public List getConditions() { return conditions; } - public void setConditionList(ConditionList conditionList) { this.conditionList = conditionList; } + public void setConditions(List conditions) { this.conditions = conditions; } @XmlElement(name = "Checkbox") public Checkbox getCheckbox() { @@ -139,8 +142,9 @@ public class Item { this.radioGroup = radioGroup; } - @XmlElement(name = "NotificationList") - public NotificationList getNotificationList() { return notificationList; } + @XmlElementWrapper(name = "Notifications") + @XmlElement(name = "Notification") + public List getNotifications() { return notifications; } - public void setNotificationList(NotificationList notificationList) { this.notificationList = notificationList; } + public void setNotifications(List notifications) { this.notifications = notifications; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Key.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Key.java deleted file mode 100644 index 303d634d26f..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Key.java +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. licenses this file to you 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.wso2.carbon.device.mgt.common.policy.mgt.ui; - -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "Key") -public class Key { - - private String name; - private String value; - - @XmlAttribute(name = "name") - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @XmlAttribute(name = "value") - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/NotificationList.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/NotificationList.java deleted file mode 100644 index cc63fe4a406..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/NotificationList.java +++ /dev/null @@ -1,37 +0,0 @@ -/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. licenses this file to you 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.wso2.carbon.device.mgt.common.policy.mgt.ui; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.List; - -@XmlRootElement(name = "NotificationList") -public class NotificationList { - - List notification; - - @XmlElement(name = "Notification") - public List getConditions() { - return notification; - } - - public void setConditions(List notification) { - this.notification = notification; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Rule.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Rule.java new file mode 100644 index 00000000000..5a8df96bd10 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/Rule.java @@ -0,0 +1,40 @@ +package org.wso2.carbon.device.mgt.common.policy.mgt.ui;/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you 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. + */ + +import javax.xml.bind.annotation.XmlElement; + +public class Rule { + + private String type; + private String logic; + private String validationMessage; + + @XmlElement(name = "Type") + public String getType() { return type; } + + public void setType(String type) { this.type = type; } + + @XmlElement(name = "Logic") + public String getLogic() { return logic; } + + public void setLogic(String logic) { this.logic = logic; } + + @XmlElement(name = "ValidationMsg") + public String getValidationMessage() { return validationMessage; } + + public void setValidationMessage(String validationMessage) { this.validationMessage = validationMessage; } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/SubContent.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/SubContent.java index b3981ee7d47..c9eba46873f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/SubContent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ui/SubContent.java @@ -28,7 +28,7 @@ public class SubContent { private String key; private List items; - private ConditionList conditionList; + private List conditions; private List subContents; @XmlAttribute(name = "key", required = true) @@ -50,13 +50,14 @@ public class SubContent { this.items = items; } - @XmlElement(name = "ConditionList") - public ConditionList getConditionList() { - return conditionList; + @XmlElementWrapper(name = "Conditions") + @XmlElement(name = "Condition") + public List getConditions() { + return conditions; } - public void setConditionList(ConditionList conditionList) { - this.conditionList = conditionList; + public void setConditions(List conditions) { + this.conditions = conditions; } @XmlElementWrapper(name = "SubContents") From d4abcf0fc88b1fefddd62bac08fd09657329fd26 Mon Sep 17 00:00:00 2001 From: Shamalka Navod Date: Fri, 15 May 2020 07:38:36 +0000 Subject: [PATCH 2/3] Add user claim attribute programmatically --- .../org.wso2.carbon.device.mgt.api/pom.xml | 6 ++ .../impl/UserManagementServiceImpl.java | 97 +++++++++++++------ .../device/mgt/jaxrs/util/Constants.java | 5 + .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 15 +++ 4 files changed, 94 insertions(+), 29 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index 6dd7e5c3210..f04259b896d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -417,5 +417,11 @@ powermock-api-mockito test + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.claim.metadata.mgt + ${carbon.identity.framework.version} + provided + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 5d5b0572964..81233092f34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -67,6 +67,11 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementAdminService; +import org.wso2.carbon.identity.claim.metadata.mgt.dto.AttributeMappingDTO; +import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO; +import org.wso2.carbon.identity.claim.metadata.mgt.dto.LocalClaimDTO; +import org.wso2.carbon.identity.claim.metadata.mgt.exception.ClaimMetadataException; import org.wso2.carbon.identity.user.store.count.UserStoreCountRetriever; import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException; import org.wso2.carbon.user.api.Permission; @@ -934,6 +939,12 @@ public class UserManagementServiceImpl implements UserManagementService { @PathParam("username") String username, JsonArray deviceList) { try { + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + if (!userStoreManager.isExistingUser(username)) { + String msg = "User by username: " + username + " does not exist."; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() .getUserRealm() .getRealmConfiguration(); @@ -942,14 +953,31 @@ public class UserManagementServiceImpl implements UserManagementService { if (!StringUtils.isBlank(domain)) { username = domain + Constants.FORWARD_SLASH + username; } - UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - if (!userStoreManager.isExistingUser(username)) { - if (log.isDebugEnabled()) { - log.debug("User by username: " + username + " does not exist."); - } - return Response.status(Response.Status.NOT_FOUND).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage( - "User doesn't exist.").build()).build(); + ClaimMetadataManagementAdminService + claimMetadataManagementAdminService = new ClaimMetadataManagementAdminService(); + //Get all available claim URIs + String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris(); + //Check they contains a claim attribute for external devices + if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) { + List claimPropertyDTOList = new ArrayList<>(); + claimPropertyDTOList + .add(DeviceMgtAPIUtils.buildClaimPropertyDTO + (Constants.ATTRIBUTE_DISPLAY_NAME, Constants.EXTERNAL_DEVICE_CLAIM_DISPLAY_NAME)); + claimPropertyDTOList + .add(DeviceMgtAPIUtils.buildClaimPropertyDTO + (Constants.ATTRIBUTE_DESCRIPTION, Constants.EXTERNAL_DEVICE_CLAIM_DESCRIPTION)); + + LocalClaimDTO localClaimDTO = new LocalClaimDTO(); + localClaimDTO.setLocalClaimURI(Constants.USER_CLAIM_DEVICES); + localClaimDTO.setClaimProperties(claimPropertyDTOList.toArray( + new ClaimPropertyDTO[claimPropertyDTOList.size()])); + + AttributeMappingDTO attributeMappingDTO = new AttributeMappingDTO(); + attributeMappingDTO.setAttributeName(Constants.DEVICES); + attributeMappingDTO.setUserStoreDomain(domain); + localClaimDTO.setAttributeMappings(new AttributeMappingDTO[]{attributeMappingDTO}); + + claimMetadataManagementAdminService.addLocalClaim(localClaimDTO); } Map userClaims = this.buildExternalDevicesUserClaims(username, domain, deviceList, userStoreManager); @@ -958,8 +986,11 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "Error occurred while updating external device claims of the user '" + username + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (ClaimMetadataException e) { + String msg = "Error occurred while adding claim attribute"; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } @@ -969,6 +1000,13 @@ public class UserManagementServiceImpl implements UserManagementService { public Response getUserClaimsForDevices( @PathParam("username") String username) { try { + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + Map claims = new HashMap<>(); + if (!userStoreManager.isExistingUser(username)) { + String msg = "User by username: " + username + " does not exist."; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() .getUserRealm() .getRealmConfiguration(); @@ -977,23 +1015,20 @@ public class UserManagementServiceImpl implements UserManagementService { if (!StringUtils.isBlank(domain)) { username = domain + Constants.FORWARD_SLASH + username; } - UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - if (!userStoreManager.isExistingUser(username)) { + String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris(); + if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) { if (log.isDebugEnabled()) { - log.debug("User by username: " + username + " does not exist."); + log.debug("Claim attribute for external device doesn't exist."); } - return Response.status(Response.Status.NOT_FOUND).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage( - "User doesn't exist.").build()).build(); + return Response.status(Response.Status.OK).entity(claims).build(); } String[] claimArray = {Constants.USER_CLAIM_DEVICES}; - Map claims = userStoreManager.getUserClaimValues(username, claimArray, domain); + claims = userStoreManager.getUserClaimValues(username, claimArray, domain); return Response.status(Response.Status.OK).entity(claims).build(); } catch (UserStoreException e) { - String msg = "Error occurred while retrieving external device claims of the user '" + username + "'"; + String msg = "Error occurred while retrieving external device claims of the user '" + username + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } @@ -1003,6 +1038,13 @@ public class UserManagementServiceImpl implements UserManagementService { public Response deleteUserClaimsForDevices( @PathParam("username") String username) { try { + String[] claimArray = new String[1]; + UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); + if (!userStoreManager.isExistingUser(username)) { + String msg = "User by username: " + username + " does not exist."; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext() .getUserRealm() .getRealmConfiguration(); @@ -1011,16 +1053,14 @@ public class UserManagementServiceImpl implements UserManagementService { if (!StringUtils.isBlank(domain)) { username = domain + Constants.FORWARD_SLASH + username; } - UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); - if (!userStoreManager.isExistingUser(username)) { + String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris(); + if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) { if (log.isDebugEnabled()) { - log.debug("User by username: " + username + " does not exist."); + log.debug("Claim attribute for external device doesn't exist."); } - return Response.status(Response.Status.NOT_FOUND).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage( - "User doesn't exist.").build()).build(); + return Response.status(Response.Status.OK).entity(claimArray).build(); } - String[] claimArray = {Constants.USER_CLAIM_DEVICES}; + claimArray[0] = Constants.USER_CLAIM_DEVICES; userStoreManager.deleteUserClaimValues( username, claimArray, @@ -1029,8 +1069,7 @@ public class UserManagementServiceImpl implements UserManagementService { } catch (UserStoreException e) { String msg = "Error occurred while deleting external device claims of the user '" + username + "'"; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( - new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java index 36fd22cca49..7042bb86106 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/Constants.java @@ -69,6 +69,11 @@ public class Constants { public static final String NOTNOW = "notnow"; public static final String REPEATED = "repeated"; } + public static final String DEVICES = "devices"; + public static final String ATTRIBUTE_DISPLAY_NAME = "DisplayName"; + public static final String ATTRIBUTE_DESCRIPTION = "Description"; + public static final String EXTERNAL_DEVICE_CLAIM_DISPLAY_NAME = "Devices"; + public static final String EXTERNAL_DEVICE_CLAIM_DESCRIPTION = "Device list"; public final class ErrorMessages { private ErrorMessages () { throw new AssertionError(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 9be33cd3977..ac20f838733 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -67,6 +67,7 @@ import org.wso2.carbon.event.processor.stub.EventProcessorAdminServiceStub; import org.wso2.carbon.event.publisher.stub.EventPublisherAdminServiceStub; import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub; +import org.wso2.carbon.identity.claim.metadata.mgt.dto.ClaimPropertyDTO; import org.wso2.carbon.identity.jwt.client.extension.JWTClient; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; @@ -863,4 +864,18 @@ public class DeviceMgtAPIUtils { } return operation; } + + /** + * This method is used to set property name and value to ClaimPropertyDTO + * + * @param propertyName Name of the property + * @param propertyValue Value of the property + * @return {@link ClaimPropertyDTO} + */ + public static ClaimPropertyDTO buildClaimPropertyDTO(String propertyName, String propertyValue) { + ClaimPropertyDTO claimPropertyDTO = new ClaimPropertyDTO(); + claimPropertyDTO.setPropertyName(propertyName); + claimPropertyDTO.setPropertyValue(propertyValue); + return claimPropertyDTO; + } } From df0c26f2f7997042718212d1612d82c895025566 Mon Sep 17 00:00:00 2001 From: Inosh Perara Date: Sun, 17 May 2020 08:39:17 +0000 Subject: [PATCH 3/3] Bluetooth beacon --- .../beans/analytics/reporting/Stream.java | 41 +++++++++++++ .../impl/util/RequestValidationUtil.java | 45 +++++++------- .../device/details/DeviceDetailsWrapper.java | 10 ++++ .../mgt/core/DeviceManagementConstants.java | 5 +- .../details/mgt/DeviceInformationManager.java | 12 ++++ .../impl/DeviceInformationManagerImpl.java | 58 ++++++++++++------- .../mgt/core/util/DeviceManagerUtil.java | 16 +++++ 7 files changed, 139 insertions(+), 48 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java new file mode 100644 index 00000000000..8fcc98c9cb8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/analytics/reporting/Stream.java @@ -0,0 +1,41 @@ +/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you 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.wso2.carbon.device.mgt.jaxrs.beans.analytics.reporting; + +import java.util.Map; + +public class Stream { + + String deviceIdentifier; + Map events; + + public String getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public Map getEvents() { + return events; + } + + public void setEvents(Map events) { + this.events = events; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java index e024b951ff9..98871ee2b53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/util/RequestValidationUtil.java @@ -361,35 +361,30 @@ public class RequestValidationUtil { new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) .setMessage(msg).build()); } catch (InstantiationException e) { - String msg = "Error when creating an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when creating an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (IllegalAccessException e) { - String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (ClassNotFoundException e) { - String msg = "Error when loading an instance of validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error when loading an instance of validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (NoSuchMethodException e) { - String msg = "Error occurred while constructing validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error occurred while constructing validator related to deviceType " + deviceType; + log.debug(msg, e); + } } catch (InvocationTargetException e) { - String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; - log.error(msg, e); - throw new InputValidationException( - new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) - .setMessage(msg).build()); + if (log.isDebugEnabled()) { + String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; + log.debug(msg, e); + } } return features; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java index 975d27b50fa..85622511f06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceDetailsWrapper.java @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import java.util.List; +import java.util.Map; public class DeviceDetailsWrapper { @@ -30,6 +31,7 @@ public class DeviceDetailsWrapper { Device device; List applications; DeviceLocation location; + String events; int tenantId; List groups; @@ -91,6 +93,14 @@ public class DeviceDetailsWrapper { this.location = location; } + public String getEvents() { + return events; + } + + public void setEvents(String events) { + this.events = events; + } + public String getJSONString() { Gson gson = new Gson(); return gson.toJson(this).toString(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index f6eb5182fe6..7f7610126b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -43,6 +43,7 @@ public final class DeviceManagementConstants { public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DEVICE_CACHE = "DEVICE_CACHE"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; + public static final String URL_SEPERATOR = "/"; public static final class ConfigurationManagement { private ConfigurationManagement(){ @@ -164,8 +165,8 @@ public final class DeviceManagementConstants { throw new AssertionError(); } public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; - public static final String REPORTING_CONTEXT = "/event"; - public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info"; + public static final String REPORTING_CONTEXT = "/reporting/api/analyticsadmin/v1.0/event"; + public static final String DEVICE_INFO_PARAM = "device-info"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index 7e1253ed21f..0e401955eac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -96,6 +96,18 @@ public interface DeviceInformationManager { */ List getDeviceLocations(List deviceIdentifiers) throws DeviceDetailsMgtException; + /** + * Send events to reporting backend + * @param deviceId device identifier of the reporting device + * @param deviceType device type of an device + * @param payload payload of the event + * @param eventType Event type being sent + * @return Http status code if a call is made and if failed to make a call 0 + * @throws DeviceDetailsMgtException + */ + int publishEvents(String deviceId, String deviceType, String payload, String eventType) + throws DeviceDetailsMgtException; + // /** // * This method will manage the storing of device application list. // * @param deviceApplication - Device application list. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 5f8132df346..873441968e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublish import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfigurationManagementService; +import org.wso2.carbon.device.mgt.common.device.details.DeviceData; import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; @@ -91,7 +92,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { try { - publishEvents(device, deviceInfo); + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setDeviceInfo(deviceInfo); + publishEvents(device, deviceDetailsWrapper, DeviceManagementConstants.Report.DEVICE_INFO_PARAM); + DeviceManagementDAOFactory.beginTransaction(); DeviceInfo newDeviceInfo; DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), @@ -180,14 +184,37 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } - private void publishEvents(Device device, DeviceInfo deviceInfo) { + public int publishEvents(String deviceId, String deviceType, String payload, String eventType) + throws DeviceDetailsMgtException { + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + + try { + Device device = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getDevice(deviceIdentifier, false); + DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); + deviceDetailsWrapper.setEvents(payload); + return publishEvents(device, deviceDetailsWrapper, eventType); + } catch (DeviceManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Event publishing error. Could not get device " + deviceId; + log.error(msg, e); + throw new DeviceDetailsMgtException(msg, e); + } + } + + /** + * Send device details from core to reporting backend + * @param device Device that is sending event + * @param deviceDetailsWrapper Payload to send(example, deviceinfo, applist, raw events) + */ + private int publishEvents(Device device, DeviceDetailsWrapper deviceDetailsWrapper, String + eventType) { String reportingHost = HttpReportingUtil.getReportingHost(); if (!StringUtils.isBlank(reportingHost) && HttpReportingUtil.isPublishingEnabledForTenant()) { try { - DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper(); deviceDetailsWrapper.setDevice(device); - deviceDetailsWrapper.setDeviceInfo(deviceInfo); deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId()); GroupManagementProviderService groupManagementService = DeviceManagementDataHolder .getInstance().getGroupManagementProviderService(); @@ -197,14 +224,16 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceDetailsWrapper.setGroups(groups); } - String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() + String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext + .getThreadLocalCarbonContext() .getUsername()); if (rolesOfUser != null && rolesOfUser.length > 0) { deviceDetailsWrapper.setRole(rolesOfUser); } - HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), - reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); + String eventUrl = reportingHost + DeviceManagementConstants.Report + .REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType; + return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl); } catch (EventPublishingException e) { log.error("Error occurred while sending events", e); } catch (GroupManagementException e) { @@ -218,6 +247,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { + DeviceManagerUtil.getTenantId()); } } + return 0; } @Override @@ -524,19 +554,5 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } - private String[] getRolesOfUser(String userName) throws UserStoreException { - UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); - String[] roleList; - if (userRealm != null) { - userRealm.getUserStoreManager().getRoleNames(); - roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); - } else { - String msg = "User realm is not initiated. Logged in user: " + userName; - log.error(msg); - throw new UserStoreException(msg); - } - return roleList; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 049188bdd47..1f0f16d9fc0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -32,6 +32,7 @@ import org.apache.http.protocol.HTTP; import org.w3c.dom.Document; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.caching.impl.CacheImpl; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; @@ -76,6 +77,7 @@ import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.user.api.TenantManager; +import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.ConfigurationContextService; @@ -1005,4 +1007,18 @@ public final class DeviceManagerUtil { } } + + public static String[] getRolesOfUser(String userName) throws UserStoreException { + UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String[] roleList; + if (userRealm != null) { + userRealm.getUserStoreManager().getRoleNames(); + roleList = userRealm.getUserStoreManager().getRoleListOfUser(userName); + } else { + String msg = "User realm is not initiated. Logged in user: " + userName; + log.error(msg); + throw new UserStoreException(msg); + } + return roleList; + } }