/** * Copyright 2013 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ module.exports = function(RED) { "use strict"; var util = require("util"); var vm = require("vm"); //var dgxml=require("/home/users/schinthakayala/nodered/sheshi/dgxml/dgxml2"); var _=require('lodash'); var fs=require('fs'); var path = require('path'); var appDir = path.dirname(require.main.filename); var userDir = appDir + "/" + RED.settings.userDir; var dbHost = RED.settings.dbHost; var request = require('request'); var sharedDir = appDir + "/" + RED.settings.sharedDir; var xmlDir = appDir + "/" + RED.settings.xmlPath; //console.log("appDir:" + appDir); //var dgeraw=fs.readFileSync(appDir + "/dge.json").toString(); //var dgejson=JSON.parse(dgeraw); //var uploadUrl=dgejson.slaHost + dgejson.uploadUrl; //var slaUrl=dgejson.slaHost + dgejson.slaUrl; //var uploadUrl=RED.settings.slaHost + RED.settings.uploadUrl; //var slaUrl=RED.settings.slaHost + RED.settings.slaUrl; var uploadUrl=""; var slaUrl=""; //console.log("Upload url: " + uploadUrl); function dgstart(n) { RED.nodes.createNode(this,n); this.name = n.name; this.topic = n.topic; } function writeHtmlToFile(fileName,str){ var localfile = appDir + "/" + RED.settings.htmlPath + fileName; try{ fs.writeFileSync(localfile,str); }catch(e){ console.log("Error:" + e); } } function writeXmlToFile(fileName,str){ var localfile = appDir + "/" + RED.settings.xmlPath + fileName; try{ fs.writeFileSync(localfile,str); }catch(e){ console.log("Error:" + e); } } function sendXml(fileName,res) { var needle, localfile, data; needle = require('needle') localfile = appDir + "/" + RED.settings.xmlPath + fileName; console.log("localfile:" + localfile); data={ uploadedfile: { file: localfile, content_type: 'text/xml' } } needle.post(uploadUrl, data, { multipart: true }, function(err, resp, body) { //console.log(body) if(resp != undefined && resp != null){ console.log("resp Code for sendXml:" + resp.statusCode); } fs.unlink(localfile, function (error) { if (error) { console.log("Error deleting file "+localfile); }else{ //console.log("deleted file:" + localfile); } }); if(err){ console.log("Error posting to slaUrl:" + slaUrl); console.log("Error:" +err); res.json({"error":err}); }else{ //console.dir(resp); //console.log("slaUrl:" + slaUrl); res.json({"url":slaUrl}); } }); } function oldsendXml(fileName) { console.log("In sendXML for file: " + fileName); var fileStream, formdata, localfile; localfile = appDir + "/" + RED.settings.xmlPath + fileName; formdata = { MAX_FILE_SIZE: "100000", uploadedfile: { options: { contentType: 'audio/mpeg' } } }; console.log("Attempting to upload file: " + localfile); console.log("Sending to: " + uploadUrl); formdata.uploadedfile.value = fs.createReadStream(localfile); fileStream = formdata.uploadedfile.value; //console.log("Formdata:"); //console.dir(formdata); request.post({ url: uploadUrl, proxy: false, formData: formdata }, function(err, resp, body) { fileStream.close(); console.log("err: " + err); return console.log("body: " + body); }); }; RED.nodes.registerType("dgstart",dgstart); /* RED.httpAdmin.post("/uploadxml", function(req,res) { console.dir(req); console.log("USER:" + req.user); console.log("Got request to upload xml to SDN-C."); console.log("Requested filename to upload: " + req.params.fileName); console.log("Requested xml to upload: " + req.params.xmlStr); writeToFile( req.params.fileName,req.params.xmlStr); sendXml(req.params.fileName,res); // res.send("Attempt complete."); // res.redirect(slaUrl); }); */ RED.httpAdmin.post("/OldUploadxml", function(req,res) { //console.dir(req); //console.log("USER:" + req.user); var qs = require('querystring'); var body = ''; req.on('data', function (data) { body += data; // Too much POST data, kill the connection! /*if (body.length > 1e6) request.connection.destroy(); */ }); req.on('end', function () { //console.log("BODY:" + body); var d = new Date().getTime(); var user = req.user; var fileName= user + "_" + d +".xml"; var post = qs.parse(body); //console.log(JSON.stringify(post)); // use post['blah'], etc. var localfile = appDir + "/" + RED.settings.xmlPath + fileName; //console.log("localfile:" + localfile); var xmlStr = post['flowXml']; writeXmlToFile(fileName,xmlStr); sendXml(fileName,res); }); }); RED.httpAdmin.post("/uploadxml", function(req,res) { //console.dir(req); //console.log("USER:" + req.user); var qs = require('querystring'); var body = ''; req.on('data', function (data) { body += data; // Too much POST data, kill the connection! /*if (body.length > 1e6) request.connection.destroy(); */ }); req.on('end', function () { //console.log("BODY:" + body); var d = new Date().getTime(); var user = req.user; var fileName= user + "_" + d +".xml"; var post = qs.parse(body); //console.log(JSON.stringify(post)); // use post['blah'], etc. var localfile = appDir + "/" + RED.settings.xmlPath + fileName; //console.log("localfile:" + localfile); var xmlStr = post['flowXml']; var moduleName = post['module']; var rpc = post['rpc']; writeXmlToFile(fileName,xmlStr); uploadDG(localfile,moduleName,rpc,res); }); }); function uploadDG(filePath,moduleName,rpc,res){ console.log("called uploadDG..."); var exec = require('child_process').exec; var commandToExec = appDir + "/svclogic/svclogic.sh load " + filePath + " " + userDir + "/conf/svclogic.properties"; console.log("commandToExec:" + commandToExec); var child = exec(commandToExec ,function (error,stdout,stderr){ //console.log(error); console.log("stdout:" + stdout); console.log("stderr:" + stderr); if(error){ console.log("Error occured:" + error); if(stderr){ //console.log("stderr:" + stderr); res.send(500,{'error':error,'stderr':stderr}); }else{ res.send(500,{'error':error}); } //console.log("stdout :" + stdout); }else{ if(stdout ){ //console.log("output:" + stdout); if(stdout.indexOf('Compiler error') != -1){ //console.log("compileError occured."); var resp = { 'stdout':stdout, 'stderr':"COMPILE_ERROR", 'url':dbHost, 'module':moduleName, 'rpc':rpc } res.send(500,resp); }else{ res.send(200,{'stdout':stdout,'stderr':stderr,"url":dbHost,"module" : moduleName,"rpc" : rpc}); } } if(stderr && !stdout){ //console.log("stderr:" + stderr); if(stderr.indexOf("Saving SvcLogicGraph") != -1){ res.send(200,{'error':error,'stdout' :'','stderr':stderr,"url":dbHost,"module" : moduleName,"rpc" : rpc}); }else{ res.send(500,{'error':error,'stdout' :'','stderr':stderr}); } } } }); } RED.httpAdmin.get("/displayXml", function(req,res) { var _module = req.query._module; var rpc = req.query.rpc; var version = req.query.version; var mode = req.query.mode; var d = new Date().getTime(); displayXml(_module,rpc,version,mode,res); }); function displayXml(_module,rpc,version,mode,res){ var exec = require('child_process').exec; var msg = { '_module' : _module, 'rpc' : rpc, 'version' : version, 'mode' : mode } var commandToExec = appDir + "/svclogic/svclogic.sh get-source " + _module + " " + rpc + " " + mode + " " + version + " " + userDir + "/conf/svclogic.properties"; console.log("commandToExec:" + commandToExec); var child = exec(commandToExec ,{'maxBuffer':16*1024*1024},function (error,stdout,stderr){ if(error){ console.log("Error occured:" + error); if(stderr){ //console.log("stderr:" + stderr); res.send(500,{'error':error,'stderr':stderr,'msg':msg}); }else{ res.send(500,{'error':error,'msg':msg}); } }else{ if(stderr){ console.log("stderr:" + stderr); } if(stdout){ res.send({'xmldata' : "
" + stdout.replace(/\n/g,'"; //res.json({'stdout': stdout ,'stderr':stderr,"msg":msg}); //res.set('Content-Type', 'text/xml'); //res.set('Content-Type', 'application/octet-stream'); //res.end("
') + "
" + stdout + "
" );
//var newOutput ="" + stdout + "";
//res.send(new Buffer( "" + newOutput + "
" ) );
//res.send(newOutput);
/*
var xslStr = '