usersettings injection for foundry rewrite
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 e4a8e4b..56ff658 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
@@ -1,9 +1,13 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import de.ids_mannheim.korap.config.BeanConfiguration;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.handlers.UserSettingsDao;
 import de.ids_mannheim.korap.resource.LayerMapper;
 import de.ids_mannheim.korap.user.User;
+import de.ids_mannheim.korap.user.UserSettings;
 
 /**
  * @author hanl
@@ -11,15 +15,17 @@
  */
 public class FoundryInject implements RewriteTask.IterableRewriteAt {
 
-
     @Override
     public JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
-            User user) {
+            User user) throws KustvaktException {
         LayerMapper mapper;
         // inject user settings from cache!
-        if (user != null)
-            mapper = new LayerMapper(config, null);
-        else
+        if (user != null && BeanConfiguration.hasContext()) {
+            UserSettingsDao dao = new UserSettingsDao(
+                    BeanConfiguration.getBeans().getPersistenceClient());
+            UserSettings settings = dao.get(user);
+            mapper = new LayerMapper(config, settings);
+        }else
             mapper = new LayerMapper(config);
 
         if (node.get("@type").equals("koral:term") && !node.has("foundry")) {
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 c1fefa0..b6f7785 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
@@ -2,6 +2,7 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import org.slf4j.Logger;
@@ -167,13 +168,19 @@
                     continue;
                 }
             }
-
-            if (!post && task instanceof RewriteTask.RewriteBefore)
-                ((RewriteTask.RewriteBefore) task)
-                        .preProcess(node, this.config, user);
-            else if (task instanceof RewriteTask.RewriteAfter)
-                ((RewriteTask.RewriteAfter) task).postProcess(node);
-
+            try {
+                if (!post && task instanceof RewriteTask.RewriteBefore) {
+                    ((RewriteTask.RewriteBefore) task)
+                            .preProcess(node, this.config, user);
+                }else if (task instanceof RewriteTask.RewriteAfter) {
+                    ((RewriteTask.RewriteAfter) task).postProcess(node);
+                }
+            }catch (KustvaktException e) {
+                jlog.error("Error in rewrite processor {} for node {}",
+                        task.getClass().getSimpleName(),
+                        node.rawNode().toString());
+                e.printStackTrace();
+            }
             if (node.isRemove())
                 break;
         }
@@ -189,14 +196,19 @@
                 if ((rwa.at() != null && !node.at(rwa.at()).isMissingNode()))
                     next = node.at(rwa.at());
             }
