|
|
|
@ -37,66 +37,38 @@ import org.wso2.carbon.device.mgt.iot.exception.DeviceControllerException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.util.ZipArchive;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.util.ZipUtil;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.JWTClient;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.JWTClientManager;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo;
|
|
|
|
|
import org.wso2.carbon.identity.jwt.client.extension.exception.JWTClientException;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
|
import javax.ws.rs.DELETE;
|
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
|
import javax.ws.rs.PUT;
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
import javax.ws.rs.PathParam;
|
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
@Path("enrollment")
|
|
|
|
|
public class DoorManagerManagerServiceImpl implements DoorManagerManagerService {
|
|
|
|
|
|
|
|
|
|
private static Log log = LogFactory.getLog(DoorManagerManagerServiceImpl.class);
|
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
private static ApiApplicationKey apiApplicationKey;
|
|
|
|
|
private static final String KEY_TYPE = "PRODUCTION";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate UUID
|
|
|
|
|
*
|
|
|
|
|
* @return generated UUID
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean register(String deviceId,String name) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(DoorManagerConstants.DEVICE_TYPE);
|
|
|
|
|
if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
|
|
|
|
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);
|
|
|
|
|
device.setName(name);
|
|
|
|
|
device.setType(DoorManagerConstants.DEVICE_TYPE);
|
|
|
|
|
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
|
|
|
|
|
return added;
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response removeDevice(String deviceId) {
|
|
|
|
|
@Path("devices/{device_id}")
|
|
|
|
|
@DELETE
|
|
|
|
|
public Response removeDevice(@PathParam("device_id") String deviceId) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
@ -113,7 +85,9 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response updateDevice(String deviceId, String name) {
|
|
|
|
|
@Path("devices/{device_id}")
|
|
|
|
|
@PUT
|
|
|
|
|
public Response updateDevice(@PathParam("device_id") String deviceId, @QueryParam("name") String name) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
@ -134,7 +108,11 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response getDevice(String deviceId) {
|
|
|
|
|
@Path("devices/{device_id}")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Response getDevice(@PathParam("device_id") String deviceId) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
@ -146,7 +124,10 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response downloadSketch(String deviceName) {
|
|
|
|
|
@Path("devices/download")
|
|
|
|
|
@GET
|
|
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
|
|
public Response downloadSketch(@QueryParam("deviceName") String deviceName) {
|
|
|
|
|
try {
|
|
|
|
|
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
|
|
|
|
Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile()));
|
|
|
|
@ -170,7 +151,9 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Response generateSketchLink(String deviceName) {
|
|
|
|
|
@Path("devices/generate_link")
|
|
|
|
|
@GET
|
|
|
|
|
public Response generateSketchLink(@QueryParam("deviceName") String deviceName){
|
|
|
|
|
try {
|
|
|
|
|
ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName);
|
|
|
|
|
Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId());
|
|
|
|
@ -207,14 +190,14 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(
|
|
|
|
|
DoorManagerConstants.DEVICE_TYPE, tags, KEY_TYPE, applicationUsername, true);
|
|
|
|
|
}
|
|
|
|
|
JWTClient jwtClient = JWTClientManager.getInstance().getJWTClient();
|
|
|
|
|
JWTClient jwtClient = APIUtil.getJWTClientManagerService().getJWTClient();
|
|
|
|
|
String scopes = "device_type_" + DoorManagerConstants.DEVICE_TYPE + " device_" + deviceId;
|
|
|
|
|
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(apiApplicationKey.getConsumerKey(),
|
|
|
|
|
apiApplicationKey.getConsumerSecret(), owner,
|
|
|
|
|
scopes);
|
|
|
|
|
//create token
|
|
|
|
|
String accessToken = accessTokenInfo.getAccess_token();
|
|
|
|
|
String refreshToken = accessTokenInfo.getRefresh_token();
|
|
|
|
|
String accessToken = accessTokenInfo.getAccessToken();
|
|
|
|
|
String refreshToken = accessTokenInfo.getRefreshToken();
|
|
|
|
|
//Register the device with CDMF
|
|
|
|
|
boolean status = register(deviceId, deviceName);
|
|
|
|
|
if (!status) {
|
|
|
|
@ -229,4 +212,40 @@ public class DoorManagerManagerServiceImpl implements DoorManagerManagerService{
|
|
|
|
|
return zipFile;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate UUID
|
|
|
|
|
*
|
|
|
|
|
* @return generated UUID
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean register(String deviceId, String name) {
|
|
|
|
|
try {
|
|
|
|
|
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
deviceIdentifier.setId(deviceId);
|
|
|
|
|
deviceIdentifier.setType(DoorManagerConstants.DEVICE_TYPE);
|
|
|
|
|
if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) {
|
|
|
|
|
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);
|
|
|
|
|
device.setName(name);
|
|
|
|
|
device.setType(DoorManagerConstants.DEVICE_TYPE);
|
|
|
|
|
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
|
|
|
|
|
return added;
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|