Added debug flags & order on fields parameter of search get request-

Change-Id: I07ef9c234c4a6a72757f9a3385b42f0b61818ddf
diff --git a/core/Changes b/core/Changes
index ee3b3cb..c5ba7df 100644
--- a/core/Changes
+++ b/core/Changes
@@ -3,6 +3,9 @@
    - Added search timeout in meta query (margaretha)
 18/12/2018
    - Updated RewriteHandler (margaretha)
+19/12/2018
+   - Added debug flags (margaretha)
+   - Added order on the fields parameter of search get request (margaretha)   
 
 version 0.61.4
 14/11/2018
diff --git a/core/src/main/java/de/ids_mannheim/korap/interfaces/defaults/ApacheValidator.java b/core/src/main/java/de/ids_mannheim/korap/interfaces/defaults/ApacheValidator.java
index e78cd0c..6cc2224 100644
--- a/core/src/main/java/de/ids_mannheim/korap/interfaces/defaults/ApacheValidator.java
+++ b/core/src/main/java/de/ids_mannheim/korap/interfaces/defaults/ApacheValidator.java
@@ -23,6 +23,8 @@
 
     private static final String STRING_PATTERN = "^[\\.;:,&\\|@\\[\\]\\=\\*\\/\\/_()\\-0-9\\p{L}\\p{Space}]{0,1024}$";
 
+    private static final boolean DEBUG = false;
+
     private Map<String, RegexValidator> validators;
 
 
@@ -117,8 +119,11 @@
             else
                 return this.isValid(input, "string");
         }
-        jlog.debug("validating entry "+input+" of type "+type+": "+ (
-                valid ? "Is valid!" : "Is not valid!"));
+        if (DEBUG){
+            jlog.debug("validating entry "+input+" of type "+type+": "+ (
+                    valid ? "Is valid!" : "Is not valid!"));
+        }
+            
         return valid;
     }
 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
