usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From snoopd...@apache.org
Subject [2/6] usergrid git commit: Provide some extra validation of our latitude and longitude location fields before adding it to the Elasticsearch index document. This is to avoid rejected index requests due to number format exceptions.
Date Tue, 01 Dec 2015 14:27:34 GMT
Provide some extra validation of our latitude and longitude location fields before adding it
to the Elasticsearch index document.  This is to avoid rejected index requests due to number
format exceptions.


Project: http://git-wip-us.apache.org/repos/asf/usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/usergrid/commit/29bba243
Tree: http://git-wip-us.apache.org/repos/asf/usergrid/tree/29bba243
Diff: http://git-wip-us.apache.org/repos/asf/usergrid/diff/29bba243

Branch: refs/heads/USERGRID-872
Commit: 29bba24336c22bc5ed8369c328556e8348c75391
Parents: 9bfbc53
Author: Michael Russo <michaelarusso@gmail.com>
Authored: Tue Nov 24 13:28:44 2015 -0700
Committer: Michael Russo <michaelarusso@gmail.com>
Committed: Tue Nov 24 13:28:44 2015 -0700

----------------------------------------------------------------------
 .../persistence/model/entity/EntityMap.java     | 50 ++++++++++++++++----
 1 file changed, 41 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/usergrid/blob/29bba243/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityMap.java
----------------------------------------------------------------------
diff --git a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityMap.java
b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityMap.java
index 84b6d35..5c8fd77 100644
--- a/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityMap.java
+++ b/stack/corepersistence/model/src/main/java/org/apache/usergrid/persistence/model/entity/EntityMap.java
@@ -1,20 +1,19 @@
 package org.apache.usergrid.persistence.model.entity;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.common.base.*;
-import org.apache.usergrid.persistence.model.field.*;
-import org.apache.usergrid.persistence.model.field.value.EntityObject;
-
-import java.io.IOException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.UUID;
+
 
 /**
  * Core persistence Entity Map structure to persist to
  */
 public class EntityMap extends HashMap<String,Object> {
+
+    private static final Logger logger = LoggerFactory.getLogger( EntityMap.class );
+
     private static EntityToMapConverter entityToMapConverter = new EntityToMapConverter();
 
 
@@ -39,16 +38,49 @@ public class EntityMap extends HashMap<String,Object> {
     }
 
     /**
-     * Return true if the value is a location field
+     * Return true if the value is a location field which contains valid values for latitude
and longitude
      * @param fieldValue
      * @return
      */
     public static boolean isLocationField(Map<String, ?> fieldValue) {
+
+        //short circuit since valid location objects contain only 2 properties (latitude
and longitude)
         if (fieldValue.size() != 2) {
             return false;
         }
 
-        return fieldValue.containsKey(EntityToMapConverter.LAT) && fieldValue.containsKey(EntityToMapConverter.LON);
+        // we need to make sure that latitude and longitude are numbers or strings that can
be parsed as a number
+        if (fieldValue.containsKey(EntityToMapConverter.LAT) && fieldValue.containsKey(EntityToMapConverter.LON)){
+
+            for(Map.Entry<String,?> value : fieldValue.entrySet()){
+
+                if(!(value.getValue() instanceof Number) && !isDouble(String.valueOf(value.getValue()))){
+
+                    if(logger.isDebugEnabled()){
+                        logger.debug("Field [{}] with value [{}] is not a valid geo coordinate",
+                            value.getKey(),
+                            value.getValue()
+                        );
+                    }
+                    return false;
+                }
+            }
+
+            return true;
+        }
+
+        return false;
+
+    }
+
+    public static boolean isDouble(String s){
+
+        try{
+            Double.valueOf(s);
+            return true;
+        }catch (NumberFormatException e){
+            return false;
+        }
     }
 
 }


Mime
View raw message