Update delete device api

APiTesting
Akeela Azhar 2 years ago
parent f90ec52ba8
commit 724057f841

@ -475,5 +475,10 @@
<artifactId>ehcache-terracotta</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev20230227-2.0.0</version>
</dependency>
</dependencies>
</project>

@ -36,6 +36,13 @@
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.AppendValuesResponse;
import com.google.api.services.sheets.v4.model.ValueRange;
import com.google.gson.Gson;
import io.entgra.application.mgt.common.ApplicationInstallResponse;
import io.entgra.application.mgt.common.SubscriptionType;
@ -47,7 +54,6 @@ import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.wso2.carbon.apimgt.keymgt.extension.DCRResponse;
import org.wso2.carbon.apimgt.keymgt.extension.TokenRequest;
import org.wso2.carbon.apimgt.keymgt.extension.TokenResponse;
@ -82,7 +88,6 @@ import org.wso2.carbon.device.mgt.common.type.mgt.DeviceStatus;
import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -93,10 +98,7 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService;
import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService;
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
import org.wso2.carbon.device.mgt.jaxrs.beans.*;
import org.wso2.carbon.device.mgt.jaxrs.service.api.DeviceManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException;
@ -116,20 +118,21 @@ import javax.validation.Valid;
import javax.validation.constraints.Size;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.*;
@Path("/devices")
public class DeviceManagementServiceImpl implements DeviceManagementService {
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
private static final Log log = LogFactory.getLog(DeviceManagementServiceImpl.class);
private static final String SPREADSHEET_ID = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
private static final String RANGE = "Sheet1!A:B";
@GET
@Path("/{type}/{id}/status")
@Override
@ -149,7 +152,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Override
public Response getDevices(
@ -180,7 +183,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
PaginationRequest request = new PaginationRequest(offset, limit);
PaginationResult result;
DeviceList devices = new DeviceList();
if (name != null && !name.isEmpty()) {
request.setDeviceName(name);
}
@ -209,7 +212,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
// this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
if (groupId != 0) {
try {
boolean isPermitted = DeviceMgtAPIUtils.checkPermission(groupId, authorizedUser);
@ -252,7 +255,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
request.setOwner(authorizedUser);
}
}
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
Date sinceDate;
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
@ -268,7 +271,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} else {
result = dms.getAllDevices(request, false);
}
if (result == null || result.getData() == null || result.getData().size() <= 0) {
return Response.status(Response.Status.NOT_MODIFIED).entity("No device is modified " +
"after the timestamp provided in 'If-Modified-Since' header").build();
@ -304,7 +307,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
Response.status(Response.Status.OK).entity(devices).build();
}
}
devices.setList((List<Device>) result.getData());
devices.setCount(result.getRecordsTotal());
return Response.status(Response.Status.OK).entity(devices).build();
@ -320,22 +323,22 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Override
@Path("/user-devices")
public Response getDeviceByUser(@QueryParam("requireDeviceInfo") boolean requireDeviceInfo,
@QueryParam("offset") int offset,
@QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationParameters(offset, limit);
PaginationRequest request = new PaginationRequest(offset, limit);
PaginationResult result;
DeviceList devices = new DeviceList();
String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
request.setOwner(currentUser);
try {
if (requireDeviceInfo) {
result = DeviceMgtAPIUtils.getDeviceManagementService().getDevicesOfUser(request);
@ -352,7 +355,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
/**
* Validate group Id and group Id greater than 0 and exist.
*
@ -378,7 +381,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
throw new BadRequestException(msg);
}
}
@GET
@Override
@Path("/{groupId}/location-history")
@ -393,7 +396,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
PaginationRequest request = new PaginationRequest(offset, limit);
DeviceList devices = new DeviceList();
// this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
try {
@ -416,17 +419,17 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String msg = "Error occurred while retrieving role list of user '" + authorizedUser + "'";
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}catch (BadRequestException e){
} catch (BadRequestException e) {
String msg = "Error occurred while validating the device group.";
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
PaginationResult result = dms.getAllDevices(request, false);
if (!result.getData().isEmpty()) {
devices.setList((List<Device>) result.getData());
for (Device device : devices.getList()) {
DeviceLocationHistorySnapshotWrapper snapshotWrapper = DeviceMgtAPIUtils.getDeviceHistorySnapshots(
device.getType(), device.getDeviceIdentifier(), authorizedUser, from, to, type,
@ -453,7 +456,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@DELETE
@Override
@Path("/type/{deviceType}/id/{deviceId}")
@ -462,20 +466,63 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceManagementProviderService deviceManagementProviderService =
DeviceMgtAPIUtils.getDeviceManagementService();
try {
long startTime = System.currentTimeMillis(); // Start measuring response time
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true);
if (persistedDevice == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
long endTime = System.currentTimeMillis(); // End measuring response time
long responseTime = endTime - startTime;
saveResponseTimeToGoogleSheet(responseTime); // Save response time to Google Sheet
return Response.status(Response.Status.OK).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error encountered while deleting requested device of type : " + deviceType ;
String msg = "Error encountered while deleting requested device of type : " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
private void saveResponseTimeToGoogleSheet(long responseTime) {
try {
Sheets sheetsService = createSheetsService();
// Prepare the data to be written
ValueRange requestBody = new ValueRange().setValues(Collections.singletonList(
Arrays.asList(System.currentTimeMillis(), responseTime)
));
// Write the data to the spreadsheet
AppendValuesResponse appendResponse = sheetsService.spreadsheets().values()
.append(SPREADSHEET_ID, RANGE, requestBody)
.setValueInputOption("RAW")
.execute();
System.out.println("Response Time saved to Google Sheet. Updated rows: " + appendResponse.getUpdates().getUpdatedRows());
} catch (IOException e) {
e.printStackTrace();
// Handle any exceptions that occur during the process
}
}
private Sheets createSheetsService() throws IOException {
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json"))
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
try {
return new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("ApiCall")
.build();
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
@POST
@Override
@Path("/type/{deviceType}/id/{deviceId}/rename")
@ -491,15 +538,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
boolean responseOfDeviceNameChanged = deviceManagementProviderService.sendDeviceNameChangedNotification(
persistedDevice);
boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged;
return Response.status(Response.Status.CREATED).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error encountered while updating requested device of type : " + deviceType ;
String msg = "Error encountered while updating requested device of type : " + deviceType;
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
@GET
@Path("/{type}/{id}")
@Override
@ -516,7 +563,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
// this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type);
@ -527,10 +574,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(HttpStatus.SC_UNAUTHORIZED).setMessage(msg).build()).build();
}
DeviceData deviceData = new DeviceData();
deviceData.setDeviceIdentifier(deviceIdentifier);
if (!StringUtils.isBlank(ifModifiedSince)) {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
try {
@ -542,7 +589,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
if (!StringUtils.isBlank(owner)) {
deviceData.setDeviceOwner(owner);
}
@ -571,7 +618,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
return Response.status(Response.Status.OK).entity(device).build();
}
@GET
@Path("/{deviceType}/{deviceId}/location-history")
public Response getDeviceLocationInfo(
@ -610,7 +657,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/type/any/id/{id}")
@Override
@ -624,10 +671,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
// this is the user who initiates the request
String authorizedUser = CarbonContext.getThreadLocalCarbonContext().getUsername();
Date sinceDate = null;
if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
@ -662,7 +709,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
"device id '" + id + "'";
log.error(message);
return Response.status(Response.Status.UNAUTHORIZED).entity(
new ErrorResponse.ErrorResponseBuilder().setCode(401l).setMessage(message).build()).build();
new ErrorResponse.ErrorResponseBuilder().setCode(401L).setMessage(message).build()).build();
}
} catch (DeviceManagementException e) {
String message = "Error occurred while fetching the device information.";
@ -677,7 +724,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
return Response.status(Response.Status.OK).entity(device).build();
}
@POST
@Path("/enrollment/guide")
@Override
@ -702,8 +749,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@POST
@Path("/type/any/list")
@Override
@ -724,7 +771,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/{type}/{id}/location")
@Override
@ -747,10 +794,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
return Response.status(Response.Status.OK).entity(deviceLocation).build();
}
@GET
@Path("/{type}/{id}/info")
@Override
@ -766,7 +813,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceIdentifier.setType(type);
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfo = informationManager.getDeviceInfo(deviceIdentifier);
} catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information of id : " + id + " type : " + type;
log.error(msg, e);
@ -774,8 +821,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
return Response.status(Response.Status.OK).entity(deviceInfo).build();
}
@GET
@Path("/{type}/{id}/config")
@Override
@ -783,11 +831,11 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id,
@HeaderParam("If-Modified-Since") String ifModifiedSince) {
DeviceConfig deviceConfig = new DeviceConfig();
deviceConfig.setDeviceId(id);
deviceConfig.setType(type);
// find token validity time
DeviceManagementProviderService deviceManagementProviderService =
DeviceMgtAPIUtils.getDeviceManagementService();
@ -802,82 +850,82 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
mqttEventTopicStructure = deviceType.getDeviceTypeMetaDefinition().getMqttEventTopicStructures();
} else {
String msg = "Device not found, device id : " + id + ", device type : " + type;
String msg = "Device not found, device id : " + id + ", device type : " + type;
log.error(msg);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving device, device id : " + id + ", device type : " + type;
String msg = "Error occurred while retrieving device, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
String applicationName = type.replace(" ", "").replace("_", "")
+ "_" + tenantDomain;
KeyMgtService keyMgtService = new KeyMgtServiceImpl();
try {
DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username,
"client_credentials", null, new String[] {"device_management"}, false, validityTime);
"client_credentials", null, new String[]{"device_management"}, false, validityTime);
deviceConfig.setClientId(dcrResponse.getClientId());
deviceConfig.setClientSecret(dcrResponse.getClientSecret());
StringBuilder scopes = new StringBuilder("device_" + type.replace(" ", "")
.replace("_", "") + "_" + id);
for (String topic : mqttEventTopicStructure) {
if (topic.contains("${deviceId}")) {
topic = topic.replace("${deviceId}", id);
}
topic = topic.replace("/",":");
topic = topic.replace("/", ":");
// scopes.append(" perm:topic:sub:".concat(topic));
scopes.append(" perm:topic:pub:".concat(topic));
}
// add scopes for retrieve operation topic /tenantDomain/deviceType/deviceId/operation/#
scopes.append(" perm:topic:sub:" + tenantDomain + ":" + type + ":" + id + ":operation");
// add scopes for update operation /tenantDomain/deviceType/deviceId/update/operation
scopes.append(" perm:topic:pub:" + tenantDomain + ":" + type + ":" + id + ":update:operation");
TokenRequest tokenRequest = new TokenRequest(dcrResponse.getClientId(), dcrResponse.getClientSecret(),
null, scopes.toString(), "client_credentials", null,
null, null, null, validityTime);
null, null, null, validityTime);
TokenResponse tokenResponse = keyMgtService.generateAccessToken(tokenRequest);
deviceConfig.setAccessToken(tokenResponse.getAccessToken());
deviceConfig.setRefreshToken(tokenResponse.getRefreshToken());
try {
deviceConfig.setPlatformConfiguration(deviceManagementProviderService.getConfiguration(type));
} catch (DeviceManagementException e) {
String msg = "Error occurred while reading platform configurations token, device id : " + id + ", device type : " + type;
String msg = "Error occurred while reading platform configurations token, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
deviceConfig.setMqttGateway("tcp://" + System.getProperty("mqtt.broker.host") + ":" + System.getProperty("mqtt.broker.port"));
deviceConfig.setHttpGateway("http://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.http.port"));
deviceConfig.setHttpsGateway("https://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.https.port"));
} catch (KeyMgtException e) {
String msg = "Error occurred while creating oauth application, device id : " + id + ", device type : " + type;
String msg = "Error occurred while creating oauth application, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (org.wso2.carbon.apimgt.keymgt.extension.exception.BadRequestException e) {
String msg = "Error occurred while generating token, device id : " + id + ", device type : " + type;
String msg = "Error occurred while generating token, device id : " + id + ", device type : " + type;
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
return Response.status(Response.Status.OK).entity(deviceConfig).build();
}
@GET
@Path("/device-type/{type}/features")
@Override
@ -892,7 +940,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
try {
fm = dms.getFeatureManager(type);
} catch (DeviceTypeNotFoundException e) {
String msg = "No device type found with name : " + type ;
String msg = "No device type found with name : " + type;
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
}
if (fm != null) {
@ -906,7 +954,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
return Response.status(Response.Status.OK).entity(features).build();
}
@POST
@Path("/search-devices")
@Override
@ -928,7 +976,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceList.setCount(devices.size());
return Response.status(Response.Status.OK).entity(deviceList).build();
}
@POST
@Path("/query-devices")
@Override
@ -961,7 +1009,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceList.setCount(devices.size());
return Response.status(Response.Status.OK).entity(deviceList).build();
}
@GET
@Path("/{type}/{id}/applications")
@Override
@ -993,7 +1041,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@POST
@Path("/{type}/{id}/uninstallation")
@Override
@ -1046,7 +1094,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (SubscriptionManagementException |
io.entgra.application.mgt.common.exception.ApplicationManagementException
io.entgra.application.mgt.common.exception.ApplicationManagementException
e) {
String msg = "Error occurred while getting the " + type + "application is of device " + id + "subscribed " +
"at entgra store";
@ -1069,7 +1117,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/{type}/{id}/operations")
@Override
@ -1123,12 +1171,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (DeviceTypeNotFoundException e) {
String msg = "No device type found with name : " + type ;
String msg = "No device type found with name : " + type;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
}
}
@GET
@Path("/{type}/{id}/effective-policy")
@Override
@ -1141,7 +1189,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
.getDevice(new DeviceIdentifier(id, type), false);
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
Policy policy = policyManagementService.getAppliedPolicyToDevice(device);
return Response.status(Response.Status.OK).entity(policy).build();
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving the current policy associated with the '" + type +
@ -1156,12 +1204,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Path("{type}/{id}/compliance-data")
public Response getComplianceDataOfDevice(@PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id) {
RequestValidationUtil.validateDeviceIdentifier(type, id);
Device device;
try {
@ -1177,7 +1225,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
Policy policy;
NonComplianceData complianceData;
DeviceCompliance deviceCompliance = new DeviceCompliance();
try {
policy = policyManagementService.getAppliedPolicyToDevice(device);
} catch (PolicyManagementException e) {
@ -1187,7 +1235,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
if (policy == null) {
deviceCompliance.setDeviceID(id);
deviceCompliance.setComplianceData(null);
@ -1207,7 +1255,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
}
}
/**
* Change device status.
*
@ -1233,12 +1281,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
boolean response = deviceManagementProviderService.changeDeviceStatus(deviceIdentifier, newsStatus);
return Response.status(Response.Status.OK).entity(response).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while changing device status of device type : " + type ;
String msg = "Error occurred while changing device status of device type : " + type;
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
/**
* List device status history
*
@ -1263,12 +1311,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
List<DeviceStatus> deviceStatusHistory = deviceManagementProviderService.getDeviceStatusHistory(persistedDevice);
return Response.status(Response.Status.OK).entity(deviceStatusHistory).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving device status history for device of type : " + type ;
String msg = "Error occurred while retrieving device status history for device of type : " + type;
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
/**
* List device status history for the current enrolment
*
@ -1298,7 +1346,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
@POST
@Path("/{type}/operations")
public Response addOperation(@PathParam("type") String type, @Valid OperationRequest operationRequest) {
@ -1332,7 +1380,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
operation.setCode(commandOperation.getCode());
operation.setEnabled(commandOperation.isEnabled());
operation.setStatus(commandOperation.getStatus());
} else if (operationType == Operation.Type.CONFIG) {
Operation configOperation = operationRequest.getOperation();
operation = new ConfigOperation();
@ -1341,7 +1389,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
operation.setEnabled(configOperation.isEnabled());
operation.setPayLoad(configOperation.getPayLoad());
operation.setStatus(configOperation.getStatus());
} else {
Operation profileOperation = operationRequest.getOperation();
operation = new ProfileOperation();
@ -1360,7 +1408,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
String message = "Only Command and Config operation is supported through this api";
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(message).build();
}
} catch (InvalidDeviceException e) {
String errorMessage = "Invalid Device Identifiers found.";
log.error(errorMessage, e);
@ -1381,7 +1429,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@GET
@Override
@Path("/type/{type}/status/{status}/count")
@ -1397,7 +1445,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
}
}
@GET
@Override
@Path("/type/{type}/status/{status}/ids")
@ -1413,7 +1461,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(errorMessage).build()).build();
}
}
@PUT
@Override
@Path("/type/{type}/status/{status}")
@ -1429,7 +1477,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
}
return Response.status(Response.Status.OK).build();
}
@GET
@Override
@Path("/compliance/{complianceStatus}")
@ -1444,15 +1492,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("offset") int offset,
@DefaultValue("10")
@QueryParam("limit") int limit) {
PaginationRequest request = new PaginationRequest(offset, limit);
ComplianceDeviceList complianceDeviceList = new ComplianceDeviceList();
PaginationResult paginationResult;
try {
PolicyManagerService policyManagerService = DeviceMgtAPIUtils.getPolicyManagementService();
paginationResult = policyManagerService.getPolicyCompliance(request, policyId, complianceStatus, isPending, fromDate, toDate);
if (paginationResult.getData().isEmpty()) {
return Response.status(Response.Status.OK)
.entity("No policy compliance or non compliance devices are available").build();
@ -1468,7 +1516,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Override
@Path("/{id}/features")
@ -1478,7 +1526,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
try {
PolicyManagerService policyManagerService = DeviceMgtAPIUtils.getPolicyManagementService();
complianceFeatureList = policyManagerService.getNoneComplianceFeatures(id);
if (complianceFeatureList.isEmpty()) {
return Response.status(Response.Status.OK).entity("No non compliance features are available").build();
} else {
@ -1491,7 +1539,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
}
@GET
@Override
@Consumes("application/json")
@ -1511,7 +1559,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
PaginationResult paginationResult = DeviceMgtAPIUtils
.getDeviceManagementService()
.getApplications(request);
if (paginationResult.getData().isEmpty()) {
return Response.status(Response.Status.OK)
.entity("No applications are available under " + deviceType + " platform.").build();
@ -1532,7 +1580,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/application/{packageName}/versions")
@Override
@ -1548,7 +1596,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@PUT
@Path("/{deviceType}/{id}/operation")
@Override
@ -1580,7 +1628,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
operation.setId(operationStatusBean.getOperationId());
operation.setCode(operationStatusBean.getOperationCode());
DeviceMgtAPIUtils.getDeviceManagementService().updateOperation(device, operation);
if (MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION.equals(operation.getCode()) ||
MDMAppConstants.AndroidConstants.OPCODE_UNINSTALL_APPLICATION.equals(operation.getCode())) {
ApplicationManager applicationManager = DeviceMgtAPIUtils.getApplicationManager();
@ -1592,7 +1640,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
} catch (DeviceManagementException e) {
String msg = "Error occurred when fetching device " + deviceIdentifier.toString();
String msg = "Error occurred when fetching device " + deviceIdentifier;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} catch (OperationManagementException e) {
@ -1606,7 +1654,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/filters")
@Override
@ -1638,7 +1686,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
@GET
@Path("/{clientId}/{clientSecret}/default-token")
@Override

Loading…
Cancel
Save