Added more fixes for missing exceptions in JsonUtils.

Change-Id: I7c4bd43aa23b6041dbc837c9d025a3cf13b9de25
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/ClientInfo.java b/core/src/main/java/de/ids_mannheim/korap/config/ClientInfo.java
index 5344b98..bf98fe9 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/ClientInfo.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/ClientInfo.java
@@ -1,6 +1,8 @@
 package de.ids_mannheim.korap.config;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import lombok.Data;
 
@@ -28,7 +30,7 @@
     }
 
 
-    public String toJSON () {
+    public String toJSON () throws KustvaktException {
         ObjectNode node = JsonUtils.createObjectNode();
         node.put("client_id", client_id);
         node.put("client_secret", client_secret);
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
index 7c3a1cf..7b75f37 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/KustvaktConfiguration.java
@@ -96,6 +96,12 @@
 
 	private String ldapConfig;
 
+    private String freeOnlyRegex;
+
+    private String publicOnlyRegex;
+
+    private String allOnlyRegex;
+
 	public KustvaktConfiguration (Properties properties) throws IOException {
         load(properties);
     }
@@ -172,25 +178,16 @@
         
         ldapConfig = properties.getProperty("ldap.config");
         
-        // EM: regex for matching availability in Krill matches
-        freeLicensePattern = compilePattern(properties.getProperty("kustvakt.regex.free",""));
-        publicLicensePattern = compilePattern(properties.getProperty("kustvakt.regex.public",""));
-        allLicensePattern = compilePattern(properties.getProperty("kustvakt.regex.all",""));
+        // EM: regex used for storing vc
+        freeOnlyRegex = properties.getProperty("kustvakt.regex.free","");
+        publicOnlyRegex = properties.getProperty("kustvakt.regex.public","");
+        allOnlyRegex = properties.getProperty("kustvakt.regex.all","");
         
-        // EM: not use in the future
-        //policyConfig = properties.getProperty("policies.config");
-        //setFoundriesAndLayers(policyConfig);
+        // EM: pattern for matching availability in Krill matches
+        freeLicensePattern = compilePattern(freeOnlyRegex);
+        publicLicensePattern = compilePattern(freeOnlyRegex + "|" + publicOnlyRegex);
+        allLicensePattern = compilePattern(freeOnlyRegex + "|" + publicOnlyRegex + "|"+allOnlyRegex);
         
-//        KUSTVAKT_USER.put(Attributes.ID, Integer.parseInt(properties.getProperty("kustvakt.init.user.id")));
-//        KUSTVAKT_USER.put(Attributes.USERNAME, properties.getProperty("kustvakt.init.user.username"));
-//        KUSTVAKT_USER.put(Attributes.PASSWORD, properties.getProperty("kustvakt.init.user.password"));
-//        KUSTVAKT_USER.put(Attributes.EMAIL, properties.getProperty("kustvakt.init.user.email"));
-//        KUSTVAKT_USER.put(Attributes.COUNTRY, properties.getProperty("kustvakt.init.user.country"));
-//        KUSTVAKT_USER.put(Attributes.ADDRESS, properties.getProperty("kustvakt.init.user.address"));
-//        KUSTVAKT_USER.put(Attributes.FIRSTNAME, properties.getProperty("kustvakt.init.user.firstname"));
-//        KUSTVAKT_USER.put(Attributes.LASTNAME, properties.getProperty("kustvakt.init.user.lastname"));
-//        KUSTVAKT_USER.put(Attributes.INSTITUTION, properties.getProperty("kustvakt.init.user.institution"));
-//        KUSTVAKT_USER.put(Attributes.IS_ADMIN, properties.getProperty("kustvakt.init.user.admin"));
         return properties;
     }
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/config/Scopes.java b/core/src/main/java/de/ids_mannheim/korap/config/Scopes.java
index 0262cc2..1cc07cf 100644
--- a/core/src/main/java/de/ids_mannheim/korap/config/Scopes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/config/Scopes.java
@@ -1,5 +1,6 @@
 package de.ids_mannheim.korap.config;
 
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.user.Userdata;
 import de.ids_mannheim.korap.utils.JsonUtils;
 
@@ -80,7 +81,7 @@
     }
 
 
