Added support for public metadata response in the search api.

Change-Id: Ic6297c192c813520d1c83c1a35d2206059eadb8e
diff --git a/full/src/main/resources/ehcache.xml b/full/src/main/resources/ehcache.xml
index 27a4b56..2531fdb 100644
--- a/full/src/main/resources/ehcache.xml
+++ b/full/src/main/resources/ehcache.xml
@@ -1,6 +1,7 @@
 <ehcache xsi:noNamespaceSchemaLocation="http://www.ehcache.org/ehcache.xsd"
 	updateCheck="true" monitoring="autodetect" dynamicConfig="true">
 	
+	<sizeOfPolicy maxDepth="100" maxDepthExceededBehavior="abort" />
     <defaultCache eternal='true' overflowToDisk='false'/>
     <!--maxBytesLocalHeap="200M"-->
     <diskStore path="./cache_store"/>
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/PublicMetadataTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/PublicMetadataTest.java
new file mode 100644
index 0000000..5412949
--- /dev/null
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/PublicMetadataTest.java
@@ -0,0 +1,133 @@
+package de.ids_mannheim.korap.web.controller;
+
+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;
+import com.google.common.net.HttpHeaders;
+import com.sun.jersey.api.client.ClientResponse;
+
+import de.ids_mannheim.korap.config.SpringJerseyTest;
+import de.ids_mannheim.korap.exceptions.KustvaktException;
+import de.ids_mannheim.korap.exceptions.StatusCodes;
+import de.ids_mannheim.korap.query.serialize.MetaQueryBuilder;
+import de.ids_mannheim.korap.query.serialize.QuerySerializer;
+import de.ids_mannheim.korap.utils.JsonUtils;
+
+public class PublicMetadataTest extends SpringJerseyTest {
+
+    @Test
+    public void testSearchPublicMetadata () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "Sonne").queryParam("ql", "poliqarp")
+                .queryParam("access-rewrite-disabled", "true")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        
+        assertEquals("availability(ALL)",
+                node.at("/collection/rewrites/0/scope").asText());
+
+        assertTrue(node.at("/matches/0/snippet").isMissingNode());
+    }
+
+    @Test
+    public void testSearchPublicMetadataExtern () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "Sonne").queryParam("ql", "poliqarp")
+                .queryParam("access-rewrite-disabled", "true")
+                .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        
+        assertEquals("availability(ALL)",
+                node.at("/collection/rewrites/0/scope").asText());
+
+        assertTrue(node.at("/matches/0/snippet").isMissingNode());
+    }
+
+//  EM: The API is disabled
+    @Ignore
+    @Test
+    public void testSearchPostPublicMetadata () throws KustvaktException {
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery("[orth=der]", "poliqarp");
+        s.setCollection("corpusSigle=GOE");
+        s.setQuery("Wasser", "poliqarp");
+        
+        MetaQueryBuilder meta = new MetaQueryBuilder();
+        meta.addEntry("snippets", "true");
+        s.setMeta(meta);
+        
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .post(ClientResponse.class, s.toJSON());
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertEquals("availability(ALL)",
+                node.at("/collection/rewrites/0/scope").asText());
+        assertTrue(node.at("/matches/0/snippet").isMissingNode());
+    }
+    
+    @Test
+    public void testSearchPublicMetadataWithSystemVC ()
+            throws KustvaktException {
+
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "Sonne").queryParam("ql", "poliqarp")
+                .queryParam("cq", "referTo system-vc")
+                .queryParam("access-rewrite-disabled", "true")
+                .get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+
+        assertEquals("operation:and",
+                node.at("/collection/operation").asText());
+        node = node.at("/collection/operands/1");
+        assertEquals("koral:doc", node.at("/@type").asText());
+        assertEquals("GOE", node.at("/value").asText());
+        assertEquals("match:eq", node.at("/match").asText());
+        assertEquals("corpusSigle", node.at("/key").asText());
+
+        assertEquals("operation:deletion",
+                node.at("/rewrites/0/operation").asText());
+        assertEquals("@type(koral:docGroupRef)",
+                node.at("/rewrites/0/scope").asText());
+
+        assertEquals("operation:deletion",
+                node.at("/rewrites/1/operation").asText());
+        assertEquals("ref(system-vc)", node.at("/rewrites/1/scope").asText());
+
+        assertEquals("operation:insertion",
+                node.at("/rewrites/2/operation").asText());
+    }
+
+    @Test
+    public void testSearchPublicMetadataWithPrivateVC ()
+            throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .queryParam("q", "Sonne").queryParam("ql", "poliqarp")
+                .queryParam("cq", "referTo \"dory/dory-vc\"")
+                .queryParam("access-rewrite-disabled", "true")
+                .get(ClientResponse.class);
+
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+
+        assertEquals(StatusCodes.AUTHORIZATION_FAILED,
+                node.at("/errors/0/0").asInt());
+        assertEquals("guest", node.at("/errors/0/2").asText());
+    }
+}
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/QuerySerializationControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/QuerySerializationControllerTest.java
index 483a059..cd35e86 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/QuerySerializationControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/QuerySerializationControllerTest.java
@@ -24,8 +24,8 @@
 import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.web.FastJerseyTest;
 
