|
|
@ -18,26 +18,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.api.services.sheets.v4.model.ValueRange;
|
|
|
|
|
|
|
|
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.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionManagementException;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
|
|
|
|
|
|
|
|
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 io.entgra.device.mgt.core.apimgt.keymgt.extension.DCRResponse;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.DCRResponse;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenRequest;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenRequest;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenResponse;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenResponse;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.exception.KeyMgtException;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.exception.KeyMgtException;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtServiceImpl;
|
|
|
|
import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtServiceImpl;
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionManagementException;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.*;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.DeviceManagementService;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.InputValidationException;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.*;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.*;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationManagementException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationManagementException;
|
|
|
@ -64,7 +69,6 @@ import io.entgra.device.mgt.core.device.mgt.common.type.mgt.DeviceStatus;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.app.mgt.ApplicationManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.app.mgt.ApplicationManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceManagementConfig;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.dao.TrackerManagementDAOException;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceDetailsMgtException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceInformationManager;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.device.details.mgt.DeviceInformationManager;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
|
|
|
@ -75,22 +79,19 @@ import io.entgra.device.mgt.core.device.mgt.core.search.mgt.SearchManagerService
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.search.mgt.SearchMgtException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.search.mgt.SearchMgtException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.traccar.api.service.DeviceAPIClientService;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.traccar.common.TraccarHandlerConstants;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.util.HttpReportingUtil;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.*;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.DeviceManagementService;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.InputValidationException;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService;
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService;
|
|
|
|
import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException;
|
|
|
|
import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException;
|
|
|
|
import io.entgra.device.mgt.core.policy.mgt.core.PolicyManagerService;
|
|
|
|
import io.entgra.device.mgt.core.policy.mgt.core.PolicyManagerService;
|
|
|
|
|
|
|
|
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.wso2.carbon.context.CarbonContext;
|
|
|
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
|
|
|
|
|
|
|
|
|
|
@ -98,13 +99,12 @@ import javax.validation.Valid;
|
|
|
|
import javax.validation.constraints.Size;
|
|
|
|
import javax.validation.constraints.Size;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
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.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/devices")
|
|
|
|
@Path("/devices")
|
|
|
|
public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
@ -112,6 +112,61 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
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 Log log = LogFactory.getLog(DeviceManagementServiceImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Validate group Id and group Id greater than 0 and exist.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param groupId Group ID of the group
|
|
|
|
|
|
|
|
* @param from time to start getting DeviceLocationHistorySnapshotWrapper in milliseconds
|
|
|
|
|
|
|
|
* @param to time to end getting DeviceLocationHistorySnapshotWrapper in milliseconds
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static void validateGroupId(int groupId, long from, long to) throws GroupManagementException, BadRequestException {
|
|
|
|
|
|
|
|
if (from == 0 || to == 0) {
|
|
|
|
|
|
|
|
String msg = "Invalid values for from/to";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupId <= 0) {
|
|
|
|
|
|
|
|
String msg = "Invalid group ID '" + groupId + "'";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
|
|
|
|
|
|
|
|
if (service.getGroup(groupId, false) == null) {
|
|
|
|
|
|
|
|
String msg = "Invalid group ID '" + groupId + "'";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveResponseTimeToSpreadsheet(long startTime, long endTime, long responseTime) throws IOException, GeneralSecurityException {
|
|
|
|
|
|
|
|
// Load credentials from JSON file
|
|
|
|
|
|
|
|
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("/home/entgra/MyProject/device-mgt-core/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/resources/apicall-382608-48aa6a62800d.json"))
|
|
|
|
|
|
|
|
.createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build Google Sheets service
|
|
|
|
|
|
|
|
Sheets sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(),
|
|
|
|
|
|
|
|
JacksonFactory.getDefaultInstance(), credential)
|
|
|
|
|
|
|
|
.setApplicationName("ApiCall")
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Define the spreadsheet ID and range
|
|
|
|
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
String range = "Sheet1!A:C";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare the values to be written
|
|
|
|
|
|
|
|
List<Object> rowValues = Arrays.asList(startTime, endTime, responseTime);
|
|
|
|
|
|
|
|
List<List<Object>> values = Collections.singletonList(rowValues);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Build the value range object
|
|
|
|
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write the response time value to the spreadsheet
|
|
|
|
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
|
|
|
|
.append(spreadsheetId, range, body)
|
|
|
|
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/{type}/{id}/status")
|
|
|
|
@Path("/{type}/{id}/status")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -335,32 +390,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Validate group Id and group Id greater than 0 and exist.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param groupId Group ID of the group
|
|
|
|
|
|
|
|
* @param from time to start getting DeviceLocationHistorySnapshotWrapper in milliseconds
|
|
|
|
|
|
|
|
* @param to time to end getting DeviceLocationHistorySnapshotWrapper in milliseconds
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static void validateGroupId(int groupId, long from, long to) throws GroupManagementException, BadRequestException {
|
|
|
|
|
|
|
|
if (from == 0 || to == 0) {
|
|
|
|
|
|
|
|
String msg = "Invalid values for from/to";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupId <= 0) {
|
|
|
|
|
|
|
|
String msg = "Invalid group ID '" + groupId + "'";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
|
|
|
|
|
|
|
|
if (service.getGroup(groupId, false) == null) {
|
|
|
|
|
|
|
|
String msg = "Invalid group ID '" + groupId + "'";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
@Path("/{groupId}/location-history")
|
|
|
|
@Path("/{groupId}/location-history")
|
|
|
@ -398,7 +427,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
String msg = "Error occurred while retrieving role list of user '" + authorizedUser + "'";
|
|
|
|
String msg = "Error occurred while retrieving role list of user '" + authorizedUser + "'";
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
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.";
|
|
|
|
String msg = "Error occurred while validating the device group.";
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
@ -444,17 +473,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService =
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService =
|
|
|
|
DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis(); // start measuring API call response time
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType);
|
|
|
|
Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true);
|
|
|
|
Device persistedDevice = deviceManagementProviderService.getDevice(deviceIdentifier, true);
|
|
|
|
if (persistedDevice == null) {
|
|
|
|
if (persistedDevice == null) {
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
|
|
|
|
boolean response = deviceManagementProviderService.disenrollDevice(deviceIdentifier);
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis(); //end measuring API call response time
|
|
|
|
|
|
|
|
long responseTime = endTime - startTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// save the start time, end time and response time to google sheet
|
|
|
|
|
|
|
|
saveResponseTimeToSpreadsheet(startTime, endTime, responseTime);
|
|
|
|
return Response.status(Response.Status.OK).entity(response).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(response).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
|
|
|
|
} catch (IOException | GeneralSecurityException e) {
|
|
|
|
|
|
|
|
String msg = "Error encountered while saving response time to spreadsheet";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -465,6 +504,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
@PathParam("deviceId") String deviceId) {
|
|
|
|
@PathParam("deviceId") String deviceId) {
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceManagementService();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Start measuring API call response time
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier
|
|
|
|
Device persistedDevice = deviceManagementProviderService.getDevice(new DeviceIdentifier
|
|
|
|
(deviceId, deviceType), true);
|
|
|
|
(deviceId, deviceType), true);
|
|
|
|
persistedDevice.setName(device.getName());
|
|
|
|
persistedDevice.setName(device.getName());
|
|
|
@ -474,11 +515,22 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
persistedDevice);
|
|
|
|
persistedDevice);
|
|
|
|
boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged;
|
|
|
|
boolean response = responseOfmodifyEnrollment && responseOfDeviceNameChanged;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// End measuring API call response time
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
long responseTime = endTime - startTime;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time, start time, and end time to Google Spreadsheet
|
|
|
|
|
|
|
|
saveResponseTimeToSpreadsheet(startTime, endTime, responseTime);
|
|
|
|
|
|
|
|
|
|
|
|
return Response.status(Response.Status.CREATED).entity(response).build();
|
|
|
|
return Response.status(Response.Status.CREATED).entity(response).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
|
|
|
|
} catch (IOException | GeneralSecurityException e) {
|
|
|
|
|
|
|
|
String msg = "Error encountered while saving response time to spreadsheet";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -644,7 +696,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
"device id '" + id + "'";
|
|
|
|
"device id '" + id + "'";
|
|
|
|
log.error(message);
|
|
|
|
log.error(message);
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(
|
|
|
|
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) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String message = "Error occurred while fetching the device information.";
|
|
|
|
String message = "Error occurred while fetching the device information.";
|
|
|
@ -758,6 +810,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
return Response.status(Response.Status.OK).entity(deviceInfo).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(deviceInfo).build();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/{type}/{id}/config")
|
|
|
|
@Path("/{type}/{id}/config")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -784,13 +837,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mqttEventTopicStructure = deviceType.getDeviceTypeMetaDefinition().getMqttEventTopicStructures();
|
|
|
|
mqttEventTopicStructure = deviceType.getDeviceTypeMetaDefinition().getMqttEventTopicStructures();
|
|
|
|
} else {
|
|
|
|
} 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);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
@ -804,7 +857,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
KeyMgtService keyMgtService = new KeyMgtServiceImpl();
|
|
|
|
KeyMgtService keyMgtService = new KeyMgtServiceImpl();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DCRResponse dcrResponse = keyMgtService.dynamicClientRegistration(applicationName, username,
|
|
|
|
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.setClientId(dcrResponse.getClientId());
|
|
|
|
deviceConfig.setClientSecret(dcrResponse.getClientSecret());
|
|
|
|
deviceConfig.setClientSecret(dcrResponse.getClientSecret());
|
|
|
|
|
|
|
|
|
|
|
@ -813,7 +866,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
if (topic.contains("${deviceId}")) {
|
|
|
|
if (topic.contains("${deviceId}")) {
|
|
|
|
topic = topic.replace("${deviceId}", id);
|
|
|
|
topic = topic.replace("${deviceId}", id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
topic = topic.replace("/",":");
|
|
|
|
topic = topic.replace("/", ":");
|
|
|
|
// scopes.append(" perm:topic:sub:".concat(topic));
|
|
|
|
// scopes.append(" perm:topic:sub:".concat(topic));
|
|
|
|
scopes.append(" perm:topic:pub:".concat(topic));
|
|
|
|
scopes.append(" perm:topic:pub:".concat(topic));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -826,7 +879,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
|
|
|
|
|
|
|
|
TokenRequest tokenRequest = new TokenRequest(dcrResponse.getClientId(), dcrResponse.getClientSecret(),
|
|
|
|
TokenRequest tokenRequest = new TokenRequest(dcrResponse.getClientId(), dcrResponse.getClientSecret(),
|
|
|
|
null, scopes.toString(), "client_credentials", null,
|
|
|
|
null, scopes.toString(), "client_credentials", null,
|
|
|
|
null, null, null, validityTime);
|
|
|
|
null, null, null, validityTime);
|
|
|
|
TokenResponse tokenResponse = keyMgtService.generateAccessToken(tokenRequest);
|
|
|
|
TokenResponse tokenResponse = keyMgtService.generateAccessToken(tokenRequest);
|
|
|
|
deviceConfig.setAccessToken(tokenResponse.getAccessToken());
|
|
|
|
deviceConfig.setAccessToken(tokenResponse.getAccessToken());
|
|
|
|
deviceConfig.setRefreshToken(tokenResponse.getRefreshToken());
|
|
|
|
deviceConfig.setRefreshToken(tokenResponse.getRefreshToken());
|
|
|
@ -834,7 +887,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
deviceConfig.setPlatformConfiguration(deviceManagementProviderService.getConfiguration(type));
|
|
|
|
deviceConfig.setPlatformConfiguration(deviceManagementProviderService.getConfiguration(type));
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
@ -845,12 +898,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
deviceConfig.setHttpsGateway("https://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.https.port"));
|
|
|
|
deviceConfig.setHttpsGateway("https://" + System.getProperty("iot.gateway.host") + ":" + System.getProperty("iot.gateway.https.port"));
|
|
|
|
|
|
|
|
|
|
|
|
} catch (KeyMgtException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
} catch (io.entgra.device.mgt.core.apimgt.keymgt.extension.exception.BadRequestException e) {
|
|
|
|
} catch (io.entgra.device.mgt.core.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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
@ -873,7 +926,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
fm = dms.getFeatureManager(type);
|
|
|
|
fm = dms.getFeatureManager(type);
|
|
|
|
} catch (DeviceTypeNotFoundException e) {
|
|
|
|
} 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();
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fm != null) {
|
|
|
|
if (fm != null) {
|
|
|
@ -1027,7 +1080,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
return Response.serverError().entity(
|
|
|
|
return Response.serverError().entity(
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
|
|
|
|
} catch (SubscriptionManagementException |
|
|
|
|
} catch (SubscriptionManagementException |
|
|
|
|
io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException
|
|
|
|
io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException
|
|
|
|
e) {
|
|
|
|
e) {
|
|
|
|
String msg = "Error occurred while getting the " + type + "application is of device " + id + "subscribed " +
|
|
|
|
String msg = "Error occurred while getting the " + type + "application is of device " + id + "subscribed " +
|
|
|
|
"at entgra store";
|
|
|
|
"at entgra store";
|
|
|
@ -1104,7 +1157,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
} catch (DeviceTypeNotFoundException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1214,7 +1267,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
boolean response = deviceManagementProviderService.changeDeviceStatus(deviceIdentifier, newsStatus);
|
|
|
|
boolean response = deviceManagementProviderService.changeDeviceStatus(deviceIdentifier, newsStatus);
|
|
|
|
return Response.status(Response.Status.OK).entity(response).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(response).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1244,7 +1297,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
List<DeviceStatus> deviceStatusHistory = deviceManagementProviderService.getDeviceStatusHistory(persistedDevice);
|
|
|
|
List<DeviceStatus> deviceStatusHistory = deviceManagementProviderService.getDeviceStatusHistory(persistedDevice);
|
|
|
|
return Response.status(Response.Status.OK).entity(deviceStatusHistory).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(deviceStatusHistory).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} 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);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1573,7 +1626,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String msg = "Error occurred when fetching device " + deviceIdentifier.toString();
|
|
|
|
String msg = "Error occurred when fetching device " + deviceIdentifier;
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|