Added support for public metadata response in the search api.

Change-Id: Ic6297c192c813520d1c83c1a35d2206059eadb8e
diff --git a/core/Changes b/core/Changes
index 62aa351..5051644 100644
--- a/core/Changes
+++ b/core/Changes
@@ -1,3 +1,10 @@
+# version 0.62.1
+08/07/2019
+   - Added support for public metadata response in search api (margaretha, 
+     issue #43)
+   - Disabled some unused web-services: search post, query serialization, 
+     collocation base (margaretha)
+
 # version 0.62
 18/03/2019
    - Added close index controller (margaretha)
diff --git a/core/pom.xml b/core/pom.xml
index ae28c9c..a3c9da7 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -3,7 +3,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>de.ids_mannheim.korap</groupId>
 	<artifactId>Kustvakt-core</artifactId>
-	<version>0.62</version>
+	<version>0.62.1</version>
 
 	<properties>
 		<java.version>1.8</java.version>
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 5d3953f..35f3547 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
@@ -16,6 +16,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 
 import de.ids_mannheim.de.init.VCLoader;
@@ -28,6 +29,7 @@
 import de.ids_mannheim.korap.rewrite.RewriteHandler;
 import de.ids_mannheim.korap.user.User;
 import de.ids_mannheim.korap.user.User.CorpusAccess;
+import de.ids_mannheim.korap.utils.JsonUtils;
 import de.ids_mannheim.korap.web.ClientsHandler;
 import de.ids_mannheim.korap.web.SearchKrill;
 
@@ -62,7 +64,8 @@
     @SuppressWarnings("unchecked")
     public String serializeQuery (String q, String ql, String v, String cq,
             Integer pageIndex, Integer startPage, Integer pageLength,
-            String context, Boolean cutoff) throws KustvaktException {
+            String context, Boolean cutoff, boolean accessRewriteDisabled)
+            throws KustvaktException {
         QuerySerializer ss = new QuerySerializer().setQuery(q, ql, v);
         if (cq != null) ss.setCollection(cq);
 
@@ -88,38 +91,56 @@
         authenticationManager.setAccessAndLocation(user, headers);
         if (DEBUG) {
             if (user != null) {
-                jlog.debug("Debug: /getMatchInfo/: location="
-                        + user.locationtoString() + ", access="
-                        + user.accesstoString());
+                jlog.debug("Debug: user location=" + user.locationtoString()
+                        + ", access=" + user.accesstoString());
             }
         }
         return user;
     }
 
-    public String search (String jsonld) {
+    public String search (String jsonld, String username, HttpHeaders headers)
+            throws KustvaktException {
+
+        User user = createUser(username, headers);
+
+        JsonNode node  = JsonUtils.readTree(jsonld);
+        node = node.at("/meta/snippets");
+        if (node !=null && node.asBoolean()){
+            user.setCorpusAccess(CorpusAccess.ALL);
+        }
+        
+        String query = this.rewriteHandler.processQuery(jsonld, user);
         // MH: todo: should be possible to add the meta part to
         // the query serialization
         // User user = controller.getUser(ctx.getUsername());
         // jsonld = this.processor.processQuery(jsonld, user);
-        return searchKrill.search(jsonld);
+        return searchKrill.search(query);
     }
 
     @SuppressWarnings("unchecked")
     public String search (String engine, String username, HttpHeaders headers,
             String q, String ql, String v, String cq, String fields,
             Integer pageIndex, Integer pageInteger, String ctx,
-            Integer pageLength, Boolean cutoff) throws KustvaktException {
+            Integer pageLength, Boolean cutoff, boolean accessRewriteDisabled)
+            throws KustvaktException {
 
         KustvaktConfiguration.BACKENDS eng = this.config.chooseBackend(engine);
         User user = createUser(username, headers);
         CorpusAccess corpusAccess = user.getCorpusAccess();
         
+        // EM: TODO: check if requested fields are public metadata. Currently 
+        // it is not needed because all metadata are public.        
+        if (accessRewriteDisabled){
+            corpusAccess = CorpusAccess.ALL;
+            user.setCorpusAccess(CorpusAccess.ALL);
+        }
+            
         QuerySerializer serializer = new QuerySerializer();
         serializer.setQuery(q, ql, v);
         if (cq != null) serializer.setCollection(cq);
 
         MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx,
-                pageLength, cutoff, corpusAccess, fields);
+                pageLength, cutoff, corpusAccess, fields, accessRewriteDisabled);
         serializer.setMeta(meta.raw());
 
         // There is an error in query processing
@@ -148,7 +169,8 @@
 
     private MetaQueryBuilder createMetaQuery (Integer pageIndex,
             Integer pageInteger, String ctx, Integer pageLength,
-            Boolean cutoff, CorpusAccess corpusAccess, String fields) {
+            Boolean cutoff, CorpusAccess corpusAccess, String fields,
+            boolean accessRewriteDisabled) {
         MetaQueryBuilder meta = new MetaQueryBuilder();
         meta.addEntry("startIndex", pageIndex);
         meta.addEntry("startPage", pageInteger);
@@ -156,6 +178,7 @@
         meta.addEntry("count", pageLength);
         // todo: what happened to cutoff?
         meta.addEntry("cutOff", cutoff);
+        meta.addEntry("snippets", !accessRewriteDisabled);
         // meta.addMeta(pageIndex, pageInteger, pageLength, ctx,
         // cutoff);
         // fixme: should only apply to CQL queries per default!
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 ef3b16b..8ccb8f1 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
@@ -6,6 +6,7 @@
 import java.util.Set;
 
 import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
@@ -41,7 +42,7 @@
  * 
  * @author hanl, margaretha, diewald
  * @date 29/01/2014
- * @lastUpdate 09/07/2018
+ * @lastUpdate 05/07/2019
  * 
  */
 @Controller
@@ -77,31 +78,13 @@
     }
     
     
-    /**
-     * Builds a json query serialization from the given parameters.
-     * 
-     * @param locale
-     * @param securityContext
-     * @param q
-     *            query string
-     * @param ql
-     *            query language
-     * @param v
-     *            version
-     * @param context
-     * @param cutoff
-     *            true if the number of results should be limited
-     * @param pageLength
-     *            number of results per page
-     * @param pageIndex
-     * @param startPage
-     * @param cq
-     *            corpus query
-     * @return
-     */
-    // ref query parameter removed!
-    @GET
-    @Path("{version}/query")
+//     EM: This web service is DISABLED until there is a need for it.
+//     ND: In case rewrite is supported, it could be used to check the authorization 
+//         scope without searching etc. In case not, it helps to compare queries in 
+//         different query languages.
+//     MH: ref query parameter removed!
+//    @GET
+//    @Path("{version}/query")
     public Response serializeQuery (@Context Locale locale,
             @Context SecurityContext securityContext, @QueryParam("q") String q,
             @QueryParam("ql") String ql, @QueryParam("v") String v,
@@ -110,12 +93,14 @@
             @QueryParam("count") Integer pageLength,
             @QueryParam("offset") Integer pageIndex,
             @QueryParam("page") Integer startPage,
+            @QueryParam("access-rewrite-disabled") boolean accessRewriteDisabled,
             @QueryParam("cq") String cq) {
         TokenContext ctx = (TokenContext) securityContext.getUserPrincipal();
         try {
             scopeService.verifyScope(ctx, OAuth2Scope.SERIALIZE_QUERY);
             String result = searchService.serializeQuery(q, ql, v, cq,
-                    pageIndex, startPage, pageLength, context, cutoff);
+                    pageIndex, startPage, pageLength, context, cutoff,
+                    accessRewriteDisabled);
             if (DEBUG){
                 jlog.debug("Query: " + result);
             }
@@ -126,32 +111,35 @@
         }
     }
 
