metron-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ceste...@apache.org
Subject metron git commit: METRON-1455: Patch and Replace methods in the REST UpdateController return 400 this closes apache/incubator-metron#937
Date Thu, 15 Feb 2018 18:00:48 GMT
Repository: metron
Updated Branches:
  refs/heads/master 15b98dea4 -> e59059bd9


METRON-1455: Patch and Replace methods in the REST UpdateController return 400 this closes
apache/incubator-metron#937


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

Branch: refs/heads/master
Commit: e59059bd9707a6ca46c4137d796b8f2943f06b43
Parents: 15b98de
Author: cstella <cestella@gmail.com>
Authored: Thu Feb 15 13:00:36 2018 -0500
Committer: cstella <cestella@gmail.com>
Committed: Thu Feb 15 13:00:36 2018 -0500

----------------------------------------------------------------------
 .../apache/metron/common/utils/JSONUtils.java   |  7 ++++++
 .../dao/ElasticsearchMetaAlertDao.java          | 15 ++++++++-----
 .../apache/metron/indexing/dao/IndexDao.java    | 23 +++++---------------
 .../indexing/dao/update/PatchRequest.java       |  7 +++---
 4 files changed, 26 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metron/blob/e59059bd/metron-platform/metron-common/src/main/java/org/apache/metron/common/utils/JSONUtils.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/utils/JSONUtils.java
b/metron-platform/metron-common/src/main/java/org/apache/metron/common/utils/JSONUtils.java
index 135546d..c02f19d 100644
--- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/utils/JSONUtils.java
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/utils/JSONUtils.java
@@ -184,4 +184,11 @@ public enum JSONUtils {
     return toJSONPretty(JsonPatch.apply(patchNode, sourceNode));
   }
 
+  public Map<String, Object> applyPatch(List<Map<String, Object>> patch,
Map<String, Object> source) {
+    JsonNode originalNode = convert(source, JsonNode.class);
+    JsonNode patchNode = convert(patch, JsonNode.class);
+    JsonNode patched = JsonPatch.apply(patchNode, originalNode);
+    return _mapper.get().convertValue(patched, new TypeReference<Map<String, Object>>()
{ });
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/metron/blob/e59059bd/metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchMetaAlertDao.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchMetaAlertDao.java
b/metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchMetaAlertDao.java
index 9740272..2311a2b 100644
--- a/metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchMetaAlertDao.java
+++ b/metron-platform/metron-elasticsearch/src/main/java/org/apache/metron/elasticsearch/dao/ElasticsearchMetaAlertDao.java
@@ -473,12 +473,15 @@ public class ElasticsearchMetaAlertDao implements MetaAlertDao {
   }
 
   protected boolean isPatchAllowed(PatchRequest request) {
-    Iterator patchIterator = request.getPatch().iterator();
-    while(patchIterator.hasNext()) {
-      JsonNode patch = (JsonNode) patchIterator.next();
-      String path = patch.path("path").asText();
-      if (STATUS_PATH.equals(path) || ALERT_PATH.equals(path)) {
-        return false;
+    if(request.getPatch() != null && !request.getPatch().isEmpty()) {
+      for(Map<String, Object> patch : request.getPatch()) {
+        Object pathObj = patch.get("path");
+        if(pathObj != null && pathObj instanceof String) {
+          String path = (String)pathObj;
+          if (STATUS_PATH.equals(path) || ALERT_PATH.equals(path)) {
+            return false;
+          }
+        }
       }
     }
     return true;

http://git-wip-us.apache.org/repos/asf/metron/blob/e59059bd/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/IndexDao.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/IndexDao.java
b/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/IndexDao.java
index 2c057d4..fe546bd 100644
--- a/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/IndexDao.java
+++ b/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/IndexDao.java
@@ -17,16 +17,11 @@
  */
 package org.apache.metron.indexing.dao;
 
-import com.fasterxml.jackson.annotation.JsonInclude;
-import com.fasterxml.jackson.core.type.TypeReference;
-import com.fasterxml.jackson.databind.JsonNode;
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.flipkart.zjsonpatch.JsonPatch;
 import org.apache.metron.common.utils.JSONUtils;
 import org.apache.metron.indexing.dao.search.FieldType;
 import org.apache.metron.indexing.dao.search.GetRequest;
@@ -46,9 +41,6 @@ import org.apache.metron.indexing.dao.update.ReplaceRequest;
  */
 public interface IndexDao {
 
-  public static ThreadLocal<ObjectMapper> _mapper = ThreadLocal.withInitial(() ->
-      new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL));
-
   /**
    * Return search response based on the search request
    *
@@ -136,7 +128,7 @@ public interface IndexDao {
 
   default Document getPatchedDocument(PatchRequest request
       , Optional<Long> timestamp
-      ) throws OriginalNotFoundException, IOException {
+  ) throws OriginalNotFoundException, IOException {
     Map<String, Object> latest = request.getSource();
     if(latest == null) {
       Document latestDoc = getLatest(request.getGuid(), request.getSensorType());
@@ -147,14 +139,11 @@ public interface IndexDao {
         throw new OriginalNotFoundException("Unable to patch an document that doesn't exist
and isn't specified.");
       }
     }
-    JsonNode originalNode = _mapper.get().convertValue(latest, JsonNode.class);
-    JsonNode patched = JsonPatch.apply(request.getPatch(), originalNode);
-    Map<String, Object> updated = _mapper.get()
-        .convertValue(patched, new TypeReference<Map<String, Object>>() {});
-    return new Document( updated
-        , request.getGuid()
-        , request.getSensorType()
-        , timestamp.orElse(System.currentTimeMillis()));
+    Map<String, Object> updated = JSONUtils.INSTANCE.applyPatch(request.getPatch(),
latest);
+    return new Document(updated
+            , request.getGuid()
+            , request.getSensorType()
+            , timestamp.orElse(System.currentTimeMillis()));
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/metron/blob/e59059bd/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/update/PatchRequest.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/update/PatchRequest.java
b/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/update/PatchRequest.java
index 77f5958..6650666 100644
--- a/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/update/PatchRequest.java
+++ b/metron-platform/metron-indexing/src/main/java/org/apache/metron/indexing/dao/update/PatchRequest.java
@@ -19,10 +19,11 @@ package org.apache.metron.indexing.dao.update;
 
 import com.fasterxml.jackson.databind.JsonNode;
 
+import java.util.List;
 import java.util.Map;
 
 public class PatchRequest {
-  JsonNode patch;
+  List<Map<String, Object>> patch;
   Map<String, Object> source;
   String guid;
   String sensorType;
@@ -54,11 +55,11 @@ public class PatchRequest {
    * </pre>
    * @return
    */
-  public JsonNode getPatch() {
+  public List<Map<String, Object>> getPatch() {
     return patch;
   }
 
-  public void setPatch(JsonNode patch) {
+  public void setPatch(List<Map<String, Object>> patch) {
     this.patch = patch;
   }
 


Mime
View raw message