diff --git a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/AbstractManagerService.java b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/AbstractManagerService.java new file mode 100644 index 0000000000..fd1ae0e286 --- /dev/null +++ b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/AbstractManagerService.java @@ -0,0 +1,59 @@ +package org.wso2.carbon.device.mgt.iot; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; + +/** + * This class provides util methods for manager services + */ +public abstract class AbstractManagerService { + private static Log log = LogFactory.getLog(AbstractManagerService.class); + + protected PrivilegedCarbonContext ctx; + + // Returns current username + // this method assumes WebappAuthenticationValve is setting username, tenant + // upon successful authentication. Add context param doAuthentication to "true" on web.xml + + /** + * Returns current username. this method assumes WebappAuthenticationValve is setting username, + * tenant_domain, tenant_id upon successful authentication. + * Add context param doAuthentication to "true" on web.xml. + * + * @return current username + */ + protected String getCurrentUserName(){ + return CarbonContext.getThreadLocalCarbonContext().getUsername(); + } + + /** + * Returns OSGi service. Should invoke endTenantFlow() to end the tenant flow once osgi service + * is consumed. + * @param osgiServiceClass + * @param OSGi service class + * @return OSGi service + */ + protected T getServiceProvider(Class osgiServiceClass) { + String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + PrivilegedCarbonContext.startTenantFlow(); + ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + ctx.setTenantDomain(tenantDomain, true); + if (log.isDebugEnabled()) { + log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain); + } + return (T) ctx.getOSGiService(osgiServiceClass.getClass(), null); + } + + /** + * Ends tenant flow. + */ + protected void endTenantFlow() { + PrivilegedCarbonContext.endTenantFlow(); + ctx = null; + if (log.isDebugEnabled()) { + log.debug("Tenant flow ended"); + } + } +} diff --git a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/api/GroupManagerService.java b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/api/GroupManagerService.java index 2f3eec377d..116a2968c0 100644 --- a/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/api/GroupManagerService.java +++ b/components/device-mgt-iot/org.wso2.carbon.device.mgt.iot.api/src/main/java/org/wso2/carbon/device/mgt/iot/api/GroupManagerService.java @@ -20,8 +20,6 @@ package org.wso2.carbon.device.mgt.iot.api; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -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.PaginationResult; @@ -29,10 +27,10 @@ import org.wso2.carbon.device.mgt.group.common.DeviceGroup; import org.wso2.carbon.device.mgt.group.common.GroupManagementException; import org.wso2.carbon.device.mgt.group.common.GroupUser; import org.wso2.carbon.device.mgt.group.core.providers.GroupManagementServiceProvider; +import org.wso2.carbon.device.mgt.iot.AbstractManagerService; import javax.jws.WebService; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; import javax.ws.rs.GET; @@ -48,7 +46,7 @@ import java.util.Date; import java.util.List; @WebService -public class GroupManagerService { +public class GroupManagerService extends AbstractManagerService { private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String DEFAULT_OPERATOR_ROLE = "invoke-device-operations"; @@ -66,53 +64,33 @@ public class GroupManagerService { private static Log log = LogFactory.getLog(GroupManagerService.class); @Context //injected response proxy supporting multiple threads private HttpServletResponse response; - private PrivilegedCarbonContext ctx; - private GroupManagementServiceProvider getServiceProvider() { - String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - PrivilegedCarbonContext.startTenantFlow(); - ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - ctx.setTenantDomain(tenantDomain, true); - if (log.isDebugEnabled()) { - log.debug("Getting thread local carbon context for tenant domain: " + tenantDomain); - } - return (GroupManagementServiceProvider) ctx.getOSGiService(GroupManagementServiceProvider.class, null); - } - - private void endTenantFlow() { - PrivilegedCarbonContext.endTenantFlow(); - ctx = null; - if (log.isDebugEnabled()) { - log.debug("Tenant flow ended"); - } - } - - @Path("/group") + @Path("/groups") @POST - @Consumes("application/json") @Produces("application/json") - public boolean createGroup(@FormParam("name") String name, @FormParam("username") String username, + public boolean createGroup(@FormParam("name") String name, @FormParam("userName") String userName, @FormParam("description") String description) { DeviceGroup group = new DeviceGroup(); group.setName(name); group.setDescription(description); - group.setOwner(username); + group.setOwner(userName); group.setDateOfCreation(new Date().getTime()); group.setDateOfLastUpdate(new Date().getTime()); boolean isAdded = false; try { - GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); - int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class); + int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE, + DEFAULT_ADMIN_PERMISSIONS); response.setStatus(Response.Status.OK.getStatusCode()); - isAdded = (groupId > 0) && groupManagementService.addSharing(username, groupId, DEFAULT_OPERATOR_ROLE, + isAdded = (groupId > 0) && groupManagementService.addSharing(userName, groupId, DEFAULT_OPERATOR_ROLE, DEFAULT_OPERATOR_PERMISSIONS); - groupManagementService.addSharing(username, groupId, DEFAULT_STATS_MONITOR_ROLE, + groupManagementService.addSharing(userName, groupId, DEFAULT_STATS_MONITOR_ROLE, DEFAULT_STATS_MONITOR_PERMISSIONS); - groupManagementService.addSharing(username, groupId, DEFAULT_VIEW_POLICIES, + groupManagementService.addSharing(userName, groupId, DEFAULT_VIEW_POLICIES, DEFAULT_VIEW_POLICIES_PERMISSIONS); - groupManagementService.addSharing(username, groupId, DEFAULT_MANAGE_POLICIES, + groupManagementService.addSharing(userName, groupId, DEFAULT_MANAGE_POLICIES, DEFAULT_MANAGE_POLICIES_PERMISSIONS); - groupManagementService.addSharing(username, groupId, DEFAULT_VIEW_EVENTS, + groupManagementService.addSharing(userName, groupId, DEFAULT_VIEW_EVENTS, DEFAULT_VIEW_EVENTS_PERMISSIONS); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); @@ -123,22 +101,21 @@ public class GroupManagerService { return isAdded; } - @Path("/group/id/{groupId}") + @Path("/groups/{groupId}") @PUT - @Consumes("application/json") @Produces("application/json") public boolean updateGroup(@PathParam("groupId") int groupId, @FormParam("name") String name, - @FormParam("username") String username, @FormParam("description") String description) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/modify")){ + @FormParam("userName") String userName, @FormParam("description") String description) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/modify")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } try { - GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); + GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class); DeviceGroup group = groupManagementService.getGroup(groupId); group.setName(name); group.setDescription(description); - group.setOwner(username); + group.setOwner(userName); group.setDateOfLastUpdate(new Date().getTime()); response.setStatus(Response.Status.OK.getStatusCode()); groupManagementService.updateGroup(group); @@ -152,19 +129,19 @@ public class GroupManagerService { } } - @Path("/group/id/{groupId}") + @Path("/groups/{groupId}") @DELETE - @Consumes("application/json") @Produces("application/json") - public boolean deleteGroup(@PathParam("groupId") int groupId, @QueryParam("username") String username) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/delete")){ + public boolean deleteGroup(@PathParam("groupId") int groupId, @QueryParam("userName") String userName) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/delete")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } boolean isDeleted = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isDeleted = this.getServiceProvider().deleteGroup(groupId); + isDeleted = this.getServiceProvider(GroupManagementServiceProvider.class).deleteGroup( + groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -174,15 +151,14 @@ public class GroupManagerService { return isDeleted; } - @Path("/group/id/{groupId}") + @Path("/groups/{groupId}") @GET - @Consumes("application/json") @Produces("application/json") - public DeviceGroup getGroup(@PathParam("groupId") int groupId, @FormParam("username") String username) { + public DeviceGroup getGroup(@PathParam("groupId") int groupId) { DeviceGroup deviceGroup = null; try { response.setStatus(Response.Status.OK.getStatusCode()); - deviceGroup = this.getServiceProvider().getGroup(groupId); + deviceGroup = this.getServiceProvider(GroupManagementServiceProvider.class).getGroup(groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -192,14 +168,14 @@ public class GroupManagerService { return deviceGroup; } - @Path("/group/name/{groupName}") + //get groups for autocomplete + @Path("/groups/search") @GET - @Consumes("application/json") @Produces("application/json") - public DeviceGroup[] findGroups(@PathParam("groupName") String groupName, @FormParam("username") String username) { + public DeviceGroup[] findGroups(@QueryParam("groupName") String groupName, @QueryParam("userName") String userName) { DeviceGroup[] deviceGroups = null; try { - List groups = this.getServiceProvider().findGroups(groupName, username); + List groups = this.getServiceProvider(GroupManagementServiceProvider.class).findGroups(groupName, userName); deviceGroups = new DeviceGroup[groups.size()]; response.setStatus(Response.Status.OK.getStatusCode()); groups.toArray(deviceGroups); @@ -212,20 +188,20 @@ public class GroupManagerService { return deviceGroups; } - @Path("/group/user/{username}/all") + //get groups by an specific permision + @Path("/groups") @GET - @Consumes("application/json") @Produces("application/json") - public DeviceGroup[] getGroups(@PathParam("username") String username, + public DeviceGroup[] getGroups(@QueryParam("userName") String userName, @QueryParam("permission") String permission) { DeviceGroup[] deviceGroups = null; try { - GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); + GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class); List groups; if(permission != null){ - groups = groupManagementService.getGroups(username, permission); + groups = groupManagementService.getGroups(userName, permission); }else{ - groups = groupManagementService.getGroups(username); + groups = groupManagementService.getGroups(userName); } deviceGroups = new DeviceGroup[groups.size()]; response.setStatus(Response.Status.OK.getStatusCode()); @@ -239,15 +215,15 @@ public class GroupManagerService { return deviceGroups; } - @Path("/group/user/{username}/all/count") + @Path("/groups/count") @GET - @Consumes("application/json") @Produces("application/json") - public int getGroupCount(@PathParam("username") String username) { + public int getGroupCount(@QueryParam("userName") String userName) { int count = -1; try { response.setStatus(Response.Status.OK.getStatusCode()); - count = this.getServiceProvider().getGroupCount(username); + count = this.getServiceProvider(GroupManagementServiceProvider.class).getGroupCount( + userName); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -257,20 +233,20 @@ public class GroupManagerService { return count; } - @Path("/group/id/{groupId}/share") + @Path("/groups/{groupId}/share") @POST - @Consumes("application/json") @Produces("application/json") - public boolean shareGroup(@FormParam("username") String username, @FormParam("shareUser") String shareUser, - @PathParam("groupId") int groupId, @FormParam("role") String sharingRole) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ + public boolean shareGroup(@FormParam("userName") String userName, @FormParam("shareUser") String shareUser, + @PathParam("groupId") int groupId, @FormParam("roleName") String sharingRole) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } boolean isShared = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isShared = this.getServiceProvider().shareGroup(shareUser, groupId, sharingRole); + isShared = this.getServiceProvider(GroupManagementServiceProvider.class).shareGroup( + shareUser, groupId, sharingRole); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -280,20 +256,20 @@ public class GroupManagerService { return isShared; } - @Path("/group/id/{groupId}/unshare") + @Path("/groups/{groupId}/unshare") @POST - @Consumes("application/json") @Produces("application/json") - public boolean unShareGroup(@FormParam("username") String username, @FormParam("unShareUser") String unShareUser, - @PathParam("groupId") int groupId, @FormParam("role") String sharingRole) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ + public boolean unShareGroup(@FormParam("userName") String userName, @FormParam("unShareUser") String unShareUser, + @PathParam("groupId") int groupId, @FormParam("roleName") String sharingRole) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } boolean isUnShared = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isUnShared = this.getServiceProvider().unShareGroup(unShareUser, groupId, sharingRole); + isUnShared = this.getServiceProvider(GroupManagementServiceProvider.class).unShareGroup( + unShareUser, groupId, sharingRole); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -303,20 +279,21 @@ public class GroupManagerService { return isUnShared; } - @Path("/group/id/{groupId}/role") + //add sharing permissions + @Path("/groups/{groupId}/share/roles/{roleName}/permissions") @POST - @Consumes("application/json") @Produces("application/json") - public boolean addSharing(@FormParam("username") String username, @PathParam("groupId") int groupId, - @FormParam("role") String roleName, @FormParam("permissions") String[] permissions) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ + public boolean addSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId, + @PathParam("roleName") String roleName, @FormParam("permissions") String[] permissions) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } boolean isAdded = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isAdded = this.getServiceProvider().addSharing(username, groupId, roleName, permissions); + isAdded = this.getServiceProvider(GroupManagementServiceProvider.class).addSharing( + userName, groupId, roleName, permissions); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -326,19 +303,19 @@ public class GroupManagerService { return isAdded; } - @Path("/group/id/{groupId}/role/{role}") + //remove sharing permissions + @Path("/groups/{groupId}/share/roles/{roleName}/permissions") @DELETE - @Consumes("application/json") @Produces("application/json") - public boolean removeSharing(@QueryParam("username") String username, @PathParam("groupId") int groupId, - @PathParam("role") String roleName) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ + public boolean removeSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId, + @PathParam("roleName") String roleName) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); } boolean isRemoved = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isRemoved = this.getServiceProvider().removeSharing(groupId, roleName); + isRemoved = this.getServiceProvider(GroupManagementServiceProvider.class).removeSharing(groupId, roleName); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -348,34 +325,19 @@ public class GroupManagerService { return isRemoved; } - @Path("/group/id/{groupId}/role/all") + @Path("/groups/{groupId}/share/roles") @GET - @Consumes("application/json") @Produces("application/json") - public String[] getRoles(@PathParam("groupId") int groupId) { + public String[] getRoles(@PathParam("groupId") int groupId, @QueryParam("userName") String userName) { String[] rolesArray = null; try { - List roles = this.getServiceProvider().getRoles(groupId); - rolesArray = new String[roles.size()]; - response.setStatus(Response.Status.OK.getStatusCode()); - roles.toArray(rolesArray); - } catch (GroupManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - log.error(e.getErrorMessage(), e); - } finally { - this.endTenantFlow(); - } - return rolesArray; - } - - @Path("/group/id/{groupId}/{user}/role/all") - @GET - @Consumes("application/json") - @Produces("application/json") - public String[] getRoles(@PathParam("user") String user, @PathParam("groupId") int groupId) { - String[] rolesArray = null; - try { - List roles = this.getServiceProvider().getRoles(user, groupId); + List roles = null; + if(userName != null && !userName.isEmpty()) { + roles = this.getServiceProvider(GroupManagementServiceProvider.class).getRoles( + userName, groupId); + }else { + roles = this.getServiceProvider(GroupManagementServiceProvider.class).getRoles(groupId); + } rolesArray = new String[roles.size()]; response.setStatus(Response.Status.OK.getStatusCode()); roles.toArray(rolesArray); @@ -388,14 +350,13 @@ public class GroupManagerService { return rolesArray; } - @Path("/group/id/{groupId}/user/all") + @Path("/groups/{groupId}/users") @GET - @Consumes("application/json") @Produces("application/json") public GroupUser[] getUsers(@PathParam("groupId") int groupId) { GroupUser[] usersArray = null; try { - List users = this.getServiceProvider().getUsers(groupId); + List users = this.getServiceProvider(GroupManagementServiceProvider.class).getUsers(groupId); usersArray = new GroupUser[users.size()]; response.setStatus(Response.Status.OK.getStatusCode()); users.toArray(usersArray); @@ -408,14 +369,13 @@ public class GroupManagerService { return usersArray; } - @Path("/group/id/{groupId}/device/all") + @Path("/groups/{groupId}/devices/all") @GET - @Consumes("application/json") @Produces("application/json") public Device[] getDevices(@PathParam("groupId") int groupId) { Device[] deviceArray = null; try { - List devices = this.getServiceProvider().getDevices(groupId); + List devices = this.getServiceProvider(GroupManagementServiceProvider.class).getDevices(groupId); deviceArray = new Device[devices.size()]; response.setStatus(Response.Status.OK.getStatusCode()); devices.toArray(deviceArray); @@ -428,13 +388,12 @@ public class GroupManagerService { return deviceArray; } - @Path("/group/id/{groupId}/device/count") + @Path("/groups/{groupId}/devices/count") @GET - @Consumes("application/json") @Produces("application/json") public int getDeviceCount(@PathParam("groupId") int groupId) { try { - return this.getServiceProvider().getDeviceCount(groupId); + return this.getServiceProvider(GroupManagementServiceProvider.class).getDeviceCount(groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -444,15 +403,14 @@ public class GroupManagerService { } } - @Path("/group/id/{groupId}/device") + @Path("/groups/{groupId}/devices") @GET - @Consumes("application/json") @Produces("application/json") public PaginationResult getDevices(@PathParam("groupId") int groupId, @QueryParam("index") int index, @QueryParam("limit") int limit) { try { - return this.getServiceProvider().getDevices(groupId, index, limit); + return this.getServiceProvider(GroupManagementServiceProvider.class).getDevices(groupId, index, limit); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -462,13 +420,12 @@ public class GroupManagerService { } } - @Path("/group/id/{groupId}/device/assign") + @Path("/groups/{groupId}/devices/{deviceType}/{deviceId}") @PUT - @Consumes("application/json") @Produces("application/json") - public boolean addDevice(@PathParam("groupId") int groupId, @FormParam("deviceId") String deviceId, - @FormParam("deviceType") String deviceType, @FormParam("username") String username) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/add_devices")){ + public boolean addDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId, + @PathParam("deviceType") String deviceType, @FormParam("userName") String userName) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/add_devices")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } @@ -476,7 +433,7 @@ public class GroupManagerService { try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); response.setStatus(Response.Status.OK.getStatusCode()); - isAdded = this.getServiceProvider().addDevice(deviceIdentifier, groupId); + isAdded = this.getServiceProvider(GroupManagementServiceProvider.class).addDevice(deviceIdentifier, groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -486,13 +443,12 @@ public class GroupManagerService { return isAdded; } - @Path("/group/id/{groupId}/device/assign") + @Path("/groups/{groupId}/devices/{deviceType}/{deviceId}") @DELETE - @Consumes("application/json") @Produces("application/json") - public boolean removeDevice(@PathParam("groupId") int groupId, @FormParam("deviceId") String deviceId, - @FormParam("deviceType") String deviceType, @FormParam("username") String username) { - if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/remove_devices")){ + public boolean removeDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId, + @PathParam("deviceType") String deviceType) { + if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/remove_devices")){ response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); return false; } @@ -500,7 +456,7 @@ public class GroupManagerService { try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); response.setStatus(Response.Status.OK.getStatusCode()); - isRemoved = this.getServiceProvider().removeDevice(deviceIdentifier, groupId); + isRemoved = this.getServiceProvider(GroupManagementServiceProvider.class).removeDevice(deviceIdentifier, groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -510,15 +466,14 @@ public class GroupManagerService { return isRemoved; } - @Path("/group/id/{groupId}/user/{username}/permissions") + @Path("/groups/{groupId}/users/{userName}/permissions") @GET - @Consumes("application/json") @Produces("application/json") - public String[] getPermissions(@PathParam("username") String username, @PathParam("groupId") int groupId) { + public String[] getPermissions(@PathParam("userName") String userName, @PathParam("groupId") int groupId) { String[] permissions = null; try { response.setStatus(Response.Status.OK.getStatusCode()); - permissions = this.getServiceProvider().getPermissions(username, groupId); + permissions = this.getServiceProvider(GroupManagementServiceProvider.class).getPermissions(userName, groupId); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e); @@ -528,16 +483,15 @@ public class GroupManagerService { return permissions; } - @Path("/group/id/{groupId}/user/{username}/authorized") + @Path("/groups/{groupId}/users/{userName}/authorized") @GET - @Consumes("application/json") @Produces("application/json") - public boolean isAuthorized(@PathParam("username") String username, @PathParam("groupId") int groupId, + public boolean isAuthorized(@PathParam("userName") String userName, @PathParam("groupId") int groupId, @QueryParam("permission") String permission){ boolean isAuthorized = false; try { response.setStatus(Response.Status.OK.getStatusCode()); - isAuthorized = this.getServiceProvider().isAuthorized(username, groupId, permission); + isAuthorized = this.getServiceProvider(GroupManagementServiceProvider.class).isAuthorized(userName, groupId, permission); } catch (GroupManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); log.error(e.getErrorMessage(), e);