-    @POST
-    @Path("{version}/search")
+    
+//    This web service is DISABLED until there is a need for it. 
+//    @POST
+//    @Path("{version}/search")
     public Response searchPost (@Context SecurityContext context,
-            @Context Locale locale, @QueryParam("engine") String engine,
+            @Context Locale locale, 
+            @Context HttpHeaders headers,
             String jsonld) {
+        
+        if (DEBUG){
+            jlog.debug("Serialized search: " + jsonld);
+        }
+        
         TokenContext ctx = (TokenContext) context.getUserPrincipal();
         try {
             scopeService.verifyScope(ctx, OAuth2Scope.SEARCH);
+            String result = searchService.search(jsonld, ctx.getUsername(),
+                    headers);
+            return Response.ok(result).build();
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-
-        if (DEBUG){
-            jlog.debug("Serialized search: " + jsonld);
-        }
-        String result = searchService.search(jsonld);
-        if (DEBUG){
-            jlog.debug("The result set: " + result);
-        }
-        return Response.ok(result).build();
     }
 
     @GET
     @Path("{version}/search")
     public Response searchGet (@Context SecurityContext securityContext,
+            @Context HttpServletRequest request,
             @Context HttpHeaders headers, @Context Locale locale,
             @QueryParam("q") String q, @QueryParam("ql") String ql,
             @QueryParam("v") String v, @QueryParam("context") String ctx,
@@ -160,7 +148,9 @@
             @QueryParam("offset") Integer pageIndex,
             @QueryParam("page") Integer pageInteger,
             @QueryParam("fields") String fields,
-            @QueryParam("cq") String cq, @QueryParam("engine") String engine) {
+            @QueryParam("access-rewrite-disabled") boolean accessRewriteDisabled,
+            @QueryParam("cq") String cq, 
+            @QueryParam("engine") String engine) {
 
         TokenContext context =
                 (TokenContext) securityContext.getUserPrincipal();
@@ -170,7 +160,7 @@
             scopeService.verifyScope(context, OAuth2Scope.SEARCH);
             result = searchService.search(engine, context.getUsername(),
                     headers, q, ql, v, cq, fields, pageIndex, pageInteger, ctx,
-                    pageLength, cutoff);
+                    pageLength, cutoff, accessRewriteDisabled);
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
@@ -257,8 +247,9 @@
         }
     }
 
-    @POST
-    @Path("{version}/colloc")
+//  EM: This web service requires Karang and is DISABLED.
+//    @POST
+//    @Path("{version}/colloc")
     public Response getCollocationBase (@QueryParam("q") String query) {
         String result;
         try {