query rewrite at rest endpoints
diff --git a/src/main/java/de/ids_mannheim/korap/config/AuditingHandler.java b/src/main/java/de/ids_mannheim/korap/config/AuditingHandler.java
index 4daf76c..30d08f1 100644
--- a/src/main/java/de/ids_mannheim/korap/config/AuditingHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/AuditingHandler.java
@@ -1,8 +1,15 @@
 package de.ids_mannheim.korap.config;
 
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
 /**
  * @author hanl
  * @date 17/06/2015
  */
-public class AuditingHandler {
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface AuditingHandler {
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/AuthenticationHandler.java b/src/main/java/de/ids_mannheim/korap/config/AuthenticationHandler.java
index 74a3a11..d712490 100644
--- a/src/main/java/de/ids_mannheim/korap/config/AuthenticationHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/AuthenticationHandler.java
@@ -4,5 +4,5 @@
  * @author hanl
  * @date 17/06/2015
  */
-public class AuthenticationHandler {
+public @interface AuthenticationHandler {
 }
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 f4b72b1..7f53159 100644
--- a/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/BeanConfiguration.java
@@ -1,13 +1,10 @@
 package de.ids_mannheim.korap.config;
 
 import de.ids_mannheim.korap.interfaces.AuditingIface;
-import de.ids_mannheim.korap.plugins.PluginManager;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 import org.springframework.context.support.FileSystemXmlApplicationContext;
 
-import java.net.URL;
-
 /**
  * User: hanl
  * Date: 10/9/13
@@ -18,28 +15,17 @@
     private static final String config_file = "default-config.xml";
 
     private static ApplicationContext context = null;
-    private static PluginManager plugins;
 
-    private static void loadPlugins() {
-        plugins = new PluginManager();
-        plugins.loadPluginInterfaces();
-    }
-
-    public static void loadContext() {
-        URL url = BeanConfiguration.class.getClassLoader()
-                .getResource(config_file);
-        if (url != null && context == null)
+    public static void loadClasspathContext(String... files) {
+        if (context == null && files == null)
             context = new ClassPathXmlApplicationContext(config_file);
+        else if (context == null)
+            context = new ClassPathXmlApplicationContext(files);
     }
 
-    public static void loadContext(String filepath) {
-        if (filepath == null)
-            loadContext();
-        else {
-            if (context == null)
-                context = new FileSystemXmlApplicationContext(
-                        "file:" + filepath);
-        }
+    public static void loadFileContext(String filepath) {
+        if (context == null)
+            context = new FileSystemXmlApplicationContext("file:" + filepath);
     }
 
     public static <T extends KustvaktConfiguration> T getConfiguration() {
diff --git a/src/main/java/de/ids_mannheim/korap/config/EncryptionHandler.java b/src/main/java/de/ids_mannheim/korap/config/EncryptionHandler.java
index cab7f6d..b0a28ac 100644
--- a/src/main/java/de/ids_mannheim/korap/config/EncryptionHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/EncryptionHandler.java
@@ -4,5 +4,5 @@
  * @author hanl
  * @date 18/06/2015
  */
-public class EncryptionHandler {
+public @interface EncryptionHandler {
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
index de4748e..4cc7f12 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktClassLoader.java
@@ -2,6 +2,7 @@
 
 import org.reflections.Reflections;
 
+import java.lang.annotation.Annotation;
 import java.util.Set;
 
 /**
@@ -23,4 +24,9 @@
     public static <T> Set<Class<? extends T>> load(Class<T> iface) {
         return reflections.getSubTypesOf(iface);
     }
+
+    public static Set<Class<?>> loadFromAnnotation(
+            Class<? extends Annotation> annotation) {
+        return reflections.getTypesAnnotatedWith(annotation);
+    }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index 52ac3c7..3b089d4 100644
--- a/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -7,6 +7,7 @@
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -14,6 +15,7 @@
 
 /**
  * if configuration class is extended, load method should be overriden
+ *
  * @author hanl
  * @date 05/02/2014
  */
@@ -23,21 +25,29 @@
 
     private final Logger jlog = KorAPLogger
             .initiate(KustvaktConfiguration.class);
+    private String indexDir;
+    private int port;
+    // todo: make exclusive so that the containg languages can really only be used then
+    private List<String> queryLanguages;
 
-    // deprec?!
-    private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
     private int maxhits;
 
-    private int port;
     private int returnhits;
     private String serverHost;
-    private String indexDir;
 
-    private List<String> queryLanguages;
     private String host;
 
     private URL issuer;
 
+    private String default_pos;
+    private String default_lemma;
+    private String default_surface;
+    private String default_dep;
+    private String default_const;
+
+    // deprec?!
+    private final BACKENDS DEFAULT_ENGINE = BACKENDS.LUCENE;
+
     /**
      * loading of the properties and mapping to parameter variables
      *
@@ -61,13 +71,42 @@
         for (String querylang : qls)
             queryLanguages.add(querylang.trim().toUpperCase());
         //        issuer = new URL(korap.getProperty("korap.issuer", ""));
+
+        default_const = korap.getProperty("kustvakt.default.const", "mate");
+        default_dep = korap.getProperty("kustvakt.default.dep", "mate");
+        default_lemma = korap.getProperty("kustvakt.default.lemma", "tt");
+        default_pos = korap.getProperty("kustvakt.default.pos", "tt");
+        default_surface = korap
+                .getProperty("kustvakt.default.opennlp", "opennlp");
+
         return korap;
     }
 
+    /**
+     * set properties
+     *
+     * @param props
+     */
     public void setProperties(Properties props) {
         this.load(props);
     }
 
+    /**
+     * properties can be overloaded after spring init
+     *
+     * @param stream
+     */
+    public void setProperties(InputStream stream) {
+        try {
+            Properties p = new Properties();
+            p.load(stream);
+            this.load(p);
+        }catch (IOException e) {
+            e.printStackTrace();
+        }
+
+    }
+
     public BACKENDS chooseBackend(String value) {
         if (value == null || value.equals("null"))
             return DEFAULT_ENGINE;
diff --git a/src/main/java/de/ids_mannheim/korap/config/PluginHandler.java b/src/main/java/de/ids_mannheim/korap/config/PluginHandler.java
index b0c473b..2944aa1 100644
--- a/src/main/java/de/ids_mannheim/korap/config/PluginHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/PluginHandler.java
@@ -1,8 +1,78 @@
 package de.ids_mannheim.korap.config;
 
+import de.ids_mannheim.korap.interfaces.AuditingIface;
+import de.ids_mannheim.korap.interfaces.AuthenticationManagerIface;
+import de.ids_mannheim.korap.interfaces.EncryptionIface;
+import de.ids_mannheim.korap.interfaces.EntityHandlerIface;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
 /**
  * @author hanl
  * @date 17/06/2015
  */
 public class PluginHandler {
+
+    private static Map<Class<? extends Annotation>, Class> interfaces;
+    private Map<Class<? extends Annotation>, Object> plugins;
+
+    // add resource handler annotation
+    static {
+        interfaces = new HashMap<>();
+        interfaces.put(AuditingHandler.class, AuditingIface.class);
+        interfaces.put(UserDbHandler.class, EntityHandlerIface.class);
+        interfaces.put(AuthenticationHandler.class,
+                AuthenticationManagerIface.class);
+        interfaces.put(EncryptionHandler.class, EncryptionIface.class);
+        //todo:
+        interfaces.put(ResourceHandler.class, ResourceHandler.class);
+    }
+
+    public PluginHandler() {
+        this.plugins = new HashMap<>();
+        this.load();
+    }
+
+    public void load() {
+        for (Map.Entry<Class<? extends Annotation>, Class> en : new HashSet<>(
+                interfaces.entrySet())) {
+            Set<Class<?>> set = KustvaktClassLoader
+                    .loadFromAnnotation(en.getKey());
+            if (set.size() > 1)
+                throw new UnsupportedOperationException(
+                        "handler declaration not unique!");
+            else if (set.size() == 0)
+                interfaces.remove(en.getKey());
+        }
+    }
+
+    public void addInterface(Class<? extends Annotation> anno, Class<?> iface) {
+        interfaces.put(anno, iface);
+    }
+
+    public void registerPlugin(Object ob) {
+        for (Map.Entry<Class<? extends Annotation>, Class> en : interfaces
+                .entrySet()) {
+            if (en.getValue().isInstance(ob))
+                this.plugins.put(en.getKey(), ob);
+        }
+    }
+
+    public Object getPluginInstance(Class<? extends Annotation> anno) {
+        Object o = this.plugins.get(anno);
+        if (o == null)
+            return new NullPointerException(
+                    "no plugin defined for type " + anno.toString());
+        return o;
+    }
+
+    @Override
+    public String toString() {
+        System.out.println("PRINT INTERFACES " + interfaces.toString());
+        return plugins.toString();
+    }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/ProtectedResource.java b/src/main/java/de/ids_mannheim/korap/config/ProtectedResource.java
index d93e4f0..2d24c8b 100644
--- a/src/main/java/de/ids_mannheim/korap/config/ProtectedResource.java
+++ b/src/main/java/de/ids_mannheim/korap/config/ProtectedResource.java
@@ -4,5 +4,5 @@
  * @author hanl
  * @date 18/06/2015
  */
-public class ProtectedResource {
+public @interface ProtectedResource {
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/ResourceHandler.java b/src/main/java/de/ids_mannheim/korap/config/ResourceHandler.java
index c354a01..aae143f 100644
--- a/src/main/java/de/ids_mannheim/korap/config/ResourceHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/ResourceHandler.java
@@ -4,5 +4,5 @@
  * @author hanl
  * @date 18/06/2015
  */
-public class ResourceHandler {
+public @interface ResourceHandler {
 }
diff --git a/src/main/java/de/ids_mannheim/korap/config/UserDbHandler.java b/src/main/java/de/ids_mannheim/korap/config/UserDbHandler.java
index 73fcc56..304f16c 100644
--- a/src/main/java/de/ids_mannheim/korap/config/UserDbHandler.java
+++ b/src/main/java/de/ids_mannheim/korap/config/UserDbHandler.java
@@ -1,8 +1,15 @@
 package de.ids_mannheim.korap.config;
 
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
 /**
  * @author hanl
  * @date 17/06/2015
  */
-public class UserDbHandler {
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface UserDbHandler {
 }
diff --git a/src/main/java/de/ids_mannheim/korap/exceptions/BaseException.java b/src/main/java/de/ids_mannheim/korap/exceptions/BaseException.java
index 408f591..66e1197 100644
--- a/src/main/java/de/ids_mannheim/korap/exceptions/BaseException.java
+++ b/src/main/java/de/ids_mannheim/korap/exceptions/BaseException.java
@@ -7,6 +7,10 @@
 import java.util.List;
 
 /**
+ * Serves as base exception to allow auditing the exception.
+ * By extending the exception, different types of exceptions and ways of handling the audits can be created
+ * (database, service, etc.)
+ *
  * @author hanl
  * @date 29/01/2014
  */
@@ -17,7 +21,6 @@
     private Integer statusCode;
     private String entity;
 
-
     public BaseException(int code) {
         this.statusCode = code;
     }
diff --git a/src/main/java/de/ids_mannheim/korap/exceptions/KorAPException.java b/src/main/java/de/ids_mannheim/korap/exceptions/KorAPException.java
index a050770..d3435f7 100644
--- a/src/main/java/de/ids_mannheim/korap/exceptions/KorAPException.java
+++ b/src/main/java/de/ids_mannheim/korap/exceptions/KorAPException.java
@@ -7,6 +7,8 @@
  * @author hanl
  * @date 11/12/2013
  */
+//fixme: redundant with baseexception
+@Deprecated
 @Setter
 @Getter
 public class KorAPException extends BaseException {
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/AuditingIface.java b/src/main/java/de/ids_mannheim/korap/interfaces/AuditingIface.java
index 82bdd55..873f435 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/AuditingIface.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/AuditingIface.java
@@ -17,7 +17,7 @@
 public abstract class AuditingIface implements Runnable {
 
     protected static int BATCH_SIZE = 15;
-    protected final List<AuditRecord> records = new ArrayList<>(BATCH_SIZE + 5);
+    private final List<AuditRecord> records = new ArrayList<>(BATCH_SIZE + 5);
     private final List<AuditRecord> buffer = new ArrayList<>(BATCH_SIZE + 5);
 
     public abstract <T extends AuditRecord> List<T> retrieveRecords(
@@ -53,4 +53,8 @@
             addAndRun(rec);
     }
 
+    protected List<AuditRecord> getRecordsToSave() {
+        return this.records;
+    }
+
 }
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationIface.java b/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationIface.java
index e02933e..600981f 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationIface.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationIface.java
@@ -1,25 +1,22 @@
 package de.ids_mannheim.korap.interfaces;
 
 import de.ids_mannheim.korap.exceptions.KorAPException;
-import de.ids_mannheim.korap.ext.security.AccessLevel;
 import de.ids_mannheim.korap.user.TokenContext;
 import de.ids_mannheim.korap.user.User;
 
 import java.util.Map;
 
-public abstract class AuthenticationIface {
+public interface AuthenticationIface {
 
-    public abstract TokenContext getUserStatus(String authToken)
+    public TokenContext getUserStatus(String authToken) throws KorAPException;
+
+    public TokenContext createUserSession(User user, Map<String, Object> attr)
             throws KorAPException;
 
-    public abstract TokenContext createUserSession(User user,
-            Map<String, Object> attr) throws KorAPException;
+    public void removeUserSession(String token) throws KorAPException;
 
-    public abstract void removeUserSession(String token) throws KorAPException;
+    public TokenContext refresh(TokenContext context) throws KorAPException;
 
-    public abstract AccessLevel[] retrieveLevelAccess(String authToken)
-            throws KorAPException;
-
-    public abstract String getIdentifier();
+    public String getIdentifier();
 
 }
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationManagerIface.java b/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationManagerIface.java
index a3fb44a..4021836 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationManagerIface.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/AuthenticationManagerIface.java
@@ -1,9 +1,11 @@
 package de.ids_mannheim.korap.interfaces;
 
+import de.ids_mannheim.korap.config.KustvaktClassLoader;
 import de.ids_mannheim.korap.exceptions.KorAPException;
 import de.ids_mannheim.korap.user.*;
 
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 
@@ -15,10 +17,23 @@
 
     private Map<String, AuthenticationIface> providers;
 
-    //todo: test if constr actually called
     public AuthenticationManagerIface() {
-        System.out.println("TEST CONSTRUCTOR CALL");
         this.providers = new HashMap<>();
+        loadProviders();
+    }
+
+    private void loadProviders() {
+        Set<Class<? extends AuthenticationIface>> set = KustvaktClassLoader
+                .load(AuthenticationIface.class);
+        Set<AuthenticationIface> set2 = new HashSet<>();
+        for (Class<? extends AuthenticationIface> i : set) {
+            try {
+                set2.add(i.newInstance());
+            }catch (InstantiationException | IllegalAccessException e) {
+                e.printStackTrace();
+            }
+        }
+        this.setProviders(set2);
     }
 
     public void setProviders(Set<AuthenticationIface> providers) {
@@ -35,14 +50,26 @@
         return iface;
     }
 
+    public abstract TokenContext getTokenStatus(String token, String host,
+            String useragent) throws KorAPException;
+
+    public abstract User getUser(String username) throws KorAPException;
+
     public abstract User authenticate(int type, String username,
             String password, Map<String, Object> attributes)
             throws KorAPException;
 
+    public abstract TokenContext createTokenContext(User user,
+            Map<String, Object> attr, String provider_key)
+            throws KorAPException;
+
     public abstract void logout(TokenContext context) throws KorAPException;
 
     public abstract void lockAccount(User user) throws KorAPException;
 
+    public abstract User createUserAccount(Map<String, Object> attributes)
+            throws KorAPException;
+
     public abstract boolean updateAccount(User user) throws KorAPException;
 
     public abstract boolean deleteAccount(User user) throws KorAPException;
@@ -57,4 +84,10 @@
 
     public abstract void updateUserSettings(User user, UserSettings settings)
             throws KorAPException;
+
+    public abstract Object[] validateResetPasswordRequest(String username,
+            String email) throws KorAPException;
+
+    public abstract void confirmRegistration(String uriFragment,
+            String username) throws KorAPException;
 }
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultAuditing.java b/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultAuditing.java
index cb0eef7..27bb3a1 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultAuditing.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultAuditing.java
@@ -6,6 +6,10 @@
 import org.joda.time.DateTime;
 import org.joda.time.LocalDate;
 
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.List;
 
 /**
@@ -14,37 +18,51 @@
  */
 public class DefaultAuditing extends AuditingIface {
 
-    public DefaultAuditing() {
+    private FileOutputStream stream;
 
+    public DefaultAuditing() {
+        try {
+            File f = new File("logs");
+            f.mkdirs();
+            stream = new FileOutputStream(new File(f, "audit.log"));
+        }catch (FileNotFoundException e) {
+            e.printStackTrace();
+        }
     }
 
     @Override
     public <T extends AuditRecord> List<T> retrieveRecords(
             AuditRecord.CATEGORY category, DateTime day, DateTime until,
             boolean exact, int limit) {
-        return null;
+        throw new UnsupportedOperationException("operation not supported!");
     }
 
     @Override
     public <T extends AuditRecord> List<T> retrieveRecords(
             AuditRecord.CATEGORY category, User user, int limit) {
-        return null;
+        throw new UnsupportedOperationException("operation not supported!");
     }
 
     @Override
     public <T extends AuditRecord> List<T> retrieveRecords(LocalDate day,
             int hitMax) {
-        return null;
+        throw new UnsupportedOperationException("operation not supported!");
     }
 
     @Override
     public <T extends AuditRecord> List<T> retrieveRecords(String userID,
             LocalDate start, LocalDate end, int hitMax) {
-        return null;
+        throw new UnsupportedOperationException("operation not supported!");
     }
 
     @Override
     public void run() {
-        //todo: append to logging file or other auditing file
+        try {
+            for (AuditRecord r : getRecordsToSave())
+                stream.write((r.toString() + "\n").getBytes());
+            stream.flush();
+        }catch (IOException e) {
+            e.printStackTrace();
+        }
     }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultEncryption.java b/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultEncryption.java
index 8c787db..64f3b5e 100644
--- a/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultEncryption.java
+++ b/src/main/java/de/ids_mannheim/korap/interfaces/defaults/DefaultEncryption.java
@@ -14,6 +14,7 @@
  * @author hanl
  * @date 05/06/2015
  */
+//todo: init!
 public class DefaultEncryption implements EncryptionIface {
 
     private SecureRandom randomizer;
diff --git a/src/main/java/de/ids_mannheim/korap/resource/LayerMapper.java b/src/main/java/de/ids_mannheim/korap/resource/LayerMapper.java
index 4b31330..f5f339d 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/LayerMapper.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/LayerMapper.java
@@ -1,5 +1,7 @@
 package de.ids_mannheim.korap.resource;
 
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.user.UserSettings;
 
 /**
@@ -9,11 +11,15 @@
 public class LayerMapper {
 
     private UserSettings settings;
-    private ExtConfiguration config;
+    private KustvaktConfiguration config;
 
     public LayerMapper(UserSettings settings) {
         this.settings = settings;
-        this.config = ExtensionBeans.getConfiguration();
+        this.config = BeanConfiguration.getConfiguration();
+    }
+
+    public LayerMapper() {
+        this.config = BeanConfiguration.getConfiguration();
     }
 
     /**
diff --git a/src/main/java/de/ids_mannheim/korap/resource/LayerProcessor.java b/src/main/java/de/ids_mannheim/korap/resource/LayerProcessor.java
index 78fd005..71d30b1 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/LayerProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/LayerProcessor.java
@@ -1,8 +1,44 @@
 package de.ids_mannheim.korap.resource;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.Iterator;
+
 /**
  * @author hanl
  * @date 19/06/2015
  */
-public class LayerProcessor {
+public class LayerProcessor extends NodeProcessor {
+
+    private LayerMapper mapper;
+
+    public LayerProcessor() {
+        this.mapper = new LayerMapper();
+    }
+
+    @Override
+    public JsonNode process(JsonNode node) {
+        if (node.at("/query/wrap/@type").asText().equals("koral:term")) {
+            JsonNode n = node.at("/query/wrap");
+            if (n.path("foundry").isMissingNode()) {
+                String layer = n.path("layer").asText();
+                ObjectNode obj = (ObjectNode) n;
+                obj.put("foundry", mapper.findFoundry(layer));
+            }
+        }else if (node.at("/query/wrap/@type").asText()
+                .equals("koral:termGroup")) {
+            Iterator<JsonNode> nodes = node.at("/query/wrap/operands")
+                    .elements();
+            while (nodes.hasNext()) {
+                JsonNode n = nodes.next();
+                if (n.path("foundry").isMissingNode()) {
+                    String layer = n.path("layer").asText();
+                    ObjectNode obj = (ObjectNode) n;
+                    obj.put("foundry", mapper.findFoundry(layer));
+                }
+            }
+        }
+        return node;
+    }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/resource/NodeProcessor.java b/src/main/java/de/ids_mannheim/korap/resource/NodeProcessor.java
index 4f27347..c7a9451 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/NodeProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/NodeProcessor.java
@@ -1,8 +1,13 @@
 package de.ids_mannheim.korap.resource;
 
+import com.fasterxml.jackson.databind.JsonNode;
+
 /**
  * @author hanl
  * @date 19/06/2015
  */
-public class NodeProcessor {
+public abstract class NodeProcessor {
+
+    public abstract JsonNode process(JsonNode node);
+
 }
diff --git a/src/main/java/de/ids_mannheim/korap/resource/RewriteProcessor.java b/src/main/java/de/ids_mannheim/korap/resource/RewriteProcessor.java
index 9950266..6031261 100644
--- a/src/main/java/de/ids_mannheim/korap/resource/RewriteProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/resource/RewriteProcessor.java
@@ -1,8 +1,41 @@
 package de.ids_mannheim.korap.resource;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * @author hanl
  * @date 19/06/2015
  */
 public class RewriteProcessor {
+
+    private KustvaktConfiguration config;
+    private List<NodeProcessor> processors;
+
+    public RewriteProcessor() {
+        this.config = BeanConfiguration.getConfiguration();
+        this.processors = new ArrayList<>();
+        addProcessor(new LayerProcessor());
+    }
+
+    public JsonNode process(JsonNode node) {
+        for (NodeProcessor p : this.processors)
+            node = p.process(node);
+        return node;
+    }
+
+    public String process(String json) {
+        JsonNode node = JsonUtils.readTree(json);
+        return JsonUtils.toJSON(process(node));
+    }
+
+    public void addProcessor(NodeProcessor processor) {
+        this.processors.add(processor);
+    }
+
 }
diff --git a/src/main/java/de/ids_mannheim/korap/utils/CollectionQueryBuilder3.java b/src/main/java/de/ids_mannheim/korap/utils/CollectionQueryBuilder3.java
index 1217085..1b52306 100644
--- a/src/main/java/de/ids_mannheim/korap/utils/CollectionQueryBuilder3.java
+++ b/src/main/java/de/ids_mannheim/korap/utils/CollectionQueryBuilder3.java
@@ -1,5 +1,7 @@
 package de.ids_mannheim.korap.utils;
 
+import de.ids_mannheim.korap.query.serialize.CollectionQueryProcessor;
+
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.LinkedList;
@@ -64,10 +66,9 @@
         List list = new ArrayList();
         if (!this.rq.isEmpty())
             list.addAll(this.rq);
-        System.out.println("RAW QUERY " + this.builder.toString());
-//        CollectionQueryProcessor tree = new CollectionQueryProcessor(this.verbose);
-//        tree.process(this.builder.toString());
-//        list.add(tree.getRequestMap());
+        CollectionQueryProcessor tree = new CollectionQueryProcessor(this.verbose);
+        tree.process(this.builder.toString());
+        list.add(tree.getRequestMap());
         return list;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/web/Kustvakt.java b/src/main/java/de/ids_mannheim/korap/web/Kustvakt.java
index f9349e6..a8e4c94 100644
--- a/src/main/java/de/ids_mannheim/korap/web/Kustvakt.java
+++ b/src/main/java/de/ids_mannheim/korap/web/Kustvakt.java
@@ -6,6 +6,8 @@
 import de.ids_mannheim.korap.config.BeanConfiguration;
 import de.ids_mannheim.korap.utils.KorAPLogger;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 
 /**
@@ -15,10 +17,17 @@
 public class Kustvakt {
 
     private static Integer PORT = -1;
+    private static String CONFIG = null;
 
     public static void main(String[] args) throws Exception {
         attributes(args);
-        BeanConfiguration.loadContext();
+        BeanConfiguration.loadClasspathContext();
+
+        if (CONFIG != null) {
+            BeanConfiguration.getConfiguration()
+                    .setProperties(new FileInputStream(new File(CONFIG)));
+
+        }
         grizzlyServer(PORT);
     }
 
@@ -44,6 +53,9 @@
                 case "--debug":
                     KorAPLogger.DEBUG = true;
                     break;
+                case "config":
+                    CONFIG = args[i + 1];
+                    break;
                 case "--port":
                     PORT = Integer.valueOf(args[i + 1]);
                     break;
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/LightService.java b/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
index bd1be22..35e1c6b 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
@@ -1,18 +1,14 @@
 package de.ids_mannheim.korap.web.service;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 import de.ids_mannheim.korap.config.BeanConfiguration;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KorAPException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
-import de.ids_mannheim.korap.graphdb.collo.ColloQuery;
-import de.ids_mannheim.korap.graphdb.collo.Dependency;
-import de.ids_mannheim.korap.query.serialize.IdWriter;
 import de.ids_mannheim.korap.query.serialize.MetaQueryBuilder;
 import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.resource.RewriteProcessor;
 import de.ids_mannheim.korap.utils.CollectionQueryBuilder;
-import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.KorAPLogger;
 import de.ids_mannheim.korap.utils.StringUtils;
 import de.ids_mannheim.korap.web.ClientsHandler;
@@ -25,13 +21,13 @@
 import javax.ws.rs.core.*;
 import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 /**
  * @author hanl
  * @date 29/01/2014
  */
+//todd test functions
 @Path("v0.1" + "/")
 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
 public class LightService {
@@ -40,68 +36,19 @@
 
     private SearchLucene searchLucene;
     private ClientsHandler graphDBhandler;
+    private RewriteProcessor processor;
 
     public LightService() {
         this.searchLucene = new SearchLucene(
                 BeanConfiguration.getConfiguration().getIndexDir());
         UriBuilder builder = UriBuilder.fromUri("http://10.0.10.13").port(9997);
         this.graphDBhandler = new ClientsHandler(builder.build());
-    }
-
-    /**
-     * @param properties
-     * @param sfs
-     * @param limit
-     * @param query
-     * @param ql
-     * @param context
-     * @param foundry
-     * @param wPaths
-     * @return
-     */
-    //todo: add entry point where a json collection can be injected as well
-    @GET
-    @Path("colloc")
-    public Response getCollocationsAll(@QueryParam("props") String properties,
-            @QueryParam("sfskip") Integer sfs,
-            @QueryParam("sflimit") Integer limit, @QueryParam("q") String query,
-            @QueryParam("ql") String ql, @QueryParam("context") Integer context,
-            @QueryParam("foundry") String foundry,
-            @QueryParam("paths") Boolean wPaths) {
-        ColloQuery.ColloQueryBuilder builder;
-        String result;
-        try {
-            builder = new ColloQuery.ColloQueryBuilder();
-            QuerySerializer s = new QuerySerializer().setQuery(query, ql);
-            //todo: fix collectionbuilder
-            //        s.setDeprCollection(builder);
-            IdWriter writer = new IdWriter(s.toJSON()).process();
-            List collprops = JsonUtils.convertToList(properties);
-
-            if (context != null)
-                builder.context(context).collocateProps(collprops);
-            if (limit != null)
-                builder.surfaceLimit(limit);
-            if (sfs != null)
-                builder.surfaceSkip(sfs);
-            if (foundry != null)
-                builder.foundry(foundry);
-            builder.nodeQueryJS(writer.toJSON())
-                    .dep(new ArrayList<Dependency>()).withPaths(wPaths);
-
-            result = graphDBhandler
-                    .getResponse("distCollo", "q", builder.build().toJSON());
-        }catch (KorAPException e) {
-            throw KustvaktResponseHandler.throwit(e);
-        }catch (JsonProcessingException e) {
-            throw KustvaktResponseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT);
-        }
-        return Response.ok(result).build();
+        this.processor = new RewriteProcessor();
     }
 
     @POST
     @Path("colloc")
-    public Response getCollocatioBase(@QueryParam("q") String query) {
+    public Response getCollocationBase(@QueryParam("q") String query) {
         String result;
         try {
             result = graphDBhandler.getResponse("distCollo", "q", query);
@@ -111,6 +58,7 @@
         return Response.ok(result).build();
     }
 
+    // todo
     public Response postMatchFavorite() {
         return Response.ok().build();
     }
@@ -140,7 +88,7 @@
         ss.addMeta(meta);
         //todo: test this
         ss.setCollection(cq);
-        return Response.ok(ss.toJSON()).build();
+        return Response.ok(processor.process(ss.toJSON())).build();
     }
 
     @POST
@@ -149,11 +97,10 @@
             String jsonld) {
         KustvaktConfiguration.BACKENDS eng = BeanConfiguration
                 .getConfiguration().chooseBackend(engine);
-
+        jsonld = processor.process(jsonld);
         // todo: should be possible to add the meta part to the query serialization
         jlog.info("Serialized search: {}", jsonld);
 
-        // todo: Security Parsing and rewrite
         // fixme: to use the systemarchitecture pointcut thingis, searchlucene must be injected via
         String result = searchLucene.search(jsonld);
         KorAPLogger.QUERY_LOGGER.trace("The result set: {}", result);
@@ -181,8 +128,7 @@
         //        meta.addEntry("itemsPerResource", 1);
         serializer.addMeta(meta);
 
-        // policy rewrite!
-        String query = serializer.toJSON();
+        String query = processor.process(serializer.toJSON());
         jlog.info("the serialized query {}", query);
 
         if (eng.equals(KustvaktConfiguration.BACKENDS.NEO4J)) {
@@ -248,7 +194,7 @@
             // should only apply to CQL queries
             //                meta.addEntry("itemsPerResource", 1);
             s.addMeta(meta);
-            query = s.toJSON();
+            query = processor.process(s.toJSON());
         }
         String result;
         try {
@@ -326,7 +272,6 @@
                 results = searchLucene
                         .getMatch(matchid, new ArrayList<>(f_list),
                                 new ArrayList<>(l_list), spans, false, true);
-
             }
         }
         try {
diff --git a/src/main/java/de/ids_mannheim/korap/web/utils/FormWrapper.java b/src/main/java/de/ids_mannheim/korap/web/utils/FormWrapper.java
index b1dad85..913c848 100644
--- a/src/main/java/de/ids_mannheim/korap/web/utils/FormWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/web/utils/FormWrapper.java
@@ -6,10 +6,11 @@
 import java.util.Map;
 
 /**
- * @author hanl
- * @date 18/05/2015
  * Helper class to wrapp multivaluedmap into a hashmap. Depending on the strict parameter,
  * list values are retained in the resulting wrapper map.
+ *
+ * @author hanl
+ * @date 18/05/2015
  */
 public class FormWrapper extends HashMap<String, Object> {
 
diff --git a/src/main/resources/default-config.xml b/src/main/resources/default-config.xml
index 7e3c557..f0c380f 100644
--- a/src/main/resources/default-config.xml
+++ b/src/main/resources/default-config.xml
@@ -8,8 +8,6 @@
                             http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 
 
-    <!--<context:property-placeholder location="classpath:kustvakt.conf"/>-->
-
     <util:properties id="props" location="classpath:kustvakt.conf"/>
 
     <bean id="auditingProvider"
diff --git a/src/test/java/RewriteTest.java b/src/test/java/RewriteTest.java
index 70bf72a..db33a1a 100644
--- a/src/test/java/RewriteTest.java
+++ b/src/test/java/RewriteTest.java
@@ -1,6 +1,58 @@
+import de.ids_mannheim.korap.config.BeanConfiguration;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.resource.LayerMapper;
+import de.ids_mannheim.korap.resource.RewriteProcessor;
+import de.ids_mannheim.korap.utils.CollectionQueryBuilder3;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
 /**
  * @author hanl
  * @date 18/06/2015
  */
 public class RewriteTest {
+
+    private static String simple_add_query = "[pos=ADJA]";
+    private static String simple_rewrite_query = "[base=Haus]";
+    private static String complex_rewrite_query = "<c=INFC>";
+    private static String complex_rewrite_query2 = "[orth=laufe/i & base!=Lauf]";
+
+    public RewriteTest() {
+
+    }
+
+    @BeforeClass
+    public static void init() {
+        BeanConfiguration.loadFileContext(
+                "/Users/hanl/Projects/KorAP-project/KorAP-modules/Kustvakt-core/src/main/resources/default-config.xml");
+    }
+
+    @Test
+    public void testQuery() {
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery(simple_add_query, "poliqarp");
+        System.out.println("query " + s.toJSON());
+
+        CollectionQueryBuilder3 b = new CollectionQueryBuilder3();
+        b.add("textClass=politik & corpusID=WPD");
+        System.out.println("collection query " + b.toJSON());
+    }
+
+    @Test
+    public void testLayerMapper() {
+        LayerMapper m = new LayerMapper();
+        System.out.println("foundry " + m.findFoundry("lemma"));
+        System.out.println("foundry " + m.findFoundry("surface"));
+        System.out.println("foundry " + m.findFoundry("pos"));
+    }
+
+    @Test
+    public void testRewrite() {
+        RewriteProcessor processor = new RewriteProcessor();
+
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery(complex_rewrite_query2, "poliqarp");
+        System.out.println("query " + s.toJSON());
+        System.out.println("finished node " + processor.process(s.toJSON()));
+    }
 }