rewrite post processor; refactoring; tests
diff --git a/src/main/.DS_Store b/src/main/.DS_Store
deleted file mode 100644
index 4f51b81..0000000
--- a/src/main/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store
deleted file mode 100644
index 175efa1..0000000
--- a/src/main/java/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/src/main/java/de/.DS_Store b/src/main/java/de/.DS_Store
deleted file mode 100644
index dcbce40..0000000
--- a/src/main/java/de/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/src/main/java/de/ids_mannheim/.DS_Store b/src/main/java/de/ids_mannheim/.DS_Store
deleted file mode 100644
index f61ad44..0000000
--- a/src/main/java/de/ids_mannheim/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/src/main/java/de/ids_mannheim/korap/.DS_Store b/src/main/java/de/ids_mannheim/korap/.DS_Store
deleted file mode 100644
index f8811c7..0000000
--- a/src/main/java/de/ids_mannheim/korap/.DS_Store
+++ /dev/null
Binary files differ
diff --git a/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
index af98785..ae5aeac 100644
--- a/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
@@ -10,6 +10,7 @@
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -20,7 +21,7 @@
  */
 public class BeanConfiguration {
 
-    private static final String config_file = "light-config.xml";
+    private static final String CONFIG_FILE = "light-config.xml";
     public static final String KUSTVAKT_DB = "kustvakt_db";
 
     public static final String KUSTVAKT_ENCRYPTION = "kustvakt_encryption";
@@ -71,14 +72,25 @@
         if (!hasContext()) {
             ApplicationContext context;
             if (files.length == 0)
-                context = new ClassPathXmlApplicationContext(config_file);
+                context = new ClassPathXmlApplicationContext(CONFIG_FILE);
             else
                 context = new ClassPathXmlApplicationContext(files);
 
             BeanConfiguration.beans = new BeanHolderHelper(context);
+            setManualBeans();
         }
     }
 
+    private static void setManualBeans() {
+        if (getBeans().getPolicyDbProvider() != null
+                && getBeans().getEncryption() != null
+                && getBeans().getResourceProvider() != null)
+            de.ids_mannheim.korap.security.ac.SecurityManager
+                    .setProviders(getBeans().getPolicyDbProvider(),
+                            getBeans().getEncryption(),
+                            Arrays.asList(getBeans().getResourceProvider()));
+    }
+
     public static void loadFileContext(String filepath) {
         if (!hasContext()) {
             ApplicationContext context = new FileSystemXmlApplicationContext(
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java b/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
index 1cef65f..fe192db 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/DocumentDao.java
@@ -49,7 +49,7 @@
                         public Document extractData(ResultSet rs)
                                 throws SQLException, DataAccessException {
                             Document doc = new Document(
-                                    rs.getString("persistentID"));
+                                    rs.getString("persistent_id"));
                             doc.setId(rs.getInt("id"));
                             doc.setCreated(
                                     rs.getTimestamp("created").getTime());
@@ -67,7 +67,7 @@
     public Document findbyId(String id, User user) throws KustvaktException {
         MapSqlParameterSource s = new MapSqlParameterSource();
         s.addValue("id", id);
-        String sql = "select * from doc_store where persistentID=:id";
+        String sql = "select id, persistent_id, strftime('%s', created) as created from doc_store where persistent_id=:id";
 
         try {
             return this.jdbcTemplate
@@ -76,15 +76,15 @@
                         public Document extractData(ResultSet rs)
                                 throws SQLException, DataAccessException {
                             Document doc = new Document(
-                                    rs.getString("persistentID"));
+                                    rs.getString("persistent_id"));
                             doc.setId(rs.getInt("id"));
-                            doc.setCreated(
-                                    rs.getTimestamp("created").getTime());
+                            doc.setCreated(rs.getLong("created"));
                             doc.setDisabled(rs.getBoolean("disabled"));
                             return doc;
                         }
                     });
         }catch (DataAccessException e) {
+            e.printStackTrace();
             throw new KustvaktException(StatusCodes.CONNECTION_ERROR);
         }
     }
@@ -101,7 +101,7 @@
         MapSqlParameterSource source = new MapSqlParameterSource();
         source.addValue("pid", document.getPersistentID());
         source.addValue("dis", BooleanUtils.getBoolean(document.isDisabled()));
-        final String sql = "UPDATE doc_store set disabled=:dis where persistentID=:pid;";
+        final String sql = "UPDATE doc_store set disabled=:dis where persistent_id=:pid;";
         return this.jdbcTemplate.update(sql, source);
     }
 
@@ -117,7 +117,7 @@
         source.addValue("corpus", corpus + "%");
         source.addValue("offset", (offset * index));
         source.addValue("limit", offset);
-        final String sql = "select * from doc_store where (persistentID like :corpus) limit :offset, :limit";
+        final String sql = "select id, persistent_id, disabled, strftime('%s', created) as created from doc_store where (persistent_id like :corpus) limit :offset, :limit";
         try {
             return this.jdbcTemplate
                     .query(sql, source, new RowMapper<Document>() {
@@ -125,8 +125,9 @@
                         public Document mapRow(ResultSet rs, int rowNum)
                                 throws SQLException {
                             Document doc = new Document(
-                                    rs.getString("persistentID"));
+                                    rs.getString("persistent_id"));
                             doc.setId(rs.getInt("id"));
+                            doc.setCreated(rs.getLong("created"));
                             doc.setDisabled(rs.getBoolean("disabled"));
                             return doc;
                         }
@@ -141,11 +142,10 @@
         MapSqlParameterSource s = new MapSqlParameterSource();
         s.addValue("corpus", corpus + "%");
         s.addValue("dis", BooleanUtils.getBoolean(disabled));
-        String sql = "SELECT persistentID FROM doc_store WHERE (persistentID like :corpus) AND disabled=:dis;";
+        String sql = "SELECT persistent_id FROM doc_store WHERE (persistent_id like :corpus) AND disabled=:dis;";
         try {
             return this.jdbcTemplate.queryForList(sql, s, String.class);
         }catch (DataAccessException e) {
-            e.printStackTrace();
             throw new KustvaktException(StatusCodes.CONNECTION_ERROR);
         }
     }
@@ -159,10 +159,11 @@
         s.addValue("corpus", resource.getCorpus());
         s.addValue("dis", BooleanUtils.getBoolean(resource.isDisabled()));
 
-        String sql = "INSERT INTO doc_store (persistentID, disabled) VALUES (:id, :dis)";
+        String sql = "INSERT INTO doc_store (persistent_id, disabled) VALUES (:id, :dis)";
         try {
             return this.jdbcTemplate.update(sql, s);
         }catch (DataAccessException e) {
+            e.printStackTrace();
             throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT,
                     "illegal argument given", resource.getPersistentID());
         }