-
-            if (!post && task instanceof RewriteTask.RewriteBefore)
-                ((RewriteTask.RewriteBefore) task)
-                        .preProcess(KoralNode.wrapNode(next), this.config,
-                                user);
-            else
-                ((RewriteTask.RewriteAfter) task)
-                        .postProcess(KoralNode.wrapNode(next));
+            try {
+                if (!post && task instanceof RewriteTask.RewriteBefore)
+                    ((RewriteTask.RewriteBefore) task)
+                            .preProcess(KoralNode.wrapNode(next), this.config,
+                                    user);
+                else
+                    ((RewriteTask.RewriteAfter) task)
+                            .postProcess(KoralNode.wrapNode(next));
+            }catch (KustvaktException e) {
+                jlog.error("Error in rewrite processor {} for node {}",
+                        task.getClass().getSimpleName(), node.toString());
+                e.printStackTrace();
+            }
         }
     }
 
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 47b65ae..8166961 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
@@ -2,6 +2,7 @@
 
 import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.user.User;
 
 /**
@@ -18,7 +19,7 @@
          * @return
          */
         JsonNode preProcess(KoralNode node, KustvaktConfiguration config,
-                User user);
+                User user) throws KustvaktException;
     }
 
     /**
@@ -27,7 +28,7 @@
      * after {@link de.ids_mannheim.korap.resource.rewrite.RewriteTask.IterableRewriteAt} have been processed
      */
     interface RewriteAfter extends RewriteTask {
-        JsonNode postProcess(KoralNode node);
+        JsonNode postProcess(KoralNode node) throws KustvaktException;
     }
 
     /**
diff --git a/src/test/java/de/ids_mannheim/korap/config/TestHelper.java b/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
index 6ddfe1b..ae35e02 100644
--- a/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
+++ b/src/test/java/de/ids_mannheim/korap/config/TestHelper.java
@@ -43,6 +43,9 @@
             m.put(Attributes.LASTNAME, "user");
             m.put(Attributes.EMAIL, "test@ids-mannheim.de");
             m.put(Attributes.ADDRESS, "Mannheim");
+            m.put(Attributes.DEFAULT_LEMMA_FOUNDRY, "test_l");
+            m.put(Attributes.DEFAULT_POS_FOUNDRY, "test_p");
+            m.put(Attributes.DEFAULT_CONST_FOUNDRY, "test_const");
 
             Assert.assertNotNull("userdatabase handler must not be null", dao);
 
diff --git a/src/test/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandlerTest.java b/src/test/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandlerTest.java
index d8638f6..eff4638 100644
--- a/src/test/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandlerTest.java
+++ b/src/test/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandlerTest.java
@@ -1,7 +1,11 @@
 package de.ids_mannheim.korap.resource.rewrite;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import de.ids_mannheim.korap.config.BeanConfiguration;
-import org.junit.AfterClass;
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
+import de.ids_mannheim.korap.config.TestHelper;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.utils.JsonUtils;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -13,7 +17,14 @@
 
     @BeforeClass
     public static void setup() {
-        BeanConfiguration.loadClasspathContext();
+        BeanConfiguration.loadClasspathContext("default-config.xml");
+        TestHelper.setupAccount();
+    }
+
+    //    @AfterClass
+    public static void close() {
+        TestHelper.dropUser();
+        BeanConfiguration.closeApplication();
     }
 
     @Test
@@ -23,17 +34,42 @@
     }
 
     @Test
-    public void testRewriteTastAdd() {
+    public void testRewriteTaskAdd() {
         RewriteHandler handler = new RewriteHandler(null);
-        handler.add(FoundryInject.class);
-        handler.add(DocMatchRewrite.class);
-        handler.add(CollectionCleanupFilter.class);
-        handler.add(IdWriter.class);
+        assert handler.add(FoundryInject.class);
+        assert handler.add(DocMatchRewrite.class);
+        assert handler.add(CollectionCleanupFilter.class);
+        assert handler.add(IdWriter.class);
     }
 
-    @AfterClass
-    public static void close() {
-        BeanConfiguration.closeApplication();
+    @Test
+    public void testRewriteFoundryInjectLemma() {
+        KustvaktConfiguration c = BeanConfiguration.getBeans()
+                .getConfiguration();
+        RewriteHandler handler = new RewriteHandler(c);
+        handler.add(FoundryInject.class);
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery("[base=Haus]", "poliqarp");
+        String result = handler.preProcess(s.toJSON(), TestHelper.getUser());
+        JsonNode node = JsonUtils.readTree(result);
+        assert node != null;
+        assert node.at("/query/wrap/layer").asText().equals("lemma");
+        assert node.at("/query/wrap/foundry").asText().equals("test_l");
+    }
+
+    @Test
+    public void testRewriteFoundryInjectPOS() {
+        KustvaktConfiguration c = BeanConfiguration.getBeans()
+                .getConfiguration();
+        RewriteHandler handler = new RewriteHandler(c);
+        handler.add(FoundryInject.class);
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery("[pos=ADJA]", "poliqarp");
+        String result = handler.preProcess(s.toJSON(), TestHelper.getUser());
+        JsonNode node = JsonUtils.readTree(result);
+        assert node != null;
+        assert node.at("/query/wrap/layer").asText().equals("pos");
+        assert node.at("/query/wrap/foundry").asText().equals("test_p");
     }
 
 }