Merge branch 'IoTS-1.0.0-M2' of https://github.com/wso2/carbon-device-mgt-plugins into IoTS-1.0.0-M2

Shabirmean 9 years ago
commit d001a1d47f

@ -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 <T> OSGi service class
* @return OSGi service
*/
protected <T> T getServiceProvider(Class<T> 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");
}
}
}

@ -20,8 +20,6 @@ package org.wso2.carbon.device.mgt.iot.api;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationResult; 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.GroupManagementException;
import org.wso2.carbon.device.mgt.group.common.GroupUser; 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.group.core.providers.GroupManagementServiceProvider;
import org.wso2.carbon.device.mgt.iot.AbstractManagerService;
import javax.jws.WebService; import javax.jws.WebService;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -48,7 +46,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
@WebService @WebService
public class GroupManagerService { public class GroupManagerService extends AbstractManagerService {
private static final String DEFAULT_ADMIN_ROLE = "admin"; private static final String DEFAULT_ADMIN_ROLE = "admin";
private static final String DEFAULT_OPERATOR_ROLE = "invoke-device-operations"; 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); private static Log log = LogFactory.getLog(GroupManagerService.class);
@Context //injected response proxy supporting multiple threads @Context //injected response proxy supporting multiple threads
private HttpServletResponse response; private HttpServletResponse response;
private PrivilegedCarbonContext ctx;
private GroupManagementServiceProvider getServiceProvider() { @Path("/groups")
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")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean createGroup(@FormParam("name") String name, @FormParam("username") String username, public boolean createGroup(@FormParam("groupName") String groupName, @FormParam("userName") String userName,
@FormParam("description") String description) { @FormParam("description") String description) {
DeviceGroup group = new DeviceGroup(); DeviceGroup group = new DeviceGroup();
group.setName(name); group.setName(groupName);
group.setDescription(description); group.setDescription(description);
group.setOwner(username); group.setOwner(userName);
group.setDateOfCreation(new Date().getTime()); group.setDateOfCreation(new Date().getTime());
group.setDateOfLastUpdate(new Date().getTime()); group.setDateOfLastUpdate(new Date().getTime());
boolean isAdded = false; boolean isAdded = false;
try { try {
GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class);
int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); int groupId = groupManagementService.createGroup(group, DEFAULT_ADMIN_ROLE,
DEFAULT_ADMIN_PERMISSIONS);
response.setStatus(Response.Status.OK.getStatusCode()); 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); DEFAULT_OPERATOR_PERMISSIONS);
groupManagementService.addSharing(username, groupId, DEFAULT_STATS_MONITOR_ROLE, groupManagementService.addSharing(userName, groupId, DEFAULT_STATS_MONITOR_ROLE,
DEFAULT_STATS_MONITOR_PERMISSIONS); DEFAULT_STATS_MONITOR_PERMISSIONS);
groupManagementService.addSharing(username, groupId, DEFAULT_VIEW_POLICIES, groupManagementService.addSharing(userName, groupId, DEFAULT_VIEW_POLICIES,
DEFAULT_VIEW_POLICIES_PERMISSIONS); DEFAULT_VIEW_POLICIES_PERMISSIONS);
groupManagementService.addSharing(username, groupId, DEFAULT_MANAGE_POLICIES, groupManagementService.addSharing(userName, groupId, DEFAULT_MANAGE_POLICIES,
DEFAULT_MANAGE_POLICIES_PERMISSIONS); DEFAULT_MANAGE_POLICIES_PERMISSIONS);
groupManagementService.addSharing(username, groupId, DEFAULT_VIEW_EVENTS, groupManagementService.addSharing(userName, groupId, DEFAULT_VIEW_EVENTS,
DEFAULT_VIEW_EVENTS_PERMISSIONS); DEFAULT_VIEW_EVENTS_PERMISSIONS);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
@ -123,22 +101,21 @@ public class GroupManagerService {
return isAdded; return isAdded;
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@PUT @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean updateGroup(@PathParam("groupId") int groupId, @FormParam("name") String name, public boolean updateGroup(@PathParam("groupId") int groupId, @FormParam("groupName") String groupName,
@FormParam("username") String username, @FormParam("description") String description) { @FormParam("userName") String userName, @FormParam("description") String description) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/modify")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/modify")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
try { try {
GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class);
DeviceGroup group = groupManagementService.getGroup(groupId); DeviceGroup group = groupManagementService.getGroup(groupId);
group.setName(name); group.setName(groupName);
group.setDescription(description); group.setDescription(description);
group.setOwner(username); group.setOwner(userName);
group.setDateOfLastUpdate(new Date().getTime()); group.setDateOfLastUpdate(new Date().getTime());
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
groupManagementService.updateGroup(group); groupManagementService.updateGroup(group);
@ -152,19 +129,19 @@ public class GroupManagerService {
} }
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@DELETE @DELETE
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean deleteGroup(@PathParam("groupId") int groupId, @QueryParam("username") String username) { public boolean deleteGroup(@PathParam("groupId") int groupId, @QueryParam("userName") String userName) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/delete")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/delete")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
boolean isDeleted = false; boolean isDeleted = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
isDeleted = this.getServiceProvider().deleteGroup(groupId); isDeleted = this.getServiceProvider(GroupManagementServiceProvider.class).deleteGroup(
groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -174,15 +151,14 @@ public class GroupManagerService {
return isDeleted; return isDeleted;
} }
@Path("/group/id/{groupId}") @Path("/groups/{groupId}")
@GET @GET
@Consumes("application/json")
@Produces("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; DeviceGroup deviceGroup = null;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
deviceGroup = this.getServiceProvider().getGroup(groupId); deviceGroup = this.getServiceProvider(GroupManagementServiceProvider.class).getGroup(groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -192,14 +168,14 @@ public class GroupManagerService {
return deviceGroup; return deviceGroup;
} }
@Path("/group/name/{groupName}") //get groups for autocomplete
@Path("/groups/search")
@GET @GET
@Consumes("application/json")
@Produces("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; DeviceGroup[] deviceGroups = null;
try { try {
List<DeviceGroup> groups = this.getServiceProvider().findGroups(groupName, username); List<DeviceGroup> groups = this.getServiceProvider(GroupManagementServiceProvider.class).findGroups(groupName, userName);
deviceGroups = new DeviceGroup[groups.size()]; deviceGroups = new DeviceGroup[groups.size()];
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
groups.toArray(deviceGroups); groups.toArray(deviceGroups);
@ -212,20 +188,20 @@ public class GroupManagerService {
return deviceGroups; return deviceGroups;
} }
@Path("/group/user/{username}/all") //get groups by an specific permision
@Path("/groups")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public DeviceGroup[] getGroups(@PathParam("username") String username, public DeviceGroup[] getGroups(@QueryParam("userName") String userName,
@QueryParam("permission") String permission) { @QueryParam("permission") String permission) {
DeviceGroup[] deviceGroups = null; DeviceGroup[] deviceGroups = null;
try { try {
GroupManagementServiceProvider groupManagementService = this.getServiceProvider(); GroupManagementServiceProvider groupManagementService = this.getServiceProvider(GroupManagementServiceProvider.class);
List<DeviceGroup> groups; List<DeviceGroup> groups;
if(permission != null){ if(permission != null){
groups = groupManagementService.getGroups(username, permission); groups = groupManagementService.getGroups(userName, permission);
}else{ }else{
groups = groupManagementService.getGroups(username); groups = groupManagementService.getGroups(userName);
} }
deviceGroups = new DeviceGroup[groups.size()]; deviceGroups = new DeviceGroup[groups.size()];
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
@ -239,15 +215,15 @@ public class GroupManagerService {
return deviceGroups; return deviceGroups;
} }
@Path("/group/user/{username}/all/count") @Path("/groups/count")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public int getGroupCount(@PathParam("username") String username) { public int getGroupCount(@QueryParam("userName") String userName) {
int count = -1; int count = -1;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
count = this.getServiceProvider().getGroupCount(username); count = this.getServiceProvider(GroupManagementServiceProvider.class).getGroupCount(
userName);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -257,20 +233,20 @@ public class GroupManagerService {
return count; return count;
} }
@Path("/group/id/{groupId}/share") @Path("/groups/{groupId}/share")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean shareGroup(@FormParam("username") String username, @FormParam("shareUser") String shareUser, public boolean shareGroup(@FormParam("userName") String userName, @FormParam("shareUser") String shareUser,
@PathParam("groupId") int groupId, @FormParam("role") String sharingRole) { @PathParam("groupId") int groupId, @FormParam("roleName") String sharingRole) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
boolean isShared = false; boolean isShared = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); 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) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -280,20 +256,20 @@ public class GroupManagerService {
return isShared; return isShared;
} }
@Path("/group/id/{groupId}/unshare") @Path("/groups/{groupId}/unshare")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean unShareGroup(@FormParam("username") String username, @FormParam("unShareUser") String unShareUser, public boolean unShareGroup(@FormParam("userName") String userName, @FormParam("unShareUser") String unShareUser,
@PathParam("groupId") int groupId, @FormParam("role") String sharingRole) { @PathParam("groupId") int groupId, @FormParam("roleName") String sharingRole) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
boolean isUnShared = false; boolean isUnShared = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); 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) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -303,20 +279,21 @@ public class GroupManagerService {
return isUnShared; return isUnShared;
} }
@Path("/group/id/{groupId}/role") //add sharing permissions
@Path("/groups/{groupId}/share/roles/{roleName}/permissions")
@POST @POST
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean addSharing(@FormParam("username") String username, @PathParam("groupId") int groupId, public boolean addSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId,
@FormParam("role") String roleName, @FormParam("permissions") String[] permissions) { @PathParam("roleName") String roleName, @FormParam("permissions") String[] permissions) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
boolean isAdded = false; boolean isAdded = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); 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) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -326,19 +303,19 @@ public class GroupManagerService {
return isAdded; return isAdded;
} }
@Path("/group/id/{groupId}/role/{role}") //remove sharing permissions
@Path("/groups/{groupId}/share/roles/{roleName}/permissions")
@DELETE @DELETE
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean removeSharing(@QueryParam("username") String username, @PathParam("groupId") int groupId, public boolean removeSharing(@QueryParam("userName") String userName, @PathParam("groupId") int groupId,
@PathParam("role") String roleName) { @PathParam("roleName") String roleName) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/share")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/share")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
} }
boolean isRemoved = false; boolean isRemoved = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
isRemoved = this.getServiceProvider().removeSharing(groupId, roleName); isRemoved = this.getServiceProvider(GroupManagementServiceProvider.class).removeSharing(groupId, roleName);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -348,34 +325,19 @@ public class GroupManagerService {
return isRemoved; return isRemoved;
} }
@Path("/group/id/{groupId}/role/all") @Path("/groups/{groupId}/share/roles")
@GET @GET
@Consumes("application/json")
@Produces("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; String[] rolesArray = null;
try { try {
List<String> roles = this.getServiceProvider().getRoles(groupId); List<String> roles = null;
rolesArray = new String[roles.size()]; if(userName != null && !userName.isEmpty()) {
response.setStatus(Response.Status.OK.getStatusCode()); roles = this.getServiceProvider(GroupManagementServiceProvider.class).getRoles(
roles.toArray(rolesArray); userName, groupId);
} catch (GroupManagementException e) { }else {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); roles = this.getServiceProvider(GroupManagementServiceProvider.class).getRoles(groupId);
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<String> roles = this.getServiceProvider().getRoles(user, groupId);
rolesArray = new String[roles.size()]; rolesArray = new String[roles.size()];
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
roles.toArray(rolesArray); roles.toArray(rolesArray);
@ -388,14 +350,13 @@ public class GroupManagerService {
return rolesArray; return rolesArray;
} }
@Path("/group/id/{groupId}/user/all") @Path("/groups/{groupId}/users")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public GroupUser[] getUsers(@PathParam("groupId") int groupId) { public GroupUser[] getUsers(@PathParam("groupId") int groupId) {
GroupUser[] usersArray = null; GroupUser[] usersArray = null;
try { try {
List<GroupUser> users = this.getServiceProvider().getUsers(groupId); List<GroupUser> users = this.getServiceProvider(GroupManagementServiceProvider.class).getUsers(groupId);
usersArray = new GroupUser[users.size()]; usersArray = new GroupUser[users.size()];
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
users.toArray(usersArray); users.toArray(usersArray);
@ -408,14 +369,13 @@ public class GroupManagerService {
return usersArray; return usersArray;
} }
@Path("/group/id/{groupId}/device/all") @Path("/groups/{groupId}/devices/all")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public Device[] getDevices(@PathParam("groupId") int groupId) { public Device[] getDevices(@PathParam("groupId") int groupId) {
Device[] deviceArray = null; Device[] deviceArray = null;
try { try {
List<Device> devices = this.getServiceProvider().getDevices(groupId); List<Device> devices = this.getServiceProvider(GroupManagementServiceProvider.class).getDevices(groupId);
deviceArray = new Device[devices.size()]; deviceArray = new Device[devices.size()];
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
devices.toArray(deviceArray); devices.toArray(deviceArray);
@ -428,13 +388,12 @@ public class GroupManagerService {
return deviceArray; return deviceArray;
} }
@Path("/group/id/{groupId}/device/count") @Path("/groups/{groupId}/devices/count")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public int getDeviceCount(@PathParam("groupId") int groupId) { public int getDeviceCount(@PathParam("groupId") int groupId) {
try { try {
return this.getServiceProvider().getDeviceCount(groupId); return this.getServiceProvider(GroupManagementServiceProvider.class).getDeviceCount(groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -444,15 +403,14 @@ public class GroupManagerService {
} }
} }
@Path("/group/id/{groupId}/device") @Path("/groups/{groupId}/devices")
@GET @GET
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public PaginationResult getDevices(@PathParam("groupId") int groupId, public PaginationResult getDevices(@PathParam("groupId") int groupId,
@QueryParam("index") int index, @QueryParam("index") int index,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
try { try {
return this.getServiceProvider().getDevices(groupId, index, limit); return this.getServiceProvider(GroupManagementServiceProvider.class).getDevices(groupId, index, limit);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); 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 @PUT
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean addDevice(@PathParam("groupId") int groupId, @FormParam("deviceId") String deviceId, public boolean addDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId,
@FormParam("deviceType") String deviceType, @FormParam("username") String username) { @PathParam("deviceType") String deviceType, @FormParam("userName") String userName) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/add_devices")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/add_devices")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
@ -476,7 +433,7 @@ public class GroupManagerService {
try { try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
isAdded = this.getServiceProvider().addDevice(deviceIdentifier, groupId); isAdded = this.getServiceProvider(GroupManagementServiceProvider.class).addDevice(deviceIdentifier, groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -486,13 +443,12 @@ public class GroupManagerService {
return isAdded; return isAdded;
} }
@Path("/group/id/{groupId}/device/assign") @Path("/groups/{groupId}/devices/{deviceType}/{deviceId}")
@DELETE @DELETE
@Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public boolean removeDevice(@PathParam("groupId") int groupId, @FormParam("deviceId") String deviceId, public boolean removeDevice(@PathParam("groupId") int groupId, @PathParam("deviceId") String deviceId,
@FormParam("deviceType") String deviceType, @FormParam("username") String username) { @PathParam("deviceType") String deviceType) {
if (!isAuthorized(username, groupId, "/permission/device-mgt/admin/groups/remove_devices")){ if (!isAuthorized(getCurrentUserName(), groupId, "/permission/device-mgt/admin/groups/remove_devices")){
response.setStatus(Response.Status.FORBIDDEN.getStatusCode()); response.setStatus(Response.Status.FORBIDDEN.getStatusCode());
return false; return false;
} }
@ -500,7 +456,7 @@ public class GroupManagerService {
try { try {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
isRemoved = this.getServiceProvider().removeDevice(deviceIdentifier, groupId); isRemoved = this.getServiceProvider(GroupManagementServiceProvider.class).removeDevice(deviceIdentifier, groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -510,15 +466,14 @@ public class GroupManagerService {
return isRemoved; return isRemoved;
} }
@Path("/group/id/{groupId}/user/{username}/permissions") @Path("/groups/{groupId}/users/{userName}/permissions")
@GET @GET
@Consumes("application/json")
@Produces("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; String[] permissions = null;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
permissions = this.getServiceProvider().getPermissions(username, groupId); permissions = this.getServiceProvider(GroupManagementServiceProvider.class).getPermissions(userName, groupId);
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);
@ -528,16 +483,15 @@ public class GroupManagerService {
return permissions; return permissions;
} }
@Path("/group/id/{groupId}/user/{username}/authorized") @Path("/groups/{groupId}/users/{userName}/authorized")
@GET @GET
@Consumes("application/json")
@Produces("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){ @QueryParam("permission") String permission){
boolean isAuthorized = false; boolean isAuthorized = false;
try { try {
response.setStatus(Response.Status.OK.getStatusCode()); 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) { } catch (GroupManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
log.error(e.getErrorMessage(), e); log.error(e.getErrorMessage(), e);

@ -2,7 +2,6 @@
"deviceType": { "deviceType": {
"label": "Virtual Firealarm", "label": "Virtual Firealarm",
"category": "virtual", "category": "virtual",
"downloadAgentUri": "manager/device/virtual_firealarm/download",
"downloadAgentLinkGenUri": "manager/device/virtual_firealarm/generate_link" "downloadAgentLinkGenUri": "manager/device/virtual_firealarm/generate_link"
}, },
"analyticStreams": [ "analyticStreams": [

@ -61,9 +61,9 @@
<br/><br/> <br/><br/>
<input type="hidden" class="deviceType" name="deviceType" <input type="hidden" class="deviceType" name="deviceType"
value="{{@uriParams.deviceType}}"/> value="{{@uriParams.deviceType}}"/>
<input type="radio" name="sketchType" value="simple_agent" id="simple_agent" checked> <input type="radio" name="sketchType" value="virtual_firealarm" id="virtual_firealarm" checked>
<label for="simple_agent">Simple Agent</label> &nbsp;&nbsp; <label for="simple_agent">Simple Agent</label> &nbsp;&nbsp;
<input type="radio" name="sketchType" value="advanced_agent" id="advanced_agent"> <input type="radio" name="sketchType" value="virtual_firealarm_advanced" id="virtual_firealarm_advanced">
<label for="advanced_agent">Advanced Agent</label> <label for="advanced_agent">Advanced Agent</label>
</div> </div>
</div> </div>

Loading…
Cancel
Save