Example #1
0
exports.registerPlace = function (req, res) {
    logger.info("Register place...");

    var registerReq = req.body;
    // We create a new folder for the created group.
    // Since this is a sample code we don't handle the case of 2 groups with the same name.
    var containerPath = jive.service.options.rootFolder + "/" + registerReq.container.name;
    var containerGuid = jive.util.guid();

    var tenantId = jive.util.getJiveAuthorizationHeaderValue(req.headers["authorization"],"tenant_id");

    jive.community.requestAccessToken(tenantId, registerReq.oauthCode)
        .then(function (oauthResponse) {
            registerReq.oauth = oauthResponse.entity;
            return jive.util.fsexists(containerPath);
        })
        .then(function (exists) {
            if (!exists) {
                return jive.util.fsmkdir(containerPath);
            } else {
                return q.fcall(function () {
                });
            }
        })
        .then(function () {
            logger.info("Directory for group " + req.body.container.name + " was created in: " + containerPath);
            req.body.containerPath = containerPath;
            return jive.context.persistence.save("places", containerGuid, registerReq);
        })
        .then(function () {
            var responseBody =
            {
                'externalId': containerGuid,

                //These resources are used by the jive server side to upload data later on to this service.
                //By using to container guid as part of the URL we can later identify where to save the uploaded file.
                'resources': [
                    {
                        'name': 'self',
                        'url': '/fsstorage/place?container=' + containerGuid,
                        'verbs': ['GET', 'PUT', 'DELETE']
                    },
                    {
                        'name': 'uploadFile',
                        'url': '/fsstorage/upload?container=' + containerGuid,
                        'verbs': ['POST']
                    }
                ]
            };

            res.status(200).send(responseBody);
        })
        .catch(function (err) {
            logger.error(err);
            res.writeHead(500);
            res.end();
        });
};
    function getClient() {
        var deferred = q.defer();

//        if ( new Date().getTime() - t > 10000 && new Date().getTime() % 4 == 0 ) {
//            return q.reject(new Error("Failed to get a client"));
//        }

        var clientID = jive.util.guid();
        requestClient(clientID, deferred);

        return deferred.promise;
    }
Example #3
0
function processRegistration(context) {
    var db = jive.service.persistence();
    var licenseKey = jive.util.guid();
    var tenantID = context['tenantId'];

    // using tenantID as licenseKey table primary key
    db.save( 'licenseKey', tenantID, {
        'licenseKey' : licenseKey
    }).then( function() {
            jive.logger.info('Persisted license key', licenseKey, 'for tenantID', tenantID, '(', context['jiveUrl'], ')');
        });
}
/**
 * Helper function to push file information to Jive.
 */
function pushFileToJive(filePath, filename, place) {
    logger.info("Pushing file " + filePath);
    var fileGuid = jive.util.guid();
    var fileObj = {
        fileDirectoryPath: place.containerPath + "/" + filename
    };
    var versionObj = {
        fileName: filename,
        contentType: 'TBD'
    };

    // Create the ESF REST API url:
    // http://{jive url}/api/core/v3/exstorage/containers/{containerID}/files
    var pushFileUrl = place.jiveUrl + place.containerApiSuffix + "files";

    var tempFile;
    jive.util.fsGetSize(filePath)
        .then(function (size) {
            tempFile = {
                name: filename,
                path: filePath,
                size: size
            };
            return jive.util.fsexists(fileObj.fileDirectoryPath);
        })
        .then(function (exists) {
            if (!exists) {
                return jive.util.fsmkdir(fileObj.fileDirectoryPath);
            } else {
                return q.fcall(function () { });
            }
        })
        .then(function () {
            return handleFileVersion(fileGuid, fileObj, versionObj, tempFile);
        })
        .then(function (reqPayload) {
            var headers = {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + place.oauth.access_token
            };

            // Perform a POST request to upload the file
            return jive.util.buildRequest(pushFileUrl, "POST", reqPayload, headers);
        })
        .then(function () {
            logger.info("done pushing file.")
        })
        .catch(function (err) {
            logger.error(err);
        });
};
Example #5
0
exports.uploadFile = function (req, res) {
    logger.info("Upload file...");

    var containerGuid = req.query['container'];
    var fileGuid = jive.util.guid();
    var fileObj = JSON.parse(req.body.metadata);
    var fileDirectory;

    jive.context.persistence.findByID("places", containerGuid)
        .then(function (container) {
            if (container) {
                fileDirectory = container.containerPath + "/" + req.files[0].originalname;
                return q.fcall(function () {
                });
            }
            else {
                throw new Error("The container was not found");
            }
        })
        .then(function () {
            return jive.util.fsexists(fileDirectory);
        })
        .then(function (exists) {
            if (!exists) {
                return jive.util.fsmkdir(fileDirectory);
            } else {
                return q.fcall(function () {
                });
            }
        })
        .then(function () {
            logger.info("Directory for file " + req.files[0].originalname + " was created in: " + fileDirectory);
            fileObj.fileDirectoryPath = fileDirectory;
            return handleFileVersion(fileGuid, fileObj, fileObj.version, req.files[0]);
        })
        .then(function (responseData) {
            res.status(200).send(responseData);
        })
        .catch(function (err) {
            logger.error(err);
            res.writeHead(500);
            res.end();
        });
};
Example #6
0
var chai = require('chai')
    , expect = chai.expect
    , should = chai.should();
