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/08/11 22:46:31 UTC

incubator-ignite git commit: IGNITE-1223 Fix for ignite-http-rest

Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-1223 e7f6cdb97 -> 1341e9ee9


IGNITE-1223 Fix for ignite-http-rest


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

Branch: refs/heads/ignite-1223
Commit: 1341e9ee982aff3413e0b98ade6a29726bb9860d
Parents: e7f6cdb
Author: sevdokimov <se...@jetbrains.com>
Authored: Tue Aug 11 23:46:22 2015 +0300
Committer: sevdokimov <se...@jetbrains.com>
Committed: Tue Aug 11 23:46:22 2015 +0300

----------------------------------------------------------------------
 modules/rest-http/pom.xml                       |  6 +-
 .../http/jetty/GridJettyJsonConfig.java         | 50 ---------------
 .../http/jetty/GridJettyRestHandler.java        | 49 +++------------
 .../http/jetty/IgniteFieldNamingStrategy.java   | 66 ++++++++++++++++++++
 4 files changed, 77 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1341e9ee/modules/rest-http/pom.xml
----------------------------------------------------------------------
diff --git a/modules/rest-http/pom.xml b/modules/rest-http/pom.xml
index 4ce41ae..5bac967 100644
--- a/modules/rest-http/pom.xml
+++ b/modules/rest-http/pom.xml
@@ -89,10 +89,8 @@
         </dependency>
 
         <dependency>
-            <groupId>net.sf.json-lib</groupId>
-            <artifactId>json-lib</artifactId>
-            <version>2.4</version>
-            <classifier>jdk15</classifier>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
         </dependency>
 
         <dependency>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1341e9ee/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
