ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sevdoki...@apache.org
Subject incubator-ignite git commit: # IGNITE-1121 Implement POST call.
Date Thu, 16 Jul 2015 07:45:22 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-1121 dd27f3c00 -> 2e814bd59


# IGNITE-1121 Implement POST call.


Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/2e814bd5
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/2e814bd5
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/2e814bd5

Branch: refs/heads/ignite-1121
Commit: 2e814bd59eed708c99b9d65a3f05b195bd19ee61
Parents: dd27f3c
Author: sevdokimov <sergey.evdokimov@jetbrains.com>
Authored: Thu Jul 16 10:45:00 2015 +0300
Committer: sevdokimov <sergey.evdokimov@jetbrains.com>
Committed: Thu Jul 16 10:45:00 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/ignite/agent/Agent.java     | 60 ++++++++++++++------
 .../org/apache/ignite/agent/AgentSocket.java    |  2 +-
 .../ignite/agent/messages/RestRequest.java      | 17 ++++++
 .../nodejs/agents/agentManager.js               | 22 ++++++-
 .../web-control-center/nodejs/routes/test.js    | 23 +++++++-
 5 files changed, 103 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2e814bd5/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 c420fa9..f8e95a5 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
@@ -19,12 +19,15 @@ package org.apache.ignite.agent;
 
 import org.apache.commons.codec.*;
 import org.apache.http.*;
+import org.apache.http.client.entity.*;
 import org.apache.http.client.methods.*;
 import org.apache.http.impl.client.*;
+import org.apache.http.message.*;
 import org.apache.ignite.agent.messages.*;
 
 import java.io.*;
 import java.nio.charset.*;
+import java.util.*;
 
 /**
  *
@@ -59,33 +62,56 @@ public class Agent {
     }
 
     /**
-     * @param uri Url.
+     * @param restReq Request.
      */
-    public RestResult executeRest(String uri) throws IOException {
-        HttpGet get = new HttpGet(uri);
+    public RestResult executeRest(RestRequest restReq) throws IOException {
+        HttpRequestBase httpReq;
 
-        CloseableHttpResponse resp = httpclient.execute(get);
+        if ("GET".equalsIgnoreCase(restReq.getMethod())) {
+            httpReq = new HttpGet(restReq.getUrl());
 
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
-        resp.getEntity().writeTo(out);
+            if (restReq.getParams() != null)
+                throw new IOException("Parameters of GET method should be passed in URL");
+        }
+        else if ("POST".equalsIgnoreCase(restReq.getMethod())) {
+            HttpPost post = new HttpPost(restReq.getUrl());
 
-        Charset charset = Charsets.UTF_8;
+            if (restReq.getParams() != null) {
+                List<NameValuePair> nvps = new ArrayList<>();
 
-        Header encodingHdr = resp.getEntity().getContentEncoding();
+                for (Map.Entry<String, String> entry : restReq.getParams().entrySet())
+                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
 
-        if (encodingHdr != null) {
-            String encoding = encodingHdr.getValue();
+                post.setEntity(new UrlEncodedFormEntity(nvps));
+            }
 
-            charset = Charsets.toCharset(encoding);
+            httpReq = post;
         }
+        else
+            throw new IOException("Unknown HTTP-method: " + restReq.getMethod());
+
+        try (CloseableHttpResponse resp = httpclient.execute(httpReq)) {
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+            resp.getEntity().writeTo(out);
+
+            Charset charset = Charsets.UTF_8;
 
-        RestResult res = new RestResult();
+            Header encodingHdr = resp.getEntity().getContentEncoding();
 
-        res.setCode(resp.getStatusLine().getStatusCode());
-        res.setExecuted(true);
-        res.setMessage(new String(out.toByteArray(), charset));
+            if (encodingHdr != null) {
+                String encoding = encodingHdr.getValue();
 
-        return res;
+                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/2e814bd5/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 9e6e14a..7603ed9 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
@@ -108,7 +108,7 @@ public class AgentSocket {
             RestResult restRes;
 
             try {
-                restRes = agent.executeRest(restReq.getUrl());
+                restRes = agent.executeRest(restReq);
             }
             catch (IOException e) {
                 restRes = new RestResult();

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2e814bd5/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
index 9d5181d..cad5808 100644
--- 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
@@ -30,6 +30,9 @@ public class RestRequest extends AbstractMessage {
     /** */
     private Map<String, String> params;
 
+    /** */
+    private String method;
+
     /**
      *
      */
@@ -71,4 +74,18 @@ public class RestRequest extends AbstractMessage {
     public void setParams(Map<String, String> params) {
         this.params = params;
     }
+
+    /**
+     *
+     */
+    public String getMethod() {
+        return method;
+    }
+
+    /**
+     * @param mtd Method.
+     */
+    public void setMethod(String mtd) {
+        this.method = mtd;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2e814bd5/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
index aa2506a..97a0bb1 100644
--- a/modules/web-control-center/nodejs/agents/agentManager.js
+++ b/modules/web-control-center/nodejs/agents/agentManager.js
@@ -113,7 +113,7 @@ function Client(ws) {
 
     this.cbMap = {};
 
-    this.restQuery = function(url, cb) {
+    this.invokeRest = function(url, method, params, cb) {
         var reqId = this.restCounter++;
 
         this.cbMap[reqId] = cb;
@@ -121,6 +121,8 @@ function Client(ws) {
         this.sendMessage({
             id: reqId,
             type: 'RestRequest',
+            method: method,
+            params: params,
             url: url
         }, function(err) {
             if (err) {
@@ -129,6 +131,24 @@ function Client(ws) {
                 cb(err)
             }
         })
+    };
+
+
+    this.restGet = function(url, cb) {
+        this.invokeRest(url, 'GET', null, cb);
+    };
+
+    this.restPost = function(url, params, cb) {
+        if (typeof(params) == 'function' && !cb) {
+            cb = params;
+
+            params = undefined
+        }
+
+        if (params && typeof(params) != 'object')
+            throw "'params' argument must be an object";
+
+        this.invokeRest(url, 'POST', params, cb);
     }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/2e814bd5/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
index 5bfd954..f842c8b 100644
--- a/modules/web-control-center/nodejs/routes/test.js
+++ b/modules/web-control-center/nodejs/routes/test.js
@@ -21,14 +21,33 @@ var bridge = require('../agents/agentManager');
 
 
 /* GET summary page. */
-router.get('/', function(req, res) {
+router.get('/testGet', 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) {
+    c.restGet("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);
+    });
+});
+
+/* GET summary page. */
+router.get('/testPost', function(req, res) {
+    var c = bridge.findClient("55a2ca51eef88f6c775ed9d0");
+
+    if (!c) {
+        return res.send("Client not found");
+    }
+
+    c.restPost("http://localhost:3000/configuration/caches/save", {_id: 555}, function(error,
code, message) {
         if (error) {
             res.send("Failed to execute REST query: " + error);
 


Mime
View raw message