Added show-tokens option to the search API.

Change-Id: I16c71bccdd5b21e1475edde22b656be24c11287f
diff --git a/core/Changes b/core/Changes
index 771855c..056aed2 100644
--- a/core/Changes
+++ b/core/Changes
@@ -10,7 +10,8 @@
  - [security] More log4j security updates
  - Cleaned up LDAP libraries
  - Bumped spring.version from 5.3.13 to 5.3.14
-
+2022-01-25
+ - Added show-tokens option to the search API.
 
 # version 0.64
 
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 1e30a9e..b1b5575 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
@@ -135,7 +135,8 @@
     public String search (String engine, String username, HttpHeaders headers,
             String q, String ql, String v, List<String> cqList, String fields,
             String pipes, Integer pageIndex, Integer pageInteger, String ctx,
-            Integer pageLength, Boolean cutoff, boolean accessRewriteDisabled)
+            Integer pageLength, Boolean cutoff, boolean accessRewriteDisabled,
+            boolean showTokens)
             throws KustvaktException {
 
         if (pageInteger != null && pageInteger < 1) {
@@ -168,7 +169,8 @@
         handleNonPublicFields(fieldList, accessRewriteDisabled, serializer);
         
         MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx,
-                pageLength, cutoff, corpusAccess, fieldList, accessRewriteDisabled);
+                pageLength, cutoff, corpusAccess, fieldList, accessRewriteDisabled,
+                showTokens);
         serializer.setMeta(meta.raw());
         
         // There is an error in query processing
@@ -178,6 +180,15 @@
         }
 
         String query = serializer.toJSON();
+        
+        if (accessRewriteDisabled && showTokens) {
+            Notifications n = new Notifications();
+            n.addWarning(StatusCodes.NOT_ALLOWED,
+                    "Tokens cannot be shown without access.");
+            JsonNode warning = n.toJsonNode();
+            query = addWarning(query, warning);
+        }
+        
         query = runPipes(query,pipeArray);
         
         query = this.rewriteHandler.processQuery(query, user);
@@ -263,6 +274,14 @@
                 "Pipe failed", url, message);
         JsonNode warning = n.toJsonNode();
         
+        query = addWarning(query, warning);
+        return query; 
+    }
+    
+
+    private String addWarning (String query, JsonNode warning)
+            throws KustvaktException {
+        
         ObjectNode node = (ObjectNode) JsonUtils.readTree(query);
         if (node.has("warnings")){
             warning = warning.at("/warnings/0");
@@ -273,8 +292,7 @@
         else{
             node.setAll((ObjectNode) warning);
         }
-        
-        return node.toString(); 
+        return node.toString();
     }
 
     private void handleNonPublicFields (List<String> fieldList,
@@ -300,7 +318,8 @@
     private MetaQueryBuilder createMetaQuery (Integer pageIndex,
             Integer pageInteger, String ctx, Integer pageLength,
             Boolean cutoff, CorpusAccess corpusAccess, List<String> fieldList,
-            boolean accessRewriteDisabled) {
+            boolean accessRewriteDisabled,
+            boolean showTokens) {
         MetaQueryBuilder meta = new MetaQueryBuilder();
         meta.addEntry("startIndex", pageIndex);
         meta.addEntry("startPage", pageInteger);
@@ -309,6 +328,10 @@
         // todo: what happened to cutoff?
         meta.addEntry("cutOff", cutoff);
         meta.addEntry("snippets", !accessRewriteDisabled);
+        if (!accessRewriteDisabled) {
+            meta.addEntry("tokens", showTokens);
+        }
+        
         // 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 dfab860..0f49d48 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
@@ -221,6 +221,7 @@
             @QueryParam("fields") String fields,
             @QueryParam("pipes") String pipes,
             @QueryParam("access-rewrite-disabled") boolean accessRewriteDisabled,
+            @QueryParam("show-tokens") boolean showTokens,
             @QueryParam("cq") List<String> cq, 
             @QueryParam("engine") String engine) {
 
@@ -233,7 +234,7 @@
             result = searchService.search(engine, context.getUsername(),
                     headers, q, ql, v, cq, fields, pipes, pageIndex,
                     pageInteger, ctx, pageLength, cutoff,
-                    accessRewriteDisabled);
+                    accessRewriteDisabled, showTokens);
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);