Added a web-service to retrieve KoralQuery of a VC

Change-Id: I1b4a249e8e43380f57ab9d504d36118f775eb94d
diff --git a/full/Changes b/full/Changes
index fa378a7..1693033 100644
--- a/full/Changes
+++ b/full/Changes
@@ -5,6 +5,7 @@
 - Enabled listing system vc for authorized users.
 2021-12-08
 - Removed koralQuery and statistics from VC list.
+- Added a web-service to retrieve KoralQuery of a VC
 
 # version 0.64.1
 2021-10-26
diff --git a/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java b/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
index 2589406..6cfe8ee 100644
--- a/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/service/QueryService.java
@@ -617,6 +617,14 @@
         }
 
     }
+    
+    public JsonNode retrieveKoralQuery (String username, String queryName,
+            String createdBy, QueryType queryType) throws KustvaktException {
+        QueryDO query = searchQueryByName(username, queryName, createdBy, queryType);
+        String koralQuery = query.getKoralQuery();
+        JsonNode kq = JsonUtils.readTree(koralQuery); 
+        return kq;
+    }
 
     public QueryDO searchQueryByName (String username, String queryName,
             String createdBy, QueryType queryType) throws KustvaktException {
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
index d62769a..8a9f16f 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/VirtualCorpusController.java
@@ -20,6 +20,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.sun.jersey.spi.container.ResourceFilters;
 
 import de.ids_mannheim.korap.constant.OAuth2Scope;
@@ -123,6 +124,7 @@
 
     /**
      * Returns the virtual corpus with the given name and creator.
+     * This web-service is also available for guests.
      * 
      * @param securityContext
      * @param createdBy
@@ -152,7 +154,26 @@
         }
     }
 
-
+    @GET
+    @Path("/koralQuery/~{createdBy}/{vcName}")
+    @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
+    public JsonNode retrieveVCKoralQuery (
+            @Context SecurityContext securityContext,
+            @PathParam("createdBy") String createdBy,
+            @PathParam("vcName") String vcName) {
+        TokenContext context =
+                (TokenContext) securityContext.getUserPrincipal();
+        try {
+            scopeService.verifyScope(context, OAuth2Scope.VC_INFO);
+            return service.retrieveKoralQuery(context.getUsername(), vcName,
+                    createdBy, QueryType.VIRTUAL_CORPUS);
+        }
+        catch (KustvaktException e) {
+            throw kustvaktResponseHandler.throwit(e);
+        }
+    }
+    
+    
     /**
      * Lists all virtual corpora available to the authenticated user.
      *
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
index d93bb08..fa39d4d 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/VirtualCorpusControllerTest.java
@@ -726,6 +726,11 @@
     @Test
     public void testEditCorpusQuery () throws UniformInterfaceException,
             ClientHandlerException, KustvaktException {
+        JsonNode node = testRetrieveKoralQuery("dory", "dory-vc");
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands").size());
+        
         String json = "{\"corpusQuery\": \"corpusSigle=WPD17\"}";
 
         ClientResponse response = resource().path(API_VERSION).path("vc")
@@ -737,9 +742,26 @@
 
         assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
 
-        // check VC
-//        JsonNode node = testListVC("dory");
-//        assertEquals("WPD17", node.at("/0/koralQuery/collection/value").asText());
+        node = testRetrieveKoralQuery("dory", "dory-vc");
+        assertEquals("koral:doc", node.at("/collection/@type").asText());
+        assertEquals("corpusSigle", node.at("/collection/key").asText());
+        assertEquals("WPD17", node.at("/collection/value").asText());
+    }
+    
+    private JsonNode testRetrieveKoralQuery (String username, String vcName)
+            throws UniformInterfaceException, ClientHandlerException,
+            KustvaktException {
+        ClientResponse response = resource().path(API_VERSION).path("vc")
+                .path("koralQuery").path("~" + username).path(vcName)
+                .header(Attributes.AUTHORIZATION, HttpAuthorizationHandler
+                        .createBasicAuthorizationHeaderValue("dory", "pass"))
+                .header(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON)
+                .get(ClientResponse.class);
+        
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        String entity = response.getEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        return node;
     }
 
     @Test