Produces expTemplate as a service and on the command line (closes #127)
Change-Id: I361b13ccdeb4e655474f8c4309b0a9772e31f417
diff --git a/src/main/java/de/ids_mannheim/korap/plkexport/ExpTempl.java b/src/main/java/de/ids_mannheim/korap/plkexport/ExpTempl.java
new file mode 100644
index 0000000..9566492
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/plkexport/ExpTempl.java
@@ -0,0 +1,56 @@
+package de.ids_mannheim.korap.plkexport;
+
+import org.tinylog.Logger;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+
+public class ExpTempl {
+
+/*
+ * Returns export template as JSON
+ */
+public static String getExportTempl(String scheme, String host, String port){
+
+ String json = "";
+ try {
+
+ ObjectMapper mapper = new ObjectMapper();
+ ObjectNode templ = mapper.createObjectNode();
+
+ templ.put("name", "Export");
+ templ.put("desc", "Exports Kalamar results");
+
+ ObjectNode embed = mapper.createObjectNode();
+ embed.put("panel", "result");
+ embed.put("title", "exports KWICs and snippets");
+ embed.put("icon", "\uf019");
+
+ ArrayNode classes = mapper.createArrayNode();
+ classes.add("button-icon");
+ classes.add("plugin");
+ embed.set("classes", classes);
+
+ ObjectNode onClick = mapper.createObjectNode();
+ onClick.put("action", "addWidget");
+ onClick.put("template", scheme + "://" + host + ":" + port +"/export");
+
+ ArrayNode perm = mapper.createArrayNode();
+ perm.add("forms");
+ perm.add("scripts");
+ perm.add("downloads");
+
+ onClick.set("permissions", perm);
+ embed.set("onClick", onClick);
+ templ.set("embed", embed);
+
+ json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(templ);
+
+} catch (Exception ex) {
+ Logger.error(ex);
+ return null;
+}
+ return json;
+ }
+}
diff --git a/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java b/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
index b12c2fa..323b4ab 100644
--- a/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
+++ b/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
@@ -1,5 +1,6 @@
package de.ids_mannheim.korap.plkexport;
+
import java.util.Properties;
import org.eclipse.jetty.server.Handler;
@@ -14,6 +15,7 @@
import org.tinylog.Logger;
+
import jakarta.servlet.Servlet;
/**
@@ -28,11 +30,27 @@
contextHandler.setContextPath("/");
String propfile = null;
- if(args.length >= 1) {
- propfile = args[0];
+
+ boolean printhelp = false;
+ boolean argexc = false;
+
+ if(args.length >= 1 & args.length <= 2) {
+ for (int i = 0; i <= args.length-1; i++) {
+ if (args[i].equals("-h" ) | args[i].equals("--help")){
+ printhelp = true;
+ }
+ else {
+ propfile = args[i];
+ }
+ }
+ }
+ else if(args.length >= 3){
+ argexc = true;
}
Properties properties = ExWSConf.properties(propfile);
-
+
+ String usage = "\n Usage is java -jar KalamarExportPlugin-"+ ExWSConf.version() +".jar [-h|--help] [myconf_exportPlugin.conf]";
+
// Default: Server is available under http://localhost:7070/
String portStr = properties.getProperty("server.port", "7070");
String host = properties.getProperty("server.host", "localhost");
@@ -72,6 +90,22 @@
properties.getProperty("api.port") +
properties.getProperty("api.path","")
);
+
+
+ if(printhelp){
+ System.out.println(usage);
+ String templString = ExpTempl.getExportTempl(properties.getProperty("server.scheme"), properties.getProperty("server.host"), properties.getProperty("server.port"));
+ System.out.println(" \n Export template to pass to the plugin registration handler: \n "
+ + templString);
+ }
+ else {
+ System.out.println("\n You can use -h or --help for more information about usage");
+ }
+
+ if(argexc){
+ System.out.println("\n Too much arguments: " + usage);
+ }
+
jettyServer.join();
}
finally {
@@ -79,4 +113,5 @@
jettyServer.destroy();
}
}
+
}
diff --git a/src/main/java/de/ids_mannheim/korap/plkexport/Service.java b/src/main/java/de/ids_mannheim/korap/plkexport/Service.java
index 093f802..6b9d02f 100644
--- a/src/main/java/de/ids_mannheim/korap/plkexport/Service.java
+++ b/src/main/java/de/ids_mannheim/korap/plkexport/Service.java
@@ -578,6 +578,18 @@
/**
+ *
+ * Returns the export template as JSON.
+ */
+ @GET
+ @Path("export/template")
+ @Produces(MediaType.APPLICATION_JSON)
+ public String exportDisplayTemplate () {
+ Properties properties = ExWSConf.properties(null);
+ return ExpTempl.getExportTempl(properties.getProperty("server.scheme"),properties.getProperty("server.host"), properties.getProperty("server.port"));
+ };
+
+ /**
* The export script.
*
* Returns a static JavaScript file.
diff --git a/src/test/java/de/ids_mannheim/korap/plkexport/ExpTemlTest.java b/src/test/java/de/ids_mannheim/korap/plkexport/ExpTemlTest.java
new file mode 100644
index 0000000..f0b6744
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/plkexport/ExpTemlTest.java
@@ -0,0 +1,57 @@
+package de.ids_mannheim.korap.plkexport;
+
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import jakarta.ws.rs.core.Response;
+import jakarta.ws.rs.core.Response.Status;
+import org.glassfish.jersey.test.JerseyTest;
+import org.glassfish.jersey.server.ResourceConfig;
+import java.util.Properties;
+import jakarta.ws.rs.core.Application;
+
+/**
+ * @author Helge
+ *
+ * Tests if export template is correctly displayed
+ */
+public class ExpTemlTest extends JerseyTest {
+
+ @Override
+ protected Application configure () {
+ return new ResourceConfig(Service.class);
+ }
+
+ /*
+ * Tests if export template is produced from service /export/template
+ */
+ @Test
+ public void testTemplService(){
+ Response response = target("/export/template").request()
+ .get();
+ assertEquals("Http Response should be 200:",
+ Status.OK.getStatusCode(), response.getStatus());
+ String json = response.readEntity(String.class);
+ assertTrue(json.contains("\"name\" : \"Export\""));
+ assertTrue(json.contains(" \"classes\" : [ \"button-icon\", \"plugin\" ]"));
+ Properties properties = ExWSConf.properties(null);
+ String templurl= properties.getProperty("server.scheme") + "://" + properties.getProperty("server.host")
+ + ":" + properties.getProperty("server.port") + "/export";
+ assertTrue(json.contains(templurl));
+ }
+
+ /*
+ * Tests if exportTemplate is returned correctly
+ */
+ @Test
+ public void testGetTempl(){
+ String scheme = "httpx";
+ String host = "xlocalhost";
+ String port = "1234";
+ String json = ExpTempl.getExportTempl("httpx", "xlocalhost", "1234");
+ assertTrue(json.contains("\"desc\" : \"Exports Kalamar results\""));
+ assertTrue(json.contains( "\"title\" : \"exports KWICs and snippets\""));
+ String templ = "\"template\" : \""+ scheme +"://"+host + ":" + port + "/export\"";
+ assertTrue(json.contains(templ));
+ }
+}