# IGNITE-1121 Allow to execute remote REST queries with callback.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/dd27f3c0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/dd27f3c0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/dd27f3c0 Branch: refs/heads/ignite-1121 Commit: dd27f3c00784ba608bd4d48a48a6517b34dd0683 Parents: 3e9c9d8 Author: sevdokimov <sergey.evdoki...@jetbrains.com> Authored: Wed Jul 15 22:16:32 2015 +0300 Committer: sevdokimov <sergey.evdoki...@jetbrains.com> Committed: Wed Jul 15 22:16:32 2015 +0300 ---------------------------------------------------------------------- .../java/org/apache/ignite/agent/Agent.java | 8 +- .../org/apache/ignite/agent/AgentSocket.java | 22 ++- .../ignite/agent/messages/ExecuteRest.java | 57 ------- .../ignite/agent/messages/RestRequest.java | 74 ++++++++ .../ignite/agent/messages/RestResult.java | 34 ++++ .../nodejs/agents/agentManager.js | 170 +++++++++++++++++++ modules/web-control-center/nodejs/app.js | 4 +- .../web-control-center/nodejs/bridge/bridge.js | 118 ------------- .../web-control-center/nodejs/routes/test.js | 42 +++++ 9 files changed, 339 insertions(+), 190 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Agent.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Agent.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Agent.java index f232e2f..c420fa9 100644 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Agent.java +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/Agent.java @@ -66,10 +66,6 @@ public class Agent { CloseableHttpResponse resp = httpclient.execute(get); - RestResult res = new RestResult(); - - res.setCode(resp.getStatusLine().getStatusCode()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); resp.getEntity().writeTo(out); @@ -84,6 +80,10 @@ public class Agent { charset = Charsets.toCharset(encoding); } + RestResult res = new RestResult(); + + res.setCode(resp.getStatusLine().getStatusCode()); + res.setExecuted(true); res.setMessage(new String(out.toByteArray(), charset)); return res; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java index 789fd12..9e6e14a 100644 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java @@ -96,27 +96,31 @@ public class AgentSocket { if (m instanceof AuthResult) { if (((AuthResult)m).isSuccess()) System.out.println("Authentication success"); - else + else { System.out.println("Authentication failed: " + ((AuthResult)m).getMessage()); - ses.close(); + ses.close(); + } } - else if (m instanceof ExecuteRest) { - ExecuteRest execRest = (ExecuteRest)m; + else if (m instanceof RestRequest) { + RestRequest restReq = (RestRequest)m; - RestResult res; + RestResult restRes; try { - res = agent.executeRest(execRest.getUrl()); + restRes = agent.executeRest(restReq.getUrl()); } catch (IOException e) { - res = new RestResult(); + restRes = new RestResult(); - res.setCode(500); + restRes.setCode(500); + restRes.setMessage(e.getMessage()); } + restRes.setRequestId(((RestRequest)m).getId()); + try { - ses.getRemote().sendString(MessageFactory.toString(res)); + ses.getRemote().sendString(MessageFactory.toString(restRes)); } catch (IOException e) { e.printStackTrace(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/ExecuteRest.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/ExecuteRest.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/ExecuteRest.java deleted file mode 100644 index 217852f..0000000 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/ExecuteRest.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.apache.ignite.agent.messages;/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -import java.util.*; - -/** - * - */ -public class ExecuteRest extends AbstractMessage { - /** */ - private String url; - - /** */ - private Map<String, String> params; - - /** - * - */ - public String getUrl() { - return url; - } - - /** - * @param url Url. - */ - public void setUrl(String url) { - this.url = url; - } - - /** - * - */ - public Map<String, String> getParams() { - return params; - } - - /** - * @param params Params. - */ - public void setParams(Map<String, String> params) { - this.params = params; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestRequest.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestRequest.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestRequest.java new file mode 100644 index 0000000..9d5181d --- /dev/null +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestRequest.java @@ -0,0 +1,74 @@ +package org.apache.ignite.agent.messages;/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +import java.util.*; + +/** + * + */ +public class RestRequest extends AbstractMessage { + /** */ + private int id; + + /** */ + private String url; + + /** */ + private Map<String, String> params; + + /** + * + */ + public int getId() { + return id; + } + + /** + * @param id Id. + */ + public void setId(int id) { + this.id = id; + } + + /** + * + */ + public String getUrl() { + return url; + } + + /** + * @param url Url. + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * + */ + public Map<String, String> getParams() { + return params; + } + + /** + * @param params Params. + */ + public void setParams(Map<String, String> params) { + this.params = params; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java ---------------------------------------------------------------------- diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java index b23a203..d337384 100644 --- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java +++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java @@ -20,6 +20,12 @@ package org.apache.ignite.agent.messages;/* */ public class RestResult extends AbstractMessage { /** */ + private int requestId; + + /** */ + private boolean executed; + + /** */ private int code; /** */ @@ -28,6 +34,20 @@ public class RestResult extends AbstractMessage { /** * */ + public int getRequestId() { + return requestId; + } + + /** + * @param reqId Request id. + */ + public void setRequestId(int reqId) { + this.requestId = reqId; + } + + /** + * + */ public int getCode() { return code; } @@ -52,4 +72,18 @@ public class RestResult extends AbstractMessage { public void setMessage(String msg) { this.message = msg; } + + /** + * + */ + public boolean isExecuted() { + return executed; + } + + /** + * @param executed Executed. + */ + public void setExecuted(boolean executed) { + this.executed = executed; + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/web-control-center/nodejs/agents/agentManager.js ---------------------------------------------------------------------- diff --git a/modules/web-control-center/nodejs/agents/agentManager.js b/modules/web-control-center/nodejs/agents/agentManager.js new file mode 100644 index 0000000..aa2506a --- /dev/null +++ b/modules/web-control-center/nodejs/agents/agentManager.js @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +var WebSocketServer = require('ws').Server; + +var config = require('../helpers/configuration-loader.js'); + +var https = require('https'); + +var db = require('../db'); + +var fs = require('fs'); + +var srv; + +var clients = {}; + +function Client(ws) { + var self = this; + + this.ws = ws; + + ws.on('close', function() { + if (self.userId) { + var connections = clients[self.userId]; + + if (connections) { + removeFromArray(connections, self); + } + } + }); + + ws.on('message', function (msg) { + var m = JSON.parse(msg); + + switch (m.type) { + case 'AuthMessage': + var account = db.Account.findByUsername(m.login, function(err, account) { + if (err) { + ws.send("{type: 'AuthResult', success: false}"); + } + else { + account.authenticate(m.password, function(err, user, res) { + if (!user) { + ws.send(JSON.stringify({type: 'AuthResult', success: false, message: res.message})); + } + else { + ws.send("{type: 'AuthResult', success: true}"); + + self.userId = account._id; + + var existingConnections = clients[account._id]; + + if (!existingConnections) { + existingConnections = []; + + clients[account._id] = existingConnections; + } + + existingConnections.push(self); + } + }); + } + }); + + break; + + case 'RestResult': + var cb = self.cbMap[m.requestId]; + + if (!cb) + break; + + delete self.cbMap[m.requestId]; + + if (!m.executed) { + cb("Failed to execute RESQ query: " + m.message) + } + else { + cb(null, m.code, m.message) + } + + break; + + default: + ws.close() + } + }); + + this.sendMessage = function(msg, cb) { + if (typeof(msg) == 'object') { + msg = JSON.stringify(msg); + } + + ws.send(msg, cb); + }; + + this.restCounter = 0; + + this.cbMap = {}; + + this.restQuery = function(url, cb) { + var reqId = this.restCounter++; + + this.cbMap[reqId] = cb; + + this.sendMessage({ + id: reqId, + type: 'RestRequest', + url: url + }, function(err) { + if (err) { + delete this.cbMap[reqId]; + + cb(err) + } + }) + } +} + +function Server() { + var server = https.createServer({ + key: fs.readFileSync(config.get('monitor:server:key')), + cert: fs.readFileSync(config.get('monitor:server:cert')), + passphrase: config.get('monitor:server:keyPassphrase') + }); + + server.listen(config.get('monitor:server:port')); + + var wss = new WebSocketServer({ server: server }); + + wss.on('connection', function(ws) { + var client = new Client(ws); + }) +} + +function removeFromArray(arr, val) { + var idx; + + while ((idx = arr.indexOf(val)) !== -1) { + arr.splice(idx, 1); + } +} + +exports.startServer = function() { + srv = new Server(); +}; + +exports.findClient = function(userId) { + var clientsList = clients[userId]; + + if (!clientsList) + return null; + + return clientsList[0]; +}; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/web-control-center/nodejs/app.js ---------------------------------------------------------------------- diff --git a/modules/web-control-center/nodejs/app.js b/modules/web-control-center/nodejs/app.js index a08ad17..d1a4121 100644 --- a/modules/web-control-center/nodejs/app.js +++ b/modules/web-control-center/nodejs/app.js @@ -34,7 +34,7 @@ var summary = require('./routes/summary'); var adminRouter = require('./routes/admin'); var profileRouter = require('./routes/profile'); var sqlRouter = require('./routes/sql'); -var bridge = require('./bridge/bridge'); +var agentManager = require('./agents/agentManager'); var passport = require('passport'); @@ -154,6 +154,6 @@ app.use(function (err, req, res) { }); }); -bridge.startServer(); +agentManager.startServer(); module.exports = app; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/web-control-center/nodejs/bridge/bridge.js ---------------------------------------------------------------------- diff --git a/modules/web-control-center/nodejs/bridge/bridge.js b/modules/web-control-center/nodejs/bridge/bridge.js deleted file mode 100644 index d850098..0000000 --- a/modules/web-control-center/nodejs/bridge/bridge.js +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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. - */ - -var WebSocketServer = require('ws').Server; - -var config = require('../helpers/configuration-loader.js'); - -var https = require('https'); - -var db = require('../db'); - -var fs = require('fs'); - -var srv; - -var clients = {}; - -function Client(ws) { - var self = this; - - this.ws = ws; - - ws.on('close', function() { - if (self.userId) { - var connections = clients[self.userId]; - - if (connections) { - removeFromArray(connections, self); - } - } - }); - - ws.on('message', function (msg) { - var m = JSON.parse(msg); - - switch (m.type) { - case 'AuthMessage': - var account = db.Account.findByUsername(m.login, function(err, account) { - if (err) { - ws.send("{type: 'AuthResult', success: false}"); - } - else { - account.authenticate(m.password, function(err, user, res) { - if (!user) { - ws.send(JSON.stringify({type: 'AuthResult', success: false, message: res.message})); - } - else { - ws.send("{type: 'AuthResult', success: true}"); - - self.userId = account._id; - - var existingConnections = clients[account._id]; - - if (!existingConnections) { - existingConnections = []; - - clients[account._id] = existingConnections; - } - - existingConnections.push(self); - } - }); - } - }); - - break; - - default: - ws.close() - } - }); -} - -function Server() { - var server = https.createServer({ - key: fs.readFileSync(config.get('monitor:server:key')), - cert: fs.readFileSync(config.get('monitor:server:cert')), - passphrase: config.get('monitor:server:keyPassphrase') - }); - - server.listen(config.get('monitor:server:port')); - - var wss = new WebSocketServer({ server: server }); - - wss.on('connection', function(ws) { - var client = new Client(ws); - }) -} - -function removeFromArray(arr, val) { - var idx; - - while ((idx = arr.indexOf(val)) !== -1) { - arr.splice(idx, 1); - } -} - -exports.startServer = function() { - srv = new Server(); -}; - -exports.findClient = function(userId) { - return clients[userId]; -}; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/dd27f3c0/modules/web-control-center/nodejs/routes/test.js ---------------------------------------------------------------------- diff --git a/modules/web-control-center/nodejs/routes/test.js b/modules/web-control-center/nodejs/routes/test.js new file mode 100644 index 0000000..5bfd954 --- /dev/null +++ b/modules/web-control-center/nodejs/routes/test.js @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +var router = require('express').Router(); +var bridge = require('../agents/agentManager'); + + + +/* GET summary page. */ +router.get('/', function(req, res) { + var c = bridge.findClient("55a2ca51eef88f6c775ed9d0"); + + if (!c) { + return res.send("Client not found"); + } + + c.restQuery("http://ya.ru/", function(error, code, message) { + if (error) { + res.send("Failed to execute REST query: " + error); + + return + } + + res.send("code: " + code + '<br>message: ' + message); + }); +}); + +module.exports = router;