Do not pass broken queries to Krill

Change-Id: I7f6d6f949ad4b4cc4ce1febf523cedb982bec45f
diff --git a/core/pom.xml b/core/pom.xml
index 3e09cee..2525eff 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -223,7 +223,7 @@
 		<dependency>
 			<groupId>de.ids_mannheim.korap</groupId>
 			<artifactId>Koral</artifactId>
-			<version>0.28</version>
+			<version>0.29</version>
 			<exclusions>
 				<exclusion>
 					<groupId>org.eclipse.jetty</groupId>
diff --git a/full/Changes b/full/Changes
index 738c78f..bdf5134 100644
--- a/full/Changes
+++ b/full/Changes
@@ -1,5 +1,5 @@
 version 0.60.4
-04/07/2018
+05/07/2018
     - implemented OAuth2 authorization code request with OpenID Authentication (margaretha)
     - enabled OAuth2 authorization without OpenID authentication using Nimbus library (margaretha)
     - implemented response handler for OpenID authentication errors in authorization requests (margaretha)
@@ -20,6 +20,7 @@
     - fixed OAuth2 client unique URL-hashcode (margaretha)
     - migrated logging to log4j 2 and adapted java.util.logging to log4j(margaretha)
     - Added support for unrestricted corpus statistics (ndiewald)
+    - Do not pass broken queries to Krill (diewald)
     
 version 0.60.3
 06/06/2018
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
index 0b345cb..d78eaea 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
@@ -444,6 +444,14 @@
                 pageLength, cutoff);
         serializer.setMeta(meta.raw());
 
+		// There is an error in query processing
+		// - either query, corpus or meta
+		if (serializer.hasErrors()) {
+
+			// Do not pass further to backend
+			return Response.status(Response.Status.BAD_REQUEST).entity(serializer.toJSON()).build();		
+		};
+		
         String query;
         try {
             query = this.processor.processQuery(serializer.toJSON(), user);
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
index ecede59..13836be 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
@@ -4,6 +4,7 @@
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import javax.ws.rs.core.MediaType;
 
@@ -42,7 +43,6 @@
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
-//        System.out.println(ent);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertEquals("koral:doc", node.at("/collection/@type").asText());
@@ -54,6 +54,28 @@
                 node.at("/collection/rewrites/0/operation").asText());
     }
 
+	
+    @Test
+    public void testSearchQueryFailure () throws KustvaktException{
+        ClientResponse response = resource()
+			.path("search").queryParam("q", "[orth=der")
+			.queryParam("ql", "poliqarp")
+			.queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
+			.queryParam("count", "13")
+			.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+
+        String ent = response.getEntity(String.class);
+		JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertEquals(302, node.at("/errors/0/0").asInt());
+        assertEquals(302, node.at("/errors/1/0").asInt());
+		assertTrue(node.at("/errors/2").isMissingNode());
+		assertFalse(node.at("/collection").isMissingNode());
+        assertEquals(13, node.at("/meta/count").asInt());
+    }
+
 
     @Test
     public void testSearchQueryWithMeta () throws KustvaktException{
diff --git a/lite/Changes b/lite/Changes
index bd0f1db..03c57d6 100644
--- a/lite/Changes
+++ b/lite/Changes
@@ -1,6 +1,7 @@
 version 0.60.2
-29/06/2018
+05/07/2018
     - Added support for unrestricted corpus statistics (ndiewald)
+    - Do not pass broken queries to Krill (diewald)
 
 version 0.60.1
 12/06/2018
diff --git a/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java b/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
index 3a384d5..0ed2b95 100644
--- a/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
+++ b/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
@@ -24,6 +24,8 @@
 
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 
+import com.fasterxml.jackson.databind.JsonNode;
+
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.config.QueryBuilderUtil;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
@@ -35,6 +37,7 @@
 import de.ids_mannheim.korap.web.ClientsHandler;
 import de.ids_mannheim.korap.web.CoreResponseHandler;
 import de.ids_mannheim.korap.web.SearchKrill;
+import de.ids_mannheim.korap.utils.JsonUtils;
 
 /**
  * @author hanl
@@ -144,17 +147,18 @@
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        // todo: should be possible to add the meta part to the query serialization
-        jlog.info("Serialized search: "+jsonld);
-        try {
-            String result = searchKrill.search(jsonld);
-            jlog.debug("The result set: "+result);
-            return Response.ok(result).build();
-        }
-        catch (Exception e) {
-            e.printStackTrace();
-        }
-        return Response.ok().build();
+
+		// todo: should be possible to add the meta part to the query serialization
+		jlog.info("Serialized search: "+jsonld);
+		try {
+			String result = searchKrill.search(jsonld);
+			jlog.debug("The result set: "+result);
+			return Response.ok(result).build();
+		}
+		catch (Exception e) {
+			e.printStackTrace();
+		};
+		return Response.ok().build();
     }
 
 
@@ -181,6 +185,15 @@
         serializer.setMeta(meta.raw());
         if (cq != null) serializer.setCollection(cq);
 
+
+		// There is an error in query processing
+		// - either query, corpus or meta
+		if (serializer.hasErrors()) {
+			// Do not pass further to backend
+			return Response.status(Response.Status.BAD_REQUEST).entity(serializer.toJSON()).build();		
+		}
+
+		
         String query;
         try {
             query = this.processor.processQuery(serializer.toJSON(), null);
@@ -188,7 +201,8 @@
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        jlog.info("the serialized query "+ query);
+
+		jlog.info("the serialized query "+ query);
 
         // This may not work with the the KoralQuery
         if (eng.equals(KustvaktConfiguration.BACKENDS.NEO4J)) {
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
index 8c815e8..42f6ebc 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
@@ -3,6 +3,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.*;
@@ -168,6 +170,27 @@
         assertNotEquals(0, node.at("/matches").size());
     }
 
+	@Test
+    public void testQueryFailure () throws KustvaktException{
+        ClientResponse response = resource()
+                .path("search").queryParam("q", "[orth=das")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
+			.queryParam("count", "13")
+			.get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+
+		JsonNode node = JsonUtils.readTree(query);
+        assertNotNull(node);
+        assertEquals(302, node.at("/errors/0/0").asInt());
+        assertEquals(302, node.at("/errors/1/0").asInt());
+		assertTrue(node.at("/errors/2").isMissingNode());
+		assertFalse(node.at("/collection").isMissingNode());
+        assertEquals(13, node.at("/meta/count").asInt());
+    }
+
 
     @Test
     public void testFoundryRewrite () throws KustvaktException{