Added fields to metadata controller, resolved issue #39.

Change-Id: I32e1a77767c8b680ec0b0ff3e2da25f14a547db8
diff --git a/core/Changes b/core/Changes
index 9b7e5b6..8ce8948 100644
--- a/core/Changes
+++ b/core/Changes
@@ -5,6 +5,8 @@
    - Updated search krill error handling (margaretha)
 18/02/2019
    - Updated handling errors from Koral (margaretha)
+19/02/2019
+   - Added fields to metadata controller (margaretha, issue #39)   
 
 # version 0.61.5
 17/12/2018
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 87f0838..35c99c6 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
@@ -98,6 +98,9 @@
     private String secureRandomAlgorithm;
     private String messageDigestAlgorithm;
 
+    // EM: metadata restriction
+    // another variable might be needed to define which metadata fields are restricted 
+    private boolean isMetadataRestricted = false;
     
     public KustvaktConfiguration (Properties properties) throws Exception {
         load(properties);
diff --git a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
index ca63c08..3a30fa2 100644
--- a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
+++ b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
@@ -166,16 +166,21 @@
         }
         
         if (fields != null && !fields.isEmpty()){
-            String[] fieldArray = fields.split(",");
-            List<String> fieldList = new ArrayList<>(fieldArray.length);
-            for (String field :  fieldArray){
-                fieldList.add(field.trim());
-            }
+            List<String> fieldList = convertFieldsToList(fields);
             meta.addEntry("fields", fieldList);
         }
         return meta;
     }
 
+    private List<String> convertFieldsToList (String fields) {
+        String[] fieldArray = fields.split(",");
+        List<String> fieldList = new ArrayList<>(fieldArray.length);
+        for (String field :  fieldArray){
+            fieldList.add(field.trim());
+        }
+        return fieldList;
+    }
+    
     private String searchNeo4J (String query, int pageLength,
             MetaQueryBuilder meta, boolean raw) throws KustvaktException {
 
@@ -260,14 +265,21 @@
     }
 
     public String retrieveDocMetadata (String corpusId, String docId,
-            String textId, String username, HttpHeaders headers)
+            String textId, String fields, String username, HttpHeaders headers)
             throws KustvaktException {
-        User user = createUser(username, headers);
-        Pattern p = determineAvailabilityPattern(user);
+        List<String> fieldList = null;
+        if (fields != null && !fields.isEmpty()){
+            fieldList = convertFieldsToList(fields);
+        }
+        Pattern p = null;
+        if (config.isMetadataRestricted()){
+            User user = createUser(username, headers);
+            p = determineAvailabilityPattern(user);
+        }
         String textSigle = searchKrill.getTextSigle(corpusId, docId, textId);
-        return searchKrill.getFields(textSigle, p);
+        return searchKrill.getFields(textSigle, fieldList, p);
     }
-
+    
     public String getCollocationBase (String query) throws KustvaktException {
         return graphDBhandler.getResponse("distCollo", "q", query);
     }
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
index 0580d7e..4ed62d9 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
@@ -164,7 +164,7 @@
     /*
      * Retrieve the meta fields for a certain document
      */
-    public String getFields (String id, Pattern licensePattern)
+    public String getFields (String id, List<String> fields, Pattern licensePattern)
             throws KustvaktException {
         MetaFields meta;
 
@@ -175,16 +175,24 @@
         }
 
         // Index available
+        else if (fields !=null){
+            // Get fields
+            meta = index.getFields(id, fields);
+        }
         else {
             // Get fields
             meta = index.getFields(id);
-        };
-
-        String availability = meta.getFieldValue("availability");
-        checkAvailability(licensePattern, availability, id);
+        }
+        
+        // EM: this approach forbids the whole metadata
+        // this should be refined by filtering out only the restricted
+        // metadata fields
+        // String availability = meta.getFieldValue("availability");
+        // checkAvailability(licensePattern, availability, id);
 
         return meta.toJsonString();
     };
+    
 
     public String getMatch (String id, List<String> foundries,
             List<String> layers, boolean includeSpans,
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java b/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
index f3299a8..13a89a1 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
@@ -224,7 +224,8 @@
     @Path("{version}/corpus/{corpusId}/{docId}/{textId}")
     public Response getMetadata (@PathParam("corpusId") String corpusId,
             @PathParam("docId") String docId,
-            @PathParam("textId") String textId, 
+            @PathParam("textId") String textId,
+            @QueryParam("fields") String fields,
             @Context SecurityContext ctx,
             @Context HttpHeaders headers
             // @QueryParam("fields") Set<String> fields
@@ -232,7 +233,7 @@
         TokenContext tokenContext = (TokenContext) ctx.getUserPrincipal();
         try {
             String results = searchService.retrieveDocMetadata(corpusId, docId,
-                    textId, tokenContext.getUsername(), headers);
+                    textId, fields, tokenContext.getUsername(), headers);
             return Response.ok(results).build();
         }
         catch (KustvaktException e) {