|
|
@ -53,7 +53,7 @@ import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
|
|
@API( name="connectedcup_mgt", version="1.0.0", context="/connectedcup_mgt")
|
|
|
|
@API( name="connectedcup_mgt", version="1.0.0", context="/connectedcup_mgt")
|
|
|
|
public class ConnectedCupManagerService {
|
|
|
|
public class ConnectedCupManagerService {
|
|
|
|
private static Log log = LogFactory.getLog(ConnectedCupManagerService.class);
|
|
|
|
private static Log log = LogFactory.getLog(ConnectedCupManagerService.class);
|
|
|
|
private static final String SUPER_TENANT = "carbon.super";
|
|
|
|
private static final String SUPER_TENANT = "carbon.super";
|
|
|
|
|
|
|
|
|
|
|
@ -69,175 +69,175 @@ public class ConnectedCupManagerService {
|
|
|
|
public boolean register(@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
|
|
|
public boolean register(@QueryParam("name") String name, @QueryParam("owner") String owner) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
String deviceId = shortUUID();
|
|
|
|
String deviceId = shortUUID();
|
|
|
|
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
|
|
|
if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.CONFLICT.getStatusCode());
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device device = new Device();
|
|
|
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
|
|
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
|
|
|
|
|
|
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
|
|
|
|
|
|
|
device.setName(name);
|
|
|
|
|
|
|
|
device.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
enrolmentInfo.setOwner(owner);
|
|
|
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KeyGenerationUtil.createApplicationKeys(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TokenClient accessTokenClient = new TokenClient(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//create token
|
|
|
|
|
|
|
|
String accessToken = accessTokenInfo.getAccess_token();
|
|
|
|
|
|
|
|
String refreshToken = accessTokenInfo.getRefresh_token();
|
|
|
|
|
|
|
|
List<Device.Property> properties = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device.Property accessTokenProperty = new Device.Property();
|
|
|
|
|
|
|
|
accessTokenProperty.setName("accessToken");
|
|
|
|
|
|
|
|
accessTokenProperty.setValue(accessToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device.Property refreshTokenProperty = new Device.Property();
|
|
|
|
|
|
|
|
refreshTokenProperty.setName("refreshToken");
|
|
|
|
|
|
|
|
refreshTokenProperty.setValue(refreshToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
properties.add(accessTokenProperty);
|
|
|
|
|
|
|
|
properties.add(refreshTokenProperty);
|
|
|
|
|
|
|
|
device.setProperties(properties);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
|
|
|
|
|
|
|
if (added) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return added;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
} catch (AccessTokenException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device device = new Device();
|
|
|
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
|
|
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfEnrolment(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE);
|
|
|
|
|
|
|
|
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD);
|
|
|
|
|
|
|
|
device.setName(name);
|
|
|
|
|
|
|
|
device.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
enrolmentInfo.setOwner(owner);
|
|
|
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
KeyGenerationUtil.createApplicationKeys(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TokenClient accessTokenClient = new TokenClient(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
AccessTokenInfo accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//create token
|
|
|
|
|
|
|
|
String accessToken = accessTokenInfo.getAccess_token();
|
|
|
|
|
|
|
|
String refreshToken = accessTokenInfo.getRefresh_token();
|
|
|
|
|
|
|
|
List<Device.Property> properties = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device.Property accessTokenProperty = new Device.Property();
|
|
|
|
|
|
|
|
accessTokenProperty.setName("accessToken");
|
|
|
|
|
|
|
|
accessTokenProperty.setValue(accessToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Device.Property refreshTokenProperty = new Device.Property();
|
|
|
|
|
|
|
|
refreshTokenProperty.setName("refreshToken");
|
|
|
|
|
|
|
|
refreshTokenProperty.setValue(refreshToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
properties.add(accessTokenProperty);
|
|
|
|
|
|
|
|
properties.add(refreshTokenProperty);
|
|
|
|
|
|
|
|
device.setProperties(properties);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device);
|
|
|
|
|
|
|
|
if (added) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return added;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} catch (AccessTokenException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/device/remove/{device_id}")
|
|
|
|
}
|
|
|
|
@DELETE
|
|
|
|
|
|
|
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
|
|
|
@Path("/device/remove/{device_id}")
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
@DELETE
|
|
|
|
|
|
|
|
public void removeDevice(@PathParam("device_id") String deviceId,
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
try {
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
|
|
|
|
|
|
|
|
deviceIdentifier);
|
|
|
|
try {
|
|
|
|
if (removed) {
|
|
|
|
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
deviceIdentifier);
|
|
|
|
|
|
|
|
if (removed) {
|
|
|
|
} else {
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/device/update/{device_id}")
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
|
|
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
|
|
|
|
|
|
|
@QueryParam("name") String name,
|
|
|
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
@Path("/device/update/{device_id}")
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
@POST
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
public boolean updateDevice(@PathParam("device_id") String deviceId,
|
|
|
|
|
|
|
|
@QueryParam("name") String name,
|
|
|
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
|
|
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// device.setDeviceTypeId(deviceTypeId);
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
device.setName(name);
|
|
|
|
try {
|
|
|
|
device.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
|
|
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
|
|
|
device.setDeviceIdentifier(deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(
|
|
|
|
// device.setDeviceTypeId(deviceTypeId);
|
|
|
|
device);
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
device.setName(name);
|
|
|
|
|
|
|
|
device.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
device);
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
if (updated) {
|
|
|
|
return updated;
|
|
|
|
response.setStatus(Response.Status.OK.getStatusCode());
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
|
|
|
|
|
|
|
@Path("/device/{device_id}")
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
|
|
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
|
|
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return updated;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String shortUUID() {
|
|
|
|
}
|
|
|
|
UUID uuid = UUID.randomUUID();
|
|
|
|
|
|
|
|
long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong();
|
|
|
|
@Path("/device/{device_id}")
|
|
|
|
return Long.toString(l, Character.MAX_RADIX);
|
|
|
|
@GET
|
|
|
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
|
|
|
public Device getDevice(@PathParam("device_id") String deviceId) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT);
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
|
|
|
deviceIdentifier.setType(ConnectedCupConstants.DEVICE_TYPE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Device device = deviceManagement.getDeviceManagementService().getDevice(
|
|
|
|
|
|
|
|
deviceIdentifier);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
deviceManagement.endTenantFlow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|