diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/pom.xml index 35c9b3526f..c25a725a24 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.annotations/pom.xml @@ -22,13 +22,13 @@ apimgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml 4.0.0 org.wso2.carbon.apimgt.annotations - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT bundle WSO2 Carbon - API Management Annotations WSO2 Carbon - API Management Custom Annotation Module diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/pom.xml index 94914c3436..541c1cf744 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/pom.xml @@ -21,12 +21,12 @@ apimgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml 4.0.0 - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT org.wso2.carbon.apimgt.application.extension.api war WSO2 Carbon - API Application Management API diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/pom.xml index f5dcc64a25..1658e255d7 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/pom.xml @@ -22,12 +22,12 @@ apimgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml 4.0.0 - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT org.wso2.carbon.apimgt.application.extension bundle WSO2 Carbon - API Application Management diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java index 51be025f2b..f205419084 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension/src/main/java/org/wso2/carbon/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -33,7 +33,6 @@ import org.wso2.carbon.apimgt.impl.APIManagerFactory; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import java.util.Date; import java.util.Map; import java.util.Set; diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml index 1b3a0ebb4b..71769abc48 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.webapp.publisher/pom.xml @@ -22,13 +22,13 @@ apimgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml 4.0.0 org.wso2.carbon.apimgt.webapp.publisher - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT bundle WSO2 Carbon - API Management Webapp Publisher WSO2 Carbon - API Management Webapp Publisher diff --git a/components/apimgt-extensions/pom.xml b/components/apimgt-extensions/pom.xml index 8f38ed4b2d..d4d363786d 100644 --- a/components/apimgt-extensions/pom.xml +++ b/components/apimgt-extensions/pom.xml @@ -22,13 +22,13 @@ org.wso2.carbon.devicemgt carbon-devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../../pom.xml 4.0.0 apimgt-extensions - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT pom WSO2 Carbon - API Management Extensions Component http://wso2.org diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/pom.xml index 2bfe2c2519..dd6f47d9bb 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.api/pom.xml @@ -22,7 +22,7 @@ certificate-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/pom.xml index 4c655c8511..ade79eddc5 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.cert.admin.api/pom.xml @@ -22,7 +22,7 @@ certificate-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml index c1ee115741..573b3dfbc5 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/pom.xml @@ -21,13 +21,13 @@ org.wso2.carbon.devicemgt certificate-mgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml 4.0.0 org.wso2.carbon.certificate.mgt.core - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT bundle WSO2 Carbon - Certificate Management Core WSO2 Carbon - Certificate Management Core diff --git a/components/certificate-mgt/pom.xml b/components/certificate-mgt/pom.xml index 1ef6cab841..4186644877 100644 --- a/components/certificate-mgt/pom.xml +++ b/components/certificate-mgt/pom.xml @@ -22,14 +22,14 @@ org.wso2.carbon.devicemgt carbon-devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../../pom.xml 4.0.0 org.wso2.carbon.devicemgt certificate-mgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT pom WSO2 Carbon - Certificate Management Component http://wso2.org diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm/pom.xml index 64496be804..0d16db4fac 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm/pom.xml @@ -22,7 +22,7 @@ device-mgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml index 0d7f75bc16..8270ba7386 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml @@ -22,7 +22,7 @@ device-mgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java index 29d623b349..fae2658228 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -77,17 +78,22 @@ public class MQTTNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { Map dynamicProperties = new HashMap<>(); - Properties properties = ctx.getOperation().getProperties(); + Operation operation = ctx.getOperation(); + Properties properties = operation.getProperties(); if (properties != null && properties.get(MQTT_ADAPTER_TOPIC) != null) { dynamicProperties.put("topic", (String) properties.get(MQTT_ADAPTER_TOPIC)); } else { String topic = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true) + "/" - + ctx.getDeviceId().getType() + "/" + ctx.getDeviceId().getId() + "/" + ctx.getOperation().getType(); + + ctx.getDeviceId().getType() + "/" + ctx.getDeviceId().getId() + "/" + operation.getType() + + "/" + operation.getCode(); dynamicProperties.put("topic", topic); + if (operation.getPayLoad() == null) { + operation.setPayLoad(""); + } } MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, - ctx.getOperation().getPayLoad()); + operation.getPayLoad()); } @Override diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml index 6f92797c92..50df0a6e2d 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml @@ -22,7 +22,7 @@ device-mgt-extensions org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt-extensions/pom.xml b/components/device-mgt-extensions/pom.xml index 44152d8ebd..4efadd9980 100644 --- a/components/device-mgt-extensions/pom.xml +++ b/components/device-mgt-extensions/pom.xml @@ -22,7 +22,7 @@ carbon-devicemgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/pom.xml index d92dceb939..bfce63e997 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.dashboard/pom.xml @@ -3,7 +3,7 @@ org.wso2.carbon.devicemgt device-mgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml index ab37f25e99..6c0eeec7f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml @@ -22,7 +22,7 @@ org.wso2.carbon.devicemgt device-mgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml index 3523606ef7..f40894843b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/pom.xml @@ -22,7 +22,7 @@ device-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java index 79fbf42489..8aa54a5378 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceTypeManagementService.java @@ -18,17 +18,29 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api; -import io.swagger.annotations.*; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.ResponseHeader; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; -import javax.ws.rs.*; +import javax.validation.constraints.Size; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -@API(name = "Device Type Management", version = "1.0.0", context = "/api/device-mgt/v1.0/admin/device-types", tags = {"devicemgt_admin"}) +@API(name = "Device Type Management", version = "1.0.0", context = "/api/device-mgt/v1.0/admin/device-types", + tags = {"devicemgt_admin"}) @Path("/device-types") @Api(value = "Device Type Management", description = "This API corresponds to all tasks related to device " + @@ -60,14 +72,17 @@ public interface DeviceTypeManagementService { "Used by caches, or in conditional requests."), @ResponseHeader( name = "Last-Modified", - description = "Date and time the resource has been modified the last time.\n" + - "Used by caches, or in conditional requests."), + description = + "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), } ), @ApiResponse( code = 304, - message = "Not Modified. \n Empty body because the client has already the latest version of " + - "the requested resource."), + message = + "Not Modified. \n Empty body because the client has already the latest version of" + + " " + + "the requested resource."), @ApiResponse( code = 406, message = "Not Acceptable.\n The requested media type is not supported"), @@ -85,6 +100,66 @@ public interface DeviceTypeManagementService { value = "Validates if the requested variant has not been modified since the time specified", required = false) @HeaderParam("If-Modified-Since") - String ifModifiedSince); + String ifModifiedSince); + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the list of features for a give device type.", + notes = "Retrieves the list of features of a given device type.", + tags = "Device Type Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of supported features.", + response = DeviceTypeList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = + "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 304, + message = + "Not Modified. \n Empty body because the client has already the latest version of" + + " " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the " + + "list of supported device types.", + response = ErrorResponse.class) + } + ) + @Permission(name = "View Device Types", permission = "/device-mgt/devices/owning-device/view") + Response getFeatures( + @ApiParam( + name = "type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("type") + @Size(max = 45) + String type, + @ApiParam( + name = "If-Modified-Since", + value = "Validates if the requested variant has not been modified since the time specified", + required = false) + @HeaderParam("If-Modified-Since") + String ifModifiedSince); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index cf5b2882fa..f21d1c9a4b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -18,85 +18,96 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api; -import org.wso2.carbon.apimgt.annotations.api.Scope; +import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -//@Path("/groups") -//@Produces(MediaType.APPLICATION_JSON) -//@Consumes(MediaType.APPLICATION_JSON) +@API(name = "Group Management", version = "1.0.0", context = "/api/device-mgt/v1.0/groups", tags = {"devicemgt_admin"}) + +@Path("/groups") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) public interface GroupManagementService { -// @GET -// @Permission(scope = "group-view", permissions = {"/permission/admin/device-mgt/user/groups/list"}) -// Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, -// @QueryParam("limit") int limit); -// -// @POST -// @Permission(scope = "group-add", permissions = {"/permission/admin/device-mgt/user/groups/add"}) -// Response createGroup(DeviceGroup group); -// -// @Path("/{groupName}") -// @GET -// @Permission(scope = "group-view", permissions = {"/permission/admin/device-mgt/user/groups/view"}) -// Response getGroup(@PathParam("groupName") String groupName); -// -// @Path("/{groupName}") -// @PUT -// @Permission(scope = "group-modify", permissions = {"/permission/admin/device-mgt/user/groups/update"}) -// Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup); -// -// @Path("/{groupName}") -// @DELETE -// @Permission(scope = "group-remove", permissions = {"/permission/admin/device-mgt/user/groups/remove"}) -// Response deleteGroup(@PathParam("groupName") String groupName); -// -// @Path("/{groupName}/share-with-user") -// @POST -// @Permission(scope = "group-share", permissions = {"/permission/admin/device-mgt/user/groups/share"}) -// Response shareGroupWithUser(@PathParam("groupName") String groupName, String targetUser); -// -// @Path("/{groupName}/share-with-role") -// @POST -// @Permission(scope = "group-share", permissions = {"/permission/admin/device-mgt/user/groups/share"}) -// Response shareGroupWithRole(@PathParam("groupName") String groupName, String targetRole); -// -// @Path("/{groupName}/remove-share-with-user") -// @POST -// @Permission(scope = "group-share", permissions = {"/permission/admin/device-mgt/user/groups/unshare"}) -// Response removeShareWithUser(@PathParam("groupName") String groupName, String targetUser); -// -// @Path("/{groupName}/remove-share-with-role") -// @POST -// @Permission(scope = "group-share", permissions = {"/permission/admin/device-mgt/user/groups/unshare"}) -// Response removeShareWithRole(@PathParam("groupName") String groupName, String targetUser); -// -// @GET -// @Path("/{groupName}/users") -// @Permission(scope = "group-view", permissions = {"/permission/admin/device-mgt/user/groups/list"}) -// Response getUsersOfGroup(@PathParam("groupName") String groupName); -// -// @GET -// @Path("/{groupName}/devices") -// @Permission(scope = "group-view", permissions = {"/permission/admin/device-mgt/groups/roles"}) -// Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, -// @QueryParam("limit") int limit); -// -// @POST -// @Path("/{groupName}/devices") -// @Produces("application/json") -// @Permission(scope = "group-add", permissions = {"/permission/admin/device-mgt/user/groups/devices/add"}) -// Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier); -// -// @DELETE -// @Path("/{groupName}/devices") -// @Permission(scope = "group-remove", permissions = {"/permission/admin/device-mgt/user/groups/devices/remove"}) -// Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, -// @QueryParam("id") String id); + @GET + @Permission(name = "View Group", permission = "/permission/admin/device-mgt/user/groups/list") + Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, + @QueryParam("limit") int limit); + + @POST + @Permission(name = "Add Group", permission = "/permission/admin/device-mgt/user/groups/add") + Response createGroup(DeviceGroup group); + + @Path("/{groupName}") + @GET + @Permission(name = "View Group", permission = "/permission/admin/device-mgt/user/groups/view") + Response getGroup(@PathParam("groupName") String groupName); + + @Path("/{groupName}") + @PUT + @Permission(name = "Update Group", permission = "/permission/admin/device-mgt/user/groups/update") + Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup); + + @Path("/{groupName}") + @DELETE + @Permission(name = "Remove Groups", permission = "/permission/admin/device-mgt/user/groups/remove") + Response deleteGroup(@PathParam("groupName") String groupName); + + @Path("/{groupName}/share-with-user") + @POST + @Permission(name = "Share Group to a User", permission = "/permission/admin/device-mgt/user/groups/share") + Response shareGroupWithUser(@PathParam("groupName") String groupName, String targetUser); + + @Path("/{groupName}/share-with-role") + @POST + @Permission(name = "Share Group to a Role", permission = "/permission/admin/device-mgt/user/groups/share") + Response shareGroupWithRole(@PathParam("groupName") String groupName, String targetRole); + + @Path("/{groupName}/remove-share-with-user") + @POST + @Permission(name = "Unshare a Group", permission = "/permission/admin/device-mgt/user/groups/unshare") + Response removeShareWithUser(@PathParam("groupName") String groupName, String targetUser); + + @Path("/{groupName}/remove-share-with-role") + @POST + @Permission(name = "Unshare a Group", permission = "/permission/admin/device-mgt/user/groups/unshare") + Response removeShareWithRole(@PathParam("groupName") String groupName, String targetUser); + + @GET + @Path("/{groupName}/users") + @Permission(name = "Get Users of Group", permission = "/permission/admin/device-mgt/user/groups/list") + Response getUsersOfGroup(@PathParam("groupName") String groupName); + + @GET + @Path("/{groupName}/devices") + @Permission(name = "Get Devices of Group", permission = "/permission/admin/device-mgt/groups/roles") + Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, + @QueryParam("limit") int limit); + + @POST + @Path("/{groupName}/devices") + @Produces("application/json") + @Permission(name = "Add Device to a Group", permission = "/permission/admin/device-mgt/user/groups/devices/add") + Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier); + @DELETE + @Path("/{groupName}/devices") + @Permission(name = "Remove Devices from Group", + permission = "/permission/admin/device-mgt/user/groups/devices/remove") + Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, + @QueryParam("id") String id); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java index 0580d8504d..885005944a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -18,82 +18,92 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; -import io.swagger.annotations.*; -import org.wso2.carbon.apimgt.annotations.api.Scope; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.ResponseHeader; +import org.wso2.carbon.apimgt.annotations.api.API; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.Date; -//@Path("/admin/groups") -//@Produces(MediaType.APPLICATION_JSON) -//@Consumes(MediaType.APPLICATION_JSON) -//@Api(value = "Group Management Administrative Service", description = "This an API intended to be used by " + -// "'internal' components to log in as an admin user and do a selected number of operations. " + -// "Further, this is strictly restricted to admin users only ") -public interface GroupManagementAdminService { - -// @GET -// @ApiOperation( -// produces = MediaType.APPLICATION_JSON, -// httpMethod = "GET", -// value = "Get groups by the name.", -// notes = "Get devices the name of device and tenant.", -// response = DeviceGroupWrapper.class, -// responseContainer = "List", -// tags = "Group Management Administrative Service") -// @ApiResponses(value = { -// @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of groups.", -// response = DeviceGroupWrapper.class, -// responseContainer = "List", -// responseHeaders = { -// @ResponseHeader( -// name = "Content-Type", -// description = "The content type of the body"), -// @ResponseHeader( -// name = "ETag", -// description = "Entity Tag of the response resource.\n" + -// "Used by caches, or in conditional requests."), -// @ResponseHeader( -// name = "Last-Modified", -// description = "Date and time the resource has been modified the last time.\n" + -// "Used by caches, or in conditional requests."), -// }), -// @ApiResponse( -// code = 304, -// message = "Not Modified. \n Empty body because the client has already the latest version of the requested resource."), -// @ApiResponse( -// code = 406, -// message = "Not Acceptable.\n The requested media type is not supported"), -// @ApiResponse( -// code = 500, -// message = "Internal Server ErrorResponse. \n Server error occurred while fetching the group list.") -// }) -// @Permission(scope = "group-view", permissions = {"/permission/admin/device-mgt/user/groups/list"}) -// Response getGroupsOfUser( -// @ApiParam( -// name = "username", -// value = "Username of the user.", -// required = true) -// @QueryParam("username") String username, -// @ApiParam( -// name = "If-Modified-Since", -// value = "Timestamp of the last modified date", -// required = false) -// @HeaderParam("If-Modified-Since") String timestamp, -// @ApiParam( -// name = "offset", -// value = "Starting point within the complete list of items qualified.", -// required = false) -// @QueryParam("offset") int offset, -// @ApiParam( -// name = "limit", -// value = "Maximum size of resource array to return.", -// required = false) -// @QueryParam("limit") int limit); -// +@API(name = "Group Management Admin", version = "1.0.0", context = "/api/device-mgt/v1.0/admin/groups", tags = {"devicemgt_admin"}) +@Path("/admin/groups") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Api(value = "Group Management Administrative Service", description = "This an API intended to be used by " + + "'internal' components to log in as an admin user and do a selected number of operations. " + + "Further, this is strictly restricted to admin users only ") +public interface GroupManagementAdminService { + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get groups by the name.", + notes = "Get devices the name of device and tenant.", + response = DeviceGroupWrapper.class, + responseContainer = "List", + tags = "Group Management Administrative Service") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of groups.", + response = DeviceGroupWrapper.class, + responseContainer = "List", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of the " + + "requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server ErrorResponse. \n Server error occurred while fetching the group list.") + }) + @Permission(name = "View All Groups", permission = "/permission/admin/device-mgt/user/groups/list") + Response getGroupsOfUser( + @ApiParam( + name = "username", + value = "Username of the user.", + required = true) + @QueryParam("username") String username, + @ApiParam( + name = "If-Modified-Since", + value = "Timestamp of the last modified date", + required = false) + @HeaderParam("If-Modified-Since") String timestamp, + @ApiParam( + name = "offset", + value = "Starting point within the complete list of items qualified.", + required = false) + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Maximum size of resource array to return.", + required = false) + @QueryParam("limit") int limit); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java index e38f02a027..3bd43681e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceTypeManagementServiceImpl.java @@ -21,14 +21,20 @@ 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.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceTypeManagementService; +import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import javax.validation.constraints.Size; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import java.util.List; @@ -56,4 +62,28 @@ public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementServ } } + @GET + @Override + @Path("/{type}/features") + public Response getFeatures(@PathParam("type") @Size(max = 45) String type, @HeaderParam("If-Modified-Since") String ifModifiedSince) { + List features; + DeviceManagementProviderService dms; + try { + dms = DeviceMgtAPIUtils.getDeviceManagementService(); + FeatureManager fm = dms.getFeatureManager(type); + if (fm == null) { + return Response.status(Response.Status.NOT_FOUND).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage("No feature manager is " + + "registered with the given type '" + type + "'").build()).build(); + } + features = fm.getFeatures(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while retrieving the list of features of '" + type + "' device type"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(features).build(); + } + } 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 0b12ebb687..10e6fef2c9 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 @@ -18,10 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; -import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -33,105 +29,108 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; -//@Path("/groups") -//@Produces(MediaType.APPLICATION_JSON) -//@Consumes(MediaType.APPLICATION_JSON) +@Path("/groups") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) public class GroupManagementServiceImpl implements GroupManagementService { -// private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); -// -// @Override -// public Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, -// @QueryParam("limit") int limit) { -// try { -// List groupWrappers = new ArrayList<>(); -// GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); -// List deviceGroups = service.getGroups(user); -// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// for (DeviceGroup dg : deviceGroups) { -// DeviceGroupWrapper gw = new DeviceGroupWrapper(); -// gw.setId(dg.getId()); -// gw.setOwner(dg.getOwner()); -// gw.setName(dg.getName()); -// gw.setTenantId(tenantId); -// groupWrappers.add(gw); -// } -// return Response.status(Response.Status.OK).entity(groupWrappers).build(); -// } catch (GroupManagementException e) { -// String error = "ErrorResponse occurred while getting the groups related to users for policy."; -// log.error(error, e); -// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); -// } -// } -// -// @Override -// public Response createGroup(DeviceGroup group) { -// return null; -// } -// -// @Override -// public Response getGroup(@PathParam("groupName") String groupName) { -// return null; -// } -// -// @Override -// public Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup) { -// return null; -// } -// -// @Override -// public Response deleteGroup(@PathParam("groupName") String groupName) { -// return null; -// } -// -// @Override -// public Response shareGroupWithUser(String groupName, String targetUser) { -// return null; -// } -// -// @Override -// public Response shareGroupWithRole(String groupName, String targetRole) { -// return null; -// } -// -// @Override -// public Response removeShareWithUser(@PathParam("groupName") String groupName, -// @QueryParam("username") String targetUser) { -// return null; -// } -// -// @Override -// public Response removeShareWithRole(@PathParam("groupName") String groupName, -// @QueryParam("roleName") String targetUser) { -// return null; -// } -// -// @Override -// public Response getUsersOfGroup(@PathParam("groupName") String groupName) { -// return null; -// } -// -// @Override -// public Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, -// @QueryParam("limit") int limit) { -// return null; -// } -// -// @Override -// public Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier) { -// return null; -// } -// -// @Override -// public Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, -// @QueryParam("id") String id) { -// return null; -// } - -} + private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); + + @Override + public Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + try { + List groupWrappers = new ArrayList<>(); + GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); + List deviceGroups = service.getGroups(user); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + for (DeviceGroup dg : deviceGroups) { + DeviceGroupWrapper gw = new DeviceGroupWrapper(); + gw.setId(dg.getId()); + gw.setOwner(dg.getOwner()); + gw.setName(dg.getName()); + gw.setTenantId(tenantId); + groupWrappers.add(gw); + } + return Response.status(Response.Status.OK).entity(groupWrappers).build(); + } catch (GroupManagementException e) { + String error = "ErrorResponse occurred while getting the groups related to users for policy."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + + @Override + public Response createGroup(DeviceGroup group) { + return null; + } + + @Override + public Response getGroup(@PathParam("groupName") String groupName) { + return null; + } + + @Override + public Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup) { + return null; + } + + @Override + public Response deleteGroup(@PathParam("groupName") String groupName) { + return null; + } + + @Override + public Response shareGroupWithUser(String groupName, String targetUser) { + return null; + } + + @Override + public Response shareGroupWithRole(String groupName, String targetRole) { + return null; + } + + @Override + public Response removeShareWithUser(@PathParam("groupName") String groupName, + @QueryParam("username") String targetUser) { + return null; + } + + @Override + public Response removeShareWithRole(@PathParam("groupName") String groupName, + @QueryParam("roleName") String targetUser) { + return null; + } + + @Override + public Response getUsersOfGroup(@PathParam("groupName") String groupName) { + return null; + } + + @Override + public Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + return null; + } + + @Override + public Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier) { + return null; + } + + @Override + public Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, + @QueryParam("id") String id) { + return null; + } +} \ No newline at end of file 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 7573df0f5e..9cf5253c2c 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 @@ -18,7 +18,6 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; -import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -26,37 +25,42 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.util.Date; -//@Path("/admin/groups") -//@Produces(MediaType.APPLICATION_JSON) -//@Consumes(MediaType.APPLICATION_JSON) +@Path("/admin/groups") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) public class GroupManagementAdminServiceImpl implements GroupManagementAdminService { -// -// private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); -// -// @Override -// public Response getGroupsOfUser( -// @QueryParam("username") String username, -// @HeaderParam("If-Modified-Since") String timestamp, -// @QueryParam("offset") int offset, -// @QueryParam("limit") int limit) { -// try { -// PaginationResult result = -// DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(username, offset, limit); -// if (result != null && result.getRecordsTotal() > 0) { -// return Response.status(Response.Status.OK).entity(result).build(); -// } else { -// return Response.status(Response.Status.NOT_FOUND).build(); -// } -// } catch (GroupManagementException e) { -// String msg = "ErrorResponse occurred while retrieving the groups of user '" + username + "'"; -// log.error(msg, e); -// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); -// } -// } + + private static final Log log = LogFactory.getLog(GroupManagementAdminServiceImpl.class); + + @GET + @Override + public Response getGroupsOfUser( + @QueryParam("username") String username, + @HeaderParam("If-Modified-Since") String timestamp, + @QueryParam("offset") int offset, + @QueryParam("limit") int limit) { + try { + PaginationResult result = + DeviceMgtAPIUtils.getGroupManagementProviderService().getGroups(username, offset, limit); + if (result != null && result.getRecordsTotal() > 0) { + return Response.status(Response.Status.OK).entity(result).build(); + } else { + return Response.status(Response.Status.NOT_FOUND).build(); + } + } catch (GroupManagementException e) { + String msg = "ErrorResponse occurred while retrieving the groups of user '" + username + "'"; + 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/webapp/WEB-INF/cxf-servlet.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 8405fb99fc..a5f5ed308a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -73,7 +73,7 @@ - + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml index ad937f4a86..56f0bc063b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/pom.xml @@ -21,7 +21,7 @@ device-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml @@ -48,10 +48,10 @@ org.wso2.carbon.device.mgt.common.* - javax.xml.bind.annotation, + javax.xml.bind.annotation; version="${javax.xml.bind.imp.pkg.version}", com.fasterxml.jackson.annotation;version="${jackson-annotations.version}", org.wso2.carbon.apimgt.api.model.*;version="${carbon.api.mgt.version.range}", - io.swagger.annotations.*;resolution:=optional + io.swagger.annotations; version="${swagger.annotations.version}"; resolution:=optional diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 1f98fef208..73d121aa39 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -22,7 +22,7 @@ org.wso2.carbon.devicemgt device-mgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 27d415f33c..cc1219c3a1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -334,6 +334,16 @@ public interface DeviceDAO { EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to retrieve current active enrollment of a given device and tenant id. + * + * @param deviceId device id. + * @param tenantId tenant id. + * @return returns EnrolmentInfo object. + * @throws DeviceManagementDAOException + */ + EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve devices of a given enrollment status. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrollmentDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrollmentDAO.java index f5c21de39d..d59d5733f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrollmentDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/EnrollmentDAO.java @@ -34,7 +34,7 @@ public interface EnrollmentDAO { int removeEnrollment(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; - boolean setStatus(int deviceId, String currentOwner, Status status, + boolean setStatus(int enrolmentId, String currentOwner, Status status, int tenantId) throws DeviceManagementDAOException; Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index a7af240b3a..648c7e2a42 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -28,6 +28,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.utils.xml.StringUtils; import java.sql.Connection; import java.sql.PreparedStatement; @@ -323,8 +324,10 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { rs = stmt.executeQuery(); devices = new ArrayList<>(); while (rs.next()) { - Device device = DeviceManagementDAOUtil.loadDevice(rs); - devices.add(device); + Device device = DeviceManagementDAOUtil.loadActiveDevice(rs, false); + if (device != null) { + devices.add(device); + } } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while listing devices for type '" + type + "'", e); @@ -780,7 +783,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { stmt.setInt(5, tenantId); rs = stmt.executeQuery(); if (rs.next()) { - enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); + enrolmentInfo = DeviceManagementDAOUtil.loadMatchingEnrolment(rs); } return enrolmentInfo; } catch (SQLException e) { @@ -791,6 +794,38 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } + @Override + public EnrolmentInfo getActiveEnrolment(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + EnrolmentInfo enrolmentInfo = null; + try { + conn = this.getConnection(); + String sql = "SELECT ID AS ENROLMENT_ID, DEVICE_ID, OWNER, OWNERSHIP, STATUS, DATE_OF_ENROLMENT, " + + "DATE_OF_LAST_UPDATE, TENANT_ID FROM DM_ENROLMENT WHERE DEVICE_ID = (SELECT d.ID " + + "FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + + "AND d.DEVICE_IDENTIFICATION = ? AND t.NAME = ? AND d.TENANT_ID = ?) " + + "AND TENANT_ID = ? AND STATUS in ('ACTIVE','UNREACHABLE','INACTIVE')"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceId.getId()); + stmt.setString(2, deviceId.getType()); + stmt.setInt(3, tenantId); + stmt.setInt(4, tenantId); + rs = stmt.executeQuery(); + if (rs.next()) { + enrolmentInfo = DeviceManagementDAOUtil.loadEnrolment(rs); + } + return enrolmentInfo; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while retrieving the enrolment " + + "information of device '" + deviceId.getId() + "' of type : " + + deviceId.getType(), e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + public int getEnrolmentByStatus(DeviceIdentifier deviceId, Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java index 2972697b95..9e8bd4680a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java @@ -145,16 +145,16 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { } @Override - public boolean setStatus(int deviceId, String currentOwner, EnrolmentInfo.Status status, + public boolean setStatus(int enrolmentID, String currentOwner, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE ID = ? AND OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); - stmt.setInt(2, deviceId); + stmt.setInt(2, enrolmentID); stmt.setString(3, currentOwner); stmt.setInt(4, tenantId); stmt.executeUpdate(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index cf4531c172..ea998fdec7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -147,6 +147,40 @@ public final class DeviceManagementDAOUtil { return enrolmentInfo; } + public static EnrolmentInfo loadMatchingEnrolment(ResultSet rs) throws SQLException { + Map enrolmentInfos = new HashMap<>(); + EnrolmentInfo enrolmentInfo = loadEnrolment(rs); + if (EnrolmentInfo.Status.ACTIVE.equals(enrolmentInfo.getStatus())) { + return enrolmentInfo; + } + enrolmentInfos.put(enrolmentInfo.getStatus(), enrolmentInfo); + while (rs.next()) { + enrolmentInfo = loadEnrolment(rs); + if (EnrolmentInfo.Status.ACTIVE.equals(enrolmentInfo.getStatus())) { + return enrolmentInfo; + } + enrolmentInfos.put(enrolmentInfo.getStatus(), enrolmentInfo); + } + if (enrolmentInfos.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { + return enrolmentInfos.get(EnrolmentInfo.Status.UNREACHABLE); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.INACTIVE)) { + return enrolmentInfos.get(EnrolmentInfo.Status.INACTIVE); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.CREATED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.CREATED); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.REMOVED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.REMOVED); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.UNCLAIMED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.UNCLAIMED); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.SUSPENDED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.SUSPENDED); + } else if (enrolmentInfos.containsKey(EnrolmentInfo.Status.BLOCKED)) { + return enrolmentInfos.get(EnrolmentInfo.Status.BLOCKED); + } + return enrolmentInfo; + } + public static Device loadDevice(ResultSet rs) throws SQLException { Device device = new Device(); device.setId(rs.getInt("DEVICE_ID")); @@ -158,6 +192,43 @@ public final class DeviceManagementDAOUtil { return device; } + //This method will retrieve most appropriate device information when there are multiple device enrollments for + //a single device. Here we'll consider only active status. + public static Device loadActiveDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException { + Map deviceMap = new HashMap<>(); + Device device = loadDevice(rs); + if (deviceInfoIncluded) { + device.setDeviceInfo(loadDeviceInfo(rs)); + } + + if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { + return device; + } + deviceMap.put(device.getEnrolmentInfo().getStatus(), device); + while (rs.next()) { + device = loadDevice(rs); + if (deviceInfoIncluded) { + device.setDeviceInfo(loadDeviceInfo(rs)); + } + if (EnrolmentInfo.Status.ACTIVE.equals(device.getEnrolmentInfo().getStatus())) { + return device; + } + if (device.getEnrolmentInfo() != null) { + deviceMap.put(device.getEnrolmentInfo().getStatus(), device); + } + } + if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { + return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE); + } else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { + return deviceMap.get(EnrolmentInfo.Status.INACTIVE); + } else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) { + return deviceMap.get(EnrolmentInfo.Status.CREATED); + } else if (deviceMap.containsKey(EnrolmentInfo.Status.UNCLAIMED)) { + return deviceMap.get(EnrolmentInfo.Status.UNCLAIMED); + } + return null; + } + //This method will retrieve most appropriate device information when there are multiple device enrollments for //a single device. We'll give the highest priority to active devices. public static Device loadMatchingDevice(ResultSet rs, boolean deviceInfoIncluded) throws SQLException { @@ -182,10 +253,10 @@ public final class DeviceManagementDAOUtil { deviceMap.put(device.getEnrolmentInfo().getStatus(), device); } } - if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { - return deviceMap.get(EnrolmentInfo.Status.INACTIVE); - } else if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { + if (deviceMap.containsKey(EnrolmentInfo.Status.UNREACHABLE)) { return deviceMap.get(EnrolmentInfo.Status.UNREACHABLE); + } else if (deviceMap.containsKey(EnrolmentInfo.Status.INACTIVE)) { + return deviceMap.get(EnrolmentInfo.Status.INACTIVE); } else if (deviceMap.containsKey(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED)) { return deviceMap.get(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED); } else if (deviceMap.containsKey(EnrolmentInfo.Status.CREATED)) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 6944d60511..284a3bc892 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -34,6 +34,7 @@ import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.EnrollmentDAO; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -68,6 +69,7 @@ public class OperationManagerImpl implements OperationManager { private OperationMappingDAO operationMappingDAO; private OperationDAO operationDAO; private DeviceDAO deviceDAO; + private EnrollmentDAO enrollmentDAO; private NotificationStrategy notificationStrategy; public OperationManagerImpl() { @@ -78,6 +80,7 @@ public class OperationManagerImpl implements OperationManager { operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); } public OperationManagerImpl(NotificationStrategy notificationStrategy) { @@ -138,8 +141,10 @@ public class OperationManagerImpl implements OperationManager { } } else if (isNotRepeated) { operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.PENDING, + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.REPEATED); operationMappingDAO.addOperationMapping(operationId, enrolmentId); } else { operationMappingDAO.addOperationMapping(operationId, enrolmentId); @@ -165,7 +170,8 @@ public class OperationManagerImpl implements OperationManager { if (!isScheduledOperation) { //Get the device-type from 1st valid DeviceIdentifier. We know the 1st element is definitely there. String deviceType = validDeviceIds.get(0).getType(); - activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, deviceType)); + activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, + deviceType)); } return activity; } else { @@ -268,15 +274,15 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { return null; } try { OperationManagementDAOFactory.openConnection(); List operationList = - operationDAO.getOperationsForDevice(enrolmentId); + operationDAO.getOperationsForDevice(enrolmentInfo.getId()); operations = new ArrayList<>(); for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : operationList) { @@ -308,13 +314,13 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); } - + int enrolmentId = enrolmentInfo.getId(); try { OperationManagementDAOFactory.openConnection(); List operationList = @@ -358,7 +364,7 @@ public class OperationManagerImpl implements OperationManager { } // - EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId); + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + deviceId.getId() + " and given type:" + @@ -373,7 +379,7 @@ public class OperationManagerImpl implements OperationManager { case INACTIVE: case UNREACHABLE: this.resetAttemptCount(enrolmentId); - this.setEnrolmentStatus(deviceId, EnrolmentInfo.Status.ACTIVE); + this.setEnrolmentStatus(enrolmentId, EnrolmentInfo.Status.ACTIVE); break; } @@ -419,8 +425,8 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type" + deviceId.getType()); @@ -429,7 +435,7 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getNextOperation( - enrolmentId); + enrolmentInfo.getId()); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND.equals(dtoOperation.getType() )) { @@ -474,17 +480,22 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { + throw new OperationManagementException( + "Device not found for device id:" + deviceId.getId() + " " + "type:" + + deviceId.getType()); + } try { + int enrolmentId = enrolmentInfo.getId(); OperationManagementDAOFactory.beginTransaction(); boolean isUpdated = false; if (operation.getStatus() != null) { isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status - . - valueOf(operation.getStatus() - .toString())); + org.wso2.carbon.device.mgt.core.dto.operation.mgt. + Operation.Status.valueOf(operation.getStatus(). + toString())); } if (isUpdated && operation.getOperationResponse() != null) { operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); @@ -538,8 +549,8 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for given device identifier: " + deviceId.getId() + " type: " + deviceId.getType()); } @@ -547,17 +558,14 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperationByDeviceAndId( - enrolmentId, - operationId); + getOperationByDeviceAndId(enrolmentInfo.getId(), + operationId); if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation( - dtoOperation - .getId()); + getOperation(dtoOperation.getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { @@ -601,24 +609,28 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + EnrolmentInfo enrolmentInfo = this.getActiveEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException( "Device not found for device id:" + deviceId.getId() + " " + "type:" + deviceId.getType()); } try { + int enrolmentId = enrolmentInfo.getId(); OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status dtoOpStatus = org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation. + Status.PENDING)); Operation operation; @@ -646,9 +658,8 @@ public class OperationManagerImpl implements OperationManager { Operation operation; try { OperationManagementDAOFactory.openConnection(); - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperation( - operationId); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO.getOperation( + operationId); if (dtoOperation == null) { throw new OperationManagementException("Operation not found for given Id:" + operationId); } @@ -658,9 +669,7 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation( - dtoOperation - .getId()); + getOperation(dtoOperation.getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { @@ -919,19 +928,37 @@ public class OperationManagerImpl implements OperationManager { return enrolmentInfo; } - private boolean setEnrolmentStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws OperationManagementException { + private EnrolmentInfo getActiveEnrolmentInfo(DeviceIdentifier deviceId) throws OperationManagementException { + EnrolmentInfo enrolmentInfo; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + enrolmentInfo = deviceDAO.getActiveEnrolment(deviceId, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving enrollment data of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return enrolmentInfo; + } + + private boolean setEnrolmentStatus(int enrolmentId, EnrolmentInfo.Status status) throws OperationManagementException { boolean updateStatus; try { DeviceManagementDAOFactory.beginTransaction(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); String user = this.getUser(); - updateStatus = deviceDAO.setEnrolmentStatus(deviceId, user, status, tenantId); + updateStatus = enrollmentDAO.setStatus(enrolmentId, user, status, tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new OperationManagementException("Error occurred while updating enrollment status of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'", e); + throw new OperationManagementException("Error occurred while updating enrollment status of device of " + + "enrolment-id '" + enrolmentId + "'", e); } catch (TransactionManagementException e) { throw new OperationManagementException("Error occurred while initiating a transaction", e); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java index 7ba75c60c0..6f0f020482 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/push/notification/mgt/PushNotificationBasedOperationManager.java @@ -46,7 +46,7 @@ public class PushNotificationBasedOperationManager implements OperationManager { Activity activity = this.operationManager.addOperation(operation, devices); for (DeviceIdentifier deviceId : devices) { try { - this.notificationProvider.execute(new NotificationContext(deviceId)); + this.notificationProvider.execute(new NotificationContext(deviceId, operation)); } catch (PushNotificationExecutionFailedException e) { throw new OperationManagementException("Error occurred while sending push notification to device", e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index dda2caf592..471989a062 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -240,8 +240,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { int tenantId = this.getTenantId(); DeviceManagementDAOFactory.beginTransaction(); - - DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId); Device currentDevice = deviceDAO.getDevice(deviceIdentifier, tenantId); device.setId(currentDevice.getId()); device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId()); @@ -1019,10 +1017,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException { try { + boolean success = false; DeviceManagementDAOFactory.beginTransaction(); int tenantId = this.getTenantId(); Device device = deviceDAO.getDevice(deviceId, tenantId); - boolean success = enrollmentDAO.setStatus(device.getId(), currentOwner, status, tenantId); + EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); + if (enrolmentInfo != null) { + success = enrollmentDAO.setStatus(enrolmentInfo.getId(), currentOwner, status, tenantId); + } DeviceManagementDAOFactory.commitTransaction(); return success; } catch (DeviceManagementDAOException e) { @@ -1032,6 +1034,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml index a160e0103a..31481bee4b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml @@ -22,7 +22,7 @@ device-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.ui/pom.xml index 72501b0956..be5c0fc18c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/pom.xml @@ -22,7 +22,7 @@ device-mgt org.wso2.carbon.devicemgt - 1.2.2-SNAPSHOT + 1.2.3-SNAPSHOT ../pom.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js index 6f57d8ae56..f38e2b69fa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js @@ -104,10 +104,12 @@ deviceModule = function () { if (device["properties"] && device["properties"].length > 0) { var propertiesList = device["properties"]; var properties = {}; - for (var i = 0; i < propertiesList.length; i++) { - if (propertiesList[i]["value"]) { - properties[propertiesList[i]["name"]] = - propertiesList[i]["value"]; + if (propertiesList) { + for (var i = 0; i < propertiesList.length; i++) { + if (propertiesList[i]["value"]) { + properties[propertiesList[i]["name"]] = + propertiesList[i]["value"]; + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js index 6be734ec27..c34cc04302 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/operation.js @@ -38,42 +38,42 @@ var operationModule = function () { } privateMethods.getOperationsFromFeatures = function (deviceType, operationType) { - var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/features/" + deviceType; + var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/device-types/" + deviceType + "/features"; var featuresList = serviceInvokers.XMLHttp.get(url, function (responsePayload) { - var features = responsePayload; - var featureList = []; - var feature; - for (var i = 0; i < features.length; i++) { - feature = {}; - var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; - if (analyticStreams) { - for (var stream in analyticStreams) { - if (analyticStreams[stream].name == features[i].name) { - feature.ui_unit = analyticStreams[stream].ui_unit; - break; - } - } - } + var features = JSON.parse(responsePayload.responseText); + var featureList = []; + var feature; + for (var i = 0; i < features.length; i++) { + feature = {}; + var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; + if (analyticStreams) { + for (var stream in analyticStreams) { + if (analyticStreams[stream].name == features[i].name) { + feature.ui_unit = analyticStreams[stream].ui_unit; + break; + } + } + } - feature["operation"] = features[i].code; - feature["name"] = features[i].name; - feature["description"] = features[i].description; - feature["deviceType"] = deviceType; - feature["params"] = []; - var metaData = features[i].metadataEntries; - if (metaData) { - for (var j = 0; j < metaData.length; j++) { - feature["params"].push(metaData[j].value); - } - featureList.push(feature); - } - } - return featureList; - }, function (responsePayload) { - var response = {}; - response["status"] = "error"; - return response; - } + feature["operation"] = features[i].code; + feature["name"] = features[i].name; + feature["description"] = features[i].description; + feature["deviceType"] = deviceType; + feature["params"] = []; + var metaData = features[i].metadataEntries; + if (metaData) { + for (var j = 0; j < metaData.length; j++) { + feature["params"].push(metaData[j].value); + } + featureList.push(feature); + } + } + return featureList; + }, function (responsePayload) { + var response = {}; + response["status"] = "error"; + return response; + } ); return featuresList; }; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js index d4fdbc3e89..1600618775 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js @@ -72,7 +72,7 @@ var invokers = function () { var xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open(httpMethod, endpoint); - for(var i in headers){ + for (var i in headers) { xmlHttpRequest.setRequestHeader(headers[i].name, headers[i].value); } xmlHttpRequest.setRequestHeader(constants["CONTENT_TYPE_IDENTIFIER"], constants["APPLICATION_JSON"]); @@ -117,9 +117,10 @@ var invokers = function () { * @param endpoint Backend REST API url. * @param responseCallback a function to be called with response retrieved. */ - privateMethods["initiateXMLHTTPRequest"] = function (httpMethod, requestPayload, endpoint, responseCallback, headers) { - return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0, headers); - }; + privateMethods["initiateXMLHTTPRequest"] = + function (httpMethod, requestPayload, endpoint, responseCallback, headers) { + return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0, headers); + }; /** * This method invokes return initiateXMLHttpRequest for get calls. @@ -128,7 +129,8 @@ var invokers = function () { */ publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback, headers) { var requestPayload = null; - return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback, headers); + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback, + headers); }; /** @@ -138,7 +140,8 @@ var invokers = function () { * @param responseCallback a function to be called with response retrieved. */ publicXMLHTTPInvokers["post"] = function (endpoint, requestPayload, responseCallback, headers) { - return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback, headers); + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback, + headers); }; /** @@ -148,7 +151,8 @@ var invokers = function () { * @param responseCallback a function to be called with response retrieved. */ publicXMLHTTPInvokers["put"] = function (endpoint, requestPayload, responseCallback, headers) { - return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback, headers); + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback, + headers); }; /** @@ -158,7 +162,8 @@ var invokers = function () { */ publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback, headers) { var requestPayload = null; - return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback, headers); + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, + responseCallback, headers); }; /** @@ -245,100 +250,105 @@ var invokers = function () { * @param errorCallback a function to be called if en error is reserved. * @param headers a list of name value pairs for additional http headers. */ - privateMethods["initiateHTTPClientRequest"] = function (method, url, successCallback, errorCallback, payload, headers) { - //noinspection JSUnresolvedVariable - var HttpClient = Packages.org.apache.commons.httpclient.HttpClient; - var httpMethodObject; - switch (method) { - case constants["HTTP_GET"]: - //noinspection JSUnresolvedVariable - var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod; - httpMethodObject = new GetMethod(url); - break; - case constants["HTTP_POST"]: - //noinspection JSUnresolvedVariable - var PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod; - httpMethodObject = new PostMethod(url); - break; - case constants["HTTP_PUT"]: - //noinspection JSUnresolvedVariable - var PutMethod = Packages.org.apache.commons.httpclient.methods.PutMethod; - httpMethodObject = new PutMethod(url); - break; - case constants["HTTP_DELETE"]: - //noinspection JSUnresolvedVariable - var DeleteMethod = Packages.org.apache.commons.httpclient.methods.DeleteMethod; - httpMethodObject = new DeleteMethod(url); - break; - default: - //noinspection JSUnresolvedFunction - throw new IllegalArgumentException("Invalid HTTP request method: " + method); - } - - //noinspection JSUnresolvedVariable - var Header = Packages.org.apache.commons.httpclient.Header; - for(var i in headers){ - var header = new Header(); - header.setName(headers[i].name); - header.setValue(headers[i].value); - httpMethodObject.addRequestHeader(header); - } - - var header = new Header(); - header.setName(constants["CONTENT_TYPE_IDENTIFIER"]); - header.setValue(constants["APPLICATION_JSON"]); - //noinspection JSUnresolvedFunction - httpMethodObject.addRequestHeader(header); - header = new Header(); - header.setName(constants["ACCEPT_IDENTIFIER"]); - header.setValue(constants["APPLICATION_JSON"]); - //noinspection JSUnresolvedFunction - httpMethodObject.addRequestHeader(header); + privateMethods["initiateHTTPClientRequest"] = + function (method, url, successCallback, errorCallback, payload, headers) { + //noinspection JSUnresolvedVariable + var HttpClient = Packages.org.apache.commons.httpclient.HttpClient; + var httpMethodObject; + switch (method) { + case constants["HTTP_GET"]: + //noinspection JSUnresolvedVariable + var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod; + httpMethodObject = new GetMethod(url); + break; + case constants["HTTP_POST"]: + //noinspection JSUnresolvedVariable + var PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod; + httpMethodObject = new PostMethod(url); + break; + case constants["HTTP_PUT"]: + //noinspection JSUnresolvedVariable + var PutMethod = Packages.org.apache.commons.httpclient.methods.PutMethod; + httpMethodObject = new PutMethod(url); + break; + case constants["HTTP_DELETE"]: + //noinspection JSUnresolvedVariable + var DeleteMethod = Packages.org.apache.commons.httpclient.methods.DeleteMethod; + httpMethodObject = new DeleteMethod(url); + break; + default: + //noinspection JSUnresolvedFunction + throw new IllegalArgumentException("Invalid HTTP request method: " + method); + } - if (devicemgtProps["isOAuthEnabled"]) { - var accessToken = privateMethods.getAccessToken(); - if (accessToken) { - header = new Header(); - header.setName(constants["AUTHORIZATION_HEADER"]); - header.setValue(constants["BEARER_PREFIX"] + accessToken); - //noinspection JSUnresolvedFunction + //noinspection JSUnresolvedVariable + var Header = Packages.org.apache.commons.httpclient.Header; + for (var i in headers) { + var header = new Header(); + header.setName(headers[i].name); + header.setValue(headers[i].value); httpMethodObject.addRequestHeader(header); - } else { - response.sendRedirect(devicemgtProps["appContext"] + "login"); } - } - //noinspection JSUnresolvedFunction - if (payload != null) { - var StringRequestEntity = Packages.org.apache.commons.httpclient.methods.StringRequestEntity; - var stringRequestEntity = new StringRequestEntity(stringify(payload)); - //noinspection JSUnresolvedFunction - httpMethodObject.setRequestEntity(stringRequestEntity); - } - var client = new HttpClient(); - try { + + var header = new Header(); + header.setName(constants["CONTENT_TYPE_IDENTIFIER"]); + header.setValue(constants["APPLICATION_JSON"]); //noinspection JSUnresolvedFunction - client.executeMethod(httpMethodObject); + httpMethodObject.addRequestHeader(header); + header = new Header(); + header.setName(constants["ACCEPT_IDENTIFIER"]); + header.setValue(constants["APPLICATION_JSON"]); //noinspection JSUnresolvedFunction - var status = httpMethodObject.getStatusCode(); - if (status == 200) { - var responseContentDispositionHeader = httpMethodObject.getResponseHeader(constants["CONTENT_DISPOSITION_IDENTIFIER"]); - if (responseContentDispositionHeader) { - return successCallback(httpMethodObject.getResponseBodyAsStream(), httpMethodObject.getResponseHeaders()); + httpMethodObject.addRequestHeader(header); + + if (devicemgtProps["isOAuthEnabled"]) { + var accessToken = privateMethods.getAccessToken(); + if (accessToken) { + header = new Header(); + header.setName(constants["AUTHORIZATION_HEADER"]); + header.setValue(constants["BEARER_PREFIX"] + accessToken); + //noinspection JSUnresolvedFunction + httpMethodObject.addRequestHeader(header); } else { - return successCallback(httpMethodObject.getResponseBody()); + response.sendRedirect(devicemgtProps["appContext"] + "login"); } - } else { - return errorCallback(httpMethodObject.getResponseBody()); } - } catch (e) { - return errorCallback(response); - } finally { //noinspection JSUnresolvedFunction - if (method != constants["HTTP_GET"]) { - method.releaseConnection(); + if (payload != null) { + var StringRequestEntity = Packages.org.apache.commons.httpclient.methods.StringRequestEntity; + var stringRequestEntity = new StringRequestEntity(stringify(payload)); + //noinspection JSUnresolvedFunction + httpMethodObject.setRequestEntity(stringRequestEntity); } - } - }; + var client = new HttpClient(); + try { + //noinspection JSUnresolvedFunction + client.executeMethod(httpMethodObject); + //noinspection JSUnresolvedFunction + var status = httpMethodObject.getStatusCode(); + if (status == 200) { + var responseContentDispositionHeader = httpMethodObject.getResponseHeader( + constants["CONTENT_DISPOSITION_IDENTIFIER"]); + if (responseContentDispositionHeader) { + return successCallback(httpMethodObject.getResponseBodyAsStream(), + httpMethodObject.getResponseHeaders()); + } else { + return successCallback(httpMethodObject.getResponseBodyAsString(), + httpMethodObject.getResponseHeaders()); + } + } else { + return errorCallback(httpMethodObject.getResponseBodyAsString(), + httpMethodObject.getResponseHeaders()); + } + } catch (e) { + return errorCallback(response); + } finally { + //noinspection JSUnresolvedFunction + if (method != constants["HTTP_GET"]) { + method.releaseConnection(); + } + } + }; /** * This method invokes return initiateHTTPClientRequest for get calls. @@ -349,8 +359,8 @@ var invokers = function () { */ publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback, headers) { var requestPayload = null; - return privateMethods. - initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload, headers); + return privateMethods.initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, + requestPayload, headers); }; /** @@ -362,8 +372,8 @@ var invokers = function () { * @param headers a list of name value pairs for additional http headers. */ publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback, headers) { - return privateMethods. - initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload, headers); + return privateMethods.initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, + payload, headers); }; /** @@ -375,8 +385,8 @@ var invokers = function () { * @param headers a list of name value pairs for additional http headers. */ publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback, headers) { - return privateMethods. - initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload, headers); + return privateMethods.initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, + payload, headers); }; /** @@ -388,8 +398,8 @@ var invokers = function () { */ publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback, headers) { var requestPayload = null; - return privateMethods. - initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload, headers); + return privateMethods.initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, + requestPayload, headers); }; var publicMethods = {}; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js index 3af4f59e6a..3b96ff0056 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/utility.js @@ -24,6 +24,7 @@ utility = function () { var log = new Log("/app/modules/utility.js"); var JavaClass = Packages.java.lang.Class; var PrivilegedCarbonContext = Packages.org.wso2.carbon.context.PrivilegedCarbonContext; + var server = require("carbon")["server"]; var getOsgiService = function (className) { return PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(JavaClass.forName(className)); @@ -38,8 +39,8 @@ utility = function () { PrivilegedCarbonContext.startTenantFlow(); context = PrivilegedCarbonContext.getThreadLocalCarbonContext(); context.setTenantDomain(carbon.server.tenantDomain({ - tenantId: userInfo.tenantId - })); + tenantId: userInfo.tenantId + })); context.setTenantId(userInfo.tenantId); context.setUsername(userInfo.username || null); }; @@ -62,7 +63,7 @@ utility = function () { publicMethods.getDeviceTypeConfig = function (deviceType) { var unitName = publicMethods.getTenantedDeviceUnitName(deviceType, "type-view"); - + if (deviceType in deviceTypeConfigMap) { return deviceTypeConfigMap[deviceType]; } @@ -71,7 +72,10 @@ utility = function () { if (deviceTypeConfigFile.isExists()) { try { deviceTypeConfigFile.open("r"); - deviceTypeConfig = parse(deviceTypeConfigFile.readAll()); + var config = deviceTypeConfigFile.readAll(); + config = config.replace("%https.ip%", server.address("https")); + config = config.replace("%http.ip%", server.address("http")); + deviceTypeConfig = parse(config); } catch (err) { log.error("Error while reading device config file for `" + deviceType + "`: " + err); } finally { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs index fafc053374..bbaf402251 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs @@ -138,11 +138,11 @@ -
+
-
+