-    public String toEntity () {
+    public String toEntity () throws KustvaktException {
         if (this.values.isEmpty())
             return "";
         return JsonUtils.toJSON(this.values);
diff --git a/core/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java b/core/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
index e435b5b..2352b57 100644
--- a/core/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
+++ b/core/src/main/java/de/ids_mannheim/korap/exceptions/KustvaktException.java
@@ -81,6 +81,11 @@
         this.statusCode = status;
         this.entity = entity;
     }
+    
+    public KustvaktException (int status, String message, Exception e) {
+        super(message, e);
+        this.statusCode = status;
+    }
 
 
     public KustvaktException (Throwable cause, int status) {
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
index 189cdd5..e2e6318 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/CollectionRewrite.java
@@ -22,6 +22,7 @@
 /**
  * @author margaretha
  * @date 2 June 2017
+ * @last 9 Nov 2017
  */
 public class CollectionRewrite implements RewriteTask.RewriteQuery {
 
@@ -63,21 +64,21 @@
         List<String> userAvailabilities = new ArrayList<String>();
         switch (user.getCorpusAccess()) {
             case PUB:
-                userAvailabilities.add("CC-BY.*");
-                userAvailabilities.add("ACA.*");
+                userAvailabilities.add(config.getFreeOnlyRegex());
+                userAvailabilities.add(config.getPublicOnlyRegex());
 //                builder.with(
 //                        "availability = /CC-BY.*/ | availability = /ACA.*/");
                 break;
             case ALL:
-                userAvailabilities.add("CC-BY.*");
-                userAvailabilities.add("ACA.*");
-                userAvailabilities.add("QAO.*");
+                userAvailabilities.add(config.getFreeOnlyRegex());
+                userAvailabilities.add(config.getPublicOnlyRegex());
+                userAvailabilities.add(config.getAllOnlyRegex());
 
 //                builder.with("availability = /QAO.*/ | availability = /ACA.*/ |"
 //                        + "  availability = /CC-BY.*/");
                 break;
             case FREE:
-                userAvailabilities.add("CC-BY.*");
+                userAvailabilities.add(config.getFreeOnlyRegex());
 //                builder.with("availability   = /CC-BY.*/");
                 break;
         }
diff --git a/core/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java b/core/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
index 20908ad..6412835 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resources/KustvaktResource.java
@@ -112,7 +112,7 @@
     }
 
 
-    public String getStringData () {
+    public String getStringData () throws KustvaktException {
         return JsonUtils.toJSON(this.fields);
     }
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java b/core/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
index 76cc144..7d2b770 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resources/ResourceFactory.java
@@ -66,7 +66,7 @@
     }
 
 
-    public static <T extends KustvaktResource> T createID (T resource) {
+    public static <T extends KustvaktResource> T createID (T resource) throws KustvaktException {
         if (resource.getData() != null && !resource.getStringData().isEmpty()){
         	resource.setPersistentID(DigestUtils.sha1Hex(JsonUtils.toJSON(resource)));        	
         }
@@ -110,7 +110,7 @@
 
 
     public static VirtualCollection getPermanentCollection (
-            VirtualCollection mergable, String corpusName, String description) {
+            VirtualCollection mergable, String corpusName, String description) throws KustvaktException {
         VirtualCollection v = new VirtualCollection();
         v.merge(mergable);
         v.setName(corpusName);
diff --git a/core/src/main/java/de/ids_mannheim/korap/security/auth/BasicHttpAuth.java b/core/src/main/java/de/ids_mannheim/korap/security/auth/BasicHttpAuth.java
index 0da09f1..ba1a26c 100644
--- a/core/src/main/java/de/ids_mannheim/korap/security/auth/BasicHttpAuth.java
+++ b/core/src/main/java/de/ids_mannheim/korap/security/auth/BasicHttpAuth.java
@@ -1,6 +1,11 @@
 package de.ids_mannheim.korap.security.auth;
 
-import com.sun.org.apache.xpath.internal.SourceTree;
+import java.util.Map;
+
+import org.apache.commons.codec.binary.Base64;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.config.BeansFactory;
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.config.Scopes;
@@ -9,18 +14,11 @@
 import de.ids_mannheim.korap.interfaces.AuthenticationIface;
 import de.ids_mannheim.korap.interfaces.EncryptionIface;
 import de.ids_mannheim.korap.interfaces.db.EntityHandlerIface;
-import de.ids_mannheim.korap.config.Attributes;
 import de.ids_mannheim.korap.user.KorAPUser;
 import de.ids_mannheim.korap.user.TokenContext;
 import de.ids_mannheim.korap.user.User;
-import de.ids_mannheim.korap.utils.NamingUtils;
 import de.ids_mannheim.korap.utils.StringUtils;
 import de.ids_mannheim.korap.utils.TimeUtils;
-import org.apache.commons.codec.binary.Base64;
-import org.apache.oltu.oauth2.common.utils.OAuthUtils;
-
-import java.beans.Beans;
-import java.util.Map;
 
 /**
  * @author hanl
@@ -29,8 +27,13 @@
 // todo: bean injection!
 public class BasicHttpAuth implements AuthenticationIface {
 
+    @Autowired
     private KustvaktConfiguration config;
-
+    @Autowired
+    private EncryptionIface crypto;
+    @Autowired
+    private EntityHandlerIface dao;
+    
     public BasicHttpAuth() {
 
     }
@@ -77,11 +80,6 @@
     public TokenContext getTokenContext(String authToken)
             throws KustvaktException {
         //fixme: handle via constructor
-        this.config = BeansFactory.getKustvaktContext().getConfiguration();
-        EncryptionIface crypto = BeansFactory.getKustvaktContext()
-                .getEncryption();
-        EntityHandlerIface dao = BeansFactory.getKustvaktContext()
-                .getUserDBHandler();
         String[] values = decode(authToken);
         if (values != null) {
             TokenContext c = new TokenContext();
diff --git a/core/src/main/java/de/ids_mannheim/korap/user/DataFactory.java b/core/src/main/java/de/ids_mannheim/korap/user/DataFactory.java
index e9b1387..4b14b42 100644
--- a/core/src/main/java/de/ids_mannheim/korap/user/DataFactory.java
+++ b/core/src/main/java/de/ids_mannheim/korap/user/DataFactory.java
@@ -65,7 +65,7 @@
     public abstract boolean removeValue (Object data, String field);
 
 
-    public abstract String toStringValue (Object data);
+    public abstract String toStringValue (Object data) throws KustvaktException;
 
     public abstract Object filter(Object data, String ... keys);
 
@@ -210,7 +210,7 @@
 
 
         @Override
-        public String toStringValue (Object data) {
+        public String toStringValue (Object data) throws KustvaktException {
             if (data instanceof JsonNode)
                 return JsonUtils.toJSON(data);
             return data.toString();
diff --git a/core/src/main/java/de/ids_mannheim/korap/user/TokenContext.java b/core/src/main/java/de/ids_mannheim/korap/user/TokenContext.java
index d3b1944..9748861 100644
--- a/core/src/main/java/de/ids_mannheim/korap/user/TokenContext.java
+++ b/core/src/main/java/de/ids_mannheim/korap/user/TokenContext.java
@@ -137,7 +137,7 @@
     }
 
 
-    public String toJson() {
+    public String toJson() throws KustvaktException {
         return JsonUtils.toJSON(this.statusMap());
     }
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/utils/JsonUtils.java b/core/src/main/java/de/ids_mannheim/korap/utils/JsonUtils.java
index d011ba7..b018688 100644
--- a/core/src/main/java/de/ids_mannheim/korap/utils/JsonUtils.java
+++ b/core/src/main/java/de/ids_mannheim/korap/utils/JsonUtils.java
@@ -26,12 +26,13 @@
     private JsonUtils () {}
 
 
-    public static String toJSON (Object values) {
+    public static String toJSON (Object values) throws KustvaktException {
         try {
             return mapper.writeValueAsString(values);
         }
         catch (JsonProcessingException e) {
-            return e.getMessage();
+            throw new KustvaktException(StatusCodes.SERIALIZATION_FAILED,
+                    "Failed serializing object in json", e);
         }
     }
 
@@ -90,7 +91,7 @@
         }
         catch (IOException e) {
             throw new KustvaktException(StatusCodes.DESERIALIZATION_FAILED,
-                    "Failed deserializing json object: " + json, json, e);
+                    e.getMessage(), json, e);
         }
         return t;
     }
diff --git a/core/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java b/core/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
index 361ab2b..71b8d57 100644
--- a/core/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
+++ b/core/src/main/java/de/ids_mannheim/korap/utils/KoralCollectionQueryBuilder.java
@@ -2,8 +2,6 @@
 
 import java.util.List;
 
-import javax.ws.rs.WebApplicationException;
-
 import org.springframework.beans.factory.annotation.Autowired;
 
 import com.fasterxml.jackson.databind.JsonNode;
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/AdminController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/AdminController.java
index 53f0acf..c12f5cf 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/AdminController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/AdminController.java
@@ -85,9 +85,15 @@
         catch (NumberFormatException | NullPointerException e) {
             throw kustvaktResponseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT);
         }
-        String result = JsonUtils.toJSON(auditingController.retrieveRecords(
-                AuditRecord.CATEGORY.valueOf(type.toUpperCase()), from_date,
-                until_date, dayOnly, integer_limit));
+        String result="";
+        try {
+            result = JsonUtils.toJSON(auditingController.retrieveRecords(
+                    AuditRecord.CATEGORY.valueOf(type.toUpperCase()), from_date,
+                    until_date, dayOnly, integer_limit));
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
         // limit number of records to return
         return Response.ok(result).build();
     }
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/AnnotationController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/AnnotationController.java
index ac4f69f..dbeb447 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/AnnotationController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/AnnotationController.java
@@ -57,8 +57,12 @@
     @GET
     @Path("layers")
     public Response getLayers () {
-        String result = JsonUtils.toJSON(annotationService.getLayerDtos());
-        return Response.ok(result).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(annotationService.getLayerDtos())).build();
+        }
+        catch (KustvaktException e) {
+            throw responseHandler.throwit(e);
+        }
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/AuthenticationController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/AuthenticationController.java
index f772224..75a772e 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/AuthenticationController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/AuthenticationController.java
@@ -88,7 +88,12 @@
                 .getQueryLanguages());
         m.put("SortTypes", null); // types of sorting that are supported!
         m.put("version", ServiceInfo.getInfo().getVersion());
-        return Response.ok(JsonUtils.toJSON(m)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(m)).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
@@ -102,7 +107,12 @@
             @HeaderParam(ContainerRequest.HOST) String host,
             @Context Locale locale) {
         TokenContext ctx = (TokenContext) context.getUserPrincipal();
-        return Response.ok(ctx.toJson()).build();
+        try {
+            return Response.ok(ctx.toJson()).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
@@ -200,7 +210,12 @@
             throw kustvaktResponseHandler.throwit(e);
         }
 
-        return Response.ok(context.toJson()).build();
+        try {
+            return Response.ok(context.toJson()).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
@@ -299,7 +314,12 @@
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        return Response.ok().entity(context.toJson()).build();
+        try {
+            return Response.ok().entity(context.toJson()).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/KustvaktController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/KustvaktController.java
index f581f37..cf3cfaa 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/KustvaktController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/KustvaktController.java
@@ -1,11 +1,14 @@
 package de.ids_mannheim.korap.web.controller;
 
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.server.KustvaktServer;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.utils.ServiceInfo;
+import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
@@ -22,6 +25,8 @@
 public class KustvaktController {
 
     private static Logger jlog = LoggerFactory.getLogger(UserController.class);
+    @Autowired
+    private KustvaktResponseHandler kustvaktResponseHandler;
 
 
     @Path("info")
@@ -30,7 +35,12 @@
         m.put("version", ServiceInfo.getInfo().getVersion());
         m.put("recent_api_version", KustvaktServer.API_VERSION);
         m.put("service_name", ServiceInfo.getInfo().getName());
-        return Response.ok(JsonUtils.toJSON(m)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(m)).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuthController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuthController.java
index c21c406..2bc03d7 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuthController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/OAuthController.java
@@ -110,14 +110,16 @@
                     "Missing parameter!", "redirect_url");
         info.setRedirect_uri(rurl);
         TokenContext ctx = (TokenContext) context.getUserPrincipal();
+        String json = "";
         try {
             User user = this.controller.getUser(ctx.getUsername());
             this.handler.getPersistenceHandler().registerClient(info, user);
+            json = info.toJSON();
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        return Response.ok(info.toJSON()).build();
+        return Response.ok(json).build();
     }
 
 
@@ -128,20 +130,21 @@
             @QueryParam("scope") String scopes) {
         TokenContext ctx = (TokenContext) context.getUserPrincipal();
         Userdata data;
+        String json= "";
         try {
             User user = this.controller.getUser(ctx.getUsername());
             data = this.controller.getUserData(user, UserDetails.class);
             Set<String> base_scope = StringUtils.toSet(scopes, " ");
             base_scope.retainAll(StringUtils.toSet(scopes));
             scopes = StringUtils.toString(base_scope);
+            json = JsonUtils.toJSON(Scopes.mapScopes(scopes, data));
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
         // json format with scope callback parameter
         // todo: add other scopes as well!
-        return Response.ok(JsonUtils.toJSON(Scopes.mapScopes(scopes, data)))
-                .build();
+        return Response.ok(json).build();
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/ResourceController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/ResourceController.java
index 103072e..d64e5bd 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/ResourceController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/ResourceController.java
@@ -11,6 +11,7 @@
 
 import com.sun.jersey.spi.container.ResourceFilters;
 
+import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.service.ResourceService;
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.web.filter.PiwikFilter;
@@ -40,7 +41,14 @@
     @GET
     @Path("info")
     public Response getAllResourceInfo () {
-        String result = JsonUtils.toJSON(resourceService.getResourceDtos());
+        String result = "";
+        try {
+            result = JsonUtils.toJSON(resourceService.getResourceDtos());
+        }
+        catch (KustvaktException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
         return Response.ok(result).build();
     }
 }
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
index 5e37692..3c4f33d 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
@@ -336,19 +336,20 @@
         // EM: is this necessary at all?
         KustvaktResource resource = isCollectionIdValid(ctx.getName(), id);
         if (resource != null) {
-            if (resource instanceof VirtualCollection) {
-                JsonNode node = cquery.and().mergeWith(resource.getData());
-                query = JsonUtils.toJSON(node);
-            }
-            else if (resource instanceof Corpus) {
-                cquery.and().with(Attributes.CORPUS_SIGLE, "=",
-                        resource.getPersistentID());
-                try {
+            try {
+                if (resource instanceof VirtualCollection) {
+                    JsonNode node = cquery.and().mergeWith(resource.getData());
+                    query = JsonUtils.toJSON(node);
+                }
+                else if (resource instanceof Corpus) {
+                    cquery.and().with(Attributes.CORPUS_SIGLE, "=",
+                            resource.getPersistentID());
+
                     query = cquery.toJSON();
                 }
-                catch (KustvaktException e) {
-                   throw responseHandler.throwit(e);
-                }
+            }
+            catch (KustvaktException e) {
+                throw responseHandler.throwit(e);
             }
         }
 
@@ -555,26 +556,28 @@
             jlog.error("Failed retrieving resource: {}", e.string());
             throw responseHandler.throwit(e);
         }
+        try {
+            if (resource instanceof VirtualCollection) {
+                // test this
+                //builder.setBaseQuery(resource.getData());
+                return JsonUtils
+                        .toJSON(builder.and().mergeWith(resource.getData()));
+            }
+            else if (resource instanceof Corpus) {
+                builder.and().with(Attributes.CORPUS_SIGLE, "=",
+                        resource.getPersistentID());
 
-        if (resource instanceof VirtualCollection) {
-            // test this
-            //builder.setBaseQuery(resource.getData());
-            return JsonUtils
-                    .toJSON(builder.and().mergeWith(resource.getData()));
-        }
-        else if (resource instanceof Corpus) {
-            builder.and().with(Attributes.CORPUS_SIGLE, "=",
-                    resource.getPersistentID());
-            try {
                 return builder.toJSON();
             }
-            catch (KustvaktException e) {
-                throw responseHandler.throwit(e);
+
+
+            else {
+                throw responseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT,
+                        "Type parameter not supported", type);
             }
         }
-        else {
-            throw responseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT,
-                    "Type parameter not supported", type);
+        catch (KustvaktException e) {
+            throw responseHandler.throwit(e);
         }
     }
 
@@ -620,7 +623,12 @@
         Map vals = new HashMap();
         vals.put("id", cache.getId());
         vals.put("statistics", cache.getStats());
-        return Response.ok(JsonUtils.toJSON(vals)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(vals)).build();
+        }
+        catch (KustvaktException e) {
+            throw responseHandler.throwit(e);
+        }
     }
 
 
@@ -736,7 +744,7 @@
                     base = JsonUtils.readTree(query);
                 }
                 catch (KustvaktException e) {
-                  responseHandler.throwit(e);
+                    responseHandler.throwit(e);
                 }
             else
                 // todo: throw exception response for no resource to save!
@@ -775,7 +783,12 @@
                 throw responseHandler.throwit(e);
             }
         }
-        return Response.ok(JsonUtils.toJSON(vals)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(vals)).build();
+        }
+        catch (KustvaktException e) {
+            throw responseHandler.throwit(e);
+        }
     }
 
 
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
index ff8ccf9..b181178 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/UserController.java
@@ -116,7 +116,12 @@
             object.put("confirm_uri", uriBuilder.build());
             object.put("uri_expiration",
                     TimeUtils.format(uri.getUriExpiration()));
-            return Response.ok(JsonUtils.toJSON(object)).build();
+            try {
+                return Response.ok(JsonUtils.toJSON(object)).build();
+            }
+            catch (KustvaktException e) {
+                throw kustvaktResponseHandler.throwit(e);
+            }
         }
         else {
             jlog.error("Failed creating confirmation and expiry tokens.");
@@ -224,7 +229,12 @@
         ObjectNode obj = JsonUtils.createObjectNode();
         obj.put(Attributes.URI, builder.toString());
         obj.put(Attributes.URI_EXPIRATION, objects[1].toString());
-        return Response.ok(JsonUtils.toJSON(obj)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(obj)).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
@@ -271,7 +281,12 @@
             throw kustvaktResponseHandler
                     .throwAuthenticationException(ctx.getUsername());
         }
-        return Response.ok(m.toEntity()).build();
+        try {
+            return Response.ok(m.toEntity()).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
@@ -444,7 +459,12 @@
             jlog.error("Exception encountered!", e);
             throw kustvaktResponseHandler.throwit(e);
         }
-        return Response.ok(JsonUtils.toJSON(add)).build();
+        try {
+            return Response.ok(JsonUtils.toJSON(add)).build();
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
     }
 
 
diff --git a/full/src/test/java/de/ids_mannheim/korap/misc/LocalQueryTest.java b/full/src/test/java/de/ids_mannheim/korap/misc/LocalQueryTest.java
index ff8df15..4815ac3 100644
--- a/full/src/test/java/de/ids_mannheim/korap/misc/LocalQueryTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/misc/LocalQueryTest.java
@@ -47,7 +47,7 @@
 
 
     @Test
-    public void testCollQuery () throws IOException {
+    public void testCollQuery () throws IOException, KustvaktException {
         String qstring = "creationDate since 1800 & creationDate until 1820";
         CollectionQueryProcessor processor = new CollectionQueryProcessor();
         processor.process(qstring);
diff --git a/full/src/test/java/de/ids_mannheim/korap/user/UserdataTest.java b/full/src/test/java/de/ids_mannheim/korap/user/UserdataTest.java
index 439462a..aa2c71f 100644
--- a/full/src/test/java/de/ids_mannheim/korap/user/UserdataTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/user/UserdataTest.java
@@ -227,7 +227,7 @@
 
 
     @Test
-    public void testUserDataPointerFunction () {
+    public void testUserDataPointerFunction () throws KustvaktException {
         UserDetails details = new UserDetails(-1);
         Map<String, Object> m = new HashMap<>();
         m.put(Attributes.FIRSTNAME, "first");
@@ -324,7 +324,7 @@
 
 
     @Test
-    public void testUserdataRequiredFields () {
+    public void testUserdataRequiredFields () throws KustvaktException {
         UserDetails details = new UserDetails(-1);
         Map<String, Object> m = new HashMap<>();
         m.put(Attributes.FIRSTNAME, "first");
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
index f6a1388..e62d410 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/service/full/KustvaktServerTest.java
@@ -64,7 +64,7 @@
 
     @Test
     public void testRegisterBadPassword ()
-            throws URISyntaxException, ClientProtocolException, IOException {
+            throws URISyntaxException, ClientProtocolException, IOException, KustvaktException {
         HttpClient httpClient = HttpClients.createDefault();
 
         MultivaluedMap<String, String> map = new MultivaluedMapImpl();
@@ -102,7 +102,7 @@
 
     @Test
     public void testRegisterExistingUsername ()
-            throws URISyntaxException, ClientProtocolException, IOException {
+            throws URISyntaxException, ClientProtocolException, IOException, KustvaktException {
         HttpClient httpClient = HttpClients.createDefault();
 
         MultivaluedMap<String, String> map = new MultivaluedMapImpl();
@@ -139,7 +139,7 @@
 
     @Test
     public void testRegisterUser ()
-            throws URISyntaxException, ClientProtocolException, IOException {
+            throws URISyntaxException, ClientProtocolException, IOException, KustvaktException {
         HttpClient httpClient = HttpClients.createDefault();
 
         MultivaluedMap<String, String> map = new MultivaluedMapImpl();