|
|
@ -34,55 +34,55 @@ var result;
|
|
|
|
|
|
|
|
|
|
|
|
if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
downloadId = uriMatcher.elements().downloadId;
|
|
|
|
downloadId = uriMatcher.elements().downloadId;
|
|
|
|
//Just download the already created zip archive
|
|
|
|
//Just download the already created zip archive
|
|
|
|
var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils;
|
|
|
|
var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils;
|
|
|
|
var sketchFolder = "repository/resources/sketches";
|
|
|
|
var sketchFolder = "repository/resources/sketches";
|
|
|
|
var archivesPath = "file://"+CarbonUtils.getCarbonHome() + "/" + sketchFolder + "/archives/" + downloadId + ".zip";
|
|
|
|
var archivesPath = "file://" + CarbonUtils.getCarbonHome() + "/" + sketchFolder + "/archives/" + downloadId + ".zip";
|
|
|
|
var zipFile = new File(archivesPath);
|
|
|
|
var zipFile = new File(archivesPath);
|
|
|
|
response.addHeader('Content-type', "application/zip, application/octet-stream");
|
|
|
|
response.addHeader('Content-type', "application/zip, application/octet-stream");
|
|
|
|
response.addHeader('Cache-Control', 'public,max-age=12960000');
|
|
|
|
response.addHeader('Cache-Control', 'public,max-age=12960000');
|
|
|
|
response.addHeader("Content-Disposition", "attachment; filename=\"" + downloadId + ".zip\"");
|
|
|
|
response.addHeader("Content-Disposition", "attachment; filename=\"" + downloadId + ".zip\"");
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
zipFile.open('r');
|
|
|
|
zipFile.open('r');
|
|
|
|
var stream = zipFile.getStream();
|
|
|
|
var stream = zipFile.getStream();
|
|
|
|
print(stream);
|
|
|
|
print(stream);
|
|
|
|
}catch(err){
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
|
|
|
|
}finally{
|
|
|
|
} finally {
|
|
|
|
if(zipFile!=null) {
|
|
|
|
if (zipFile != null) {
|
|
|
|
zipFile.close();
|
|
|
|
zipFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} else if (uriMatcher.match("/{context}/api/device/sketch/download")) {
|
|
|
|
} else if (uriMatcher.match("/{context}/api/device/sketch/download")) {
|
|
|
|
//Create a new zip archive and register user calling endpoint
|
|
|
|
//Create a new zip archive and register user calling endpoint
|
|
|
|
|
|
|
|
|
|
|
|
/* This should match with $CARBON_HOME/repository/resources/sketches/{sketchType} */
|
|
|
|
/* This should match with $CARBON_HOME/repository/resources/sketches/{sketchType} */
|
|
|
|
sketchType = request.getParameter("sketchType");
|
|
|
|
sketchType = request.getParameter("sketchType");
|
|
|
|
/* This should be registered device type of the CDMF(Connected Device Management Framework) */
|
|
|
|
/* This should be registered device type of the CDMF(Connected Device Management Framework) */
|
|
|
|
deviceType = request.getParameter("deviceType");
|
|
|
|
deviceType = request.getParameter("deviceType");
|
|
|
|
|
|
|
|
|
|
|
|
if (!sketchType) {
|
|
|
|
if (!sketchType) {
|
|
|
|
log.error("Sketch Type is empty!");
|
|
|
|
log.error("Sketch Type is empty!");
|
|
|
|
// http status code 400 refers to - Bad request.
|
|
|
|
// http status code 400 refers to - Bad request.
|
|
|
|
result = 400;
|
|
|
|
result = 400;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
var user = session.get(constants.USER_SESSION_KEY);
|
|
|
|
var user = session.get(constants.USER_SESSION_KEY);
|
|
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
response.sendRedirect(dcProps.appContext + "login?#login-required");
|
|
|
|
response.sendRedirect(dcProps.appContext + "login?#login-required");
|
|
|
|
exit();
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//URL: https://localhost:9443/{deviceType}/download?owner={username}
|
|
|
|
//URL: https://localhost:9443/{deviceType}/download?owner={username}
|
|
|
|
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
|
|
|
|
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
|
|
|
|
|
|
|
|
|
|
|
|
sketchDownloadEndPoint = deviceManagerService + "/device/" + sketchType + "/download";
|
|
|
|
sketchDownloadEndPoint = deviceManagerService + "/device/" + sketchType + "/download";
|
|
|
|
response.sendRedirect(sketchDownloadEndPoint + "?owner=" + user.username);
|
|
|
|
response.sendRedirect(sketchDownloadEndPoint + "?owner=" + user.username);
|
|
|
|
exit();//stop execution
|
|
|
|
exit();//stop execution
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (uriMatcher.match("/{context}/api/device/sketch/generate_link")) {
|
|
|
|
} else if (uriMatcher.match("/{context}/api/device/sketch/generate_link")) {
|
|
|
|
|
|
|
|
|
|
|
|
var contents = request.getContent();
|
|
|
|
var contents = request.getContent();
|
|
|
@ -99,7 +99,7 @@ if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
if (!user) {
|
|
|
|
result = 403;
|
|
|
|
result = 403;
|
|
|
|
}else {
|
|
|
|
} else {
|
|
|
|
//URL: https://localhost:9443/{deviceType}/download?owner={username}
|
|
|
|
//URL: https://localhost:9443/{deviceType}/download?owner={username}
|
|
|
|
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
|
|
|
|
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
|
|
|
|
|
|
|
|
|
|
|
@ -120,15 +120,37 @@ if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
//URL: https://localhost:9443/devicecloud/manager/devices/username/{username}
|
|
|
|
//URL: https://localhost:9443/devicecloud/manager/devices/username/{username}
|
|
|
|
deviceCloudService = carbonHttpsServletTransport + "/devicecloud/device_manager";
|
|
|
|
deviceCloudService = carbonHttpsServletTransport + "/devicecloud/device_manager";
|
|
|
|
listAllDevicesEndPoint = deviceCloudService + "/devices/username/" + user.username;
|
|
|
|
listAllDevicesEndPoint = deviceCloudService + "/devices/username/" + user.username;
|
|
|
|
|
|
|
|
result = get(listAllDevicesEndPoint, {}, "json");
|
|
|
|
var data = {};
|
|
|
|
var ownDevices = result.data;
|
|
|
|
//XMLHTTPRequest's GET
|
|
|
|
|
|
|
|
try {
|
|
|
|
var allDevices = [];
|
|
|
|
result = get(listAllDevicesEndPoint, data, "json");
|
|
|
|
deviceCloudService = carbonHttpsServletTransport + "/devicecloud/group_manager";
|
|
|
|
}catch(err){
|
|
|
|
var endPoint = deviceCloudService + "/group/all";
|
|
|
|
log.error("Error occured while retrieveing all devices with username: "+user.username);
|
|
|
|
var data = {"username": user.username};
|
|
|
|
result=[];
|
|
|
|
var groups = get(endPoint, data, "json").data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (var g in groups) {
|
|
|
|
|
|
|
|
endPoint = deviceCloudService + "/group/id/" + groups[g].id + "/device/all";
|
|
|
|
|
|
|
|
data = {"username": user.username};
|
|
|
|
|
|
|
|
var deviceInGroup = get(endPoint, data, "json").data;
|
|
|
|
|
|
|
|
groups[g].devices = deviceInGroup;
|
|
|
|
|
|
|
|
allDevices.push(groups[g]);
|
|
|
|
|
|
|
|
for (var od in ownDevices) {
|
|
|
|
|
|
|
|
for (var gd in deviceInGroup) {
|
|
|
|
|
|
|
|
if (deviceInGroup[gd].deviceIdentifier == ownDevices[od].deviceIdentifier) {
|
|
|
|
|
|
|
|
ownDevices[od].deviceIdentifier = -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var unsharedDevices = [];
|
|
|
|
|
|
|
|
for (var od in ownDevices) {
|
|
|
|
|
|
|
|
if (ownDevices[od].deviceIdentifier != -1){
|
|
|
|
|
|
|
|
unsharedDevices.push(ownDevices[od]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
allDevices.push({id: 0, devices: unsharedDevices});
|
|
|
|
|
|
|
|
result.data = allDevices;
|
|
|
|
|
|
|
|
|
|
|
|
} else if (uriMatcher.match("/{context}/api/devices/types")) {
|
|
|
|
} else if (uriMatcher.match("/{context}/api/devices/types")) {
|
|
|
|
|
|
|
|
|
|
|
@ -142,7 +164,7 @@ if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var elements = uriMatcher.elements();
|
|
|
|
var elements = uriMatcher.elements();
|
|
|
|
var deviceId=elements.deviceId;
|
|
|
|
var deviceId = elements.deviceId;
|
|
|
|
var deviceType = elements.deviceType;
|
|
|
|
var deviceType = elements.deviceType;
|
|
|
|
result = deviceModule.removeDevice(deviceType, deviceId);
|
|
|
|
result = deviceModule.removeDevice(deviceType, deviceId);
|
|
|
|
|
|
|
|
|
|
|
@ -154,14 +176,14 @@ if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var elements = uriMatcher.elements();
|
|
|
|
var elements = uriMatcher.elements();
|
|
|
|
var deviceId=elements.deviceId;
|
|
|
|
var deviceId = elements.deviceId;
|
|
|
|
var deviceType = elements.deviceType;
|
|
|
|
var deviceType = elements.deviceType;
|
|
|
|
var cont = request.getContent();
|
|
|
|
var cont = request.getContent();
|
|
|
|
|
|
|
|
|
|
|
|
if(!cont.device){
|
|
|
|
if (!cont.device) {
|
|
|
|
// http status code 400 refers to - Bad request.
|
|
|
|
// http status code 400 refers to - Bad request.
|
|
|
|
result = 400;
|
|
|
|
result = 400;
|
|
|
|
}else {
|
|
|
|
} else {
|
|
|
|
result = deviceModule.updateDevice(deviceType, deviceId, cont.device);
|
|
|
|
result = deviceModule.updateDevice(deviceType, deviceId, cont.device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|