ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ivasilin...@apache.org
Subject incubator-ignite git commit: #ignite-961: use glassfish parser for JSON request.
Date Fri, 17 Jul 2015 08:16:31 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-961 e1aca64fc -> 18a3e05af


#ignite-961: use glassfish parser for JSON request.


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

Branch: refs/heads/ignite-961
Commit: 18a3e05af2ae3886603e705ae6ce469cbb36486f
Parents: e1aca64
Author: ivasilinets <ivasilinets@gridgain.com>
Authored: Fri Jul 17 11:16:24 2015 +0300
Committer: ivasilinets <ivasilinets@gridgain.com>
Committed: Fri Jul 17 11:16:24 2015 +0300

----------------------------------------------------------------------
 .../scripting/IgniteScriptingConverter.java     | 44 ++++++++++++++++++++
 .../scripting/IgniteScriptingProcessor.java     | 41 +++++++++++++++---
 .../org/apache/ignite/json/JSONCacheObject.java |  2 +-
 modules/rest-http/pom.xml                       |  6 +++
 .../http/jetty/GridJettyRestHandler.java        | 31 +++++---------
 5 files changed, 98 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18a3e05a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingConverter.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingConverter.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingConverter.java
new file mode 100644
index 0000000..bc52d58
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingConverter.java
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.internal.processors.scripting;
+
+import org.apache.ignite.json.*;
+
+/**
+ * Convert json object ot java objects.
+ */
+public class IgniteScriptingConverter {
+    /**
+     * @param o Object.
+     * @return  Object for Ignite cache.
+     */
+    public Object toJavaObject(Object o) {
+        return JSONCacheObject.toSimpleObject(o);
+    }
+
+    /**
+     * @param o Object from script.
+     * @return Object to store in cache.
+     */
+    public Object getField(String key, Object o) {
+        if (o instanceof JSONCacheObject)
+            return ((JSONCacheObject)o).getField(key);
+
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18a3e05a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
index b38c4c3..db283b8 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptingProcessor.java
@@ -35,9 +35,16 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
     /** Javascript engine name. */
     public static final String JAVA_SCRIPT_ENGINE_NAME = "JavaScript";
 
+    /** Rest converter. */
+    private static final String REST_CONV_CLS =
+        "org.apache.ignite.internal.processors.rest.protocols.http.jetty.GlassFishScriptingConverter";
+
     /** Javascript engine. */
     private ScriptEngine jsEngine;
 
+    /** Ignite scripting converter. */
+    IgniteScriptingConverter converter;
+
     /**
      * @param ctx Kernal context.
      */
@@ -47,6 +54,8 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
 
     /** {@inheritDoc} */
     @Override public void start() throws IgniteCheckedException {
+        initializeConverter();
+
         ScriptEngineManager factory = new ScriptEngineManager();
 
         jsEngine = factory.getEngineByName(JAVA_SCRIPT_ENGINE_NAME);
@@ -153,7 +162,7 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
      * @return  Object for Ignite cache.
      */
     public Object toJavaObject(Object o) {
-        return JSONCacheObject.toSimpleObject(o);
+        return converter.toJavaObject(o);
     }
 
     /**
@@ -161,10 +170,7 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
      * @return Object to store in cache.
      */
     public Object getField(String key, Object o) {
-        if (o instanceof JSONCacheObject)
-            return ((JSONCacheObject)o).getField(key);
-
-        return null;
+        return converter.getField(key, o);
     }
 
     /**
@@ -177,6 +183,31 @@ public class IgniteScriptingProcessor extends GridProcessorAdapter {
     }
 
     /**
+     * Initialize rest converter if it is available.
+     *
+     * @throws IgniteCheckedException If no constructor found for converter.
+     */
+    private void initializeConverter() throws IgniteCheckedException {
+        try {
+            Class<?> cls = Class.forName(REST_CONV_CLS);
+
+            Constructor<?> ctor = cls.getConstructor();
+
+            converter = (IgniteScriptingConverter)ctor.newInstance();
+        }
+        catch (ClassNotFoundException ignored) {
+            if (log.isDebugEnabled())
+                log.debug("Failed to initialize HTTP REST protocol (consider adding ignite-rest-http
" +
+                    "module to classpath).");
+
+            converter = new IgniteScriptingConverter();
+        }
+        catch (NoSuchMethodException | InvocationTargetException | InstantiationException
| IllegalAccessException e) {
+            throw new IgniteCheckedException("Failed to initialize HTTP REST protocol.",
e);
+        }
+    }
+
+    /**
      * Scripting cache entry.
      */
     public static class ScriptingCacheEntry {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18a3e05a/modules/core/src/main/java/org/apache/ignite/json/JSONCacheObject.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/json/JSONCacheObject.java b/modules/core/src/main/java/org/apache/ignite/json/JSONCacheObject.java
index fd4359e..13c9321 100644
--- a/modules/core/src/main/java/org/apache/ignite/json/JSONCacheObject.java
+++ b/modules/core/src/main/java/org/apache/ignite/json/JSONCacheObject.java
@@ -29,7 +29,7 @@ public class JSONCacheObject extends HashMap<Object, Object> {
     /**
      * Empty constructor.
      */
-    private JSONCacheObject() {
+    public JSONCacheObject() {
         // No-op.
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18a3e05a/modules/rest-http/pom.xml
----------------------------------------------------------------------
diff --git a/modules/rest-http/pom.xml b/modules/rest-http/pom.xml
index 4ce41ae..c301aa6 100644
--- a/modules/rest-http/pom.xml
+++ b/modules/rest-http/pom.xml
@@ -35,6 +35,12 @@
 
     <dependencies>
         <dependency>
+            <groupId>org.glassfish</groupId>
+            <artifactId>javax.json</artifactId>
+            <version>1.0.4</version>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.ignite</groupId>
             <artifactId>ignite-core</artifactId>
             <version>${project.version}</version>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/18a3e05a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
index 577a5e5..c2e6885 100644
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyRestHandler.java
@@ -30,6 +30,7 @@ import org.apache.ignite.lang.*;
 import org.apache.ignite.plugin.security.*;
 import org.eclipse.jetty.server.*;
 import org.eclipse.jetty.server.handler.*;
+import org.glassfish.json.*;
 import org.jetbrains.annotations.*;
 
 import javax.servlet.*;
@@ -578,8 +579,8 @@ public class GridJettyRestHandler extends AbstractHandler {
 
                 restReq0.script((String)params.get("func"));
 
-                JSONObject o = parseRequest(req);
-                restReq0.argument(o.get("arg"));
+                Map o = parseRequest(req);
+                restReq0.argument(ctx.scripting().toJavaObject(o.get("arg")));
 
                 restReq = restReq0;
 
@@ -592,8 +593,8 @@ public class GridJettyRestHandler extends AbstractHandler {
                 restReq0.script((String)params.get("func"));
                 restReq0.cacheName((String) params.get("cacheName"));
 
-                JSONObject o = parseRequest(req);
-                restReq0.argument(o.get("arg"));
+                Map o = parseRequest(req);
+                restReq0.argument(ctx.scripting().toJavaObject(o.get("arg")));
 
                 Object cacheObj = ctx.scripting().toJavaObject(o.get("key"));
                 restReq0.affinityKey(cacheObj);
@@ -608,8 +609,8 @@ public class GridJettyRestHandler extends AbstractHandler {
 
                 restReq0.mapFunction((String) params.get("map"));
 
-                JSONObject o = parseRequest(req);
-                restReq0.argument(o.get("arg"));
+                Map o = parseRequest(req);
+                restReq0.argument(ctx.scripting().toJavaObject(o.get("arg")));
 
                 restReq0.reduceFunction((String) params.get("reduce"));
 
@@ -624,9 +625,8 @@ public class GridJettyRestHandler extends AbstractHandler {
 
                 restReq0.sqlQuery((String)params.get("qry"));
 
-                JSONObject o = parseRequest(req);
-
-                List args = (List)o.get("arg");
+                Map o = parseRequest(req);
+                List args = (List)ctx.scripting().toJavaObject(o.get("arg"));
 
                 restReq0.arguments(args.toArray());
                 restReq0.typeName((String)params.get("type"));
@@ -851,21 +851,12 @@ public class GridJettyRestHandler extends AbstractHandler {
      * @return JSON object.
      * @throws IgniteCheckedException If failed.
      */
-    private JSONObject parseRequest(HttpServletRequest req) throws IgniteCheckedException{
-        StringBuilder builder = new StringBuilder();
-
-        Scanner reader;
-
+    private Map parseRequest(HttpServletRequest req) throws IgniteCheckedException{
         try {
-            reader = new Scanner(req.getInputStream());
+            return new JsonProviderImpl().createReader(req.getInputStream()).readObject();
         }
         catch (IOException e) {
             throw new IgniteCheckedException(e);
         }
-
-        while (reader.hasNext())
-            builder.append(reader.nextLine() + "\n");
-
-        return JSONObject.fromObject(builder.toString());
     }
 }


Mime
View raw message