|
|
|
@ -18,12 +18,6 @@
|
|
|
|
|
*/
|
|
|
|
|
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.JsonArray;
|
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
@ -62,10 +56,8 @@ import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
import javax.validation.Valid;
|
|
|
|
|
import javax.ws.rs.*;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.rmi.RemoteException;
|
|
|
|
|
import java.security.GeneralSecurityException;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@Path("/device/agent")
|
|
|
|
@ -123,26 +115,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
|
|
|
|
|
DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId);
|
|
|
|
|
pap.publishChanges();
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
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(device.getDeviceIdentifier(), device.getType(), String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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(status).build();
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" +
|
|
|
|
@ -155,11 +127,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
} catch (PolicyManagementException e) {
|
|
|
|
|
log.error("failed to add designated policies against newly enrolled device.", e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -169,29 +136,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
public Response disEnrollDevice(@PathParam("type") String type, @PathParam("id") String id) {
|
|
|
|
|
boolean result;
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(id, type);
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
try {
|
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().disenrollDevice(deviceIdentifier);
|
|
|
|
|
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(id, type, 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();
|
|
|
|
|
if (result) {
|
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
|
} else {
|
|
|
|
@ -202,11 +148,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -278,25 +219,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
try {
|
|
|
|
|
device.setType(type);
|
|
|
|
|
result = DeviceMgtAPIUtils.getDeviceManagementService().modifyEnrollment(device);
|
|
|
|
|
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
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(id, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
if (result) {
|
|
|
|
|
return Response.status(Response.Status.ACCEPTED).build();
|
|
|
|
|
} else {
|
|
|
|
@ -307,11 +229,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
id + "'";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -538,27 +455,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
|
operationsList.setList(operations);
|
|
|
|
|
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"; //Update with Spreadsheet ID
|
|
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
@ -568,23 +465,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
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);
|
|
|
|
|
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/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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -605,32 +485,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
}
|
|
|
|
|
Operation operation = DeviceMgtAPIUtils.getDeviceManagementService().getNextPendingOperation(
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
// Save the response time to Google Spreadsheet
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
// Create Sheets service using loaded credentials
|
|
|
|
|
Sheets sheetsService = createSheetsService();
|
|
|
|
|
|
|
|
|
|
// Spreadsheet ID
|
|
|
|
|
String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw";
|
|
|
|
|
|
|
|
|
|
// Calculate the response time
|
|
|
|
|
long endTime = System.currentTimeMillis(); // Record the end time
|
|
|
|
|
long responseTime = endTime - startTime; // Calculate the response time
|
|
|
|
|
|
|
|
|
|
// Create the values to be written
|
|
|
|
|
List<List<Object>> values = Collections.singletonList(
|
|
|
|
|
Arrays.asList(deviceId, type, 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(operation).build();
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
@ -640,11 +494,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
String errorMessage = "Issue in retrieving deivce 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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -717,26 +566,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
log.error(msg);
|
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
|
|
|
|
}
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
if (DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties)) {
|
|
|
|
|
// 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, type, String.valueOf(System.currentTimeMillis() - startTime))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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.ACCEPTED).entity("Device properties updated.").build();
|
|
|
|
|
} else {
|
|
|
|
|
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Device properties not updated.").build();
|
|
|
|
@ -745,11 +576,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -762,8 +588,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
log.error(errorMessage);
|
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).build();
|
|
|
|
|
}
|
|
|
|
|
long startTime = System.currentTimeMillis(); // Record the start time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) {
|
|
|
|
@ -776,27 +600,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
OperationList operationsList = new OperationList();
|
|
|
|
|
operationsList.setList(operations);
|
|
|
|
|
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, type, status.toString(), 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();
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
String errorMessage = "Issue in retrieving operation management service instance";
|
|
|
|
@ -806,11 +609,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
|
|
|
|
|
String errorMessage = "Issue in retrieving device management service";
|
|
|
|
|
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);
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|