Adding an API to return the list of device types registered in the underlying device management platform

merge-requests/7/head
prabathabey 8 years ago
parent 282ac72249
commit 78057b2992

@ -28,7 +28,7 @@ public class BasePaginatedResult {
private String previous; private String previous;
/** /**
* Number of Devices returned. * Number of Resources returned.
*/ */
@ApiModelProperty(value = "Number of resources returned.") @ApiModelProperty(value = "Number of resources returned.")
@JsonProperty("count") @JsonProperty("count")

@ -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<String> 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<String> getList() {
return deviceTypes;
}
public void setList(List<String> 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();
}
}

@ -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);
}

@ -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<String> 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();
}
}
}

@ -38,6 +38,7 @@
<!--<ref bean="groupManagementService"/>--> <!--<ref bean="groupManagementService"/>-->
<ref bean="groupManagementAdminService"/> <ref bean="groupManagementAdminService"/>
<ref bean="applicationManagementAdminService"/> <ref bean="applicationManagementAdminService"/>
<ref bean="deviceTypeManagementAdminService"/>
<ref bean="dashboardServiceBean"/> <ref bean="dashboardServiceBean"/>
<ref bean="swaggerResource"/> <ref bean="swaggerResource"/>
</jaxrs:serviceBeans> </jaxrs:serviceBeans>
@ -77,6 +78,7 @@
<bean id="groupManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.GroupManagementAdminServiceImpl"/> <bean id="groupManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.GroupManagementAdminServiceImpl"/>
<bean id="userManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.UserManagementAdminServiceImpl"/> <bean id="userManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.UserManagementAdminServiceImpl"/>
<bean id="dashboardServiceBean" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.DashboardImpl"/> <bean id="dashboardServiceBean" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.DashboardImpl"/>
<bean id="deviceTypeManagementAdminService" class="org.wso2.carbon.device.mgt.jaxrs.service.impl.admin.DeviceTypeManagementServiceImpl"/>
<bean id="jsonProvider" class="org.wso2.carbon.device.mgt.jaxrs.common.GsonMessageBodyHandler"/> <bean id="jsonProvider" class="org.wso2.carbon.device.mgt.jaxrs.common.GsonMessageBodyHandler"/>
<!--<bean id="errorHandler" class="org.wso2.carbon.device.mgt.jaxrs.common.ErrorHandler"/>--> <!--<bean id="errorHandler" class="org.wso2.carbon.device.mgt.jaxrs.common.ErrorHandler"/>-->

@ -51,17 +51,17 @@ public interface DeviceTypeDAO {
List<DeviceType> getDeviceTypes(int tenantId) throws DeviceManagementDAOException; List<DeviceType> 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. * @return return only the device types that are associated with the provider tenant.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<DeviceType> getDeviceTypesByProvider(int tenandId) throws DeviceManagementDAOException; List<String> getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException;
/** /**
* @return sharedWithAllDeviceTypes This returns public shared device types. * @return sharedWithAllDeviceTypes This returns public shared device types.
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException; List<String> getSharedDeviceTypes() throws DeviceManagementDAOException;
/** /**
* @param id retrieve the device type with its id. * @param id retrieve the device type with its id.

@ -89,24 +89,21 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
} }
@Override @Override
public List<DeviceType> getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException { public List<String> getDeviceTypesByProvider(int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<DeviceType> deviceTypes = new ArrayList<>(); List<String> deviceTypes = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = 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 = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
DeviceType deviceType = new DeviceType(); deviceTypes.add(rs.getString("DEVICE_TYPE"));
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(rs.getString("DEVICE_TYPE"));
deviceTypes.add(deviceType);
} }
return deviceTypes; return deviceTypes;
} catch (SQLException e) { } catch (SQLException e) {
@ -117,25 +114,22 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO {
} }
@Override @Override
public List<DeviceType> getSharedDeviceTypes() throws DeviceManagementDAOException { public List<String> getSharedDeviceTypes() throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
List<DeviceType> deviceTypes = new ArrayList<>(); List<String> deviceTypes = new ArrayList<>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = 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 = ?"; "SHARED_WITH_ALL_TENANTS = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setBoolean(1, true); stmt.setBoolean(1, true);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
DeviceType deviceType = new DeviceType(); deviceTypes.add(rs.getString("DEVICE_TYPE"));
deviceType.setId(rs.getInt("DEVICE_TYPE_ID"));
deviceType.setName(rs.getString("DEVICE_TYPE"));
deviceTypes.add(deviceType);
} }
return deviceTypes; return deviceTypes;
} catch (SQLException e) { } catch (SQLException e) {

@ -207,7 +207,7 @@ public interface DeviceManagementProviderService {
Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException; Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws DeviceManagementException;
List<DeviceType> getAvailableDeviceTypes() throws DeviceManagementException; List<String> getAvailableDeviceTypes() throws DeviceManagementException;
boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException; boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException;

@ -857,10 +857,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
@Override @Override
public List<DeviceType> getAvailableDeviceTypes() throws DeviceManagementException { public List<String> getAvailableDeviceTypes() throws DeviceManagementException {
List<DeviceType> deviceTypesProvidedByTenant; List<String> deviceTypesProvidedByTenant;
List<DeviceType> publicSharedDeviceTypesInDB; List<String> publicSharedDeviceTypesInDB;
List<DeviceType> deviceTypesResponse = new ArrayList<>(); List<String> deviceTypesResponse = new ArrayList<>();
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
int tenantId = this.getTenantId(); int tenantId = this.getTenantId();
@ -872,21 +872,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (registeredTypes != null) { if (registeredTypes != null) {
if (deviceTypesProvidedByTenant != null) { if (deviceTypesProvidedByTenant != null) {
for (DeviceType deviceType : deviceTypesProvidedByTenant) { for (String deviceType : deviceTypesProvidedByTenant) {
DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName(), tenantId); DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType, tenantId);
if (registeredTypes.get(providerKey) != null) { if (registeredTypes.get(providerKey) != null) {
deviceTypesResponse.add(deviceType); 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 // Get the device from the public space, however if there is another device with same name then give
// priority to that // priority to that
if (publicSharedDeviceTypesInDB != null) { if (publicSharedDeviceTypesInDB != null) {
for (DeviceType deviceType : publicSharedDeviceTypesInDB) { for (String deviceType : publicSharedDeviceTypesInDB) {
DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType.getName()); DeviceTypeIdentifier providerKey = new DeviceTypeIdentifier(deviceType);
if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains( if (registeredTypes.get(providerKey) != null && !deviceTypeSetForTenant.contains(deviceType)) {
deviceType.getName())) {
deviceTypesResponse.add(deviceType); deviceTypesResponse.add(deviceType);
} }
} }

Loading…
Cancel
Save