@@ -172,7 +173,7 @@
     public int deleteResource(String id, User user) throws KustvaktException {
         MapSqlParameterSource s = new MapSqlParameterSource();
         s.addValue("id", id);
-        String sql = "delete from doc_store where persistentID=:id;";
+        String sql = "delete from doc_store where persistent_id=:id;";
         try {
             return this.jdbcTemplate.update(sql, s);
         }catch (DataAccessException e) {
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 c243f73..74a5d06 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,8 @@
  * @author hanl
  * @date 28/07/2015
  */
-//todo: 21.10.15
-public class CollectionCleanupFilter extends RewriteTask.RewriteQuery {
+
+public class CollectionCleanupFilter implements RewriteTask.RewriteQuery {
 
     // track path to operand
     @Deprecated
@@ -24,7 +24,7 @@
     }
 
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
         if (node.has("collection")) {
             JsonNode coll = node.rawNode().path("collection");
@@ -84,4 +84,9 @@
         }
         return JsonUtils.createArrayNode();
     }
+
+    @Override
+    public JsonNode postProcess(KoralNode node) {
+        return null;
+    }
 }
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 4b753cb..4e5edf2 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,35 +13,42 @@
  * @date 03/07/2015
  */
 // todo: test
-public class CollectionConstraint extends RewriteTask.RewriteNode {
+public class CollectionConstraint implements RewriteTask.RewriteNode {
 
     @Override
-    public JsonNode rewrite(KoralNode koralnode, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
-        JsonNode node = koralnode.rawNode();
-        if (node.at("/@type").asText().equals("koral:doc")) {
-            if (node.at("/key").asText().equals("corpusID") && !check(koralnode,
-                    user)) {
-                koralnode.removeNode();
+
+        if (node.get("@type").equals("koral:doc")) {
+            if (node.get("key").equals("corpusID") && !check(node, user)) {
+                node.removeNode();
                 // todo: add message that node was removed!
             }
         }
-        return node;
+        return node.rawNode();
     }
 
+    /**
+     * @param node
+     * @param user
+     * @return boolean if true access granted
+     */
     private boolean check(KoralNode node, User user) {
+        // todo: can be used to circumvent access control!
         if (user == null)
             return true;
 
-        String id = node.rawNode().at("/value").asText();
+        String id = node.get("value");
         KustvaktResource corpus;
         try {
             SecurityManager m = SecurityManager
                     .findbyId(id, user, Corpus.class);
             corpus = m.getResource();
-        }catch (KustvaktException e) {
+        }catch (RuntimeException | KustvaktException e) {
+            e.printStackTrace();
             return false;
         }
+
         return corpus != null;
     }
 
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 d44606e..14482d1 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,14 +9,14 @@
  * @author hanl
  * @date 30/06/2015
  */
-public class FoundryInject extends RewriteTask.RewriteNode {
+public class FoundryInject implements RewriteTask.RewriteNode {
 
     public FoundryInject() {
         super();
     }
 
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
         LayerMapper mapper;
         if (user != null)
@@ -24,15 +24,14 @@
         else
             mapper = new LayerMapper(config);
 
-        if (node.rawNode().path("@type").asText().equals("koral:term") && !node
-                .rawNode().has("foundry")) {
+        if (node.get("@type").equals("koral:term") && !node.has("foundry")) {
             String layer;
-            if (node.rawNode().has("layer"))
-                layer = node.rawNode().path("layer").asText();
+            if (node.has("layer"))
+                layer = node.get("layer");
             else
-                layer = node.rawNode().path("key").asText();
+                layer = node.get("key");
             String foundry = mapper.findFoundry(layer);
-            node.set("foundry", foundry);
+            node.put("foundry", foundry);
         }
         return node.rawNode();
     }
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 5e4bece..f35597f 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
@@ -9,8 +9,7 @@
  * @author hanl
  * @date 25/09/2015
  */
-// todo: 20.10.15
-public class IdWriter extends RewriteTask.RewriteNode {
+public class IdWriter implements RewriteTask.RewriteKoralToken {
 
     private int counter;
 
@@ -21,14 +20,18 @@
     }
 
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
-        JsonNode result = node.rawNode();
-        if (result.path("@type").asText().equals("koral:token"))
-            addId(result);
-        return result;
+        if (node.get("@type").equals("koral:token")) {
+            String s = extractToken(node.rawNode());
+            if (s != null && !s.isEmpty())
+                node.put("idn", s + "_" + counter++);
+        }
+
+        return node.rawNode();
     }
 
+    @Deprecated
     private JsonNode addId(JsonNode node) {
         if (node.isObject()) {
             ObjectNode o = (ObjectNode) node;
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 f4580ad..017de70 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
@@ -17,12 +17,12 @@
 public abstract class KoralNode {
     private JsonNode node;
     private KoralRewriteBuilder builder;
-    private boolean toRemove;
+    private boolean remove;
 
     private KoralNode(JsonNode node) {
         this.node = node;
         this.builder = new KoralRewriteBuilder();
-        this.toRemove = false;
+        this.remove = false;
     }
 
     public static KoralNode wrapNode(JsonNode node) {
@@ -30,7 +30,21 @@
         };
     }
 
-    public void set(String name, Object value) {
+    public boolean setNode(Object path) {
+        JsonNode n = null;
+        if (this.node.isObject() && this.node.has((String) path))
+            n = this.node.path((String) path);
+        else if (this.node.isArray() && this.node.has((int) path))
+            n = this.node.path((int) path);
+
+        if (n != null) {
+            this.node = n;
+            return true;
+        }
+        return false;
+    }
+
+    public void put(String name, Object value) {
 
         if (this.node.isObject() && this.node.path(name).isMissingNode()) {
             ObjectNode node = (ObjectNode) this.node;
@@ -71,26 +85,30 @@
         }
     }
 
+    public String get(String name) {
+        if (this.node.isObject())
+            return this.node.path(name).asText();
+        return null;
+    }
+
     public boolean has(Object ident) {
         if (ident instanceof String)
             return this.node.has((String) ident);
-        if (ident instanceof Integer)
+        else if (ident instanceof Integer)
             return this.node.has((int) ident);
         return false;
     }
 
-
-
     public JsonNode rawNode() {
         return this.node;
     }
 
     public void removeNode() {
-        this.toRemove = true;
+        this.remove = true;
     }
 
     public boolean toRemove() {
-        return this.toRemove;
+        return this.remove;
     }
 
     //todo: 21.10.15 -- redo with better return policies!
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 bf6f2e1..8dddf4f 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,14 +8,14 @@
  * @author hanl
  * @date 04/07/2015
  */
-public class MetaConstraint extends RewriteTask.RewriteQuery {
+public class MetaConstraint implements RewriteTask.RewriteQuery {
 
     public MetaConstraint() {
         super();
     }
 
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
         if (node.rawNode().has("meta")) {
             JsonNode meta = node.rawNode().path("meta");
@@ -24,4 +24,9 @@
         }
         return node.rawNode();
     }
+
+    @Override
+    public JsonNode postProcess(KoralNode node) {
+        return null;
+    }
 }
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 d8a35dc..350bebc 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
@@ -14,7 +14,8 @@
  * @author hanl
  * @date 04/07/2015
  */
-public class PublicCollection extends RewriteTask.RewriteQuery {
+// todo: 11.11.15
+public class PublicCollection implements RewriteTask.RewriteQuery {
 
     public PublicCollection() {
         super();
@@ -22,7 +23,7 @@
 
     // todo: where to inject the array node into? --> super group with and relation plus subgroup with ids and or operation
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
         JsonNode subnode = node.rawNode();
         if (!subnode.at("/collection").findValuesAsText("key")
@@ -48,4 +49,9 @@
         JsonNode node = CollectionQueryBuilder3.Utils.buildDocGroup();
         return node;
     }
+
+    @Override
+    public JsonNode postProcess(KoralNode node) {
+        return null;
+    }
 }
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 8af6eae..49cef0c 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
@@ -1,7 +1,6 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
 import com.fasterxml.jackson.databind.JsonNode;
-import de.ids_mannheim.korap.config.KustvaktClassLoader;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.utils.JsonUtils;
@@ -18,36 +17,60 @@
  * @author hanl
  * @date 30/06/2015
  */
+// todo: do post processing!
 //todo: load rewritenode and rewritequery automatically from classpath by default, but namespaced from package
 public class RewriteHandler {
 
-    private static Logger jlog = KustvaktLogger.initiate(RewriteHandler.class);
-    private Collection<RewriteTask.RewriteNode> node_processors;
+    private static Logger jlog = KustvaktLogger.getLogger(RewriteHandler.class);
+    private Collection<RewriteTask> node_processors;
+    private Collection<RewriteTask.RewriteKoralToken> token_node_processors;
     private Collection<RewriteTask.RewriteQuery> query_processors;
+
     private KustvaktConfiguration config;
 
     // fixme: make default constructor with configuration!
     public RewriteHandler(KustvaktConfiguration config) {
         this.config = config;
         this.node_processors = new HashSet<>();
+        this.token_node_processors = new HashSet<>();
         this.query_processors = new HashSet<>();
-        KustvaktClassLoader.loadSubTypes(RewriteTask.RewriteNode.class);
     }
 
-    public void add(RewriteTask.RewriteNode rewriter) {
-        this.node_processors.add(rewriter);
+    public boolean addProcessor(RewriteTask rewriter) {
+        if (rewriter instanceof RewriteTask.RewriteKoralToken)
+            return this.token_node_processors
+                    .add((RewriteTask.RewriteKoralToken) rewriter);
+        else if (rewriter instanceof RewriteTask.RewriteQuery)
+            return this.query_processors
+                    .add((RewriteTask.RewriteQuery) rewriter);
+        else if (rewriter instanceof RewriteTask.RewriteBefore
+                | rewriter instanceof RewriteTask.RewriteAfter)
+            return this.node_processors.add(rewriter);
+        //        else if (rewriter instanceof RewriteTask.RewriteAfter)
+        //            return this.node_post_processors
+        //                    .add((RewriteTask.RewriteAfter) rewriter);
+        return false;
     }
 
-    public void add(RewriteTask.RewriteQuery rewriter) {
-        this.query_processors.add(rewriter);
+    @Override
+    public String toString() {
+        StringBuilder b = new StringBuilder();
+        b.append("--------------------------");
+        b.append("pre/post: " + this.node_processors.toString()).append("\n")
+                .append("\n")
+                .append("query: " + this.query_processors.toString())
+                .append("\n")
+                .append("koraltoken: " + this.token_node_processors.toString());
+        b.append("---------------------------");
+        return b.toString();
     }
 
     /**
-     * expects extended RewriteNode/Query class with default config constructor
+     * expects extended RewriteNode/Query class with empty default constructor
      *
      * @param rewriter
+     * @return boolean if rewriter class was successfully added to rewrite handler!
      */
-    //todo: 21.10.15
     public boolean add(Class<? extends RewriteTask> rewriter) {
         RewriteTask task;
         try {
@@ -57,69 +80,98 @@
                 | IllegalAccessException | InstantiationException e) {
             return false;
         }
-
-        if (task instanceof RewriteTask.RewriteNode) {
-            this.node_processors.add((RewriteTask.RewriteNode) task);
-            return true;
-        }
-
-        if (task instanceof RewriteTask.RewriteQuery) {
-            this.query_processors.add((RewriteTask.RewriteQuery) task);
-            return true;
-        }
-        return false;
+        return addProcessor(task);
     }
 
     public void clear() {
         this.node_processors.clear();
         this.query_processors.clear();
+        this.token_node_processors.clear();
     }
 
-    private boolean process(JsonNode root, User user) {
+    private boolean process(JsonNode root, User user, boolean post) {
         if (root.isObject()) {
             if (root.has("operands")) {
-                JsonNode node = root.at("/operands");
-                Iterator<JsonNode> it = node.elements();
+                JsonNode ops = root.at("/operands");
+                Iterator<JsonNode> it = ops.elements();
                 while (it.hasNext()) {
                     JsonNode n = it.next();
-                    if (!process(n, user))
+                    if (process(n, user, post)) {
                         it.remove();
+                    }
                 }
-            }else if (root.has("wrap")) {
-                JsonNode node = root.at("/wrap");
-                //todo: remove object nodes as well
-                process(node, user);
-            }else
-                return processNode(root, user, this.node_processors);
+
+            }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);
+            }else {
+                return processNode(KoralNode.wrapNode(root), user,
+                        this.node_processors, post);
+            }
+        }else if (root.isArray()) {
+            //todo: test!
+            Iterator<JsonNode> it = root.elements();
+            while (it.hasNext()) {
+                JsonNode n = it.next();
+                if (process(n, user, post)) {
+                    it.remove();
+                }
+            }
         }
-        return true;
+        return false;
     }
 
-    public JsonNode apply(JsonNode root, User user) {
+    public JsonNode preProcess(JsonNode root, User user) {
+        boolean post = false;
         for (JsonNode n : root)
-            process(n, user);
-        processNode(root, user, this.query_processors);
+            process(n, user, post);
+        processNode(KoralNode.wrapNode(root), user, this.query_processors,
+                post);
         return root;
     }
 
-    public String apply(String json, User user) {
-        return JsonUtils.toJSON(apply(JsonUtils.readTree(json), user));
+    public String preProcess(String json, User user) {
+        return JsonUtils.toJSON(preProcess(JsonUtils.readTree(json), user));
     }
 
+    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);
+        return root;
+    }
+
+    public String postProcess(String json, User user) {
+        return JsonUtils.toJSON(postProcess(JsonUtils.readTree(json), user));
+    }
+
+    /**
+     * @param node
+     * @param user
+     * @param tasks
+     * @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(JsonNode node, User user,
-            Collection<? extends RewriteTask> tasks) {
-        KoralNode knode = KoralNode.wrapNode(node);
+    private boolean processNode(KoralNode node, User user,
+            Collection<? extends RewriteTask> tasks, boolean post) {
         for (RewriteTask task : tasks) {
             if (jlog.isDebugEnabled()) {
-                jlog.debug("running node in processor " + node);
+                jlog.debug("running processor on node " + node);
                 jlog.debug("on processor " + task.getClass().toString());
             }
-            task.rewrite(knode, this.config, user);
-            if (knode.toRemove())
+            if (task instanceof RewriteTask.RewriteBefore)
+                ((RewriteTask.RewriteBefore) task)
+                        .preProcess(node, this.config, user);
+            if (post && task instanceof RewriteTask.RewriteAfter)
+                ((RewriteTask.RewriteAfter) task).postProcess(node);
+            if (node.toRemove())
                 break;
         }
-        return !knode.toRemove();
+        return node.toRemove();
     }
 
 }
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 07bebc4..17683ce 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
@@ -3,54 +3,51 @@
 import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.user.User;
-import lombok.Getter;
 
 /**
  * @author hanl
  * @date 30/06/2015
  */
-@Getter
-public abstract class RewriteTask {
+public interface RewriteTask {
 
-    private RewriteTask() {
+    interface RewriteBefore extends RewriteTask {
+        /**
+         * @param node   Json node in KoralNode wrapper
+         * @param config {@link KustvaktConfiguration} singleton instance to use default configuration parameters
+         * @param user   injected by rewrite handler if available. Might cause {@link NullPointerException} if not checked properly
+         * @return
+         */
+        JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
+                User user);
     }
 
-    /**
-     * @param node   Json node in KoralNode wrapper
-     * @param config {@link KustvaktConfiguration} singleton instance to use default configuration parameters
-     * @param user   injected by rewrite handler if available. Might cause {@link NullPointerException} if not checked properly
-     * @return
-     */
-    public abstract JsonNode rewrite(KoralNode node,
-            KustvaktConfiguration config, User user);
+    interface RewriteAfter extends RewriteTask {
+        JsonNode postProcess(KoralNode node);
+    }
 
     /**
      * query rewrites get injected the entire query from root containing all child nodes
      * <p/>
-     * {@link de.ids_mannheim.korap.resource.rewrite.RewriteTask.RewriteQuery} does not allow the deletion of the root node or subnode through KoralNode.
+     * {@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
-     *
-     * @author hanl
-     * @date 03/07/2015
      */
-    public static abstract class RewriteQuery extends RewriteTask {
-        public RewriteQuery() {
-            super();
-        }
+    interface RewriteQuery extends RewriteBefore, RewriteAfter {
     }
 
     /**
-     * node rewrites get injected typically object nodes that are subject to altering.
+     * Koral term nodes that are subject to rewrites
      * Be aware that node rewrites are processed before query rewrites. Thus query rewrite may override previous node rewrites
      * <p/>
-     * {@link de.ids_mannheim.korap.resource.rewrite.RewriteTask.RewriteNode} rewrite support the deletion of the respective node by simply setting the node invalid in KoralNode
-     *
-     * @author hanl
-     * @date 03/07/2015
+     * {@link RewriteNode} rewrite supports the deletion of the respective node by simply setting the node invalid in KoralNode
      */
-    public static abstract class RewriteNode extends RewriteTask {
-        public RewriteNode() {
-            super();
-        }
+    interface RewriteNode extends RewriteBefore {
+    }
+
+    /**
+     * koral token nodes that are subject to rewrites
+     * Be aware that node rewrites are processed before query rewrites. Thus query rewrite may override previous node rewrites
+     * {@link RewriteKoralToken} rewrite DOES NOT support the deletion of the respective node
+     */
+    interface RewriteKoralToken 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 245e787..e96b3fe 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 extends RewriteTask.RewriteQuery {
+public class TreeConstraint implements RewriteTask.RewriteQuery {
 
     private String pointer;
 
@@ -51,10 +51,15 @@
     }
 
     @Override
-    public JsonNode rewrite(KoralNode node, KustvaktConfiguration config,
+    public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
             User user) {
         System.out.println("FIND PATH " + node.rawNode().findParent(pointer));
 
         return node.rawNode();
     }
+
+    @Override
+    public JsonNode postProcess(KoralNode node) {
+        return null;
+    }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/PolicyEvaluator.java b/src/main/java/de/ids_mannheim/korap/security/ac/PolicyEvaluator.java
index 4cd79d5..5a311da 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/PolicyEvaluator.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/PolicyEvaluator.java
@@ -8,7 +8,9 @@
 import de.ids_mannheim.korap.security.SecurityPolicy;
 import de.ids_mannheim.korap.user.KorAPUser;
 import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.utils.KustvaktLogger;
 import edu.emory.mathcs.backport.java.util.Collections;
+import org.slf4j.Logger;
 
 import java.util.HashMap;
 import java.util.List;
@@ -19,6 +21,9 @@
  */
 public class PolicyEvaluator {
 
+    private static final Logger jlog = KustvaktLogger
+            .getLogger(PolicyEvaluator.class);
+
     private final User user;
     private final List<SecurityPolicy>[] policies;
     private String resourceID;
@@ -27,7 +32,6 @@
     private int relationError = -1;
     private Map<String, Object> flags;
 
-
     public PolicyEvaluator(User user, List<SecurityPolicy>[] policies) {
         this.user = user;
         this.policies = policies;
@@ -50,25 +54,30 @@
         return this.resourceID;
     }
 
-
+    // todo: test benchmarks
     private List<SecurityPolicy> evaluate(List<SecurityPolicy>[] policies,
-                                          Permissions.PERMISSIONS perm) throws NotAuthorizedException {
+            Permissions.PERMISSIONS perm) throws NotAuthorizedException {
         //fixme: what happens in case a parent relation does not allow changing a resource, but the owner of child per default
-        // receives all rights? --> test casing
-        if (isOwner()) return policies[0];
+        //todo: receives all rights? --> test casing
+        if (isOwner()) {
+            jlog.debug("Resource is owned by the user!");
+            return policies[0];
+        }
         if (!processed && policies != null) {
             for (int i = policies.length - 1; i >= 0; i--) {
                 int idx = 0;
                 if (policies[i] != null) {
                     int ow = getOwner(policies[i]);
-                    for (int internal = 0; internal < policies[i].size(); internal++) {
+                    for (int internal = 0;
+                         internal < policies[i].size(); internal++) {
                         SecurityPolicy s = policies[i].get(internal);
                         if (i == policies.length - 1) {
                             if (ow == user.getId())
                                 this.permissions.addPermission(127);
                             else if (!(s instanceof SecurityPolicy.OwnerPolicy))
-                                this.permissions.addPermission(s.getPermissionByte());
-                        } else {
+                                this.permissions
+                                        .addPermission(s.getPermissionByte());
+                        }else {
                             if (ow == user.getId())
                                 this.permissions.retain(127);
                             else if (!(s instanceof SecurityPolicy.OwnerPolicy))
@@ -79,16 +88,22 @@
                 }
                 if (idx == 0) {
                     relationError = i;
-                    throw new NotAuthorizedException(StatusCodes.PERMISSION_DENIED, this.getResourceID());
+                    throw new NotAuthorizedException(
+                            StatusCodes.PERMISSION_DENIED,
+                            this.getResourceID());
                 }
             }
             this.processed = true;
             System.out.println("FINAL BYTE :" + this.permissions.getPbyte());
             if (this.permissions.containsPermission(perm))
                 return policies[0];
-        } else if (processed && relationError == -1
-                && this.permissions.containsPermission(perm))
+        }else if (processed && relationError == -1 && this.permissions
+                .containsPermission(perm)) {
+            jlog.debug("Done processing resource policies");
+            jlog.debug("Will return policies to security manager: "
+                    + this.policies[0]);
             return this.policies[0];
+        }
 
         return Collections.emptyList();
     }
@@ -104,25 +119,29 @@
 
     public boolean isAllowed(Permissions.PERMISSIONS perm) {
         try {
+            System.out.println("THESE POLICIES " + this.policies[0]);
             return !evaluate(this.policies, perm).isEmpty();
-        } catch (NotAuthorizedException e) {
+        }catch (NotAuthorizedException e) {
             return false;
         }
     }
 
     public boolean isOwner() {
-        return policies != null && this.user.getId() != null && getOwner(this.policies[0]) == this.user.getId();
+        return policies != null && this.user.getId() != null
+                && getOwner(this.policies[0]) == this.user.getId();
     }
 
     private int getOwner(List<SecurityPolicy> policies) {
-        if (policies != null && policies.get(0) != null
-                && policies.get(0) instanceof SecurityPolicy.OwnerPolicy) {
+        if (policies != null && policies.get(0) != null && policies
+                .get(0) instanceof SecurityPolicy.OwnerPolicy) {
             return ((SecurityPolicy.OwnerPolicy) policies.get(0)).getOwner();
         }
         return -1;
     }
 
-    public static PolicyEvaluator setFlags(User user, KustvaktResource resource) {
+    // todo: what is this supposed to do?
+    public static PolicyEvaluator setFlags(User user,
+            KustvaktResource resource) {
         PolicyEvaluator e = new PolicyEvaluator(user, resource);
         e.setFlag("managed", resource.getOwner() == KorAPUser.ADMINISTRATOR_ID);
         e.setFlag("shared", false);
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceFinder.java b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceFinder.java
index 3ed9843..bd465b5 100755
--- a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceFinder.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceFinder.java
@@ -18,7 +18,8 @@
  */
 public class ResourceFinder {
 
-    private static final Logger log = KustvaktLogger.initiate(ResourceFinder.class);
+    private static final Logger log = KustvaktLogger.getLogger(
+            ResourceFinder.class);
     private static PolicyHandlerIface policydao;
 
     private List<KustvaktResource.Container> containers;
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
index 9e4b152..78d0a44 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/ResourceHandler.java
@@ -23,7 +23,7 @@
 @SuppressWarnings("all")
 public class ResourceHandler {
 
-    private static Logger log = KustvaktLogger.initiate(ResourceHandler.class);
+    private static Logger log = KustvaktLogger.getLogger(ResourceHandler.class);
 
     public ResourceHandler() {
     }
diff --git a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
index 2317190..ee89118 100644
--- a/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
+++ b/src/main/java/de/ids_mannheim/korap/security/ac/SecurityManager.java
@@ -43,15 +43,21 @@
     private List<SecurityPolicy>[] policies;
     private User user;
 
-    private boolean wException;
+    private boolean silent;
     private PolicyEvaluator evaluator;
     private T resource;
 
     private SecurityManager(User user) {
         this.policies = new List[1];
         this.policies[0] = new ArrayList<>();
-        this.wException = true;
+        this.silent = true;
         this.user = user;
+        checkProviders();
+    }
+
+    private static void checkProviders() {
+        if (policydao == null && crypto == null && handlers == null)
+            throw new RuntimeException("providers not set!");
     }
 
     public static final void setProviders(PolicyHandlerIface policyHandler,
@@ -79,10 +85,9 @@
      * @throws EmptyResultException
      * @throws KustvaktException
      */
+    //todo: implement a fall back that throws an exception when the user NULL, but the resource has restrictions!
     public static SecurityManager findbyId(String id, User user, Class type,
-            Permissions.PERMISSIONS... perms)
-            throws EmptyResultException, KustvaktException,
-            NotAuthorizedException {
+            Permissions.PERMISSIONS... perms) throws KustvaktException {
         SecurityManager p = new SecurityManager(user);
         p.findPolicies(id, false, perms);
         p.resource = p.findResource(type);
@@ -90,9 +95,7 @@
     }
 
     public static SecurityManager findbyId(String id, User user,
-            Permissions.PERMISSIONS... perms)
-            throws NotAuthorizedException, KustvaktException,
-            EmptyResultException {
+            Permissions.PERMISSIONS... perms) throws KustvaktException {
         SecurityManager p = new SecurityManager(user);
         p.findPolicies(id, false, perms);
         p.resource = p.findResource(null);
@@ -100,9 +103,7 @@
     }
 
     public static SecurityManager findbyId(Integer id, User user,
-            Permissions.PERMISSIONS... perms)
-            throws EmptyResultException, KustvaktException,
-            NotAuthorizedException {
+            Permissions.PERMISSIONS... perms) throws KustvaktException {
         SecurityManager p = new SecurityManager(user);
         p.findPolicies(id, false, perms);
         p.resource = p.findResource(null);
@@ -132,7 +133,7 @@
      * @return
      * @throws NotAuthorizedException
      */
-    public T getResource() throws NotAuthorizedException {
+    public final T getResource() throws NotAuthorizedException {
         if (evaluator.isAllowed(Permissions.PERMISSIONS.READ)) {
             return this.resource;
         }else {
@@ -170,8 +171,8 @@
      * @throws KustvaktException
      */
     // todo: delete only works with find, not with init constructor!resource
-    public void deleteResource() throws NotAuthorizedException,
-            KustvaktException {
+    public void deleteResource()
+            throws NotAuthorizedException, KustvaktException {
         if (evaluator.isAllowed(Permissions.PERMISSIONS.DELETE)) {
             ResourceOperationIface iface = handlers
                     .get(this.resource.getClass());
@@ -187,11 +188,9 @@
                     this.evaluator.getResourceID());
     }
 
-
     // todo: type should be deprecated and return type of policies should be containers!
     private boolean findPolicies(Object id, boolean path,
-            Permissions.PERMISSIONS... perms)
-            throws NotAuthorizedException, EmptyResultException {
+            Permissions.PERMISSIONS... perms) throws EmptyResultException {
         PermissionsBuffer b = new PermissionsBuffer();
         if (perms.length == 0)
             b.addPermission(Permissions.READ);
@@ -302,7 +301,8 @@
                 KustvaktLogger.SECURITY_LOGGER
                         .error("No policies found for '{}' for user '{}'",
                                 resource.getPersistentID(), user.getId());
-                throw new KustvaktException(user.getId(),StatusCodes.POLICY_CREATE_ERROR,
+                throw new KustvaktException(user.getId(),
+                        StatusCodes.POLICY_CREATE_ERROR,
                         "Resource could not be registered",
                         resource.toString());
             }
@@ -368,7 +368,7 @@
 
         if (evaluator.isAllowed(Permissions.PERMISSIONS.CREATE_POLICY)) {
             policydao.createPolicy(policy, this.user);
-        }else if (wException) {
+        }else if (silent) {
             KustvaktLogger.SECURITY_LOGGER
                     .error("Permission Denied (CREATE_POLICY) on '{}' for user '{}'",
                             this.evaluator.getResourceID(), this.user.getId());
@@ -385,8 +385,8 @@
         this.policies[0].add(policy);
     }
 
-    public void deletePolicies() throws NotAuthorizedException,
-            KustvaktException {
+    public void deletePolicies()
+            throws NotAuthorizedException, KustvaktException {
         for (SecurityPolicy p : new ArrayList<>(this.policies[0]))
             deletePolicy(p);
     }
@@ -411,14 +411,14 @@
             KustvaktLogger.SECURITY_LOGGER
                     .error("No policies found (DELETE_POLICY) on '{}' for '{}'",
                             this.evaluator.getResourceID(), this.user.getId());
-            throw new KustvaktException(user.getId(),StatusCodes.NO_POLICIES,
+            throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES,
                     "no policy desicion possible",
                     this.evaluator.getResourceID());
         }
         if (contains(policy) && (evaluator
                 .isAllowed(Permissions.PERMISSIONS.DELETE_POLICY))) {
             policydao.deletePolicy(policy, this.user);
-        }else if (wException) {
+        }else if (silent) {
             KustvaktLogger.SECURITY_LOGGER
                     .error("Permission Denied (DELETE_POLICY) on '{}' for '{}'",
                             this.evaluator.getResourceID(), this.user.getId());
@@ -440,7 +440,7 @@
             KustvaktLogger.SECURITY_LOGGER
                     .error("Operation not possible (MODIFY_POLICY) on '{}' for '{}'",
                             this.evaluator.getResourceID(), this.user.getId());
-            throw new KustvaktException(user.getId(),StatusCodes.NO_POLICIES,
+            throw new KustvaktException(user.getId(), StatusCodes.NO_POLICIES,
                     "no policy desicion possible",
                     this.evaluator.getResourceID());
         }
@@ -448,7 +448,7 @@
         if (contains(policy) && (evaluator
                 .isAllowed(Permissions.PERMISSIONS.MODIFY_POLICY))) {
             policydao.updatePolicy(policy, this.user);
-        }else if (wException) {
+        }else if (silent) {
             KustvaktLogger.SECURITY_LOGGER
                     .error("Permission Denied (DELETE_POLICY) on '{}' for '{}'",
                             this.evaluator.getResourceID(), this.user.getId());
diff --git a/src/main/java/de/ids_mannheim/korap/utils/UserPropertyReader.java b/src/main/java/de/ids_mannheim/korap/utils/UserPropertyReader.java
index 8b55099..e11447d 100644
--- a/src/main/java/de/ids_mannheim/korap/utils/UserPropertyReader.java
+++ b/src/main/java/de/ids_mannheim/korap/utils/UserPropertyReader.java
@@ -29,7 +29,7 @@
     private EntityHandlerIface iface;
     private EncryptionIface crypto;
     private static Logger jlog = KustvaktLogger
-            .initiate(UserPropertyReader.class);
+            .getLogger(UserPropertyReader.class);
 
     public UserPropertyReader(String path) {
         this.path = path;
diff --git a/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java b/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
index ec3dd54..d917afb 100644
--- a/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
+++ b/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
@@ -22,9 +22,9 @@
  * @author Nils Diewald
  */
 public class SearchKrill {
-    private final static Logger qlog = KustvaktLogger.initiate("queryLogger");
+    private final static Logger qlog = KustvaktLogger.getLogger("queryLogger");
     private final static Logger log = KustvaktLogger
-            .initiate(SearchKrill.class);
+            .getLogger(SearchKrill.class);
     // Temporary
     String indexDir = "/data/prep_corpus/index/";
     String i = "/Users/hanl/Projects/prep_corpus";
diff --git a/src/main/resources/db/sqlite/V1__Initial_version.sql b/src/main/resources/db/sqlite/V1__Initial_version.sql
index 912e699..ee32961 100644
--- a/src/main/resources/db/sqlite/V1__Initial_version.sql
+++ b/src/main/resources/db/sqlite/V1__Initial_version.sql
@@ -125,7 +125,7 @@
 CREATE TABLE IF NOT EXISTS doc_store (
 id INTEGER PRIMARY KEY AUTOINCREMENT,
 persistent_id VARCHAR(265) UNIQUE,
-created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+created DATE DEFAULT CURRENT_TIMESTAMP,
 disabled BOOLEAN default true
 );
 
diff --git a/src/main/resources/default-config.xml b/src/main/resources/default-config.xml
index 7d21bdd..db14aab 100644
--- a/src/main/resources/default-config.xml
+++ b/src/main/resources/default-config.xml
@@ -60,20 +60,21 @@
         <property name="properties" ref="props"/>
     </bean>
 
+    <!--class="org.apache.commons.dbcp2.BasicDataSource"-->
     <bean id="dataSource"
-          class="org.apache.commons.dbcp2.BasicDataSource"
+          class="org.springframework.jdbc.datasource.SingleConnectionDataSource"
           lazy-init="true">
         <property name="driverClassName" value="${jdbc.driverClassName}"/>
         <property name="url" value="${jdbc.url}"/>
         <property name="username" value="${jdbc.username}"/>
         <property name="password" value="${jdbc.password}"/>
         <!-- relevant for single connection datasource and sqlite -->
-        <!--<property name="suppressClose">-->
-        <!--<value>true</value>-->
-        <!--</property>-->
-        <property name="initialSize" value="1"/>
-        <property name="maxIdle" value="1"/>
-        <property name="poolPreparedStatements" value="true"/>
+        <property name="suppressClose">
+            <value>true</value>
+        </property>
+        <!--<property name="initialSize" value="1"/>-->
+        <!--<property name="maxIdle" value="1"/>-->
+        <!--<property name="poolPreparedStatements" value="true"/>-->
     </bean>
 
     <!-- to configure database for sqlite, mysql, etc. migrations -->
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index 602c496..ff5d34c 100644
--- a/src/main/resources/log4j.properties
+++ b/src/main/resources/log4j.properties
@@ -1,7 +1,7 @@
 
 # Root logger option
 #log4j.threshold=ALL
-log4j.rootLogger=INFO, stdout, debugLog
+log4j.rootLogger=DEBUG, stdout, debugLog
 log4j.logger.log=ERROR, errorLog
 
 # Direct log messages to stdout
diff --git a/src/test/java/CollectionRewriteTest.java b/src/test/java/CollectionRewriteTest.java
index 2649d1e..995cdf7 100644
--- a/src/test/java/CollectionRewriteTest.java
+++ b/src/test/java/CollectionRewriteTest.java
@@ -5,9 +5,14 @@
 import de.ids_mannheim.korap.resource.rewrite.CollectionCleanupFilter;
 import de.ids_mannheim.korap.resource.rewrite.CollectionConstraint;
 import de.ids_mannheim.korap.resource.rewrite.RewriteHandler;
+import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @author hanl
@@ -22,7 +27,7 @@
 
     @BeforeClass
     public static void init() {
-        BeanConfiguration.loadClasspathContext();
+        BeanConfiguration.loadClasspathContext("default-config.xml");
         config = BeanConfiguration.getBeans().getConfiguration();
     }
 
@@ -31,8 +36,19 @@
         BeanConfiguration.closeApplication();
     }
 
+    @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));
 
-    //    @Test
+    }
+
+    @Test
     public void testCollectionNodeRemoveCorpusIdNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -40,12 +56,13 @@
         s.setQuery(simple_add_query, "poliqarp");
         s.setCollection("textClass=politik & corpusID=WPD");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        JsonNode node = JsonUtils.readTree(
+                handler.preProcess(result, User.UserFactory.getUser("test_user")));
         assert node != null;
         assert node.at("/collection/operands").size() == 1;
     }
 
-    //@Test
+    @Test
     public void testCollectionNodeRemoveAllCorpusIdNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -53,13 +70,14 @@
         s.setQuery(simple_add_query, "poliqarp");
         s.setCollection("corpusID=BRZ13 & corpusID=WPD");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
-
+        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;
     }
 
-    //@Test
+    @Test
     public void testCollectionNodeRemoveGroupedCorpusIdNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -68,8 +86,10 @@
         s.setCollection(
                 "(corpusID=BRZ13 & textClass=Wissenschaft) & corpusID=WPD");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        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");
@@ -78,7 +98,7 @@
     }
 
     //fixme: will probably fail when one doc groups are being refactored
-    //@Test
+    @Test
     public void testCollectionCleanEmptyDocGroupNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -88,7 +108,8 @@
         s.setCollection(
                 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft & textClass=Sport");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        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;
@@ -98,7 +119,7 @@
                 .equals("textClass");
     }
 
-    //@Test
+    @Test
     public void testCollectionCleanMoveOneDocFromGroupUpNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -107,12 +128,13 @@
         s.setQuery(simple_add_query, "poliqarp");
         s.setCollection("(corpusID=BRZ13 & textClass=Wissenschaft)");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        JsonNode node = JsonUtils.readTree(
+                handler.preProcess(result, User.UserFactory.getUser("test_user")));
         assert node != null;
         assert node.at("/collection/@type").asText().equals("koral:doc");
     }
 
-    //@Test
+    @Test
     public void testCollectionCleanEmptyGroupAndMoveOneFromGroupUpNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -122,13 +144,15 @@
         s.setCollection(
                 "(corpusID=BRZ13 & corpusID=WPD) & textClass=Wissenschaft");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        JsonNode node = JsonUtils.readTree(
+                handler.preProcess(result, User.UserFactory.getUser("test_user")));
+
         assert node != null;
         assert node.at("/collection/@type").asText().equals("koral:doc");
         assert node.at("/collection/key").asText().equals("textClass");
     }
 
-    //@Test
+    @Test
     public void testCollectionRemoveAndMoveOneFromGroupUpNoErrors() {
         RewriteHandler handler = new RewriteHandler(config);
         handler.add(CollectionConstraint.class);
@@ -138,7 +162,8 @@
         s.setCollection(
                 "(docID=random & textClass=Wissenschaft) & corpusID=WPD");
         String result = s.toJSON();
-        JsonNode node = JsonUtils.readTree(handler.apply(result, null));
+        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;
diff --git a/src/test/java/FoundryRewriteTest.java b/src/test/java/FoundryRewriteTest.java
index 6d23964..1a477d9 100644
--- a/src/test/java/FoundryRewriteTest.java
+++ b/src/test/java/FoundryRewriteTest.java
@@ -42,7 +42,7 @@
         processor.add(FoundryInject.class);
         QuerySerializer s = new QuerySerializer();
         s.setQuery(simple_add_query, "poliqarp");
-        String result = processor.apply(s.toJSON(), null);
+        String result = processor.preProcess(s.toJSON(), null);
         JsonNode node = JsonUtils.readTree(result);
 
         assert node != null;
@@ -66,7 +66,7 @@
         RewriteHandler handler = new RewriteHandler(config);
         s.setQuery("[pos=ADJA]", "poliqarp");
         assert handler.add(FoundryInject.class);
-        String result = handler.apply(s.toJSON(), null);
+        String result = handler.preProcess(s.toJSON(), null);
         JsonNode node = JsonUtils.readTree(result);
 
         assert node != null;
@@ -82,7 +82,7 @@
         RewriteHandler handler = new RewriteHandler(config);
         s.setQuery("[orth=laufe/i & base!=Lauf]", "poliqarp");
         assert handler.add(FoundryInject.class);
-        String result = handler.apply(s.toJSON(), null);
+        String result = handler.preProcess(s.toJSON(), null);
         JsonNode node = JsonUtils.readTree(result);
 
         assert node != null;
@@ -99,7 +99,7 @@
         RewriteHandler handler = new RewriteHandler(config);
         s.setQuery("[(base=laufen | base=gehen) & tt/pos=VVFIN]", "poliqarp");
         assert handler.add(FoundryInject.class);
-        String result = handler.apply(s.toJSON(), null);
+        String result = handler.preProcess(s.toJSON(), null);
         JsonNode node = JsonUtils.readTree(result);
 
         assert node != null;
diff --git a/src/test/java/IdRewriteTest.java b/src/test/java/IdRewriteTest.java
index be37b7c..8415c2c 100644
--- a/src/test/java/IdRewriteTest.java
+++ b/src/test/java/IdRewriteTest.java
@@ -1,6 +1,9 @@
+import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
 import de.ids_mannheim.korap.resource.rewrite.IdWriter;
 import de.ids_mannheim.korap.resource.rewrite.RewriteHandler;
+import de.ids_mannheim.korap.utils.JsonUtils;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -27,6 +30,15 @@
                 BeanConfiguration.getBeans().getConfiguration());
         assert handler.add(IdWriter.class);
 
+        String query = "[surface=Wort]";
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery(query, "poliqarp");
+
+        String value = handler.preProcess(s.toJSON(), null);
+        JsonNode result = JsonUtils.readTree(value);
+
+        assert result != null;
+        assert result.path("query").has("idn");
 
     }
 
diff --git a/src/test/java/de/ids_mannheim/korap/web/service/KustvaktCoreRestTest.java b/src/test/java/de/ids_mannheim/korap/web/service/KustvaktCoreRestTest.java
index 1661263..7af97ea 100644
--- a/src/test/java/de/ids_mannheim/korap/web/service/KustvaktCoreRestTest.java
+++ b/src/test/java/de/ids_mannheim/korap/web/service/KustvaktCoreRestTest.java
@@ -37,6 +37,7 @@
                 .queryParam("q", "[base=Wort]").queryParam("ql", "poliqarp")
                 .get(ClientResponse.class);
         assert ClientResponse.Status.OK.getStatusCode() == response.getStatus();
+        System.out.println("RESPONSE IS " + response.getEntity(String.class));
     }
 
     @Test