@ -21,9 +21,12 @@ package org.homeautomation.currentsensor.manager.api;
import org.apache.commons.io.FileUtils ;
import org.apache.commons.io.FileUtils ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.homeautomation.currentsensor.manager.api.util.APIUtil ;
import org.homeautomation.currentsensor.plugin.constants.CurrentSensorConstants ;
import org.homeautomation.currentsensor.plugin.constants.CurrentSensorConstants ;
import org.wso2.carbon.apimgt.annotations.api.API ;
import org.wso2.carbon.apimgt.annotations.device.DeviceType ;
import org.wso2.carbon.apimgt.annotations.device.DeviceType ;
import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil ;
import org.wso2.carbon.apimgt.webapp.publisher.KeyGenerationUtil ;
import org.wso2.carbon.context.PrivilegedCarbonContext ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.Device ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
import org.wso2.carbon.device.mgt.common.DeviceManagementException ;
@ -50,205 +53,176 @@ import java.util.UUID;
@DeviceType ( value = "currentsensor" )
@DeviceType ( value = "currentsensor" )
public class CurrentSensorManagerService {
public class CurrentSensorManagerService {
private static Log log = LogFactory . getLog ( CurrentSensorManagerService . class ) ;
private static Log log = LogFactory . getLog ( CurrentSensorManagerService . class ) ;
//TODO; replace this tenant domain
@Context //injected response proxy supporting multiple thread
private final String SUPER_TENANT = "carbon.super" ;
private HttpServletResponse response ;
@Context //injected response proxy supporting multiple thread
private HttpServletResponse response ;
@Path ( "manager/device" )
@PUT
@Path ( "manager/device/register" )
public boolean register ( @QueryParam ( "name" ) String name ) {
@PUT
String deviceId = shortUUID ( ) ;
public boolean register ( @QueryParam ( "deviceId" ) String deviceId ,
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
@QueryParam ( "name" ) String name , @QueryParam ( "owner" ) String owner ) {
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
DeviceManagement deviceManagement = new DeviceManagement ( SUPER_TENANT ) ;
try {
if ( APIUtil . getDeviceManagementService ( ) . isEnrolled ( deviceIdentifier ) ) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
response . setStatus ( Response . Status . CONFLICT . getStatusCode ( ) ) ;
deviceIdentifier . setId ( deviceId ) ;
return false ;
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
}
try {
Device device = new Device ( ) ;
if ( deviceManagement . getDeviceManagementService ( ) . isEnrolled ( deviceIdentifier ) ) {
device . setDeviceIdentifier ( deviceId ) ;
response . setStatus ( Response . Status . CONFLICT . getStatusCode ( ) ) ;
EnrolmentInfo enrolmentInfo = new EnrolmentInfo ( ) ;
return false ;
enrolmentInfo . setDateOfEnrolment ( new Date ( ) . getTime ( ) ) ;
}
enrolmentInfo . setDateOfLastUpdate ( new Date ( ) . getTime ( ) ) ;
enrolmentInfo . setStatus ( EnrolmentInfo . Status . ACTIVE ) ;
Device device = new Device ( ) ;
device . setName ( name ) ;
device . setDeviceIdentifier ( deviceId ) ;
device . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
EnrolmentInfo enrolmentInfo = new EnrolmentInfo ( ) ;
enrolmentInfo . setOwner ( APIUtil . getAuthenticatedUser ( ) ) ;
enrolmentInfo . setDateOfEnrolment ( new Date ( ) . getTime ( ) ) ;
device . setEnrolmentInfo ( enrolmentInfo ) ;
enrolmentInfo . setDateOfLastUpdate ( new Date ( ) . getTime ( ) ) ;
boolean added = APIUtil . getDeviceManagementService ( ) . enrollDevice ( device ) ;
enrolmentInfo . setStatus ( EnrolmentInfo . Status . ACTIVE ) ;
if ( added ) {
device . setName ( name ) ;
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
device . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
} else {
enrolmentInfo . setOwner ( owner ) ;
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
device . setEnrolmentInfo ( enrolmentInfo ) ;
}
boolean added = deviceManagement . getDeviceManagementService ( ) . enrollDevice ( device ) ;
return added ;
if ( added ) {
} catch ( DeviceManagementException e ) {
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
response . setStatus ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) ;
} else {
return false ;
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
} finally {
}
PrivilegedCarbonContext . endTenantFlow ( ) ;
}
return added ;
}
} catch ( DeviceManagementException e ) {
response . setStatus ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) ;
@Path ( "manager/device/remove/{device_id}" )
return false ;
@DELETE
} finally {
public void removeDevice ( @PathParam ( "device_id" ) String deviceId ,
deviceManagement . endTenantFlow ( ) ;
@Context HttpServletResponse response ) {
}
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
}
deviceIdentifier . setId ( deviceId ) ;
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
@Path ( "manager/device/remove/{device_id}" )
try {
@DELETE
boolean removed = APIUtil . getDeviceManagementService ( ) . disenrollDevice (
public void removeDevice ( @PathParam ( "device_id" ) String deviceId ,
deviceIdentifier ) ;
@Context HttpServletResponse response ) {
if ( removed ) {
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
DeviceManagement deviceManagement = new DeviceManagement ( SUPER_TENANT ) ;
} else {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
deviceIdentifier . setId ( deviceId ) ;
}
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
} catch ( DeviceManagementException e ) {
try {
response . setStatus ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) ;
boolean removed = deviceManagement . getDeviceManagementService ( ) . disenrollDevice (
} finally {
deviceIdentifier ) ;
PrivilegedCarbonContext . endTenantFlow ( ) ;
if ( removed ) {
}
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
}
} else {
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
@Path ( "manager/device/update/{device_id}" )
}
@POST
} catch ( DeviceManagementException e ) {
public boolean updateDevice ( @PathParam ( "device_id" ) String deviceId ,
response . setStatus ( Response . Status . INTERNAL_SERVER_ERROR . getStatusCode ( ) ) ;
@QueryParam ( "name" ) String name ,
} finally {
@Context HttpServletResponse response ) {
deviceManagement . endTenantFlow ( ) ;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
}
deviceIdentifier . setId ( deviceId ) ;
}
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
try {
@Path ( "manager/device/update/{device_id}" )
Device device = APIUtil . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
@POST
device . setDeviceIdentifier ( deviceId ) ;
public boolean updateDevice ( @PathParam ( "device_id" ) String deviceId ,
device . getEnrolmentInfo ( ) . setDateOfLastUpdate ( new Date ( ) . getTime ( ) ) ;
@QueryParam ( "name" ) String name ,
device . setName ( name ) ;
@Context HttpServletResponse response ) {
device . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
boolean updated = APIUtil . getDeviceManagementService ( ) . modifyEnrollment ( device ) ;
DeviceManagement deviceManagement = new DeviceManagement ( SUPER_TENANT ) ;
if ( updated ) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
deviceIdentifier . setId ( deviceId ) ;
} else {
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
try {
}
Device device = deviceManagement . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
return updated ;
device . setDeviceIdentifier ( deviceId ) ;
} catch ( DeviceManagementException e ) {
// device.setDeviceTypeId(deviceTypeId);
log . error ( e . getErrorMessage ( ) ) ;
device . getEnrolmentInfo ( ) . setDateOfLastUpdate ( new Date ( ) . getTime ( ) ) ;
return false ;
} finally {
device . setName ( name ) ;
PrivilegedCarbonContext . endTenantFlow ( ) ;
device . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
}
}
boolean updated = deviceManagement . getDeviceManagementService ( ) . modifyEnrollment ( device ) ;
@Path ( "manager/device/{device_id}" )
if ( updated ) {
@GET
response . setStatus ( Response . Status . OK . getStatusCode ( ) ) ;
@Consumes ( MediaType . APPLICATION_JSON )
@Produces ( MediaType . APPLICATION_JSON )
} else {
public Device getDevice ( @PathParam ( "device_id" ) String deviceId ) {
response . setStatus ( Response . Status . NOT_ACCEPTABLE . getStatusCode ( ) ) ;
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
}
deviceIdentifier . setId ( deviceId ) ;
return updated ;
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
} catch ( DeviceManagementException e ) {
try {
log . error ( e . getErrorMessage ( ) ) ;
return APIUtil . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
return false ;
} catch ( DeviceManagementException ex ) {
} finally {
log . error ( "Error occurred while retrieving device with Id " + deviceId + "\n" + ex ) ;
deviceManagement . endTenantFlow ( ) ;
return null ;
}
} finally {
}
PrivilegedCarbonContext . endTenantFlow ( ) ;
}
@Path ( "manager/device/{device_id}" )
}
@GET
@Consumes ( MediaType . APPLICATION_JSON )
@Path ( "manager/device/{sketch_type}/download" )
@Produces ( MediaType . APPLICATION_JSON )
@GET
public Device getDevice ( @PathParam ( "device_id" ) String deviceId ) {
@Produces ( MediaType . APPLICATION_JSON )
public Response downloadSketch ( @QueryParam ( "owner" ) String owner ,
DeviceManagement deviceManagement = new DeviceManagement ( SUPER_TENANT ) ;
@QueryParam ( "deviceName" ) String deviceName ,
DeviceIdentifier deviceIdentifier = new DeviceIdentifier ( ) ;
@PathParam ( "sketch_type" ) String sketchType ) {
deviceIdentifier . setId ( deviceId ) ;
try {
deviceIdentifier . setType ( CurrentSensorConstants . DEVICE_TYPE ) ;
ZipArchive zipFile = createDownloadFile ( owner , deviceName , sketchType ) ;
Response . ResponseBuilder response = Response . ok ( FileUtils . readFileToByteArray ( zipFile . getZipFile ( ) ) ) ;
try {
response . type ( "application/zip" ) ;
return deviceManagement . getDeviceManagementService ( ) . getDevice ( deviceIdentifier ) ;
response . header ( "Content-Disposition" , "attachment; filename=\"" + zipFile . getFileName ( ) + "\"" ) ;
} catch ( DeviceManagementException ex ) {
return response . build ( ) ;
log . error ( "Error occurred while retrieving device with Id " + deviceId + "\n" + ex ) ;
} catch ( IllegalArgumentException ex ) {
return null ;
return Response . status ( 400 ) . entity ( ex . getMessage ( ) ) . build ( ) ; //bad request
} finally {
} catch ( DeviceManagementException ex ) {
deviceManagement . endTenantFlow ( ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
}
} catch ( AccessTokenException ex ) {
}
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( DeviceControllerException ex ) {
@Path ( "manager/device/{sketch_type}/download" )
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
@GET
} catch ( IOException ex ) {
@Produces ( MediaType . APPLICATION_JSON )
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
public Response downloadSketch ( @QueryParam ( "owner" ) String owner ,
}
@QueryParam ( "deviceName" ) String deviceName ,
}
@PathParam ( "sketch_type" ) String
sketchType ) {
private ZipArchive createDownloadFile ( String owner , String deviceName , String sketchType )
throws DeviceManagementException , AccessTokenException , DeviceControllerException {
try {
if ( owner = = null ) {
ZipArchive zipFile = createDownloadFile ( owner , deviceName , sketchType ) ;
throw new IllegalArgumentException ( "Error on createDownloadFile() Owner is null!" ) ;
Response . ResponseBuilder response = Response . ok ( FileUtils . readFileToByteArray ( zipFile . getZipFile ( ) ) ) ;
}
response . type ( "application/zip" ) ;
String deviceId = shortUUID ( ) ;
response . header ( "Content-Disposition" , "attachment; filename=\"" + zipFile . getFileName ( ) + "\"" ) ;
KeyGenerationUtil . createApplicationKeys ( "currentsensor" ) ;
return response . build ( ) ;
TokenClient accessTokenClient = new TokenClient ( CurrentSensorConstants . DEVICE_TYPE ) ;
AccessTokenInfo accessTokenInfo = accessTokenClient . getAccessToken ( owner , deviceId ) ;
} catch ( IllegalArgumentException ex ) {
//create token
return Response . status ( 400 ) . entity ( ex . getMessage ( ) ) . build ( ) ; //bad request
String accessToken = accessTokenInfo . getAccess_token ( ) ;
} catch ( DeviceManagementException ex ) {
String refreshToken = accessTokenInfo . getRefresh_token ( ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
//adding registering data
} catch ( AccessTokenException ex ) {
boolean status ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
//Register the device with CDMF
} catch ( DeviceControllerException ex ) {
status = register ( deviceId , deviceName , owner ) ;
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
} catch ( IOException ex ) {
if ( ! status ) {
return Response . status ( 500 ) . entity ( ex . getMessage ( ) ) . build ( ) ;
String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner ;
}
throw new DeviceManagementException ( msg ) ;
}
}
private ZipArchive createDownloadFile ( String owner , String deviceName , String sketchType )
ZipUtil ziputil = new ZipUtil ( ) ;
throws DeviceManagementException , AccessTokenException , DeviceControllerException {
ZipArchive zipFile = ziputil . createZipFile ( owner , SUPER_TENANT , sketchType , deviceId , deviceName , accessToken ,
if ( owner = = null ) {
refreshToken ) ;
throw new IllegalArgumentException ( "Error on createDownloadFile() Owner is null!" ) ;
zipFile . setDeviceId ( deviceId ) ;
}
return zipFile ;
}
//create new device id
String deviceId = shortUUID ( ) ;
private static String shortUUID ( ) {
UUID uuid = UUID . randomUUID ( ) ;
KeyGenerationUtil . createApplicationKeys ( "currentsensor" ) ;
long l = ByteBuffer . wrap ( uuid . toString ( ) . getBytes ( StandardCharsets . UTF_8 ) ) . getLong ( ) ;
return Long . toString ( l , Character . MAX_RADIX ) ;
TokenClient accessTokenClient = new TokenClient ( CurrentSensorConstants . DEVICE_TYPE ) ;
}
AccessTokenInfo accessTokenInfo = accessTokenClient . getAccessToken ( owner , deviceId ) ;
//create token
String accessToken = accessTokenInfo . getAccess_token ( ) ;
String refreshToken = accessTokenInfo . getRefresh_token ( ) ;
//adding registering data
boolean status ;
//Register the device with CDMF
status = register ( deviceId , deviceName , owner ) ;
if ( ! status ) {
String msg = "Error occurred while registering the device with " + "id: " + deviceId + " owner:" + owner ;
throw new DeviceManagementException ( msg ) ;
}
ZipUtil ziputil = new ZipUtil ( ) ;
ZipArchive zipFile = ziputil . createZipFile ( owner , SUPER_TENANT , sketchType , deviceId , deviceName , accessToken ,
refreshToken ) ;
zipFile . setDeviceId ( deviceId ) ;
return zipFile ;
}
private static String shortUUID ( ) {
UUID uuid = UUID . randomUUID ( ) ;
long l = ByteBuffer . wrap ( uuid . toString ( ) . getBytes ( StandardCharsets . UTF_8 ) ) . getLong ( ) ;
return Long . toString ( l , Character . MAX_RADIX ) ;
}
}
}