Add Export WebService

Change-Id: I8ae3901fca5ade8cb46b62bcc99e593c4bbcd40f
diff --git a/plugin/pom.xml b/plugin/pom.xml
index 7b4ca63..92c6698 100644
--- a/plugin/pom.xml
+++ b/plugin/pom.xml
@@ -19,7 +19,17 @@
             <artifactId>junit</artifactId>
             <version>4.13</version>
         </dependency>
-
+    
+        <dependency>
+            <groupId>com.fasterxml.jackson.core</groupId>
+            <artifactId>jackson-databind</artifactId>
+            <version>2.9.8</version>
+        </dependency>
+        <dependency> 
+            <groupId>com.tutego</groupId>
+             <artifactId>jrtf</artifactId>
+             <version>0.7</version>
+        </dependency>
         <!-- jetty -->
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
index 1357971..7595e87 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/IdsExportService.java
@@ -1,19 +1,120 @@
 package de.ids_mannheim.korap.plkexport;
 
-import javax.ws.rs.GET;
+import java.io.IOException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedList;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.tutego.jrtf.*;
+import static com.tutego.jrtf.Rtf.rtf;
+import static com.tutego.jrtf.RtfPara.*;
+import static com.tutego.jrtf.RtfText.*;
 
-@Path("/ids-export")
+@Path("/")
 public class IdsExportService {
 
-    @GET
-    @Path("exportHtml")
-    @Produces(MediaType.TEXT_HTML)
-    public String testHtml () {
-        return "Export Web Service under construction";
+    /**
+     * WebService calls Kustvakt Search Webservices and returns
+     * response as json(all of the response) and
+     * as rtf(matches)
+     * 
+     */
+    @POST
+    @Path("export")
+    @Produces(MediaType.APPLICATION_OCTET_STREAM)
+    public Response testjsonform (@FormParam("fname") String fname,
+            @FormParam("format") String format, @FormParam("q") String q,
+            @FormParam("ql") String ql) throws IOException {
+
+
+        Client client = ClientBuilder.newClient();
+
+        String url = "http://localhost:8089/api/v1.0/search?context=sentence"
+                + "&q=" + URLEncoder.encode(q, "UTF-8") + "&ql=" + ql;
+        WebTarget resource = client.target(url);
+        String resp = resource.request(MediaType.APPLICATION_JSON)
+                .get(String.class);
+        ResponseBuilder builder;
+
+        //format == json
+        if (format.equals("json")) {
+            builder = Response.ok(resp);
+            builder.header("Content-Disposition",
+                    "attachment; filename=" + fname + ".json");
+            builder.type(MediaType.APPLICATION_JSON);
+            Response response = builder.build();
+            return response;
+        }
+
+        // format == rtf / else
+        else {
+
+            ObjectMapper mapper = new ObjectMapper();
+            JsonFactory factory = mapper.getFactory();
+            JsonParser parser = factory.createParser(resp);
+            JsonNode actualObj = mapper.readTree(parser);
+            JsonNode jsonNode1 = actualObj.get("matches");
+            LinkedList<MatchExport> listMatches = new LinkedList();
+            ObjectMapper objectMapper = new ObjectMapper();
+            MatchExport match;
+            for (Iterator<JsonNode> itNode = jsonNode1.elements(); itNode
+                    .hasNext();) {
+                match = objectMapper.readValue(itNode.next().toString(),
+                        MatchExport.class);
+                listMatches.addFirst(match);
+            }
+
+
+            String rtfresp = writeRTF(listMatches);
+            builder = Response.ok(rtfresp);
+            builder.header("Content-Disposition",
+                    "attachment; filename=" + fname + ".rtf");
+            Response response = builder.build();
+            return response;
+        }
     }
 
 
+    public String writeRTF (LinkedList list) {
+        LinkedList matchlist = list;
+        RtfTextPara par = p((" "));
+        RtfTextPara[] pararray;
+        pararray = new RtfTextPara[matchlist.size()];
+        Collection<RtfPara> listp = new ArrayList<RtfPara>();
+
+        String reference;
+        String textSigle;
+        int j = matchlist.size();
+
+        for (int i = 0; i < j; i++) {
+            MatchExport matchakt = (MatchExport) matchlist.get(i);
+            reference = " (" + matchakt.getTitle() + " von "
+                    + matchakt.getAuthor() + " (" + matchakt.getPubDate() + ")";
+            textSigle = "[" + matchakt.getTextSigle() + "]";
+            String leftSnippet = matchakt.getSnippetO().getLeft();
+            String rightSnippet = matchakt.getSnippetO().getRight();
+            String markedMatch = matchakt.getSnippetO().getMark();
+            par = p(leftSnippet, (" "), bold(markedMatch), (" "), rightSnippet,
+                    bold(reference), (" "), bold(textSigle), "\n");
+            listp.add(par);
+        }
+
+        String rtfresp = rtf().section(listp).toString();
+        return rtfresp;
+    }
 }
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchExport.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchExport.java
new file mode 100644
index 0000000..b2275fe
--- /dev/null
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchExport.java
@@ -0,0 +1,75 @@
+package de.ids_mannheim.korap.plkexport;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class MatchExport {
+
+    private String textSigle;
+    private String author;
+    private String pubDate;
+    private Snippet snippeto;
+    private String snippet;
+    private String title;
+
+
+    public String getAuthor () {
+        return author;
+    }
+
+
+    public void setAuthor (String author) {
+        this.author = author;
+    }
+
+
+    public String getTextSigle () {
+        return textSigle;
+    }
+
+
+    public void setTextSigle (String textSigle) {
+        this.textSigle = textSigle;
+    }
+
+
+    public Snippet getSnippetO () {
+        return snippeto;
+    }
+
+
+    public void setSnippetO (Snippet snippet) {
+        this.snippeto = snippet;
+    }
+
+
+    public String getSnippet () {
+        return snippet;
+    }
+
+
+    public void setSnippet (String snippet) {
+        this.snippet = snippet;
+        this.snippeto = new Snippet(this.snippet);
+    }
+
+
+    public String getTitle () {
+        return title;
+    }
+
+
+    public void setTitle (String title) {
+        this.title = title;
+    }
+
+
+    public String getPubDate () {
+        return pubDate;
+    }
+
+
+    public void setPubDate (String pubDate) {
+        this.pubDate = pubDate;
+    }
+}
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Snippet.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Snippet.java
new file mode 100644
index 0000000..e941f81
--- /dev/null
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Snippet.java
@@ -0,0 +1,53 @@
+package de.ids_mannheim.korap.plkexport;
+
+public class Snippet {
+
+    private String left;
+    private String right;
+    private String mark;
+
+
+    public Snippet (String snippetstr) {
+        String[] split = snippetstr.split("</?mark>");
+        String splitleft = split[0];
+        String splitmatch = split[1];
+        String splitright = split[2];
+        //(?i) makes the regex case insensitive.
+        String splitleftr = splitleft.replaceAll("(?i)</?span[^>]*>", "");
+        this.setLeft(splitleftr);
+        String splitmatchr = splitmatch.replaceAll("(?i)</?span[^>]*>", "");
+        this.setMark(splitmatchr);
+        String splitrightr = splitright.replaceAll("(?i)</?span[^>]*>", "");
+        this.setRight(splitrightr);
+    }
+
+
+    public String getLeft () {
+        return left;
+    }
+
+
+    public void setLeft (String left) {
+        this.left = left;
+    }
+
+
+    public String getRight () {
+        return right;
+    }
+
+
+    public void setRight (String right) {
+        this.right = right;
+    }
+
+
+    public String getMark () {
+        return mark;
+    }
+
+
+    public void setMark (String mark) {
+        this.mark = mark;
+    }
+}