deleted file mode 100644
index e36da80..0000000
--- a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/GridJettyJsonConfig.java
+++ /dev/null
@@ -1,50 +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.
- */
-
-package org.apache.ignite.internal.processors.rest.protocols.http.jetty;
-
-import net.sf.json.*;
-import net.sf.json.processors.*;
-
-import java.util.*;
-
-/**
- * Jetty protocol json configuration.
- */
-public class GridJettyJsonConfig extends JsonConfig {
-    /**
-     * Constructs default jetty json config.
-     */
-    public GridJettyJsonConfig() {
-        registerJsonValueProcessor(UUID.class, new ToStringJsonProcessor());
-    }
-
-    /**
-     * Helper class for simple to-string conversion for the beans.
-     */
-    private static class ToStringJsonProcessor implements JsonValueProcessor {
-        /** {@inheritDoc} */
-        @Override public Object processArrayValue(Object val, JsonConfig jsonCfg) {
-            throw new UnsupportedOperationException("Serialize array to string is not supported: " + val);
-        }
-
-        /** {@inheritDoc} */
-        @Override public Object processObjectValue(String key, Object val, JsonConfig jsonCfg) {
-            return val == null ? null : val.toString();
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1341e9ee/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 75e80ec..1c5f5ab 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
@@ -17,11 +17,9 @@
 
 package org.apache.ignite.internal.processors.rest.protocols.http.jetty;
 
-import net.sf.json.*;
-import net.sf.json.processors.*;
+import com.google.gson.*;
 import org.apache.ignite.*;
 import org.apache.ignite.internal.processors.rest.*;
-import org.apache.ignite.internal.processors.rest.client.message.*;
 import org.apache.ignite.internal.processors.rest.request.*;
 import org.apache.ignite.internal.util.typedef.*;
 import org.apache.ignite.internal.util.typedef.internal.*;
@@ -45,17 +43,6 @@ import static org.apache.ignite.internal.processors.rest.GridRestResponse.*;
  * {@code /ignite?cmd=cmdName&param1=abc&param2=123}
  */
 public class GridJettyRestHandler extends AbstractHandler {
-    /** JSON value processor that does not transform input object. */
-    private static final JsonValueProcessor SKIP_STR_VAL_PROC = new JsonValueProcessor() {
-        @Override public Object processArrayValue(Object o, JsonConfig jsonConfig) {
-            return o;
-        }
-
-        @Override public Object processObjectValue(String s, Object o, JsonConfig jsonConfig) {
-            return o;
-        }
-    };
-
     /** Logger. */
     private final IgniteLogger log;
 
@@ -71,6 +58,9 @@ public class GridJettyRestHandler extends AbstractHandler {
     /** Authentication checker. */
     private final IgniteClosure<String, Boolean> authChecker;
 
+    /** */
+    private final Gson gson;
+
     /**
      * Creates new HTTP requests handler.
      *
@@ -86,6 +76,8 @@ public class GridJettyRestHandler extends AbstractHandler {
         this.log = log;
         this.authChecker = authChecker;
 
+        gson = new GsonBuilder().setFieldNamingStrategy(new IgniteFieldNamingStrategy()).create();
+
         // Init default page and favicon.
         try {
             initDefaultPage();
@@ -277,39 +269,16 @@ public class GridJettyRestHandler extends AbstractHandler {
                 throw (Error)e;
         }
 
-        JsonConfig cfg = new GridJettyJsonConfig();
-
-        // Workaround for not needed transformation of string into JSON object.
-        if (cmdRes.getResponse() instanceof String)
-            cfg.registerJsonValueProcessor(cmdRes.getClass(), "response", SKIP_STR_VAL_PROC);
-
-        // Workaround for not needed transformation of result field string into JSON object at GridClientTaskResultBean.
-        if (cmdRes.getResponse() instanceof GridClientTaskResultBean
-            && ((GridClientTaskResultBean)cmdRes.getResponse()).getResult() instanceof String)
-            cfg.registerJsonValueProcessor(cmdRes.getResponse().getClass(), "result", SKIP_STR_VAL_PROC);
-
-        JSON json;
-
-        try {
-            json = JSONSerializer.toJSON(cmdRes, cfg);
-        }
-        catch (JSONException e) {
-            U.error(log, "Failed to convert response to JSON: " + cmdRes, e);
-
-            json = JSONSerializer.toJSON(new GridRestResponse(STATUS_FAILED, e.getMessage()), cfg);
-        }
+        Gson gson = new Gson();
 
         try {
-            if (log.isDebugEnabled())
-                log.debug("Parsed command response into JSON object: " + json.toString(2));
-
-            res.getWriter().write(json.toString());
+            gson.toJson(cmdRes, res.getWriter());
 
             if (log.isDebugEnabled())
                 log.debug("Processed HTTP request [action=" + act + ", jsonRes=" + cmdRes + ", req=" + req + ']');
         }
         catch (IOException e) {
-            U.error(log, "Failed to send HTTP response: " + json.toString(2), e);
+            U.error(log, "Failed to send HTTP response", e);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/1341e9ee/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteFieldNamingStrategy.java
----------------------------------------------------------------------
diff --git a/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteFieldNamingStrategy.java b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteFieldNamingStrategy.java
new file mode 100644
index 0000000..5bb3df2
--- /dev/null
+++ b/modules/rest-http/src/main/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/IgniteFieldNamingStrategy.java
@@ -0,0 +1,66 @@
+/*
+ * 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.rest.protocols.http.jetty;
+
+import com.google.gson.*;
+import org.apache.ignite.internal.processors.rest.*;
+
+import java.lang.reflect.*;
+import java.util.*;
+
+/**
+ * Ignite field name strategy.
+ */
+public class IgniteFieldNamingStrategy implements FieldNamingStrategy {
+    /** */
+    private static final Map<Field, String> FIELD_NAMES = new HashMap<>();
+
+    static {
+        addField(GridRestResponse.class, "obj", "response");
+        addField(GridRestResponse.class, "err", "error");
+        addField(GridRestResponse.class, "sesTokStr", "sessionToken");
+    }
+
+    /**
+     * @param cls Class.
+     * @param fldName Fld name.
+     * @param propName Property name.
+     */
+    private static void addField(Class<?> cls, String fldName, String propName) {
+        try {
+            Field field = cls.getDeclaredField(fldName);
+
+            // assert method exists.
+            cls.getDeclaredMethod("get" + Character.toUpperCase(propName.charAt(0)) + propName.substring(1));
+
+            String old = FIELD_NAMES.put(field, propName);
+
+            assert old == null;
+        }
+        catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public String translateName(Field f) {
+        String name = FIELD_NAMES.get(f);
+
+        return name == null ? f.getName() : name;
+    }
+}