Improve configuration and introduce API endpoint conf
Change-Id: Ie561b58dda8795c03d9fc33b949529108a5d9659
diff --git a/README.md b/README.md
index 8816a7f..282a730 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# KalamarExportPlugin
-#Setup
+## Setup
The jar file is located in the ```target/``` folder
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/ExWSConf.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/ExWSConf.java
index 3bd8e03..dc30d54 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/ExWSConf.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/ExWSConf.java
@@ -8,11 +8,15 @@
*/
package de.ids_mannheim.korap.plkexport;
+import java.io.*;
+import java.lang.String;
+import java.util.Properties;
+
public class ExWSConf {
/*
* maximum hits to be exported
* TODO: Define this constants after discussing it.
- * Maybe we need an distinction between user at the IDS and external user
+ * Maybe we need a distinction between users at the IDS and external users
* See also: https://www.ids-mannheim.de/cosmas2/script-app/hilfe/sitzung.html
*/
public static final int MAX_EXP_LIMIT = 10000;
@@ -21,4 +25,43 @@
public static final int VERSION_MAJOR = 0;
public static final int VERSION_MINOR = 1;
public static final int VERSION_PATCHLEVEL= 0;
+
+ private static Properties prop;
+
+ // Load properties from file
+ public static Properties properties (String propFile) {
+
+ if (prop != null)
+ return prop;
+
+ if (propFile == null)
+ propFile = "exportPlugin.conf";
+
+ InputStream iFile;
+ try {
+ iFile = new FileInputStream(propFile);
+ prop = new Properties();
+ prop.load(iFile);
+ }
+ catch (IOException t) {
+ try {
+ iFile = ExWSConf.class.getClassLoader()
+ .getResourceAsStream(propFile);
+
+ if (iFile == null) {
+ System.err.println("Unable to load properties.");
+ return null;
+ };
+
+ prop = new Properties();
+ prop.load(iFile);
+ iFile.close();
+ }
+ catch (IOException e) {
+ System.err.println(e.getLocalizedMessage());
+ return null;
+ };
+ };
+ return prop;
+ };
}
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 b02a1e4..d053fc9 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
@@ -6,6 +6,7 @@
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.Properties;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.FormParam;
@@ -34,6 +35,8 @@
@Path("/")
public class IdsExportService {
+ Properties properties = ExWSConf.properties(null);
+
/**
* WebService calls Kustvakt Search Webservices and returns
* response as json(all of the response) and
@@ -74,8 +77,10 @@
ResponseBuilder builder;
Client client = ClientBuilder.newClient();
-
- String url = "http://localhost:8089/api/v1.0/search?context=sentence"
+ String port = properties.getProperty("api.port", "8089");
+ String host = properties.getProperty("api.host", "localhost");
+
+ String url = "http://" + host + ":" + port + "/api/v1.0/search?context=sentence"
+ "&q=" + URLEncoder.encode(q, "UTF-8") + "&ql=" + ql;
if (il != null) {
@@ -85,6 +90,7 @@
else {
url = url + "&cutoff=1" + "&count=" + ExWSConf.MAX_EXP_LIMIT;
}
+
WebTarget resource = client.target(url);
String resp = resource.request(MediaType.APPLICATION_JSON)
.get(String.class);
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
index 1d09d8a..2815125 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/PluginServer.java
@@ -1,8 +1,6 @@
package de.ids_mannheim.korap.plkexport;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
import java.util.Properties;
import org.eclipse.jetty.server.Handler;
@@ -20,26 +18,10 @@
ServletContextHandler contextHandler = new ServletContextHandler(
- ServletContextHandler.NO_SESSIONS);
+ ServletContextHandler.NO_SESSIONS);
contextHandler.setContextPath("/");
- Properties properties = new Properties();
- File f = new File("exportPlugin.conf");
- InputStream in = null;
-
- if (!f.exists()) {
- String rootPath = Thread.currentThread().getContextClassLoader()
- .getResource("").getPath();
- String appConfigPath = rootPath + "exportPlugin.conf";
- in = new FileInputStream(appConfigPath);
- }
- else {
- in = new FileInputStream(f);
- }
-
-
- properties.load(in);
- in.close();
+ Properties properties = ExWSConf.properties(null);
//Default: Server is available under http://localhost:7070/
String portStr = properties.getProperty("server.port", "7070");
diff --git a/plugin/src/main/resources/exportPlugin.conf b/plugin/src/main/resources/exportPlugin.conf
index c9d18db..f2d907c 100644
--- a/plugin/src/main/resources/exportPlugin.conf
+++ b/plugin/src/main/resources/exportPlugin.conf
@@ -3,3 +3,6 @@
#Server Configuration
server.port=7777
server.host=localhost
+
+api.port=80
+api.host=korap.ids-mannheim.de
\ No newline at end of file
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 57c1d91..698c3dd 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
@@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Properties;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
@@ -20,6 +21,7 @@
import org.junit.Test;
import de.ids_mannheim.korap.plkexport.IdsExportService;
+import de.ids_mannheim.korap.plkexport.ExWSConf;
/*
* TODO Find a way to test efficiently the starting of the PluginServer with host and port of the config-file
@@ -52,6 +54,10 @@
String message;
+ Properties properties = ExWSConf.properties(null);
+ properties.setProperty("api.host", "localhost");
+ properties.setProperty("api.port", "8089");
+
Response responsejson = target("/export").request()
.post(Entity.form(frmap));
assertEquals("Request JSON: Http Response should be 200: ",