From 61ac576d6cad3d8c5462a3c10b40d56f743b947d Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 15:34:34 +0530 Subject: [PATCH 1/6] Fixing https://github.com/wso2/product-iots/issues/1376. --- .../impl/GroupManagementServiceImpl.java | 7 +-- .../group/mgt/GroupNotExistException.java | 55 +++++++++++++++++++ .../GroupManagementProviderService.java | 7 +-- .../GroupManagementProviderServiceImpl.java | 22 ++++---- 4 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 8bab08463b3..d0619e65205 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -29,10 +29,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.*; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; @@ -144,7 +141,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { String msg = "Error occurred while adding new group."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (GroupAlreadyExistException e) { + } catch (GroupNotExistException e) { String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'."; log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java new file mode 100644 index 00000000000..a4abc968537 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. 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.group.mgt; + +public class GroupNotExistException extends Exception{ + + private String errorMessage; + + public GroupNotExistException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public GroupNotExistException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public GroupNotExistException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public GroupNotExistException() { + super(); + } + + public GroupNotExistException(Throwable cause) { + super(cause); + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 054a923c437..01368523a83 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -23,10 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.*; import java.util.List; @@ -53,7 +50,7 @@ public interface GroupManagementProviderService { * @param groupId of the group. * @throws GroupManagementException */ - void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupAlreadyExistException; + void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupNotExistException; /** * Delete existing device group. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index b5a2a4f635e..484d22def90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -73,9 +73,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Creating group '" + deviceGroup.getName() + "'"); } - if (deviceGroup == null) { - throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); - } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { GroupManagementDAOFactory.beginTransaction(); @@ -95,6 +92,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid String msg = "Error occurred while initiating transaction."; log.error(msg, e); throw new GroupManagementException(msg, e); + } catch (GroupAlreadyExistException ex) { + throw ex; } catch (Exception e) { String msg = "Error occurred in creating group '" + deviceGroup.getName() + "'"; log.error(msg, e); @@ -113,7 +112,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) - throws GroupManagementException, GroupAlreadyExistException { + throws GroupManagementException, GroupNotExistException { if (deviceGroup == null) { String msg = "Received incomplete data for updateGroup"; log.error(msg); @@ -122,30 +121,29 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("update group '" + deviceGroup.getName() + "'"); } - if (deviceGroup == null) { - throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); - } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); - DeviceGroup existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); - if (existingGroup == null || existingGroup.getGroupId() == groupId) { + DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); + if (existingGroup != null) { this.groupDAO.updateGroup(deviceGroup, groupId, tenantId); GroupManagementDAOFactory.commitTransaction(); } else { - throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); + throw new GroupNotExistException("Group with ID - '" + groupId + "' doesn't exists!"); } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while modifying deviceGroup '" + deviceGroup.getName() + "'."; + String msg = "Error occurred while modifying device group with ID - '" + groupId + "'."; log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction."; log.error(msg, e); throw new GroupManagementException(msg, e); + } catch (GroupNotExistException ex) { + throw ex; } catch (Exception e) { - String msg = "Error occurred in updating group '" + deviceGroup.getName() + "'"; + String msg = "Error occurred in updating the device group with ID - '" + groupId + "'."; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { From a1d6065c7535de524aaa66af1dd97e146bca7c85 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 16:11:59 +0530 Subject: [PATCH 2/6] Sending email only when the mail transport is enabled. --- .../email/sender/core/service/EmailSenderServiceImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java index daaf8f61029..791b61daa9d 100644 --- a/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java +++ b/components/email-sender/org.wso2.carbon.email.sender.core/src/main/java/org/wso2/carbon/email/sender/core/service/EmailSenderServiceImpl.java @@ -71,12 +71,14 @@ public class EmailSenderServiceImpl implements EmailSenderService { if(EmailSenderDataHolder.getInstance().getConfigurationContextService() .getServerConfigContext().getAxisConfiguration().getTransportOut(transportSenderName) == null){ log.warn("Email invitation is not sent as the email is not configured."); + } else { + threadPoolExecutor.submit(new EmailSender(recipient, emailData.getSubject(), emailData.getBody())); } } catch (ContentProcessingInterruptedException e) { throw new EmailSendingFailedException("Error occurred while retrieving email content to be " + "sent for recipient '" + recipient + "'", e); } - threadPoolExecutor.submit(new EmailSender(recipient, emailData.getSubject(), emailData.getBody())); + } } From c4c30f5856727393a6173df182593576d082e9c5 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 16:45:23 +0530 Subject: [PATCH 3/6] Fixing imports. --- .../impl/GroupManagementServiceImpl.java | 306 ------------------ .../GroupManagementAdminServiceImpl.java | 4 - 2 files changed, 310 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index d0619e65205..e69de29bb2d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -1,306 +0,0 @@ -/* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.service.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.CarbonConstants; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; -import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.*; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; -import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceToGroupsAssignment; -import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; -import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; -import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; -import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; - -import javax.ws.rs.core.Response; -import java.util.ArrayList; -import java.util.List; - -public class GroupManagementServiceImpl implements GroupManagementService { - - private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); - - private static final String DEFAULT_ADMIN_ROLE = "admin"; - private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", - "/permission/device-mgt/user/groups"}; - private static final String EMPTY_RESULT = "EMPTY"; - - @Override - public Response getGroups(String name, String owner, int offset, int limit) { - try { - RequestValidationUtil.validatePaginationParameters(offset, limit); - String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); - request.setGroupName(name); - request.setOwner(owner); - PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() - .getGroups(currentUser, request); - DeviceGroupList deviceGroupList = new DeviceGroupList(); - if (deviceGroupsResult.getData() != null && deviceGroupsResult.getRecordsTotal() > 0) { - deviceGroupList.setList(deviceGroupsResult.getData()); - deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); - } else { - deviceGroupList.setList(new ArrayList<>()); - deviceGroupList.setCount(0); - } - return Response.status(Response.Status.OK).entity(deviceGroupList).build(); - } catch (GroupManagementException e) { - String error = "Error occurred while getting the groups."; - log.error(error, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); - } - } - - @Override - public Response getGroupCount() { - try { - String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser); - return Response.status(Response.Status.OK).entity(count).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while retrieving group count."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Override - public Response createGroup(DeviceGroup group) { - String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (group == null) { - return Response.status(Response.Status.BAD_REQUEST).build(); - } - group.setOwner(owner); - try { - DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - return Response.status(Response.Status.CREATED).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while adding new group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (GroupAlreadyExistException e) { - String msg = "Group already exists with name '" + group.getName() + "'."; - log.warn(msg); - return Response.status(Response.Status.CONFLICT).entity(msg).build(); - } - } - - @Override - public Response getGroup(int groupId) { - try { - GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - DeviceGroup deviceGroup = service.getGroup(groupId); - if (deviceGroup != null) { - return Response.status(Response.Status.OK).entity(deviceGroup).build(); - } else { - return Response.status(Response.Status.NOT_FOUND).build(); - } - } catch (GroupManagementException e) { - String error = "Error occurred while getting the group."; - log.error(error, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); - } - } - - @Override - public Response updateGroup(int groupId, DeviceGroup deviceGroup) { - if (deviceGroup == null) { - return Response.status(Response.Status.BAD_REQUEST).build(); - } - try { - DeviceMgtAPIUtils.getGroupManagementProviderService().updateGroup(deviceGroup, groupId); - return Response.status(Response.Status.OK).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while adding new group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (GroupNotExistException e) { - String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'."; - log.warn(msg); - return Response.status(Response.Status.CONFLICT).entity(msg).build(); - } - } - - @Override - public Response deleteGroup(int groupId) { - try { - if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId)) { - return Response.status(Response.Status.OK).build(); - } else { - return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); - } - } catch (GroupManagementException e) { - String msg = "Error occurred while deleting the group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Override - public Response manageGroupSharing(int groupId, List userRoles) { - try { - DeviceMgtAPIUtils.getGroupManagementProviderService() - .manageGroupSharing(groupId, userRoles); - return Response.status(Response.Status.OK).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while managing group share. "; - if (e.getErrorMessage() != null){ - msg += e.getErrorMessage(); - } - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (RoleDoesNotExistException e) { - return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); - } - } - - @Override - public Response getRolesOfGroup(int groupId) { - try { - List groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(groupId); - RoleList deviceGroupRolesList = new RoleList(); - if(groupRoles != null) { - deviceGroupRolesList.setList(groupRoles); - deviceGroupRolesList.setCount(groupRoles.size()); - } else { - deviceGroupRolesList.setList(new ArrayList()); - deviceGroupRolesList.setCount(0); - } - return Response.status(Response.Status.OK).entity(deviceGroupRolesList).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while getting roles of the group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Override - public Response getDevicesOfGroup(int groupId, int offset, int limit) { - try { - GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - List deviceList = service.getDevices(groupId, offset, limit); - int deviceCount = service.getDeviceCount(groupId); - DeviceList deviceListWrapper = new DeviceList(); - if (deviceList != null) { - deviceListWrapper.setList(deviceList); - } else { - deviceListWrapper.setList(new ArrayList()); - } - deviceListWrapper.setCount(deviceCount); - return Response.status(Response.Status.OK).entity(deviceListWrapper).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while getting devices the group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Override - public Response getDeviceCountOfGroup(int groupId) { - try { - int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(groupId); - return Response.status(Response.Status.OK).entity(count).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while getting device count of the group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Override - public Response addDevicesToGroup(int groupId, List deviceIdentifiers) { - try { - DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); - return Response.status(Response.Status.OK).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while adding devices to group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (DeviceNotFoundException e) { - return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); - } - } - - @Override - public Response removeDevicesFromGroup(int groupId, List deviceIdentifiers) { - try { - DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); - return Response.status(Response.Status.OK).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while removing devices from group."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (DeviceNotFoundException e) { - return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); - } - } - - @Override - public Response updateDeviceAssigningToGroups(DeviceToGroupsAssignment deviceToGroupsAssignment) { - try { - List deviceIdentifiers = new ArrayList<>(); - deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier()); - GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - List deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier()); - for (DeviceGroup group : deviceGroups) { - Integer groupId = group.getGroupId(); - if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) { - deviceToGroupsAssignment.getDeviceGroupIds().remove(groupId); - } else if (!CarbonConstants.REGISTRY_SYSTEM_USERNAME.equals(group.getOwner())) { - DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); - } - } - for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) { - DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); - } - return Response.status(Response.Status.OK).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while assigning device to groups."; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (DeviceNotFoundException e) { - return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); - } - } - - @Override - public Response getGroups(String deviceId, String deviceType) { - try { - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); - List deviceGroups = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(deviceIdentifier); - return Response.status(Response.Status.OK).entity(deviceGroups).build(); - } catch (GroupManagementException e) { - String msg = "Error occurred while getting groups of device."; - log.error(msg, e); - 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/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index 8222210cad9..0668b30c4e0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -21,11 +21,8 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; @@ -33,7 +30,6 @@ import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.core.Response; import java.util.ArrayList; -import java.util.List; public class GroupManagementAdminServiceImpl implements GroupManagementAdminService { From bb5afb07b7e8993eb5d9ed9f66d861e50d540605 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 16:47:33 +0530 Subject: [PATCH 4/6] Adding the class back which I mistaken deleted. --- .../impl/GroupManagementServiceImpl.java | 306 ++++++++++++++++++ 1 file changed, 306 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index e69de29bb2d..9ecf0aa1e7d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -0,0 +1,306 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.service.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.CarbonConstants; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; +import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.group.mgt.*; +import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceToGroupsAssignment; +import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList; +import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; +import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; + +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +public class GroupManagementServiceImpl implements GroupManagementService { + + private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); + + private static final String DEFAULT_ADMIN_ROLE = "admin"; + private static final String[] DEFAULT_ADMIN_PERMISSIONS = {"/permission/device-mgt/admin/groups", + "/permission/device-mgt/user/groups"}; + private static final String EMPTY_RESULT = "EMPTY"; + + @Override + public Response getGroups(String name, String owner, int offset, int limit) { + try { + RequestValidationUtil.validatePaginationParameters(offset, limit); + String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); + request.setGroupName(name); + request.setOwner(owner); + PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() + .getGroups(currentUser, request); + DeviceGroupList deviceGroupList = new DeviceGroupList(); + if (deviceGroupsResult.getData() != null && deviceGroupsResult.getRecordsTotal() > 0) { + deviceGroupList.setList(deviceGroupsResult.getData()); + deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); + } else { + deviceGroupList.setList(new ArrayList<>()); + deviceGroupList.setCount(0); + } + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); + } catch (GroupManagementException e) { + String error = "Error occurred while getting the groups."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + + @Override + public Response getGroupCount() { + try { + String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser); + return Response.status(Response.Status.OK).entity(count).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while retrieving group count."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response createGroup(DeviceGroup group) { + String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (group == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + group.setOwner(owner); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + return Response.status(Response.Status.CREATED).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name '" + group.getName() + "'."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } + } + + @Override + public Response getGroup(int groupId) { + try { + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + DeviceGroup deviceGroup = service.getGroup(groupId); + if (deviceGroup != null) { + return Response.status(Response.Status.OK).entity(deviceGroup).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } + } catch (GroupManagementException e) { + String error = "Error occurred while getting the group."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + + @Override + public Response updateGroup(int groupId, DeviceGroup deviceGroup) { + if (deviceGroup == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().updateGroup(deviceGroup, groupId); + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupNotExistException e) { + String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } + } + + @Override + public Response deleteGroup(int groupId) { + try { + if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId)) { + return Response.status(Response.Status.OK).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); + } + } catch (GroupManagementException e) { + String msg = "Error occurred while deleting the group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response manageGroupSharing(int groupId, List userRoles) { + try { + DeviceMgtAPIUtils.getGroupManagementProviderService() + .manageGroupSharing(groupId, userRoles); + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while managing group share. "; + if (e.getErrorMessage() != null){ + msg += e.getErrorMessage(); + } + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (RoleDoesNotExistException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Override + public Response getRolesOfGroup(int groupId) { + try { + List groupRoles = DeviceMgtAPIUtils.getGroupManagementProviderService().getRoles(groupId); + RoleList deviceGroupRolesList = new RoleList(); + if(groupRoles != null) { + deviceGroupRolesList.setList(groupRoles); + deviceGroupRolesList.setCount(groupRoles.size()); + } else { + deviceGroupRolesList.setList(new ArrayList()); + deviceGroupRolesList.setCount(0); + } + return Response.status(Response.Status.OK).entity(deviceGroupRolesList).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting roles of the group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response getDevicesOfGroup(int groupId, int offset, int limit) { + try { + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + List deviceList = service.getDevices(groupId, offset, limit); + int deviceCount = service.getDeviceCount(groupId); + DeviceList deviceListWrapper = new DeviceList(); + if (deviceList != null) { + deviceListWrapper.setList(deviceList); + } else { + deviceListWrapper.setList(new ArrayList()); + } + deviceListWrapper.setCount(deviceCount); + return Response.status(Response.Status.OK).entity(deviceListWrapper).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting devices the group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response getDeviceCountOfGroup(int groupId) { + try { + int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getDeviceCount(groupId); + return Response.status(Response.Status.OK).entity(count).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting device count of the group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response addDevicesToGroup(int groupId, List deviceIdentifiers) { + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding devices to group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceNotFoundException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Override + public Response removeDevicesFromGroup(int groupId, List deviceIdentifiers) { + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while removing devices from group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceNotFoundException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Override + public Response updateDeviceAssigningToGroups(DeviceToGroupsAssignment deviceToGroupsAssignment) { + try { + List deviceIdentifiers = new ArrayList<>(); + deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier()); + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + List deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier()); + for (DeviceGroup group : deviceGroups) { + Integer groupId = group.getGroupId(); + if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) { + deviceToGroupsAssignment.getDeviceGroupIds().remove(groupId); + } else if (!CarbonConstants.REGISTRY_SYSTEM_USERNAME.equals(group.getOwner())) { + DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); + } + } + for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) { + DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); + } + return Response.status(Response.Status.OK).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while assigning device to groups."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (DeviceNotFoundException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + + @Override + public Response getGroups(String deviceId, String deviceType) { + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + List deviceGroups = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(deviceIdentifier); + return Response.status(Response.Status.OK).entity(deviceGroups).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while getting groups of device."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + +} \ No newline at end of file From e6b8af986bf82084cb00f59fc51e4a90f67c70f8 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 16:48:50 +0530 Subject: [PATCH 5/6] Adding the specific imports. --- .../mgt/jaxrs/service/impl/GroupManagementServiceImpl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 9ecf0aa1e7d..8a6e8cc5b48 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -29,7 +29,11 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.*; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; From a9c5188e0d07f030c738eae87384e8e05ccfbaf4 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 16:51:28 +0530 Subject: [PATCH 6/6] Adding the specific imports. --- .../mgt/core/service/GroupManagementProviderService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 01368523a83..0187e8dca63 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -23,7 +23,11 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.*; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; +import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import java.util.List;