json pointer rewrite restriction parameter
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanupFilter.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanupFilter.java
index 74a5d06..c839afc 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanupFilter.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionCleanupFilter.java
@@ -12,8 +12,7 @@
* @author hanl
* @date 28/07/2015
*/
-
-public class CollectionCleanupFilter implements RewriteTask.RewriteQuery {
+public class CollectionCleanupFilter implements RewriteTask.RewriteNodeAt {
// track path to operand
@Deprecated
@@ -26,11 +25,11 @@
@Override
public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
User user) {
- if (node.has("collection")) {
- JsonNode coll = node.rawNode().path("collection");
- process(coll);
- }
- return null;
+ // if (node.has("collection")) {
+ // JsonNode coll = node.rawNode().path("collection");
+ // process(coll);
+ // }
+ return process(node.rawNode());
}
private JsonNode process(JsonNode root) {
@@ -89,4 +88,9 @@
public JsonNode postProcess(KoralNode node) {
return null;
}
+
+ @Override
+ public String at() {
+ return "/collection";
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionConstraint.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionConstraint.java
index 4e5edf2..d1630dd 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionConstraint.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionConstraint.java
@@ -13,12 +13,11 @@
* @date 03/07/2015
*/
// todo: test
-public class CollectionConstraint implements RewriteTask.RewriteNode {
+public class CollectionConstraint implements RewriteTask.IterableRewriteAt {
@Override
public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
User user) {
-
if (node.get("@type").equals("koral:doc")) {
if (node.get("key").equals("corpusID") && !check(node, user)) {
node.removeNode();
@@ -52,4 +51,13 @@
return corpus != null;
}
+ @Override
+ public JsonNode postProcess(KoralNode node) {
+ return null;
+ }
+
+ @Override
+ public String path() {
+ return "collection";
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java
deleted file mode 100644
index d5896e7..0000000
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/DocMatchRewrite.java
+++ /dev/null
@@ -1,55 +0,0 @@
-package de.ids_mannheim.korap.resource.rewrite;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import de.ids_mannheim.korap.config.BeanConfiguration;
-import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.handlers.DocumentDao;
-import de.ids_mannheim.korap.resources.Document;
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.CacheManager;
-import net.sf.ehcache.Element;
-
-/**
- * @author hanl
- * @date 12/11/2015
- */
-public class DocMatchRewrite implements RewriteTask.RewriteAfter {
-
- private DocumentDao docDao;
- private Cache cache;
-
- // todo: cache already matched documents with ehcache!
-
- public DocMatchRewrite() {
- this.docDao = new DocumentDao(
- BeanConfiguration.getBeans().getPersistenceClient());
- this.cache = CacheManager.getInstance().getCache("documents");
- }
-
- @Override
- public JsonNode postProcess(KoralNode node) {
- Document doc = null;
-
- // todo: check matches for parent
- if (node.has("docID")) {
- String docID = node.get("docID");
- Element e = this.cache.get(docID);
- if (e == null) {
- try {
- doc = docDao.findbyId(docID, null);
- }catch (KustvaktException ex) {
- ex.printStackTrace();
- // todo: what to do here?!
- }
-
- if (doc != null)
- this.cache.put(new Element(docID, doc));
- }else
- doc = (Document) e.getObjectValue();
-
- if (doc != null && doc.isDisabled())
- node.removeNode();
- }
- return node.rawNode();
- }
-}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
index 14482d1..eebc92c 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/FoundryInject.java
@@ -9,11 +9,8 @@
* @author hanl
* @date 30/06/2015
*/
-public class FoundryInject implements RewriteTask.RewriteNode {
+public class FoundryInject implements RewriteTask.IterableRewriteAt {
- public FoundryInject() {
- super();
- }
@Override
public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
@@ -35,4 +32,14 @@
}
return node.rawNode();
}
+
+ @Override
+ public String path() {
+ return "query";
+ }
+
+ @Override
+ public JsonNode postProcess(KoralNode node) {
+ return null;
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
index f35597f..102da60 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/IdWriter.java
@@ -49,4 +49,5 @@
return wrap.path("key").asText();
return null;
}
+
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
index 585d0bf..94dd6c7 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/KoralNode.java
@@ -18,18 +18,25 @@
private JsonNode node;
private KoralRewriteBuilder builder;
private boolean remove;
+ private final String target;
- private KoralNode(JsonNode node) {
+ private KoralNode(String target, JsonNode node) {
this.node = node;
+ this.target = target;
this.builder = new KoralRewriteBuilder();
this.remove = false;
}
public static KoralNode wrapNode(JsonNode node) {
- return new KoralNode(node) {
+ return new KoralNode(null, node) {
};
}
+ @Override
+ public String toString() {
+ return this.node.toString();
+ }
+
@Deprecated
public boolean setNode(Object path) {
JsonNode n = null;
@@ -104,7 +111,6 @@
return false;
}
-
public JsonNode rawNode() {
return this.node;
}
@@ -117,6 +123,10 @@
return this.remove;
}
+ public final String target() {
+ return this.target;
+ }
+
//todo: 21.10.15 -- redo with better return policies!
private static class KoralRewriteBuilder {
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
index 8dddf4f..00aa844 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/MetaConstraint.java
@@ -8,15 +8,13 @@
* @author hanl
* @date 04/07/2015
*/
-public class MetaConstraint implements RewriteTask.RewriteQuery {
+public class MetaConstraint implements RewriteTask.RewriteNodeAt {
- public MetaConstraint() {
- super();
- }
@Override
public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
User user) {
+ // redundant
if (node.rawNode().has("meta")) {
JsonNode meta = node.rawNode().path("meta");
//todo: check meta parameter
@@ -29,4 +27,10 @@
public JsonNode postProcess(KoralNode node) {
return null;
}
+
+
+ @Override
+ public String at() {
+ return "/meta";
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/PublicCollection.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/PublicCollection.java
index 350bebc..af6470d 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/PublicCollection.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/PublicCollection.java
@@ -15,7 +15,7 @@
* @date 04/07/2015
*/
// todo: 11.11.15
-public class PublicCollection implements RewriteTask.RewriteQuery {
+public class PublicCollection implements RewriteTask.RewriteNodeAt {
public PublicCollection() {
super();
@@ -26,6 +26,7 @@
public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
User user) {
JsonNode subnode = node.rawNode();
+ // todo: test
if (!subnode.at("/collection").findValuesAsText("key")
.contains("corpusID")) {
//todo: inject public collection node
@@ -54,4 +55,9 @@
public JsonNode postProcess(KoralNode node) {
return null;
}
+
+ @Override
+ public String at() {
+ return "collection";
+ }
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
index 8be0799..dc74cab 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
@@ -9,10 +9,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
+import java.util.*;
/**
* @author hanl
@@ -23,9 +20,12 @@
public class RewriteHandler {
private static Logger jlog = KustvaktLogger.getLogger(RewriteHandler.class);
- private Collection<RewriteTask> node_processors;
+ private Collection<RewriteTask.IterableRewriteAt> node_processors;
private Collection<RewriteTask.RewriteKoralToken> token_node_processors;
- private Collection<RewriteTask.RewriteQuery> query_processors;
+ private Collection<RewriteTask.RewriteNodeAt> query_processors;
+
+ // private Collection<RewriteTask.RewriteNode2> fixed_nodes;
+ // private Collection<RewriteTask.IterableRewrite> iterable_nodes;
private Set<Class> failed_task_registration;
@@ -44,17 +44,28 @@
if (rewriter instanceof RewriteTask.RewriteKoralToken)
return this.token_node_processors
.add((RewriteTask.RewriteKoralToken) rewriter);
- else if (rewriter instanceof RewriteTask.RewriteQuery)
+ else if (rewriter instanceof RewriteTask.RewriteNodeAt)
return this.query_processors
- .add((RewriteTask.RewriteQuery) rewriter);
- else if (rewriter instanceof RewriteTask.RewriteBefore
- | rewriter instanceof RewriteTask.RewriteAfter)
- return this.node_processors.add(rewriter);
+ .add((RewriteTask.RewriteNodeAt) rewriter);
+ else if (rewriter instanceof RewriteTask.IterableRewriteAt)
+ return this.node_processors
+ .add((RewriteTask.IterableRewriteAt) rewriter);
this.failed_task_registration.add(rewriter.getClass());
return false;
}
+ public boolean addProcessor2(RewriteTask rewriteTask) {
+ // if (rewriteTask instanceof RewriteTask.RewriteNode2)
+ // return this.fixed_nodes.add((RewriteTask.RewriteNode2) rewriteTask);
+ // else if (rewriteTask instanceof RewriteTask.IterableRewrite)
+ // return this.iterable_nodes
+ // .add((RewriteTask.IterableRewrite) rewriteTask);
+ // else if (rewriteTask instanceof RewriteTask.RewriteBefore)
+ // gets the entire pre processed query injected.
+ return false;
+ }
+
public final Collection<Class> getFailedHandlers() {
return this.failed_task_registration;
}
@@ -97,32 +108,31 @@
this.token_node_processors.clear();
}
- private boolean process(JsonNode root, User user, boolean post) {
+ private boolean process(String name, JsonNode root, User user,
+ boolean post) {
if (root.isObject()) {
if (root.has("operands")) {
JsonNode ops = root.at("/operands");
Iterator<JsonNode> it = ops.elements();
while (it.hasNext()) {
- JsonNode n = it.next();
- if (process(n, user, post)) {
+ JsonNode next = it.next();
+ if (process(name, next, user, post))
it.remove();
- }
}
-
}else if (root.path("@type").asText().equals("koral:token")) {
// todo: koral:token nodes cannot be flagged for deletion --> creates the possibility for empty koral:token nodes
- processNode(KoralNode.wrapNode(root), user,
- this.token_node_processors, post);
- return process(root.path("wrap"), user, post);
+ // processIterableNode(KoralNode.wrapNode(root), user,
+ // this.token_node_processors, post);
+ return process(name, root.path("wrap"), user, post);
}else {
- return processNode(KoralNode.wrapNode(root), user,
+ return processNode(name, KoralNode.wrapNode(root), user,
this.node_processors, post);
}
}else if (root.isArray()) {
Iterator<JsonNode> it = root.elements();
while (it.hasNext()) {
- JsonNode n = it.next();
- if (process(n, user, post))
+ JsonNode next = it.next();
+ if (process(name, next, user, post))
it.remove();
}
}
@@ -131,10 +141,12 @@
public JsonNode preProcess(JsonNode root, User user) {
boolean post = false;
- for (JsonNode n : root)
- process(n, user, post);
- processNode(KoralNode.wrapNode(root), user, this.query_processors,
- post);
+ Iterator<Map.Entry<String, JsonNode>> it = root.fields();
+ while (it.hasNext()) {
+ Map.Entry<String, JsonNode> next = it.next();
+ process(next.getKey(), next.getValue(), user, post);
+ }
+ processFixedNode(root, user, this.query_processors, post);
return root;
}
@@ -144,10 +156,12 @@
public JsonNode postProcess(JsonNode root, User user) {
boolean post = true;
- for (JsonNode n : root)
- process(n, user, post);
- processNode(KoralNode.wrapNode(root), user, this.query_processors,
- post);
+ Iterator<Map.Entry<String, JsonNode>> it = root.fields();
+ while (it.hasNext()) {
+ Map.Entry<String, JsonNode> next = it.next();
+ process(next.getKey(), next.getValue(), user, post);
+ }
+ processFixedNode(root, user, this.query_processors, post);
return root;
}
@@ -162,22 +176,44 @@
* @return boolean true if node is to be removed from parent! Only applies if parent is an array node
*/
// todo: integrate notifications into system!
- private boolean processNode(KoralNode node, User user,
- Collection<? extends RewriteTask> tasks, boolean post) {
+ private boolean processNode(String rootNode, KoralNode node, User user,
+ Collection<? extends RewriteTask.IterableRewriteAt> tasks,
+ boolean post) {
for (RewriteTask task : tasks) {
- if (jlog.isDebugEnabled()) {
- jlog.debug("running processor on node " + node);
- jlog.debug("on processor " + task.getClass().toString());
+ jlog.debug("running processor on node: " + node);
+ jlog.debug("on processor: " + task.getClass().toString());
+
+ if (task instanceof RewriteTask.IterableRewriteAt) {
+ RewriteTask.IterableRewriteAt rw = (RewriteTask.IterableRewriteAt) task;
+ if (rw.path() != null && !rw.path().equals(rootNode)) {
+ jlog.debug("skipping node: " + node);
+ continue;
+ }
}
- if (task instanceof RewriteTask.RewriteBefore)
+
+ if (!post && task instanceof RewriteTask.RewriteBefore)
((RewriteTask.RewriteBefore) task)
.preProcess(node, this.config, user);
- if (post && task instanceof RewriteTask.RewriteAfter)
+ else if (task instanceof RewriteTask.RewriteAfter)
((RewriteTask.RewriteAfter) task).postProcess(node);
+
if (node.isRemove())
break;
}
return node.isRemove();
}
+ private void processFixedNode(JsonNode node, User user,
+ Collection<RewriteTask.RewriteNodeAt> tasks, boolean post) {
+ for (RewriteTask.RewriteNodeAt task : tasks) {
+ if (!node.at(task.at()).isMissingNode()) {
+ if (!post)
+ task.preProcess(KoralNode.wrapNode(node.at(task.at())),
+ this.config, user);
+ else
+ task.postProcess(KoralNode.wrapNode(node.at(task.at())));
+ }
+ }
+ }
+
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
index 17683ce..7a7189c 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteTask.java
@@ -25,12 +25,21 @@
JsonNode postProcess(KoralNode node);
}
+ interface RewriteNodeAt extends RewriteBefore, RewriteAfter {
+ String at();
+ }
+
+ interface IterableRewriteAt extends RewriteBefore, RewriteAfter {
+ String path();
+ }
+
/**
* query rewrites get injected the entire query from root containing all child nodes
* <p/>
* {@link RewriteQuery} does not allow the deletion of the root node or subnode through KoralNode.
* The {@link de.ids_mannheim.korap.resource.rewrite.RewriteHandler} will igonore respecitve invalid requests
*/
+ @Deprecated
interface RewriteQuery extends RewriteBefore, RewriteAfter {
}
@@ -40,6 +49,7 @@
* <p/>
* {@link RewriteNode} rewrite supports the deletion of the respective node by simply setting the node invalid in KoralNode
*/
+ @Deprecated
interface RewriteNode extends RewriteBefore {
}
diff --git a/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java b/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
index e96b3fe..2e56999 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/rewrite/TreeConstraint.java
@@ -42,7 +42,7 @@
* @author hanl
* @date 02/07/2015
*/
-public class TreeConstraint implements RewriteTask.RewriteQuery {
+public class TreeConstraint implements RewriteTask.RewriteNodeAt {
private String pointer;
@@ -62,4 +62,9 @@
public JsonNode postProcess(KoralNode node) {
return null;
}
+
+ @Override
+ public String at() {
+ return null;
+ }
}
diff --git a/src/test/java/CollectionRewriteTest.java b/src/test/java/CollectionRewriteTest.java
index 60aff50..d115455 100644
--- a/src/test/java/CollectionRewriteTest.java
+++ b/src/test/java/CollectionRewriteTest.java
@@ -36,13 +36,13 @@
BeanConfiguration.closeApplication();
}
+ @Deprecated
@Test
public void test2() {
Pattern p = Pattern.compile("([\\.\\w]+)\\((.+)\\)");
String cl = de.ids_mannheim.korap.security.ac.SecurityManager.class
.getCanonicalName();
Matcher m = p.matcher(cl);
- System.out.println("FOUND SOMETHING?! " + m.find());
while (m.find())
System.out.println("group 1 " + m.group(1));
@@ -56,8 +56,8 @@
s.setQuery(simple_add_query, "poliqarp");
s.setCollection("textClass=politik & corpusID=WPD");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
assert node != null;
assert node.at("/collection/operands").size() == 1;
}
@@ -70,9 +70,9 @@
s.setQuery(simple_add_query, "poliqarp");
s.setCollection("corpusID=BRZ13 & corpusID=WPD");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
- System.out.println("RESULTING REWR NODE " + node);
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
+ // System.out.println("RESULTING REWR NODE " + node);
assert node != null;
assert node.at("/collection/operands").size() == 0;
}
@@ -86,10 +86,9 @@
s.setCollection(
"(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
- System.out.println("COLLECTION NODE " + result);
assert node != null;
assert node.at("/collection/operands/0/@type").asText()
.equals("koral:docGroup");
@@ -108,8 +107,9 @@
s.setCollection(
"(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
+
assert node != null;
assert node.at("/collection/@type").asText().equals("koral:docGroup");
assert node.at("/collection/operands").size() == 2;
@@ -128,8 +128,8 @@
s.setQuery(simple_add_query, "poliqarp");
s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
assert node != null;
assert node.at("/collection/@type").asText().equals("koral:doc");
}
@@ -144,8 +144,8 @@
s.setCollection(
"(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
assert node != null;
assert node.at("/collection/@type").asText().equals("koral:doc");
@@ -162,13 +162,15 @@
s.setCollection(
"(docID=random & textClass=Wissenschaft) & corpusID=WPD");
String result = s.toJSON();
- JsonNode node = JsonUtils.readTree(
- handler.preProcess(result, User.UserFactory.getUser("test_user")));
+ JsonNode node = JsonUtils.readTree(handler.preProcess(result,
+ User.UserFactory.getUser("test_user")));
System.out.println("original node " + result);
System.out.println("result node " + node);
assert node != null;
assert node.at("/collection/@type").asText().equals("koral:docGroup");
assert node.at("/collection/operands").size() == 2;
+ assert node.at("/collection/operands/0/@type").asText()
+ .equals("koral:doc");
}
}