From 78057b2992ca97680b002e075edd9180abcadf5d Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 5 Jul 2016 15:09:11 +0530 Subject: [PATCH] Adding an API to return the list of device types registered in the underlying device management platform --- .../mgt/jaxrs/beans/BasePaginatedResult.java | 2 +- .../mgt/jaxrs/beans/DeviceTypeList.java | 101 ++++++++++++++++++ .../admin/DeviceTypeManagementService.java | 93 ++++++++++++++++ .../DeviceTypeManagementServiceImpl.java | 55 ++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../device/mgt/core/dao/DeviceTypeDAO.java | 6 +- .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 22 ++-- .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 21 ++-- 9 files changed, 274 insertions(+), 30 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java index 9ae3ade67e1..e423f567084 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/BasePaginatedResult.java @@ -28,7 +28,7 @@ public class BasePaginatedResult { private String previous; /** - * Number of Devices returned. + * Number of Resources returned. */ @ApiModelProperty(value = "Number of resources returned.") @JsonProperty("count") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java new file mode 100644 index 00000000000..0398b6c7cfc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeList.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.Device; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceTypeList { + + private int count; + private String next; + private String previous; + private List deviceTypes = new ArrayList<>(); + + /** + * Number of Devices Types returned. + */ + @ApiModelProperty(value = "Number of resources returned.") + @JsonProperty("count") + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + + /** + * Link to the next subset of resources qualified. \nEmpty if no more resources are to be returned. + */ + @ApiModelProperty(value = "Link to the next subset of resources qualified. \n " + + "Empty if no more resources are to be returned.") + @JsonProperty("next") + public String getNext() { + return next; + } + + public void setNext(String next) { + this.next = next; + } + + /** + * Link to the previous subset of resources qualified. \nEmpty if current subset is the first subset returned. + */ + @ApiModelProperty(value = "Link to the previous subset of resources qualified. \n" + + "Empty if current subset is the first subset returned.") + @JsonProperty("previous") + public String getPrevious() { + return previous; + } + + public void setPrevious(String previous) { + this.previous = previous; + } + + @ApiModelProperty(value = "List of device types returned") + @JsonProperty("devicesTypes") + public List getList() { + return deviceTypes; + } + + public void setList(List deviceTypes) { + this.deviceTypes = deviceTypes; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" deviceTypes: [").append(deviceTypes).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java new file mode 100644 index 00000000000..2d0794e2996 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementService.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; + +import io.swagger.annotations.*; +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.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +@API(name = "Device Type Management", version = "1.0.0", context = "/devicemgt_admin/configuration", tags = {"devicemgt_admin"}) + +@Path("/admin/device-types") +@Api(value = "Device Type Management", description = "This API corresponds to all tasks related to device " + + "type management") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface DeviceTypeManagementService { + + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get the list of device types registered.", + notes = "Retrieves the list of device types of which the devices intended to be managed.", + tags = "Device Type Management") + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the list of supported device types.", + 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( + scope = "read:device-types", + permissions = {"/permission/admin/device-mgt/admin/device-types/view"} + ) + Response getDeviceTypes( + @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/impl/admin/DeviceTypeManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java new file mode 100644 index 00000000000..00029244756 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementServiceImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; + +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.jaxrs.beans.DeviceTypeList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypeManagementService; +import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; + +import javax.ws.rs.HeaderParam; +import javax.ws.rs.core.Response; +import java.util.List; + +public class DeviceTypeManagementServiceImpl implements DeviceTypeManagementService { + + private static Log log = LogFactory.getLog(DeviceTypeManagementServiceImpl.class); + + @Override + public Response getDeviceTypes(@HeaderParam("If-Modified-Since") String ifModifiedSince) { + List deviceTypes; + try { + deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes(); + + DeviceTypeList deviceTypeList = new DeviceTypeList(); + deviceTypeList.setCount(deviceTypes.size()); + deviceTypeList.setList(deviceTypes); + return Response.status(Response.Status.OK).entity(deviceTypeList).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while fetching the list of device types."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).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 42eb1188dda..1b128fb4507 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 @@ -38,6 +38,7 @@ + @@ -77,6 +78,7 @@ + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 53827abd6ec..0e34fc1ab08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -51,17 +51,17 @@ public interface DeviceTypeDAO { List getDeviceTypes(int tenantId) throws DeviceManagementDAOException; /** - * @param tenandId of the device type provider. + * @param tenantId of the device type provider. * @return return only the device types that are associated with the provider tenant. * @throws DeviceManagementDAOException */ - List getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException; + List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException; /** * @return sharedWithAllDeviceTypes This returns public shared device types. * @throws DeviceManagementDAOException */ - List getSharedDeviceTypes() throws DeviceManagementDAOException; + List getSharedDeviceTypes() throws DeviceManagementDAOException; /** * @param id retrieve the device type with its id. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index 8ef838ef8ea..c7e4952328e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -89,24 +89,21 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } @Override - public List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { + public List getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List deviceTypes = new ArrayList<>(); + List deviceTypes = new ArrayList<>(); try { conn = this.getConnection(); String sql = - "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?"; + "SELECT NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where PROVIDER_TENANT_ID =?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); rs = stmt.executeQuery(); while (rs.next()) { - DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); - deviceType.setName(rs.getString("DEVICE_TYPE")); - deviceTypes.add(deviceType); + deviceTypes.add(rs.getString("DEVICE_TYPE")); } return deviceTypes; } catch (SQLException e) { @@ -117,25 +114,22 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } @Override - public List getSharedDeviceTypes() throws DeviceManagementDAOException { + public List getSharedDeviceTypes() throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List deviceTypes = new ArrayList<>(); + List deviceTypes = new ArrayList<>(); try { conn = this.getConnection(); String sql = - "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " + + "SELECT NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE where " + "SHARED_WITH_ALL_TENANTS = ?"; stmt = conn.prepareStatement(sql); stmt.setBoolean(1, true); rs = stmt.executeQuery(); while (rs.next()) { - DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); - deviceType.setName(rs.getString("DEVICE_TYPE")); - deviceTypes.add(deviceType); + deviceTypes.add(rs.getString("DEVICE_TYPE")); } return deviceTypes; } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 95e8716af13..d0211bcd941 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -207,7 +207,7 @@ public interface DeviceManagementProviderService { Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; - List getAvailableDeviceTypes() throws DeviceManagementException; + List getAvailableDeviceTypes() throws DeviceManagementException; boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException; 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 536901f821f..087f3eb50c8 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 @@ -857,10 +857,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getAvailableDeviceTypes() throws DeviceManagementException { - List deviceTypesProvidedByTenant; - List publicSharedDeviceTypesInDB; - List deviceTypesResponse = new ArrayList<>(); + public List getAvailableDeviceTypes() throws DeviceManagementException { + List deviceTypesProvidedByTenant; + List publicSharedDeviceTypesInDB; + List deviceTypesResponse = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); int tenantId = this.getTenantId(); @@ -872,21 +872,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (registeredTypes != null) { if (deviceTypesProvidedByTenant != null) { - for (DeviceType deviceType : deviceTypesProvidedByTenant) { - DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName(), tenantId); + for (String deviceType : deviceTypesProvidedByTenant) { + DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType, tenantId); if (registeredTypes.get(providerKey) != null) { deviceTypesResponse.add(deviceType); - deviceTypeSetForTenant.add(deviceType.getName()); + deviceTypeSetForTenant.add(deviceType); } } } // Get the device from the public space, however if there is another device with same name then give // priority to that if (publicSharedDeviceTypesInDB != null) { - for (DeviceType deviceType : publicSharedDeviceTypesInDB) { - DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName()); - if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains( - deviceType.getName())) { + for (String deviceType : publicSharedDeviceTypesInDB) { + DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType); + if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains(deviceType)) { deviceTypesResponse.add(deviceType); } }