var chaiAsPromised = require("chai-as-promised");
var Q = require("q");
var sinon = require('sinon');

chai.use(chaiAsPromised);
var jive = require("jive-sdk");
var JiveFacadeLoader = require("../../common/JiveApiFacade");
var JiveBasicLoader = require("../../common/JiveBasicAuth");
var JiveOAuthLoader = require("../../common/JiveOauth");
var ContentBuilder = require("../../common/JiveContentBuilder");

var community = {jiveUrl: "http://localhost:8080"};
var tempOAuthToken = jive.util.guid();
var tempOAuthRefreshToken = jive.util.guid();
var basic = new JiveBasicLoader("admin", "admin");

function createContent(jiveFacade, type) {
    return jiveFacade.create({type: type,
        content: {
            type: "text/html",
            text: "<h1>WooHOO</h1>"},
        subject: "YAHOO"
    });
}
describe("JiveApiFacade", function () {
    var jiveFacade = new JiveFacadeLoader(community, basic);

    it("should not be null", function () {
/**
 * Helper function to handle file versioning.
 */
function handleFileVersion(fileGuid, fileObj, versionObj, tempFile) {
    var versionGuid = jive.util.guid();
    if (fileObj.versionsList) {
        fileObj.versionsList.push(versionGuid);
    }
    else {
        fileObj.versionsList = [versionGuid];
    }
    var fileExt = tempFile.name.split('.').pop();
    var fileVersionPath = fileObj.fileDirectoryPath + "/version" + fileObj.versionsList.length + "." + fileExt;
    return jive.util.fsrename(tempFile.path, fileVersionPath)
        .then(function () {
            logger.info("File version binary was saved to: " + fileVersionPath);
            return jive.context.persistence.save("files", fileGuid, fileObj);
        })
        .then(function () {
            versionObj.fileVersionPath = fileVersionPath;
            return jive.context.persistence.save("fileVersions", versionGuid, versionObj);
        })
        .then(function () {
            var version = {
                'contentType': versionObj.contentType,
                'fileName': versionObj.fileName,
                'externalId': versionGuid,
                'size': tempFile.size,
                'resources': [
                    {
                        'name': 'self',
                        'url': '/fsstorage/version?version=' + versionGuid,
                        'verbs': ['GET', 'PUT', 'DELETE'] // PUT method is not implemented in this example
                    },
                    {
                        'name': 'downloadVersion',
                        'url': '/fsstorage/download?version=' + versionGuid,
                        'verbs': ['GET']
                    },
                    {
                        'name': 'fileParent',
                        'url': '/fsstorage/file?file=' + fileGuid,
                        'verbs': ['GET']
                    }
                ]};

            var responseData = {
                'externalId': fileGuid,
                'version': version,
                'resources': [
                    {
                        'name': 'self',
                        'url': '/fsstorage/file?file=' + fileGuid,
                        'verbs': ['GET', 'PUT', 'DELETE']
                    },
                    {
                        'name': 'uploadVersion',
                        'url': '/fsstorage/uploadVersion?file=' + fileGuid,
                        'verbs': ['POST']
                    }
                ]
            };

            return q.fcall(function () {
                return responseData;
            });
        });
};
 'route': function(req,res) {
     res.render('hello.html', { routeMethod : 'explicit', name: jive.util.guid() } );
 }
Example #9
0
exports.route = function(req, res){
    var conf = jive.service.options;
    res.render('hello.html', { name: jive.util.guid() });
};
Example #10
0
exports.onBootstrap = function(app) {
    var period = 1800000; // Default to 30 min
    var task = new jive.tasks.build(poll, period, jive.util.guid());
    jive.tasks.schedule(task, scheduler);
};