index 130bc2d..6469d57 100644
--- a/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
+++ b/core/src/main/java/de/ids_mannheim/korap/resource/rewrite/RewriteHandler.java
@@ -159,6 +159,7 @@
 
     public class RewriteProcess {
 
+        private static final boolean DEBUG = false;
         private JsonNode root;
         private User user;
 
@@ -208,7 +209,9 @@
 
 
         private JsonNode start (boolean result) throws KustvaktException {
-            jlog.debug("Running rewrite process on query "+ root);
+            if (DEBUG){
+                jlog.debug("Running rewrite process on query "+ root);
+            }
             if (root != null) {
                 Iterator<Map.Entry<String, JsonNode>> it = root.fields();
                 while (it.hasNext()) {
@@ -236,8 +239,10 @@
                 throw new RuntimeException("KustvaktConfiguration must be set!");
 
             for (RewriteTask task : tasks) {
-                jlog.debug("running processor on node: " + node);
-                jlog.debug("on processor: " + task.getClass().toString());
+                if (DEBUG) {
+                    jlog.debug("running processor on node: " + node);
+                    jlog.debug("on processor: " + task.getClass().toString());
+                }
 
                 if (RewriteHandler.this.beans != null
                         && task instanceof BeanInjectable)
@@ -247,7 +252,9 @@
                 if (task instanceof RewriteTask.IterableRewritePath) {
                     RewriteTask.IterableRewritePath rw = (RewriteTask.IterableRewritePath) task;
                     if (rw.path() != null && !rw.path().equals(rootNode)) {
-                        jlog.debug("skipping node: " + node);
+                        if (DEBUG){
+                            jlog.debug("skipping node: " + node);
+                        }
                         continue;
                     }
                 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/service/AnnotationService.java b/core/src/main/java/de/ids_mannheim/korap/service/AnnotationService.java
index 3e384ec..94c2498 100644
--- a/core/src/main/java/de/ids_mannheim/korap/service/AnnotationService.java
+++ b/core/src/main/java/de/ids_mannheim/korap/service/AnnotationService.java
@@ -25,6 +25,8 @@
 @Service
 public class AnnotationService {
 
+    private static final boolean DEBUG = false;
+
     private static Logger jlog =
             LogManager.getLogger(AnnotationService.class);
 
@@ -36,7 +38,9 @@
 
     public List<LayerDto> getLayerDtos () {
         List<AnnotationLayer> layers = annotationDao.getAllFoundryLayerPairs();
-        jlog.debug("/layers " + layers.toString());
+        if (DEBUG){
+            jlog.debug("/layers " + layers.toString());
+        }
         List<LayerDto> layerDto = annotationConverter.convertToLayerDto(layers);
         return layerDto;
     }
@@ -53,7 +57,9 @@
             String[] annotationCode;
             annotationPairs = new ArrayList<AnnotationLayer>();
             for (String code : codes) {
-                jlog.debug("code " + code);
+                if (DEBUG){
+                    jlog.debug("code " + code);
+                }
                 annotationCode = code.split("/");
                 if (annotationCode.length == 1) {
                     foundry = annotationCode[0];
@@ -76,7 +82,9 @@
         if (annotationPairs != null && !annotationPairs.isEmpty()) {
             List<FoundryDto> foundryDtos = annotationConverter
                     .convertToFoundryDto(annotationPairs, language);
-            jlog.debug("/description " + annotationPairs.toString());
+            if (DEBUG){
+                jlog.debug("/description " + annotationPairs.toString());
+            }
             return foundryDtos;
         }
         else {
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 2e43a33..1552bf3 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
@@ -1,6 +1,7 @@
 package de.ids_mannheim.korap.service;
 
 import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
 import java.util.regex.Pattern;
 
@@ -84,10 +85,12 @@
             throws KustvaktException {
         User user = authenticationManager.getUser(username);
         authenticationManager.setAccessAndLocation(user, headers);
-        if (user != null) {
-            jlog.debug(
-                    "Debug: /getMatchInfo/: location=" + user.locationtoString()
-                            + ", access=" + user.accesstoString());
+        if (DEBUG) {
+            if (user != null) {
+                jlog.debug("Debug: /getMatchInfo/: location="
+                        + user.locationtoString() + ", access="
+                        + user.accesstoString());
+            }
         }
         return user;
     }
@@ -102,7 +105,7 @@
 
     @SuppressWarnings("unchecked")
     public String search (String engine, String username, HttpHeaders headers,
-            String q, String ql, String v, String cq, Set<String> fields,
+            String q, String ql, String v, String cq, String fields,
             Integer pageIndex, Integer pageInteger, String ctx,
             Integer pageLength, Boolean cutoff) throws KustvaktException {
 
@@ -115,9 +118,7 @@
         if (cq != null) serializer.setCollection(cq);
 
         MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx,
-                pageLength, cutoff, corpusAccess);
-        if (fields != null && !fields.isEmpty())
-            meta.addEntry("fields", fields);
+                pageLength, cutoff, corpusAccess, fields);
         serializer.setMeta(meta.raw());
 
         // There is an error in query processing
@@ -146,7 +147,7 @@
 
     private MetaQueryBuilder createMetaQuery (Integer pageIndex,
             Integer pageInteger, String ctx, Integer pageLength,
-            Boolean cutoff, CorpusAccess corpusAccess) {
+            Boolean cutoff, CorpusAccess corpusAccess, String fields) {
         MetaQueryBuilder meta = new MetaQueryBuilder();
         meta.addEntry("startIndex", pageIndex);
         meta.addEntry("startPage", pageInteger);
@@ -165,6 +166,15 @@
         else{
             meta.addEntry("timeout", 90000);
         }
+        
+        if (fields != null && !fields.isEmpty()){
+            String[] fieldArray = fields.split(",");
+            List<String> fieldList = new ArrayList<>(fieldArray.length);
+            for (String field :  fieldArray){
+                fieldList.add(field.trim());
+            }
+            meta.addEntry("fields", fieldList);
+        }
         return meta;
     }
 
@@ -239,7 +249,9 @@
             throw new KustvaktException(StatusCodes.ILLEGAL_ARGUMENT,
                     e.getMessage());
         }
-        jlog.debug("MatchInfo results: " + results);
+        if (DEBUG){
+            jlog.debug("MatchInfo results: " + results);
+        }
         return results;
     }
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/utils/TimeUtils.java b/core/src/main/java/de/ids_mannheim/korap/utils/TimeUtils.java
index 6ef4b56..d6f2daf 100644
--- a/core/src/main/java/de/ids_mannheim/korap/utils/TimeUtils.java
+++ b/core/src/main/java/de/ids_mannheim/korap/utils/TimeUtils.java
@@ -32,6 +32,7 @@
     private static DateTimeFormatter dtf = DateTimeFormat
             .forPattern("dd/MM/yyyy");
     private static final DateTimeZone dtz = DateTimeZone.forID(Attributes.DEFAULT_TIME_ZONE);
+    private static final boolean DEBUG = false;
     private static Logger jlog = LogManager.getLogger(TimeUtils.class);
 
 
@@ -40,8 +41,10 @@
         int finIndex = expirationVal.length() - 1;
         char entity = expirationVal.charAt(finIndex);
         int returnSec = Integer.valueOf(expirationVal.substring(0, finIndex));
-        jlog.debug("setting time value to "+returnSec+" with time in "+
-                entity);
+        if (DEBUG) {
+            jlog.debug("setting time value to " + returnSec + " with time in "
+                    + entity);
+        }
         switch (entity) {
             case 'D':
                 return returnSec * 60 * 60 * 24;
@@ -52,7 +55,9 @@
             case 'S':
                 return returnSec;
             default:
-                jlog.debug("no time unit specified. Trying to read from default (minutes)");
+                if (DEBUG){
+                    jlog.debug("no time unit specified. Trying to read from default (minutes)");
+                }
                 return Integer.valueOf(expirationVal) * 60;
         }
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
index 65be493..f81b5aa 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
@@ -132,7 +132,9 @@
             	if (licensePattern!=null && availability != null){
             		Matcher m = licensePattern.matcher(availability);
             		if (!m.matches()){
-            		    jlog.debug("availability: "+availability);
+            		    if (DEBUG){
+            		        jlog.debug("availability: "+availability);
+            		    }
                         if (availability.isEmpty()){
                             km.addError(StatusCodes.MISSING_ATTRIBUTE, 
                                     "Availability for "+ id +"is empty.", id);
@@ -198,7 +200,9 @@
             	    }
             		Matcher m = licensePattern.matcher(availability);
             		if (!m.matches()){
-            		    jlog.debug("pattern: "+ licensePattern.toString() + ", availability: "+availability);
+            		    if (DEBUG){
+            		        jlog.debug("pattern: "+ licensePattern.toString() + ", availability: "+availability);
+            		    }
             			km = new Match();
             			km.addError(StatusCodes.ACCESS_DENIED, 
             					"Retrieving match info with ID "+id+" is not allowed.", id);
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 90e9435..d83ce1f 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
@@ -49,6 +49,8 @@
 @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
 public class SearchController {
 
+    private static final boolean DEBUG = false;
+
     private static Logger jlog = LogManager.getLogger(SearchController.class);
 
     @Autowired
@@ -98,7 +100,9 @@
             scopeService.verifyScope(ctx, OAuth2Scope.SERIALIZE_QUERY);
             String result = searchService.serializeQuery(q, ql, v, cq,
                     pageIndex, startPage, pageLength, context, cutoff);
-            jlog.debug("Query: " + result);
+            if (DEBUG){
+                jlog.debug("Query: " + result);
+            }
             return Response.ok(result).build();
         }
         catch (KustvaktException e) {
@@ -119,9 +123,13 @@
             throw kustvaktResponseHandler.throwit(e);
         }
 
-        jlog.debug("Serialized search: " + jsonld);
+        if (DEBUG){
+            jlog.debug("Serialized search: " + jsonld);
+        }
         String result = searchService.search(jsonld);
-        jlog.debug("The result set: " + result);
+        if (DEBUG){
+            jlog.debug("The result set: " + result);
+        }
         return Response.ok(result).build();
     }
 
@@ -135,7 +143,7 @@
             @QueryParam("count") Integer pageLength,
             @QueryParam("offset") Integer pageIndex,
             @QueryParam("page") Integer pageInteger,
-            @QueryParam("fields") Set<String> fields,
+            @QueryParam("fields") String fields,
             @QueryParam("cq") String cq, @QueryParam("engine") String engine) {
 
         TokenContext context =
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 43970be..61bf57a 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
@@ -42,6 +42,7 @@
 public class StatisticController {
 
 
+    private static final boolean DEBUG = false;
     private static Logger jlog =
             LogManager.getLogger(StatisticController.class);
     @Autowired
@@ -86,9 +87,12 @@
 			stats = searchKrill.getStatistics(null);
 		};
 
-        if (stats.contains("-1"))
+        if (stats.contains("-1")){
             throw kustvaktResponseHandler.throwit(StatusCodes.NO_RESULT_FOUND);
-        jlog.debug("Stats: " + stats);
+        }
+        if (DEBUG){
+            jlog.debug("Stats: " + stats);
+        }
         return Response.ok(stats).build();
     }
 }
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/utils/NotFoundMapper.java b/core/src/main/java/de/ids_mannheim/korap/web/utils/NotFoundMapper.java
index 7f5db5c..e8db76d 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/utils/NotFoundMapper.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/utils/NotFoundMapper.java
@@ -27,6 +27,7 @@
             LogManager.getLogger(NotFoundMapper.class);
     public static final Pattern VERSION_PATTERN =
             Pattern.compile("/(v[0-9][^/]*)(/.*)");
+    private static final boolean DEBUG = false;
 
     @Autowired
     private KustvaktConfiguration config;
@@ -46,7 +47,9 @@
                 path = baseUrl + "/" + config.getCurrentVersion() + path;
                 URI redirectUri = UriBuilder.fromUri(notFoundUri)
                         .replacePath(path).build();
-                jlog.debug("REDIRECT: "+redirectUri.toString());
+                if (DEBUG){
+                    jlog.debug("REDIRECT: "+redirectUri.toString());
+                }
                 return Response.status(HttpStatus.PERMANENT_REDIRECT_308)
                         .location(redirectUri).build();
             }
@@ -55,7 +58,9 @@
                         + matcher.group(2);
                 URI redirectUri = UriBuilder.fromUri(notFoundUri)
                         .replacePath(path).build();
-                jlog.debug("REDIRECT replace: "+ redirectUri.toString());
+                if (DEBUG){
+                    jlog.debug("REDIRECT replace: "+ redirectUri.toString());
+                }
                 return Response.status(HttpStatus.PERMANENT_REDIRECT_308)
                         .location(redirectUri).build();
             }
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchControllerTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchControllerTest.java
index d2d9733..b30b626 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchControllerTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteSearchControllerTest.java
@@ -137,8 +137,7 @@
     public void testParameterField () throws KustvaktException {
         ClientResponse response = resource().path(API_VERSION).path("search")
                 .queryParam("q", "[orth=das]").queryParam("ql", "poliqarp")
-                .queryParam("fields", "author")
-                .queryParam("fields", "docSigle")
+                .queryParam("fields", "author,docSigle")
                 .queryParam("context", "sentence").queryParam("count", "13")
                 .get(ClientResponse.class);
         assertEquals(ClientResponse.Status.OK.getStatusCode(),