Added support for plugin errors

Change-Id: Id7e7e0e94994682177e913703bed746d2a084f63
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 f670c1a..002e279 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
@@ -56,6 +56,12 @@
    
     InputStream is = cl.getResourceAsStream("assets/export.js");
     private final String exportJsStr = streamToString(is);
+
+    Configuration cfg = new Configuration();
+    {
+        cfg.setClassForTemplateLoading(IdsExportService.class, "/assets/templates");
+        cfg.setDefaultEncoding("UTF-8");
+    }
     
     /**
      * WebService calls Kustvakt Search Webservices and returns
@@ -130,10 +136,7 @@
                 .get(String.class);
         } catch (Exception e) {
             throw new WebApplicationException(
-                Response
-                .ok(new String("Unable to reach Backend"), MediaType.TEXT_PLAIN)
-                .status(Status.BAD_GATEWAY)
-                .build()
+                responseForm(Status.BAD_GATEWAY, "Unable to reach Backend")
                 );
         }
 
@@ -182,11 +185,20 @@
     @Path("export")
     @Produces(MediaType.TEXT_HTML)
     public Response exportHTML () {
-        
-        Configuration cfg = new Configuration();
-        cfg.setClassForTemplateLoading(IdsExportService.class, "/assets/templates");
-        cfg.setDefaultEncoding("UTF-8");
+        return responseForm();
+    };
 
+
+    /**
+     * Response with form template.
+     * 
+     * Accepts an optional error code and message.
+     */
+    private Response responseForm () {
+        return responseForm(null, null);
+    }
+
+    private Response responseForm (Status code, String msg) {
         StringWriter out = new StringWriter();
         HashMap<String, Object> templateData = new HashMap<String, Object>();
 
@@ -205,6 +217,12 @@
 
         templateData.put("assetPath", uri.build());
 
+        if (code != null) {
+            templateData.put("code", code.getStatusCode());
+            templateData.put("msg", msg);            
+        };
+
+        // Generate template
         try {
             Template template = cfg.getTemplate("export.ftl");
             template.process(templateData, out);
@@ -215,11 +233,17 @@
                 .status(Status.INTERNAL_SERVER_ERROR)
                 .build();
         }
-        return Response
-            .ok(out.toString(), "text/html")
-            .build();
-    };
 
+        ResponseBuilder resp = Response.ok(out.toString(), "text/html");
+
+        if (code != null)  {
+            resp = resp.status(code);
+        };
+
+        return resp.build();
+    }
+
+    
     @GET
     @Path("export.js")
     @Produces("application/javascript")
diff --git a/plugin/src/main/resources/assets/export.js b/plugin/src/main/resources/assets/export.js
index 8d234e8..e49f4fa 100644
--- a/plugin/src/main/resources/assets/export.js
+++ b/plugin/src/main/resources/assets/export.js
@@ -25,4 +25,8 @@
       P.resize();
     }
   );
+
+  if (window.dynCall) {
+    window.dynCall(P)
+  };
 };
diff --git a/plugin/src/main/resources/assets/templates/export.ftl b/plugin/src/main/resources/assets/templates/export.ftl
index d7efd56..62940df 100644
--- a/plugin/src/main/resources/assets/templates/export.ftl
+++ b/plugin/src/main/resources/assets/templates/export.ftl
@@ -41,5 +41,13 @@
       </form>
     </section>
     <script src="/export.js" defer></script>
+
+    <#if code??>
+    <script>//<![CDATA[
+      function dynCall (P) {
+        P.log(${code}, '${msg}');
+      };
+    //]]></script>
+    </#if>
   </body>
 </html>
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
index f0a89be..ca63a0a 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/IdsExportServiceTest.java
@@ -2,6 +2,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.fail;
 
 // Mockserver tests
@@ -151,8 +152,9 @@
         assertTrue("HTTP Body", str.contains("<title>Export</title>"));
         assertTrue("Assets", str.contains("<script src=\"https://korap.ids-mannheim.de/js"));
         assertTrue("Assets", str.contains("<link href=\"https://korap.ids-mannheim.de/css"));
+        assertFalse("Errors", str.contains("dynCall("));
     }
-    
+
     @Test
     public void testJS () {
         Response responsejs = target("/export.js").request()
@@ -322,6 +324,9 @@
 
         assertEquals("Request JSON: Http Response should be 502: ",
                      Status.BAD_GATEWAY.getStatusCode(), responsejson.getStatus());
+
+        String str = responsejson.readEntity(String.class);
+        assertTrue("HTTP Body", str.contains("P.log(502, 'Unable to reach Backend');"));
     };