-/* EM: potentially an unused service! */
 
+// EM: The API is disabled
 @Ignore
 public class QuerySerializationControllerTest extends FastJerseyTest {
 
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 1fb3915..33b6729 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
@@ -42,6 +42,14 @@
         return node;
     }
     
+    private String createJsonQuery(){
+        QuerySerializer s = new QuerySerializer();
+        s.setQuery("[orth=der]", "poliqarp");
+        s.setCollection("corpusSigle=GOE");
+        s.setQuery("Wasser", "poliqarp");
+        return s.toJSON();
+    }
+    
     @Test
     public void testSearchWithField () throws KustvaktException {
         JsonNode node = requestSearchWithFields("author");
@@ -296,6 +304,8 @@
         assertNotEquals("${project.version}", "/meta/version");
     }
 
+//  EM: The API is disabled
+    @Ignore
     @Test
     public void testSearchSimpleCQL () throws KustvaktException {
         QuerySerializer s = new QuerySerializer();
@@ -313,16 +323,12 @@
         // assertEquals(17027, node.at("/meta/totalResults").asInt());
     }
 
+//  EM: The API is disabled
     @Test
+    @Ignore
     public void testSearchRawQuery () throws KustvaktException {
-        QuerySerializer s = new QuerySerializer();
-        s.setQuery("[orth=der]", "poliqarp");
-        s.setCollection("corpusSigle=GOE");
-
-        s.setQuery("Wasser", "poliqarp");
-        // System.out.println(s.toJSON());
         ClientResponse response = resource().path(API_VERSION).path("search")
-                .post(ClientResponse.class, s.toJSON());
+                .post(ClientResponse.class, createJsonQuery());
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
@@ -330,6 +336,54 @@
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertNotEquals(0, node.path("matches").size());
-        // assertEquals(10993, node.at("/meta/totalResults").asInt());
+        
+        assertEquals("availability(FREE)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+    
+//  EM: The API is disabled    
+    @Test
+    @Ignore
+    public void testSearchPostAll () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .header(HttpHeaders.X_FORWARDED_FOR, "10.27.0.32")
+                .header(Attributes.AUTHORIZATION,
+                        HttpAuthorizationHandler
+                                .createBasicAuthorizationHeaderValue("kustvakt",
+                                        "kustvakt2015"))
+                .post(ClientResponse.class, createJsonQuery());
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.path("matches").size());
+        
+        assertEquals("availability(ALL)",
+                node.at("/collection/rewrites/0/scope").asText());
+    }
+    
+//  EM: The API is disabled
+    @Test
+    @Ignore
+    public void testSearchPostPublic () throws KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("search")
+                .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32")
+                .header(Attributes.AUTHORIZATION,
+                        HttpAuthorizationHandler
+                                .createBasicAuthorizationHeaderValue("kustvakt",
+                                        "kustvakt2015"))
+                .post(ClientResponse.class, createJsonQuery());
+        assertEquals(ClientResponse.Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.getEntity(String.class);
+
+        JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertNotEquals(0, node.path("matches").size());
+        
+        assertEquals("availability(PUB)",
+                node.at("/collection/rewrites/0/scope").asText());
     }
 }