Charitha Goonetilleke 5 years ago
commit 573a9e598c

@ -417,5 +417,11 @@
<artifactId>powermock-api-mockito</artifactId> <artifactId>powermock-api-mockito</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.claim.metadata.mgt</artifactId>
<version>${carbon.identity.framework.version}</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -14,24 +14,28 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.jaxrs.beans.analytics.reporting;
package org.wso2.carbon.device.mgt.common.policy.mgt.ui; import java.util.Map;
import javax.xml.bind.annotation.XmlElement; public class Stream {
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "NotificationList") String deviceIdentifier;
public class NotificationList { Map<String, String> events;
List<Notification> notification; public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
@XmlElement(name = "Notification") public Map<String, String> getEvents() {
public List<Notification> getConditions() { return events;
return notification;
} }
public void setConditions(List<Notification> notification) { public void setEvents(Map<String, String> events) {
this.notification = notification; this.events = events;
} }
} }

@ -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.Constants;
import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder;
import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; 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.UserStoreCountRetriever;
import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException; import org.wso2.carbon.identity.user.store.count.exception.UserStoreCounterException;
import org.wso2.carbon.user.api.Permission; import org.wso2.carbon.user.api.Permission;
@ -934,6 +939,12 @@ public class UserManagementServiceImpl implements UserManagementService {
@PathParam("username") String username, @PathParam("username") String username,
JsonArray deviceList) { JsonArray deviceList) {
try { 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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -942,14 +953,31 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); ClaimMetadataManagementAdminService
if (!userStoreManager.isExistingUser(username)) { claimMetadataManagementAdminService = new ClaimMetadataManagementAdminService();
if (log.isDebugEnabled()) { //Get all available claim URIs
log.debug("User by username: " + username + " does not exist."); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
} //Check they contains a claim attribute for external devices
return Response.status(Response.Status.NOT_FOUND).entity( if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
new ErrorResponse.ErrorResponseBuilder().setMessage( List<ClaimPropertyDTO> claimPropertyDTOList = new ArrayList<>();
"User doesn't exist.").build()).build(); 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<String, String> userClaims = Map<String, String> userClaims =
this.buildExternalDevicesUserClaims(username, domain, deviceList, userStoreManager); this.buildExternalDevicesUserClaims(username, domain, deviceList, userStoreManager);
@ -958,8 +986,11 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while updating external device claims of the user '" + username + "'"; String msg = "Error occurred while updating external device claims of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).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( public Response getUserClaimsForDevices(
@PathParam("username") String username) { @PathParam("username") String username) {
try { try {
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager();
Map<String, String> 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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -977,23 +1015,20 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
if (!userStoreManager.isExistingUser(username)) { if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
if (log.isDebugEnabled()) { 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( return Response.status(Response.Status.OK).entity(claims).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build();
} }
String[] claimArray = {Constants.USER_CLAIM_DEVICES}; String[] claimArray = {Constants.USER_CLAIM_DEVICES};
Map<String, String> claims = userStoreManager.getUserClaimValues(username, claimArray, domain); claims = userStoreManager.getUserClaimValues(username, claimArray, domain);
return Response.status(Response.Status.OK).entity(claims).build(); return Response.status(Response.Status.OK).entity(claims).build();
} catch (UserStoreException e) { } 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); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }
@ -1003,6 +1038,13 @@ public class UserManagementServiceImpl implements UserManagementService {
public Response deleteUserClaimsForDevices( public Response deleteUserClaimsForDevices(
@PathParam("username") String username) { @PathParam("username") String username) {
try { 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() RealmConfiguration realmConfiguration = PrivilegedCarbonContext.getThreadLocalCarbonContext()
.getUserRealm() .getUserRealm()
.getRealmConfiguration(); .getRealmConfiguration();
@ -1011,16 +1053,14 @@ public class UserManagementServiceImpl implements UserManagementService {
if (!StringUtils.isBlank(domain)) { if (!StringUtils.isBlank(domain)) {
username = domain + Constants.FORWARD_SLASH + username; username = domain + Constants.FORWARD_SLASH + username;
} }
UserStoreManager userStoreManager = DeviceMgtAPIUtils.getUserStoreManager(); String[] allUserClaims = userStoreManager.getClaimManager().getAllClaimUris();
if (!userStoreManager.isExistingUser(username)) { if (!Arrays.asList(allUserClaims).contains(Constants.USER_CLAIM_DEVICES)) {
if (log.isDebugEnabled()) { 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( return Response.status(Response.Status.OK).entity(claimArray).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(
"User doesn't exist.").build()).build();
} }
String[] claimArray = {Constants.USER_CLAIM_DEVICES}; claimArray[0] = Constants.USER_CLAIM_DEVICES;
userStoreManager.deleteUserClaimValues( userStoreManager.deleteUserClaimValues(
username, username,
claimArray, claimArray,
@ -1029,8 +1069,7 @@ public class UserManagementServiceImpl implements UserManagementService {
} catch (UserStoreException e) { } catch (UserStoreException e) {
String msg = "Error occurred while deleting external device claims of the user '" + username + "'"; String msg = "Error occurred while deleting external device claims of the user '" + username + "'";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity( return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} }

@ -361,35 +361,30 @@ public class RequestValidationUtil {
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR) new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build()); .setMessage(msg).build());
} catch (InstantiationException e) { } catch (InstantiationException e) {
if (log.isDebugEnabled()) {
String msg = "Error when creating an instance of validator related to deviceType " + deviceType; String msg = "Error when creating an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
if (log.isDebugEnabled()) {
String msg = "Error when accessing an instance of validator related to deviceType " + deviceType; String msg = "Error when accessing an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
if (log.isDebugEnabled()) {
String msg = "Error when loading an instance of validator related to deviceType " + deviceType; String msg = "Error when loading an instance of validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
String msg = "Error occurred while constructing validator related to deviceType " + deviceType; String msg = "Error occurred while constructing validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
if (log.isDebugEnabled()) {
String msg = "Error occurred while instantiating validator related to deviceType " + deviceType; String msg = "Error occurred while instantiating validator related to deviceType " + deviceType;
log.error(msg, e); log.debug(msg, e);
throw new InputValidationException( }
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.setMessage(msg).build());
} }
return features; return features;
} }

@ -69,6 +69,11 @@ public class Constants {
public static final String NOTNOW = "notnow"; public static final String NOTNOW = "notnow";
public static final String REPEATED = "repeated"; 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 { public final class ErrorMessages {
private ErrorMessages () { throw new AssertionError(); } private ErrorMessages () { throw new AssertionError(); }

@ -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.publisher.stub.EventPublisherAdminServiceStub;
import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub; import org.wso2.carbon.event.receiver.stub.EventReceiverAdminServiceStub;
import org.wso2.carbon.event.stream.stub.EventStreamAdminServiceStub; 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.JWTClient;
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException; import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
@ -863,4 +864,18 @@ public class DeviceMgtAPIUtils {
} }
return operation; 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;
}
} }

@ -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 org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import java.util.List; import java.util.List;
import java.util.Map;
public class DeviceDetailsWrapper { public class DeviceDetailsWrapper {
@ -30,6 +31,7 @@ public class DeviceDetailsWrapper {
Device device; Device device;
List<Application> applications; List<Application> applications;
DeviceLocation location; DeviceLocation location;
String events;
int tenantId; int tenantId;
List<DeviceGroup> groups; List<DeviceGroup> groups;
@ -91,6 +93,14 @@ public class DeviceDetailsWrapper {
this.location = location; this.location = location;
} }
public String getEvents() {
return events;
}
public void setEvents(String events) {
this.events = events;
}
public String getJSONString() { public String getJSONString() {
Gson gson = new Gson(); Gson gson = new Gson();
return gson.toJson(this).toString(); return gson.toJson(this).toString();

@ -18,20 +18,24 @@
package org.wso2.carbon.device.mgt.common.policy.mgt.ui; package org.wso2.carbon.device.mgt.common.policy.mgt.ui;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List; import java.util.List;
@XmlRootElement(name = "ConditionList") @XmlRootElement(name = "Condition")
public class ConditionList { public class Condition {
List<Key> conditions; String key;
List<String> values;
@XmlElement(name = "Key") @XmlElement(name = "Key")
public List<Key> getConditions() { public String getKey() { return key; }
return conditions;
}
public void setConditions(List<Key> conditions) { public void setKey(String key) { this.key = key; }
this.conditions = conditions;
} @XmlElementWrapper(name = "Values")
@XmlElement(name = "Value")
public List<String> getValues() { return values; }
public void setValues(List<String> values) { this.values = values; }
} }

@ -29,7 +29,6 @@ public class Content {
private String key; private String key;
private List<Item> items; private List<Item> items;
private List<SubContent> subContents; private List<SubContent> subContents;
private ConditionList conditionList;
@XmlAttribute(name = "key", required = true) @XmlAttribute(name = "key", required = true)
public String getKey() { public String getKey() {
@ -56,12 +55,4 @@ public class Content {
public void setSubContents(List<SubContent> subContents) { this.subContents = subContents; } public void setSubContents(List<SubContent> subContents) { this.subContents = subContents; }
@XmlElement(name = "ConditionList")
public ConditionList getConditionList() {
return conditionList;
}
public void setConditionList(ConditionList conditionList) {
this.conditionList = conditionList;
}
} }

@ -18,15 +18,16 @@
package org.wso2.carbon.device.mgt.common.policy.mgt.ui; package org.wso2.carbon.device.mgt.common.policy.mgt.ui;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "Input") @XmlRootElement(name = "Input")
public class Input { public class Input {
private String type; private String type;
private String placeholderValue; private String placeholderValue;
private String regEx; private List<Rule> rules;
private String validationMessage;
@XmlElement(name = "Type") @XmlElement(name = "Type")
public String getType() { public String getType() {
@ -46,21 +47,9 @@ public class Input {
this.placeholderValue = placeholderValue; this.placeholderValue = placeholderValue;
} }
@XmlElement(name = "Regex") @XmlElementWrapper(name = "Rules")
public String getRegEx() { @XmlElement(name = "Rule")
return regEx; public List<Rule> getRules() { return rules; }
}
public void setRegEx(String regEx) {
this.regEx = regEx;
}
@XmlElement(name = "ValidationMsg") public void setRules(List<Rule> rules) { this.rules = rules; }
public String getValidationMessage() {
return validationMessage;
}
public void setValidationMessage(String validationMessage) {
this.validationMessage = validationMessage;
}
} }

@ -18,7 +18,9 @@
package org.wso2.carbon.device.mgt.common.policy.mgt.ui; package org.wso2.carbon.device.mgt.common.policy.mgt.ui;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "Item") @XmlRootElement(name = "Item")
public class Item { public class Item {
@ -29,14 +31,14 @@ public class Item {
private String value; private String value;
private boolean isRequired; private boolean isRequired;
private String subTitle; private String subTitle;
private ConditionList conditionList; private List<Condition> conditions;
private Checkbox checkbox; private Checkbox checkbox;
private Select select; private Select select;
private Input input; private Input input;
private TimeSelector timeSelector; private TimeSelector timeSelector;
private Table table; private Table table;
private RadioGroup radioGroup; private RadioGroup radioGroup;
private NotificationList notificationList; private List<Notification> notifications;
@XmlElement(name = "Label") @XmlElement(name = "Label")
public String getLabel() { public String getLabel() {
@ -88,10 +90,11 @@ public class Item {
public void setSubTitle(String subTitle) { this.subTitle = subTitle; } public void setSubTitle(String subTitle) { this.subTitle = subTitle; }
@XmlElement(name = "ConditionList") @XmlElementWrapper(name = "Conditions")
public ConditionList getConditionList() { return conditionList; } @XmlElement(name = "Condition")
public List<Condition> getConditions() { return conditions; }
public void setConditionList(ConditionList conditionList) { this.conditionList = conditionList; } public void setConditions(List<Condition> conditions) { this.conditions = conditions; }
@XmlElement(name = "Checkbox") @XmlElement(name = "Checkbox")
public Checkbox getCheckbox() { public Checkbox getCheckbox() {
@ -139,8 +142,9 @@ public class Item {
this.radioGroup = radioGroup; this.radioGroup = radioGroup;
} }
@XmlElement(name = "NotificationList") @XmlElementWrapper(name = "Notifications")
public NotificationList getNotificationList() { return notificationList; } @XmlElement(name = "Notification")
public List<Notification> getNotifications() { return notifications; }
public void setNotificationList(NotificationList notificationList) { this.notificationList = notificationList; } public void setNotifications(List<Notification> notifications) { this.notifications = notifications; }
} }

@ -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;
}
}

@ -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; }
}

@ -28,7 +28,7 @@ public class SubContent {
private String key; private String key;
private List<Item> items; private List<Item> items;
private ConditionList conditionList; private List<Condition> conditions;
private List<SubContent> subContents; private List<SubContent> subContents;
@XmlAttribute(name = "key", required = true) @XmlAttribute(name = "key", required = true)
@ -50,13 +50,14 @@ public class SubContent {
this.items = items; this.items = items;
} }
@XmlElement(name = "ConditionList") @XmlElementWrapper(name = "Conditions")
public ConditionList getConditionList() { @XmlElement(name = "Condition")
return conditionList; public List<Condition> getConditions() {
return conditions;
} }
public void setConditionList(ConditionList conditionList) { public void setConditions(List<Condition> conditions) {
this.conditionList = conditionList; this.conditions = conditions;
} }
@XmlElementWrapper(name = "SubContents") @XmlElementWrapper(name = "SubContents")

@ -43,6 +43,7 @@ public final class DeviceManagementConstants {
public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER";
public static final String DEVICE_CACHE = "DEVICE_CACHE"; 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 ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification";
public static final String URL_SEPERATOR = "/";
public static final class ConfigurationManagement { public static final class ConfigurationManagement {
private ConfigurationManagement(){ private ConfigurationManagement(){
@ -164,8 +165,8 @@ public final class DeviceManagementConstants {
throw new AssertionError(); throw new AssertionError();
} }
public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host"; public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host";
public static final String REPORTING_CONTEXT = "/event"; public static final String REPORTING_CONTEXT = "/reporting/api/analyticsadmin/v1.0/event";
public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info"; public static final String DEVICE_INFO_PARAM = "device-info";
public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage"; public static final String APP_USAGE_ENDPOINT = REPORTING_CONTEXT + "/app-usage";
} }
} }

@ -96,6 +96,18 @@ public interface DeviceInformationManager {
*/ */
List<DeviceLocation> getDeviceLocations(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException; List<DeviceLocation> getDeviceLocations(List<DeviceIdentifier> 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. // * This method will manage the storing of device application list.
// * @param deviceApplication - Device application list. // * @param deviceApplication - Device application list.

@ -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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.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.device.details.DeviceDetailsWrapper;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException; 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 { public void addDeviceInfo(Device device, DeviceInfo deviceInfo) throws DeviceDetailsMgtException {
try { try {
publishEvents(device, deviceInfo); DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
publishEvents(device, deviceDetailsWrapper, DeviceManagementConstants.Report.DEVICE_INFO_PARAM);
DeviceManagementDAOFactory.beginTransaction(); DeviceManagementDAOFactory.beginTransaction();
DeviceInfo newDeviceInfo; DeviceInfo newDeviceInfo;
DeviceInfo previousDeviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), 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(); String reportingHost = HttpReportingUtil.getReportingHost();
if (!StringUtils.isBlank(reportingHost) if (!StringUtils.isBlank(reportingHost)
&& HttpReportingUtil.isPublishingEnabledForTenant()) { && HttpReportingUtil.isPublishingEnabledForTenant()) {
try { try {
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setDevice(device); deviceDetailsWrapper.setDevice(device);
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId()); deviceDetailsWrapper.setTenantId(DeviceManagerUtil.getTenantId());
GroupManagementProviderService groupManagementService = DeviceManagementDataHolder GroupManagementProviderService groupManagementService = DeviceManagementDataHolder
.getInstance().getGroupManagementProviderService(); .getInstance().getGroupManagementProviderService();
@ -197,14 +224,16 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceDetailsWrapper.setGroups(groups); deviceDetailsWrapper.setGroups(groups);
} }
String[] rolesOfUser = getRolesOfUser(CarbonContext.getThreadLocalCarbonContext() String[] rolesOfUser = DeviceManagerUtil.getRolesOfUser(CarbonContext
.getThreadLocalCarbonContext()
.getUsername()); .getUsername());
if (rolesOfUser != null && rolesOfUser.length > 0) { if (rolesOfUser != null && rolesOfUser.length > 0) {
deviceDetailsWrapper.setRole(rolesOfUser); deviceDetailsWrapper.setRole(rolesOfUser);
} }
HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), String eventUrl = reportingHost + DeviceManagementConstants.Report
reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT); .REPORTING_CONTEXT + DeviceManagementConstants.URL_SEPERATOR + eventType;
return HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(), eventUrl);
} catch (EventPublishingException e) { } catch (EventPublishingException e) {
log.error("Error occurred while sending events", e); log.error("Error occurred while sending events", e);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
@ -218,6 +247,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
+ DeviceManagerUtil.getTenantId()); + DeviceManagerUtil.getTenantId());
} }
} }
return 0;
} }
@Override @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;
}
} }

@ -32,6 +32,7 @@ import org.apache.http.protocol.HTTP;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.caching.impl.CacheImpl; import org.wso2.carbon.caching.impl.CacheImpl;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService; import org.wso2.carbon.device.mgt.analytics.data.publisher.service.EventsPublisherService;
import org.wso2.carbon.device.mgt.common.AppRegistrationCredentials; 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.exception.JWTClientException;
import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerService;
import org.wso2.carbon.user.api.TenantManager; 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.user.api.UserStoreException;
import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.CarbonUtils;
import org.wso2.carbon.utils.ConfigurationContextService; 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;
}
} }

Loading…
Cancel
Save