-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfconsole.js
More file actions
176 lines (147 loc) · 5.7 KB
/
sfconsole.js
File metadata and controls
176 lines (147 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var connect = require('connect');
var sfParser = require('./lib/sfparser.js');
var sgutil = require('./lib/sgutil.js');
var pages = require('./lib/pages.js');
var auth = require('./lib/sfauth.js');
var sys = require('sys');
var cookieName = 'clientRef';
var server = connect.createServer(connect.profiler(), connect.cookieParser(), connect.favicon( '/images/favicon.png'), connect.bodyParser(), HttpHandler);
server.use('/static', connect.static(__dirname + '/static'));
var connections = new Array();
var organizations = new Array();
var io = require('socket.io').listen(server);
io.configure('production',
function(){
io.enable('browser client minification');
io.enable('browser client etag');
io.set('log level', 1);
io.set('transports', ['htmlfile', 'xhr-polling', 'jsonp-polling']);
});
server.listen(process.env.PORT);
//server.listen(8081);
io.sockets.on('connection',
function (socket){
var obj = new Object();
var orgId = getOrgId(socket.handshake.headers.cookie);
if( orgId == null )
return;
obj['orgid'] = orgId;
obj['connection'] = socket;
connections.push(obj);
});
function sendMessage(orgId, message){console.log(connections.length);
for( var i=0; i<connections.length; i++ ){
if(connections[i].orgid == orgId){console.log(connections[i].orgid );
connections[i].connection.emit('logupdate', message);
}
}
}
function HttpHandler(req,res,next){
if(req.url == '/notificationservice.asmx' ){
if (req.method === 'GET') {
var search = url.parse(req.url).search;
if (search && search.toLowerCase() === '?wsdl') {
//res.setHeader("Content-Type", "application/xml");
//res.write(self.wsdl.toXML());
res.write('hello GET');
}
res.end();
} else if (req.method === 'POST') {
var chunks = [];
req.on('data',
function(chunk) {
chunks.push(chunk);
});
req.on('end',
function() {
var xml = chunks.join('');
sfParser.parse(xml,
function(result){
//do something magical with the output - like sending some messages
if(result.soapenvBody.notifications.Notification.length == null){
// send a message
sendMessage(result.soapenvBody.notifications.OrganizationId, result.soapenvBody.notifications.Notification.sObject.sfUpdated__c + ':' + result.soapenvBody.notifications.Notification.sObject.sfMessage__c);
} else{
for(var i=0; i<result.soapenvBody.notifications.Notification.length; i++){
sendMessage(result.soapenvBody.notifications.OrganizationId, result.soapenvBody.notifications.Notification[i].sObject.sfUpdated__c + ':' + result.soapenvBody.notifications.Notification[i].sObject.sfMessage__c);
}
}
//console.log(sys.inspect(result.soapenvBody.notifications.Notification.sObject.sfMessage__c));
});
// send ack
res.end('<?xml version = "1.0" encoding = "utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><notifications xmlns="http://soap.sforce.com/2005/09/outbound"><Ack>True</Ack></notifications></soapenv:Body></soapenv:Envelope>');
});
}
else {
res.end();
}
} else if(req.url == "/"){
if(req.method == 'GET'){
var orgId = getOrgId(req.headers.cookie);
if(orgId == null){
// some response going back to home page
pages.login(function(page){
res.writeHead(200, {'Set-Cookie': cookieName + '=', 'Content-Type': 'text/html'});
res.end(page);
});
} else{
// return with the main page
pages.main(function(page){
res.writeHead(200, {'Set-Cookie': cookieName + '=', 'Content-Type': 'text/html'});
res.end(page);
});
}
}
if(req.method == "POST"){
auth.login(req.body.username, req.body.password + req.body.token, req.body.type,
function(loginRes){
//success
var ret = sfParser.parse(loginRes, function(obj){
// Save the orgid
var organization = new Object();
organization.orgId = obj.soapenvBody.loginResponse.result.userInfo.organizationId;
organization.cookie = sgutil.guidGenerator() + '-' + new Date().getTime();
organizations.push(organization);
// Send them to the next page
pages.main(function(page){
console.log('hello');
res.writeHead(200, {'Set-Cookie': cookieName + '=' + organization.cookie, 'Content-Type': 'text/html'});
res.end(page);
});
});
},
function(loginRes){
//failure
pages.login(function(page){
res.writeHead(200, {'Set-Cookie': cookieName + '=', 'Content-Type': 'text/html'});
res.end(page);
});
});
}
} else{
next();
}
}
function getOrgId(allCookies){
if(allCookies == null)
return null;
var p = allCookies.split(';');
var cookie = '';
for( var i=0; i<p.length; i++ ){
var s = p[i].split('=');
if( s.length > 1 && s[0] == cookieName ){
cookie = s[1];
break;
}
}
if( cookie == '' )
return null;
var orgId = null;
for( var i=0; i<organizations.length; i++){
if( organizations[i].cookie == cookie ){
orgId = organizations[i].orgId;
break;
}
}
return orgId;
}