You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by se...@apache.org on 2015/07/13 23:01:59 UTC

incubator-ignite git commit: # IGNITE-1121 Allow to execute remote REST queries.

Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-1121 f8b585e81 -> 5ae69f29f


# IGNITE-1121 Allow to execute remote REST queries.


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

Branch: refs/heads/ignite-1121
Commit: 5ae69f29fc38934f1f23eac6d249f6e1c512642c
Parents: f8b585e
Author: sevdokimov <se...@jetbrains.com>
Authored: Tue Jul 14 00:01:48 2015 +0300
Committer: sevdokimov <se...@jetbrains.com>
Committed: Tue Jul 14 00:01:48 2015 +0300

----------------------------------------------------------------------
 modules/control-center-agent/pom.xml            |   6 +
 .../java/org/apache/ignite/agent/Agent.java     |  93 +++++--------
 .../org/apache/ignite/agent/AgentLauncher.java  |  23 +++-
 .../org/apache/ignite/agent/AgentSocket.java    | 134 +++++++++++++++++++
 .../ignite/agent/messages/ExecuteRest.java      |  57 ++++++++
 .../ignite/agent/messages/RestResult.java       |  55 ++++++++
 6 files changed, 305 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5ae69f29/modules/control-center-agent/pom.xml
----------------------------------------------------------------------
diff --git a/modules/control-center-agent/pom.xml b/modules/control-center-agent/pom.xml
index f0401b6..9e711a2 100644
--- a/modules/control-center-agent/pom.xml
+++ b/modules/control-center-agent/pom.xml
@@ -55,6 +55,12 @@
             <artifactId>commons-cli</artifactId>
             <version>1.2</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5</version>
+        </dependency>
     </dependencies>
 
     <build>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5ae69f29/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 c3ac311..c013009 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
@@ -15,22 +15,24 @@ package org.apache.ignite.agent;/*
  * limitations under the License.
  */
 
+import org.apache.commons.codec.*;
+import org.apache.http.*;
+import org.apache.http.client.methods.*;
+import org.apache.http.impl.client.*;
 import org.apache.ignite.agent.messages.*;
-import org.eclipse.jetty.websocket.api.*;
-import org.eclipse.jetty.websocket.api.annotations.*;
 
-import java.util.concurrent.*;
+import java.io.*;
+import java.nio.charset.*;
 
 /**
  *
  */
