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) {
diff --git a/full/Changes b/full/Changes
index a1fd961..14a3e5e 100644
--- a/full/Changes
+++ b/full/Changes
@@ -10,7 +10,9 @@
    - Added metadata controller tests (margaretha)
 18/02/2019
    - Fixed tests (margaretha)
-   - Updated handling errors from Koral (margaretha)  
+   - 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/full/src/test/java/de/ids_mannheim/korap/web/controller/MetadataControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/MetadataControllerTest.java
index 71a3fff..661cad1 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/MetadataControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/MetadataControllerTest.java
@@ -3,6 +3,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import org.junit.Ignore;
 import org.junit.Test;
 
 import com.fasterxml.jackson.databind.JsonNode;
@@ -19,11 +20,41 @@
 public class MetadataControllerTest extends SpringJerseyTest {
 
     @Test
+    public void testRetrieveMetadataWithField () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("corpus")
+                .path("GOE").path("AGA").path("01784")
+                .queryParam("fields", "author").get(ClientResponse.class);
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+
+        assertEquals("author", node.at("/document/fields/0/key").asText());
+    }
+    
+
+    @Test
+    public void testRetrieveMetadataWithMultipleFields () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("corpus")
+                .path("GOE").path("AGA").path("01784")
+                .queryParam("fields", "author,title").get(ClientResponse.class);
+
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+
+        assertEquals("author", node.at("/document/fields/0/key").asText());
+        assertEquals("title", node.at("/document/fields/1/key").asText());
+    }    
+    
+    @Test
     public void testFreeMetadata () throws KustvaktException {
 
         ClientResponse response = resource().path(API_VERSION).path("corpus")
                 .path("GOE").path("AGA").path("01784")
-                .queryParam("foundry", "*").get(ClientResponse.class);
+                .get(ClientResponse.class);
 
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
@@ -34,12 +65,14 @@
 
     }
 
+    // EM: currently all metadata are allowed
     @Test
+    @Ignore
     public void testMetadataUnauthorized () throws KustvaktException {
 
         ClientResponse response = resource().path(API_VERSION).path("corpus")
                 .path("GOE").path("AGI").path("04846")
-                .queryParam("foundry", "*").get(ClientResponse.class);
+                .get(ClientResponse.class);
 
         assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(),
                 response.getStatus());
@@ -84,7 +117,9 @@
                 response.getStatus());
     }
 
+    // EM: currently all metadata are allowed
     @Test
+    @Ignore
     public void testMetadataAvailabilityAllUnauthorized ()
             throws KustvaktException {
         ClientResponse response = resource().path(API_VERSION).path("corpus")
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
index fd33b90..0a62f9d 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
@@ -23,12 +23,42 @@
 import de.ids_mannheim.korap.utils.JsonUtils;
 
 /**
- * @author margaretha, hanl
- * @lastUpdate 22/03/2018
+ * @author hanl, margaretha
+ * @lastUpdate 18/02/2019
  *
  */
 public class SearchControllerTest extends SpringJerseyTest {
 
+    
+    private JsonNode requestSearchWithFields(String fields) throws KustvaktException{
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
+                .queryParam("fields", fields)
+                .queryParam("context", "sentence").queryParam("count", "13")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(query);
+        return node;
+    }
+    
+    @Test
+    public void testSearchWithField () throws KustvaktException {
+        JsonNode node = requestSearchWithFields("author");
+        assertNotEquals(0, node.at("/matches").size());
+        assertEquals("[\"author\"]",
+                node.at("/meta/fields").toString());
+    }
+    
+    @Test
+    public void testSearchWithMultipleFields () throws KustvaktException {
+        JsonNode node = requestSearchWithFields("author, title");
+        assertNotEquals(0, node.at("/matches").size());
+        assertEquals("[\"author\",\"title\"]",
+                node.at("/meta/fields").toString());
+    }
+    
     @Test
     public void testSearchQueryPublicCorpora () throws KustvaktException {
         ClientResponse response = resource().path(API_VERSION).path("search")