Added errors when requesting VC in caching process (resolved #47)

Change-Id: I005cf20113c3badfed938117a93a505b3fedd6b9


Change-Id: I005cf20113c3badfed938117a93a505b3fedd6b9
diff --git a/core/Changes b/core/Changes
index 64c0651..6aa58f1 100644
--- a/core/Changes
+++ b/core/Changes
@@ -1,6 +1,9 @@
 # version 0.62.3
 03/12/2019
    - Implemented pipe extension in the search API (margaretha)
+11/12/2019
+   - Added errors when requesting VC in caching process (margaretha, 
+     resolved #47) 
 
 # version 0.62.2
 13/11/2019
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 dcbc3cb..273250a 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
@@ -16,8 +16,6 @@
 import java.util.Set;
 import java.util.regex.Pattern;
 
-import org.apache.commons.io.FileUtils;
-
 import de.ids_mannheim.korap.util.KrillProperties;
 import de.ids_mannheim.korap.utils.TimeUtils;
 import lombok.Getter;
@@ -41,6 +39,8 @@
 
     public static final Map<String, Object> KUSTVAKT_USER = new HashMap<>();
 
+    private String vcInCaching;
+    
     private String indexDir;
     private int port;
     // todo: make exclusive so that the containg languages can really
diff --git a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
index f65165e..a2ebe5f 100644
--- a/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
+++ b/core/src/main/java/de/ids_mannheim/korap/exceptions/StatusCodes.java
@@ -29,6 +29,7 @@
     public static final int NON_UNIQUE_RESULT_FOUND = 113;
     public static final int NO_RESOURCE_FOUND = 114;
     public static final int DEPRECATED_PARAMETER = 115;
+    public static final int CACHING_VC = 116;
     
     /**
      * 200 status codes general JSON serialization error
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/CoreResponseHandler.java b/core/src/main/java/de/ids_mannheim/korap/web/CoreResponseHandler.java
index 23ff8e6..a94d0b3 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/CoreResponseHandler.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/CoreResponseHandler.java
@@ -98,6 +98,9 @@
             case StatusCodes.NO_RESOURCE_FOUND:
                 status = Response.Status.NOT_FOUND;
                 break;
+            case StatusCodes.CACHING_VC:
+                status = Response.Status.SERVICE_UNAVAILABLE;
+                break;    
         }
         return status;
     }
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
index 43f3693..51bcaae 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/controller/StatisticController.java
@@ -18,9 +18,11 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.sun.jersey.spi.container.ResourceFilters;
 
+import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
 import de.ids_mannheim.korap.response.Notifications;
@@ -53,7 +55,9 @@
     private CoreResponseHandler kustvaktResponseHandler;
     @Autowired
     private SearchKrill searchKrill;
-
+    @Autowired
+    private KustvaktConfiguration config;
+    
     /**
      * Returns statistics of the virtual corpus defined by the given
      * corpusQuery parameter.
@@ -91,6 +95,7 @@
                 isDeprecated = true;
             }
             
+            checkVC(json);
             stats = searchKrill.getStatistics(json);
             
             if (isDeprecated){
@@ -116,6 +121,29 @@
         return Response.ok(stats).build();
     }
     
+    private void checkVC (String json) throws KustvaktException {
+        JsonNode node = JsonUtils.readTree(json);
+        node = node.at("/collection");
+        if (node.has("ref")){
+            String vcName = node.path("ref").asText();
+            if (vcName.contains("/")) {
+                String[] names = vcName.split("/");
+                if (names.length == 2) {
+                    vcName = names[1];
+                }
+            }
+            
+            String vcInCaching = config.getVcInCaching();
+            if (vcName.equals(vcInCaching)) {
+                throw new KustvaktException(
+                        de.ids_mannheim.korap.exceptions.StatusCodes.CACHING_VC,
+                        "VC is currently busy and unaccessible due to "
+                                + "caching process",
+                        node.get("ref").asText());
+            }
+        }
+    }
+
     @POST
     @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
     public Response getStatisticsFromKoralQuery (
@@ -124,6 +152,7 @@
         String stats;
         try {
             if (koralQuery != null && !koralQuery.isEmpty()) {
+                checkVC(koralQuery);
                 stats = searchKrill.getStatistics(koralQuery);
             }
             else {