-@WebSocket
 public class Agent {
     /** */
-    private final CountDownLatch closeLatch = new CountDownLatch(1);
+    private final AgentConfiguration cfg;
 
     /** */
-    private final AgentConfiguration cfg;
+    private CloseableHttpClient httpclient;
 
     /**
      * @param cfg Config.
@@ -40,69 +42,48 @@ public class Agent {
     }
 
     /**
-     * @param statusCode Status code.
-     * @param reason Reason.
+     *
      */
-    @OnWebSocketClose
-    public void onClose(int statusCode, String reason) {
-        System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
-
-        closeLatch.countDown();
+    public void start() {
+        httpclient = HttpClientBuilder.create().build();
     }
 
     /**
-     * @param ses Session.
+     *
      */
-    @OnWebSocketConnect
-    public void onConnect(Session ses) {
-        System.out.println("Authentication...");
-
-        AuthMessage authMsg = new AuthMessage(cfg.getLogin(), cfg.getPassword());
-
-        try {
-            ses.getRemote().sendString(MessageFactory.toString(authMsg));
-        } catch (Throwable t) {
-            t.printStackTrace();
-        }
+    public void stop() throws IOException {
+        if (httpclient != null)
+            httpclient.close();
     }
 
     /**
-     * @param ses Session.
-     * @param error Error.
+     * @param uri Url.
      */
-    @OnWebSocketError
-    public void onError(Session ses, Throwable error) {
-        System.out.printf("Error: " + ses);
+    public RestResult executeRest(String uri) throws IOException {
+        HttpGet get = new HttpGet(uri);
 
-        error.printStackTrace();
+        CloseableHttpResponse resp = httpclient.execute(get);
 
-        closeLatch.countDown();
-    }
+        RestResult res = new RestResult();
 
-    /**
-     * @param msg Message.
-     */
-    @OnWebSocketMessage
-    public void onMessage(Session ses, String msg) {
-        AbstractMessage m = MessageFactory.fromString(msg);
-
-        if (m instanceof AuthResult) {
-            if (((AuthResult)m).isSuccess())
-                System.out.println("Authentication success");
-            else {
-                System.out.println("Authentication failed: " + ((AuthResult)m).getMessage());
-
-                ses.close();
-            }
+        res.setCode(resp.getStatusLine().getStatusCode());
+
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+        resp.getEntity().writeTo(out);
+
+        Charset charset = Charsets.UTF_8;
+
+        Header encodingHdr = resp.getEntity().getContentEncoding();
+
+        if (encodingHdr != null) {
+            String encoding = encodingHdr.getValue();
+
+            charset = Charsets.toCharset(encoding);
         }
-        else
-            System.err.printf("Unknown message: %s%n", msg);
-    }
 
-    /**
-     *
-     */
-    public void waitForClose() throws InterruptedException {
-        closeLatch.await();
+        res.setMessage(new String(out.toByteArray(), charset));
+
+        return res;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5ae69f29/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentLauncher.java
----------------------------------------------------------------------
diff --git a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentLauncher.java b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentLauncher.java
index 1b23d96..70a6ba9d 100644
--- a/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentLauncher.java
+++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentLauncher.java
@@ -81,21 +81,30 @@ public class AgentLauncher {
         else
             cfg.setUri(uri);
 
-        WebSocketClient client = new WebSocketClient();
-
         Agent agent = new Agent(cfg);
 
-        client.start();
+        agent.start();
 
         try {
-            client.connect(agent, new URI(cfg.getUri()));
+            WebSocketClient client = new WebSocketClient();
+
+            AgentSocket agentSocket = new AgentSocket(cfg, agent);
+
+            client.start();
+
+            try {
+                client.connect(agentSocket, new URI(cfg.getUri()));
 
-            System.out.printf("Connecting to : %s%n", cfg.getUri());
+                System.out.printf("Connecting to : %s%n", cfg.getUri());
 
-            agent.waitForClose();
+                agentSocket.waitForClose();
+            }
+            finally {
+                client.stop();
+            }
         }
         finally {
-            client.stop();
+            agent.stop();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5ae69f29/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
new file mode 100644
index 0000000..79dd9fc
--- /dev/null
+++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/AgentSocket.java
@@ -0,0 +1,134 @@
+package org.apache.ignite.agent;/*
+ * 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 org.apache.ignite.agent.messages.*;
+import org.eclipse.jetty.websocket.api.*;
+import org.eclipse.jetty.websocket.api.annotations.*;
+
+import java.io.*;
+import java.util.concurrent.*;
+
+/**
+ *
+ */
+@WebSocket
+public class AgentSocket {
+    /** */
+    private final CountDownLatch closeLatch = new CountDownLatch(1);
+
+    /** */
+    private final AgentConfiguration cfg;
+
+    /** */
+    private final Agent agent;
+
+    /**
+     * @param cfg Config.
+     */
+    public AgentSocket(AgentConfiguration cfg, Agent agent) {
+        this.cfg = cfg;
+        this.agent = agent;
+    }
+
+    /**
+     * @param statusCode Status code.
+     * @param reason Reason.
+     */
+    @OnWebSocketClose
+    public void onClose(int statusCode, String reason) {
+        System.out.printf("Connection closed: %d - %s%n", statusCode, reason);
+
+        closeLatch.countDown();
+    }
+
+    /**
+     * @param ses Session.
+     */
+    @OnWebSocketConnect
+    public void onConnect(Session ses) {
+        System.out.println("Authentication...");
+
+        AuthMessage authMsg = new AuthMessage(cfg.getLogin(), cfg.getPassword());
+
+        try {
+            ses.getRemote().sendString(MessageFactory.toString(authMsg));
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
+    }
+
+    /**
+     * @param ses Session.
+     * @param error Error.
+     */
+    @OnWebSocketError
+    public void onError(Session ses, Throwable error) {
+        System.out.printf("Error: " + ses);
+
+        error.printStackTrace();
+
+        closeLatch.countDown();
+    }
+
+    /**
+     * @param msg Message.
+     */
+    @OnWebSocketMessage
+    public void onMessage(Session ses, String msg) {
+        AbstractMessage m = MessageFactory.fromString(msg);
+
+        if (m instanceof AuthResult) {
+            if (((AuthResult)m).isSuccess())
+                System.out.println("Authentication success");
+            else {
+                System.out.println("Authentication failed: " + ((AuthResult)m).getMessage());
+
+                ses.close();
+            }
+        }
+        else if (m instanceof ExecuteRest) {
+            ExecuteRest execRest = (ExecuteRest)m;
+
+            RestResult res;
+
+            try {
+                res = agent.executeRest(execRest.getUrl());
+            }
+            catch (IOException e) {
+                res = new RestResult();
+
+                res.setCode(500);
+            }
+
+            try {
+                ses.getRemote().sendString(MessageFactory.toString(res));
+            }
+            catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        else
+            System.err.printf("Unknown message: %s%n", msg);
+    }
+
+    /**
+     *
+     */
+    public void waitForClose() throws InterruptedException {
+        closeLatch.await();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5ae69f29/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
new file mode 100644
index 0000000..217852f
--- /dev/null
+++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/ExecuteRest.java
@@ -0,0 +1,57 @@
+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/5ae69f29/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
new file mode 100644
index 0000000..b23a203
--- /dev/null
+++ b/modules/control-center-agent/src/main/java/org/apache/ignite/agent/messages/RestResult.java
@@ -0,0 +1,55 @@
+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.
+ */
+
+/**
+ *
+ */
+public class RestResult extends AbstractMessage {
+    /** */
+    private int code;
+
+    /** */
+    private String message;
+
+    /**
+     *
+     */
+    public int getCode() {
+        return code;
+    }
+
+    /**
+     * @param code Code.
+     */
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    /**
+     *
+     */
+    public String getMessage() {
+        return message;
+    }
+
+    /**
+     * @param msg Message.
+     */
+    public void setMessage(String msg) {
+        this.message = msg;
+    }
+}