|
|
@ -18,6 +18,12 @@
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
package org.wso2.carbon.device.mgt.jaxrs.service.impl;
|
|
|
|
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.ValueRange;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
@ -54,23 +60,19 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
|
|
|
|
import javax.ws.rs.POST;
|
|
|
|
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.rmi.RemoteException;
|
|
|
|
import java.rmi.RemoteException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.security.GeneralSecurityException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/device/agent")
|
|
|
|
@Path("/device/agent")
|
|
|
|
public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
|
|
|
|
static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class);
|
|
|
|
private static final String POLICY_MONITOR = "POLICY_MONITOR";
|
|
|
|
private static final String POLICY_MONITOR = "POLICY_MONITOR";
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
@POST
|
|
|
|
@Path("/enroll")
|
|
|
|
@Path("/enroll")
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -127,7 +129,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(type + " device that carries id '" + id +
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(type + " device that carries id '" + id +
|
|
|
|
"' has not been dis-enrolled").build();
|
|
|
|
"' has not been dis-enrolled").build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'";
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'";
|
|
|
@ -163,7 +165,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
log.error(errorMessage);
|
|
|
|
log.error(errorMessage);
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.NOT_FOUND).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (device.getEnrolmentInfo().getStatus() == EnrolmentInfo.Status.ACTIVE ) {
|
|
|
|
if (device.getEnrolmentInfo().getStatus() == EnrolmentInfo.Status.ACTIVE) {
|
|
|
|
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
|
|
|
|
DeviceAccessAuthorizationService deviceAccessAuthorizationService =
|
|
|
|
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
|
|
|
|
DeviceMgtAPIUtils.getDeviceAccessAuthorizationService();
|
|
|
|
boolean status;
|
|
|
|
boolean status;
|
|
|
@ -179,25 +181,25 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).build();
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(updateDevice.getEnrolmentInfo() != null) {
|
|
|
|
if (updateDevice.getEnrolmentInfo() != null) {
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis());
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis());
|
|
|
|
device.setEnrolmentInfo(device.getEnrolmentInfo());
|
|
|
|
device.setEnrolmentInfo(device.getEnrolmentInfo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser());
|
|
|
|
device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser());
|
|
|
|
if(updateDevice.getDeviceInfo() != null) {
|
|
|
|
if (updateDevice.getDeviceInfo() != null) {
|
|
|
|
device.setDeviceInfo(updateDevice.getDeviceInfo());
|
|
|
|
device.setDeviceInfo(updateDevice.getDeviceInfo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
device.setDeviceIdentifier(id);
|
|
|
|
device.setDeviceIdentifier(id);
|
|
|
|
if(updateDevice.getDescription() != null) {
|
|
|
|
if (updateDevice.getDescription() != null) {
|
|
|
|
device.setDescription(updateDevice.getDescription());
|
|
|
|
device.setDescription(updateDevice.getDescription());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(updateDevice.getName() != null) {
|
|
|
|
if (updateDevice.getName() != null) {
|
|
|
|
device.setName(updateDevice.getName());
|
|
|
|
device.setName(updateDevice.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(updateDevice.getFeatures() != null) {
|
|
|
|
if (updateDevice.getFeatures() != null) {
|
|
|
|
device.setFeatures(updateDevice.getFeatures());
|
|
|
|
device.setFeatures(updateDevice.getFeatures());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(updateDevice.getProperties() != null) {
|
|
|
|
if (updateDevice.getProperties() != null) {
|
|
|
|
device.setProperties(updateDevice.getProperties());
|
|
|
|
device.setProperties(updateDevice.getProperties());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boolean result;
|
|
|
|
boolean result;
|
|
|
@ -237,7 +239,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object metaData[] = new Object[1];
|
|
|
|
Object[] metaData = new Object[1];
|
|
|
|
metaData[0] = deviceId;
|
|
|
|
metaData[0] = deviceId;
|
|
|
|
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
|
|
|
|
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
|
|
|
|
if (eventAttributeList == null) {
|
|
|
|
if (eventAttributeList == null) {
|
|
|
@ -281,7 +283,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
// , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
|
|
|
|
// , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
|
|
|
|
// , Constants.DEFAULT_STREAM_VERSION, metaData
|
|
|
|
// , Constants.DEFAULT_STREAM_VERSION, metaData
|
|
|
|
// , null, payloadData)) {
|
|
|
|
// , null, payloadData)) {
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
// } else {
|
|
|
|
// } else {
|
|
|
|
// String msg = "Error occurred while publishing the event.";
|
|
|
|
// String msg = "Error occurred while publishing the event.";
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
@ -337,7 +339,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Object metaData[] = new Object[1];
|
|
|
|
Object[] metaData = new Object[1];
|
|
|
|
metaData[0] = deviceId;
|
|
|
|
metaData[0] = deviceId;
|
|
|
|
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
|
|
|
|
EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type);
|
|
|
|
if (eventAttributeList == null) {
|
|
|
|
if (eventAttributeList == null) {
|
|
|
@ -382,7 +384,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
// , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
|
|
|
|
// , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain())
|
|
|
|
// , Constants.DEFAULT_STREAM_VERSION, metaData
|
|
|
|
// , Constants.DEFAULT_STREAM_VERSION, metaData
|
|
|
|
// , null, payloadData)) {
|
|
|
|
// , null, payloadData)) {
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
// } else {
|
|
|
|
// } else {
|
|
|
|
// String msg = "Error occurred while publishing the event.";
|
|
|
|
// String msg = "Error occurred while publishing the event.";
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
@ -433,23 +435,63 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.NO_CONTENT).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
|
|
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
|
|
|
|
List<? extends Operation> operations = DeviceMgtAPIUtils.getDeviceManagementService().getPendingOperations(
|
|
|
|
deviceIdentifier);
|
|
|
|
deviceIdentifier);
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setList(operations);
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
operationsList.setCount(operations.size());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
|
|
|
|
Sheets sheetsService = createSheetsService(); // Create Sheets service using loaded credentials
|
|
|
|
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
|
|
|
|
Arrays.asList(deviceId, String.valueOf(responseTime))
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create the value range
|
|
|
|
|
|
|
|
ValueRange body = new ValueRange().setValues(values);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Write the values to the spreadsheet
|
|
|
|
|
|
|
|
sheetsService.spreadsheets().values()
|
|
|
|
|
|
|
|
.append(spreadsheetId, "Sheet1", body)
|
|
|
|
|
|
|
|
.setValueInputOption("USER_ENTERED")
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
return Response.status(Response.Status.OK).entity(operationsList).build();
|
|
|
|
return Response.status(Response.Status.OK).entity(operationsList).build();
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
String errorMessage = "Issue in retrieving deivce management service instance";
|
|
|
|
String errorMessage = "Issue in retrieving device management service instance";
|
|
|
|
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
// Handle any errors occurred while writing to the spreadsheet
|
|
|
|
|
|
|
|
String errorMessage = "Error writing response time to Google Spreadsheet";
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
log.error(errorMessage, e);
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Sheets createSheetsService() throws IOException {
|
|
|
|
|
|
|
|
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("components/device-mgt/org.wso2.carbon.device.mgt.api/target/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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@GET
|
|
|
|
@Path("/next-pending/operation/{type}/{id}")
|
|
|
|
@Path("/next-pending/operation/{type}/{id}")
|
|
|
|
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
|
public Response getNextPendingOperation(@PathParam("type") String type, @PathParam("id") String deviceId) {
|
|
|
@ -530,14 +572,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
@PUT
|
|
|
|
@PUT
|
|
|
|
@Path("/properties/{type}/{id}")
|
|
|
|
@Path("/properties/{type}/{id}")
|
|
|
|
public Response updateDeviceProperties(@PathParam("type") String type, @PathParam("id") String deviceId,
|
|
|
|
public Response updateDeviceProperties(@PathParam("type") String type, @PathParam("id") String deviceId,
|
|
|
|
@Valid List<Device.Property> properties) {
|
|
|
|
@Valid List<Device.Property> properties) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
|
|
|
String errorMessage = "Device type is invalid";
|
|
|
|
String errorMessage = "Device type is invalid";
|
|
|
|
log.error(errorMessage);
|
|
|
|
log.error(errorMessage);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(properties == null) {
|
|
|
|
if (properties == null) {
|
|
|
|
String errorMessage = "Properties cannot be empty";
|
|
|
|
String errorMessage = "Properties cannot be empty";
|
|
|
|
log.error(errorMessage);
|
|
|
|
log.error(errorMessage);
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
@ -549,7 +591,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)){
|
|
|
|
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) {
|
|
|
|
return Response.status(Response.Status.ACCEPTED).entity("Device properties updated.").build();
|
|
|
|
return Response.status(Response.Status.ACCEPTED).entity("Device properties updated.").build();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Device properties not updated.").build();
|
|
|
|
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Device properties not updated.").build();
|
|
|
@ -595,7 +637,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
|
|
|
|
private static List<ComplianceFeature> getComplianceFeatures(Object compliancePayload) throws
|
|
|
|
PolicyComplianceException {
|
|
|
|
PolicyComplianceException {
|
|
|
|
String compliancePayloadString = new Gson().toJson(compliancePayload);
|
|
|
|
String compliancePayloadString = new Gson().toJson(compliancePayload);
|
|
|
|
if (compliancePayload == null) {
|
|
|
|
if (compliancePayload == null) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|