Fix running pipe and updated tests with mockserver

Change-Id: I9e2ad0a9e20db874765e3db957ff356fbe35ff93
diff --git a/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java b/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
index 2ec9040..647009d 100644
--- a/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
+++ b/core/src/main/java/de/ids_mannheim/korap/server/KustvaktBaseServer.java
@@ -6,7 +6,9 @@
 import java.io.OutputStreamWriter;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.security.NoSuchAlgorithmException;
+import java.util.Scanner;
 
 import javax.servlet.ServletContextListener;
 
@@ -19,7 +21,6 @@
 import org.eclipse.jetty.servlet.ServletHolder;
 import org.springframework.web.context.ContextLoaderListener;
 
-import com.google.common.io.Files;
 import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
 
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
@@ -90,8 +91,9 @@
             writer.close();
         }
         else {
-            adminToken = Files.readFirstLine(f, Charset.forName("utf-8"))
-                    .substring(6);
+            Scanner scanner = new Scanner(f);
+            adminToken = scanner.nextLine().substring(6);
+            scanner.close();
         }
 
         Server server = new Server();
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 28c526d..43114cb 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,5 +1,10 @@
 package de.ids_mannheim.korap.service;
 
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
@@ -8,7 +13,6 @@
 import javax.annotation.PostConstruct;
 import javax.servlet.ServletContext;
 import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriBuilder;
 
@@ -21,9 +25,6 @@
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.WebResource;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 
 import de.ids_mannheim.de.init.VCLoader;
@@ -207,22 +208,36 @@
             for (int i=0; i<pipeArray.length; i++){
                 String pipeURL = pipeArray[i];
                 try {
-                    Client client = Client.create();
-                    WebResource resource = client.resource(pipeURL);
-                    ClientResponse response =
-                            resource.type(MediaType.APPLICATION_JSON)
-                                    .accept(MediaType.APPLICATION_JSON)
-                                    .post(ClientResponse.class, query);
-                    if (response.getStatus() == HttpStatus.SC_OK) {
-                        String entity = response.getEntity(String.class);
-                        if (entity != null && !entity.isEmpty()) {
-                            query = entity;
+                    URL url = new URL(pipeURL);
+                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+                    connection.setRequestMethod("POST");
+                    connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
+                    connection.setRequestProperty("Accept", "application/json");
+                    connection.setDoOutput(true);
+                    OutputStream os = connection.getOutputStream();
+                    byte[] input = query.getBytes("utf-8");
+                    os.write(input, 0, input.length);     
+                    
+                    String entity = null;
+                    if (connection.getResponseCode() == HttpStatus.SC_OK) {
+                        BufferedReader br =
+                                new BufferedReader(new InputStreamReader(
+                                        connection.getInputStream(), "utf-8"));
+                        StringBuilder response = new StringBuilder();
+                        String responseLine = null;
+                        while ((responseLine = br.readLine()) != null) {
+                            response.append(responseLine.trim());
                         }
+                        entity = response.toString();
+                    }
+
+                    if (entity != null && !entity.isEmpty()) {
+                        query = entity;
                     }
                     else {
                         query = handlePipeError(query, pipeURL,
-                                response.getStatus() + " "
-                                        + response.getStatusInfo().toString());
+                                connection.getResponseCode() + " "
+                                        + connection.getResponseMessage());
                     }
                 }
                 catch (Exception e) {
diff --git a/core/src/main/java/de/ids_mannheim/korap/test/TestController.java b/core/src/main/java/de/ids_mannheim/korap/test/TestController.java
deleted file mode 100644
index 03b8f96..0000000
--- a/core/src/main/java/de/ids_mannheim/korap/test/TestController.java
+++ /dev/null
@@ -1,93 +0,0 @@
-package de.ids_mannheim.korap.test;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Controller;
-
-import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ArrayNode;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-
-import de.ids_mannheim.korap.exceptions.KustvaktException;
-import de.ids_mannheim.korap.utils.JsonUtils;
-import de.ids_mannheim.korap.web.KustvaktResponseHandler;
-
-/**
- * Controllers used only for testing
- * 
- * @author margaretha
- *
- */
-@Controller
-@Path("/{version}/test")
-@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
-public class TestController {
-
-    @Autowired
-    private KustvaktResponseHandler kustvaktResponseHandler;
-
-    public static ObjectMapper mapper = new ObjectMapper();
-
-    @POST
-    @Path("glemm")
-    @Consumes(MediaType.APPLICATION_JSON)
-    public Response dummyGlemm (String jsonld,
-            @QueryParam("param") String param) throws IOException {
-        InputStream is;
-        is = getClass().getClassLoader()
-                .getResourceAsStream("test-pipes.jsonld");
-
-        ObjectNode newJson = (ObjectNode) mapper.readTree(is);
-
-        try {
-            JsonNode node = JsonUtils.readTree(jsonld);
-            if (node.has("warnings")) {
-                node = node.get("warnings");
-                newJson.set("warnings", node);
-            }
-            if (param != null && !param.isEmpty()) {
-                ArrayNode keys = (ArrayNode) newJson.at("/query/wrap/key");
-                keys.add("die");
-                newJson.set("key", keys);
-            }
-        }
-        catch (KustvaktException e) {
-            throw kustvaktResponseHandler.throwit(e);
-        }
-        return Response.ok(newJson.toString()).build();
-    }
-
-    @POST
-    @Path("invalid-json-pipe")
-    @Consumes(MediaType.APPLICATION_JSON)
-    public Response dummyPipe1 (String jsonld) {
-        String incorrectJson = "{blah:}";
-        return Response.ok(incorrectJson).build();
-    }
-
-    @POST
-    @Path("plain-response-pipe")
-    @Consumes(MediaType.APPLICATION_JSON)
-    public Response dummyPipe2 (String jsonld) {
-        String incorrectJson = "brumbrum";
-        return Response.ok(incorrectJson).build();
-    }
-
-    @POST
-    @Path("urlencoded-pipe")
-    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
-    public Response dummyPipe3 (String jsonld) {
-        return Response.ok().build();
-    }
-}
diff --git a/core/src/main/resources/test-pipes.jsonld b/core/src/test/resources/pipe-output/test-pipes.jsonld
similarity index 100%
rename from core/src/main/resources/test-pipes.jsonld
rename to core/src/test/resources/pipe-output/test-pipes.jsonld
diff --git a/core/src/test/resources/pipe-output/with-param.jsonld b/core/src/test/resources/pipe-output/with-param.jsonld
new file mode 100644
index 0000000..08c7fda
--- /dev/null
+++ b/core/src/test/resources/pipe-output/with-param.jsonld
@@ -0,0 +1,29 @@
+{
+    "meta": {
+        "snippets": true,
+        "timeout": 10000
+    },
+    "query": {
+        "@type": "koral:token",
+        "wrap": {
+            "@type": "koral:term",
+            "match": "match:eq",
+            "key": [
+                "der",
+                "das",
+		"die"
+            ],
+            "layer": "orth",
+            "rewrites": [
+                {
+                    "@type": "koral:rewrite",
+                    "src": "Glemm",
+                    "operation": "operation:override",
+                    "scope": "key"
+                }
+            ]
+        }
+    },
+    "@context": "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld"
+}
+