diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index d53d7f54c9..82c407082a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -23,13 +23,7 @@ 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.*; - -import java.io.FileInputStream; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.util.*; - +import com.google.api.services.sheets.v4.model.ValueRange; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; @@ -66,21 +60,19 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.user.api.UserStoreException; import javax.validation.Valid; -import javax.ws.rs.DELETE; -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.*; import javax.ws.rs.core.Response; +import java.io.FileInputStream; +import java.io.IOException; import java.rmi.RemoteException; -import java.util.List; +import java.security.GeneralSecurityException; +import java.util.*; @Path("/device/agent") public class DeviceAgentServiceImpl implements DeviceAgentService { static final Log log = LogFactory.getLog(DeviceAgentServiceImpl.class); private static final String POLICY_MONITOR = "POLICY_MONITOR"; + @POST @Path("/enroll") @Override @@ -137,7 +129,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { return Response.status(Response.Status.OK).build(); } else { 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) { String msg = "Error occurred while enrolling the device, which carries the id '" + id + "'"; @@ -173,7 +165,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { log.error(errorMessage); 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 = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); boolean status; @@ -189,25 +181,25 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { return Response.status(Response.Status.UNAUTHORIZED).build(); } } - if(updateDevice.getEnrolmentInfo() != null) { + if (updateDevice.getEnrolmentInfo() != null) { device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis()); device.setEnrolmentInfo(device.getEnrolmentInfo()); } device.getEnrolmentInfo().setOwner(DeviceMgtAPIUtils.getAuthenticatedUser()); - if(updateDevice.getDeviceInfo() != null) { + if (updateDevice.getDeviceInfo() != null) { device.setDeviceInfo(updateDevice.getDeviceInfo()); } device.setDeviceIdentifier(id); - if(updateDevice.getDescription() != null) { + if (updateDevice.getDescription() != null) { device.setDescription(updateDevice.getDescription()); } - if(updateDevice.getName() != null) { + if (updateDevice.getName() != null) { device.setName(updateDevice.getName()); } - if(updateDevice.getFeatures() != null) { + if (updateDevice.getFeatures() != null) { device.setFeatures(updateDevice.getFeatures()); } - if(updateDevice.getProperties() != null) { + if (updateDevice.getProperties() != null) { device.setProperties(updateDevice.getProperties()); } boolean result; @@ -247,7 +239,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } } - Object metaData[] = new Object[1]; + Object[] metaData = new Object[1]; metaData[0] = deviceId; EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type); if (eventAttributeList == null) { @@ -291,7 +283,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { // , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()) // , Constants.DEFAULT_STREAM_VERSION, metaData // , null, payloadData)) { - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).build(); // } else { // String msg = "Error occurred while publishing the event."; // return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); @@ -347,7 +339,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } } - Object metaData[] = new Object[1]; + Object[] metaData = new Object[1]; metaData[0] = deviceId; EventAttributeList eventAttributeList = DeviceMgtAPIUtils.getDynamicEventCache().get(type); if (eventAttributeList == null) { @@ -392,7 +384,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { // , PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain()) // , Constants.DEFAULT_STREAM_VERSION, metaData // , null, payloadData)) { - return Response.status(Response.Status.OK).build(); + return Response.status(Response.Status.OK).build(); // } else { // String msg = "Error occurred while publishing the event."; // return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); @@ -459,7 +451,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { String spreadsheetId = "1OZCS5NRwwSum9ai3ra4lABtU0UGW-9yLYgZk-aQfxpw"; // Replace with the actual spreadsheet ID // Create the values to be written - List> values = Arrays.asList( + List> values = Collections.singletonList( Arrays.asList(deviceId, String.valueOf(responseTime)) ); @@ -480,13 +472,14 @@ 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) { + } 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/target/apicall-382608-48aa6a62800d.json")) .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS)); @@ -579,14 +572,14 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { @PUT @Path("/properties/{type}/{id}") public Response updateDeviceProperties(@PathParam("type") String type, @PathParam("id") String deviceId, - @Valid List properties) { + @Valid List properties) { try { if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) { String errorMessage = "Device type is invalid"; log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); } - if(properties == null) { + if (properties == null) { String errorMessage = "Properties cannot be empty"; log.error(errorMessage); return Response.status(Response.Status.BAD_REQUEST).build(); @@ -598,7 +591,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { 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(); } else { return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Device properties not updated.").build(); @@ -644,7 +637,7 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } private static List getComplianceFeatures(Object compliancePayload) throws - PolicyComplianceException { + PolicyComplianceException { String compliancePayloadString = new Gson().toJson(compliancePayload); if (compliancePayload == null) { return null;