Bundle assets for improved testing
Change-Id: Ia299b893687ff99da4da9f6c8cb9a67139518450
diff --git a/plugin/pom.xml b/plugin/pom.xml
index 834bb14..fb561f5 100644
--- a/plugin/pom.xml
+++ b/plugin/pom.xml
@@ -1,5 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.ids_mannheim.korap</groupId>
<artifactId>KalamarExportPlugin</artifactId>
@@ -8,122 +8,150 @@
<name>KalamarExportPlugin</name>
<url>http://maven.apache.org</url>
- <properties>
- <jetty.version>9.2.3.v20140905</jetty.version>
- <jersey.version>2.27</jersey.version>
- </properties>
+ <properties>
+ <jetty.version>9.4.31.v20200723</jetty.version>
+ <jersey.version>2.27</jersey.version>
+ </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13</version>
- </dependency>
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <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>
- <artifactId>jetty-server</artifactId>
- <version>${jetty.version}</version>
- </dependency>
- <dependency>
- <groupId>org.eclipse.jetty</groupId>
- <artifactId>jetty-servlet</artifactId>
- <version>${jetty.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.glassfish.jersey.core</groupId>
- <artifactId>jersey-server</artifactId>
- <version>${jersey.version}</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish.jersey.core</groupId>
- <artifactId>jersey-client</artifactId>
- <version>${jersey.version}</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish.jersey.inject</groupId>
- <artifactId>jersey-hk2</artifactId>
- <version>${jersey.version}</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish.jersey.containers</groupId>
- <artifactId>jersey-container-servlet-core</artifactId>
- <version>${jersey.version}</version>
- </dependency>
-
- <!-- Jersey test framework -->
- <dependency>
- <groupId>org.glassfish.jersey.test-framework</groupId>
- <artifactId>jersey-test-framework-core</artifactId>
- <version>${jersey.version}</version>
- </dependency>
- <dependency>
- <groupId>org.glassfish.jersey.test-framework.providers</groupId>
- <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
- <version>${jersey.version}</version>
- <scope>test</scope>
- </dependency>
-
- <!-- Mock server framework -->
- <dependency>
- <groupId>org.mock-server</groupId>
- <artifactId>mockserver-netty</artifactId>
- <version>3.10.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
+ <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>
+ <artifactId>jetty-server</artifactId>
+ <version>${jetty.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ <version>${jetty.version}</version>
+ </dependency>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-shade-plugin</artifactId>
- <version>1.6</version>
- <configuration>
- <filters>
- <filter>
- <artifact>*:*</artifact>
- <excludes>
- <exclude>META-INF/*.SF</exclude>
- <exclude>META-INF/*.DSA</exclude>
- <exclude>META-INF/*.RSA</exclude>
- </excludes>
- </filter>
- </filters>
- </configuration>
- <executions>
- <execution>
- <phase>package</phase>
- <goals>
- <goal>shade</goal>
- </goals>
- <configuration>
- <transformers>
- <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
- <transformer
- implementation='org.apache.maven.plugins.shade.resource.ManifestResourceTransformer'>
- <manifestEntries>
- <Main-Class>de.ids_mannheim.korap.plkexport.PluginServer</Main-Class>
- </manifestEntries>
- </transformer>
- </transformers>
- </configuration>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
+ <dependency>
+ <groupId>org.glassfish.jersey.core</groupId>
+ <artifactId>jersey-server</artifactId>
+ <version>${jersey.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.core</groupId>
+ <artifactId>jersey-client</artifactId>
+ <version>${jersey.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.inject</groupId>
+ <artifactId>jersey-hk2</artifactId>
+ <version>${jersey.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.containers</groupId>
+ <artifactId>jersey-container-servlet-core</artifactId>
+ <version>${jersey.version}</version>
+ </dependency>
+
+ <!-- Jersey test framework -->
+ <dependency>
+ <groupId>org.glassfish.jersey.test-framework</groupId>
+ <artifactId>jersey-test-framework-core</artifactId>
+ <version>${jersey.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.test-framework.providers</groupId>
+ <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
+ <version>${jersey.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Mock server framework -->
+ <dependency>
+ <groupId>org.mock-server</groupId>
+ <artifactId>mockserver-netty</artifactId>
+ <version>3.10.2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>3.2.4</version>
+ <configuration>
+ <filters>
+ <filter>
+ <artifact>*:*</artifact>
+ <excludes>
+ <exclude>META-INF/*.SF</exclude>
+ <exclude>META-INF/*.DSA</exclude>
+ <exclude>META-INF/*.RSA</exclude>
+ </excludes>
+ </filter>
+ </filters>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <transformers>
+ <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
+ <transformer
+ implementation='org.apache.maven.plugins.shade.resource.ManifestResourceTransformer'>
+ <manifestEntries>
+ <Main-Class>de.ids_mannheim.korap.plkexport.PluginServer</Main-Class>
+ </manifestEntries>
+ </transformer>
+ <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
+ <resource>resources</resource>
+ <file>assets/test.txt</file>
+ </transformer>
+ </transformers>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>3.2.0</version>
+ <configuration>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>testResources</goal>
+ </goals>
+ <configuration>
+ <resources>
+ <resource>
+ <directory>src/main/resources</directory>
+ </resource>
+ <resource>
+ <directory>src/test/resources</directory>
+ </resource>
+ </resources>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
</project>
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 e2dffaf..c0ecfef 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,6 +1,8 @@
package de.ids_mannheim.korap.plkexport;
import java.io.IOException;
+import java.lang.Thread;
+import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
@@ -12,6 +14,7 @@
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
+import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
@@ -29,6 +32,7 @@
import com.fasterxml.jackson.core.Version;
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.*;
@@ -41,6 +45,14 @@
public class IdsExportService {
Properties properties = ExWSConf.properties(null);
+
+ // Load export string
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ InputStream is1 = cl.getResourceAsStream("assets/export.html");
+ private final String exportStr = streamToString(is1);
+
+ InputStream is2 = cl.getResourceAsStream("assets/export.js");
+ private final String exportJsStr = streamToString(is2);
/**
* WebService calls Kustvakt Search Webservices and returns
@@ -58,7 +70,6 @@
*
*
*/
-
@POST
@Path("export")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@@ -159,15 +170,30 @@
fname = fname + ".rtf";
}
- // TODO:
- // Sanitize file name (i.e. replace extra characters)
builder.header("Content-Disposition",
"attachment; filename=" + sanitizeFileName(fname));
Response response = builder.build();
return response;
}
+ @GET
+ @Path("export")
+ @Produces(MediaType.TEXT_HTML)
+ public Response exportHTML () {
+ return Response
+ .ok(exportStr, MediaType.TEXT_HTML)
+ .build();
+ };
+ @GET
+ @Path("export.js")
+ @Produces("application/javascript")
+ public Response exportJavascript () {
+ return Response
+ .ok(exportJsStr, "application/javascript")
+ .build();
+ };
+
public String writeRTF (LinkedList list) {
LinkedList matchlist = list;
RtfTextPara par = p((" "));
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 925a465..27fa634 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
@@ -9,14 +9,12 @@
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
-import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class PluginServer {
public static void main (String[] args) throws Exception {
-
ServletContextHandler contextHandler = new ServletContextHandler(
ServletContextHandler.NO_SESSIONS);
contextHandler.setContextPath("/");
@@ -35,21 +33,8 @@
connector.setIdleTimeout(60000);
jettyServer.addConnector(connector);
- ResourceHandler resourceHandler = new ResourceHandler();
- String resourceBase ="templates";
- // If server is started as jar-file in target directory
- if(!new File("templates").exists()) {
- resourceBase = "../" + resourceBase;
- }
-
- resourceHandler.setResourceBase(resourceBase);
- //enable directory listing
- resourceHandler.setDirectoriesListed(true);
- ContextHandler contextHandRes= new ContextHandler("/res");
- contextHandRes.setHandler(resourceHandler);
-
HandlerList handlers = new HandlerList();
- handlers.setHandlers(new Handler[] { contextHandRes, contextHandler, new DefaultHandler()});
+ handlers.setHandlers(new Handler[] { contextHandler, new DefaultHandler()});
jettyServer.setHandler(handlers);
ServletHolder servletHolder = contextHandler.addServlet(
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java
index 53614b2..c60cf27 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Util.java
@@ -1,5 +1,8 @@
package de.ids_mannheim.korap.plkexport;
+import java.io.IOException;
+import java.io.*;
+
public class Util {
public static String sanitizeFileName (String fname) {
return fname
@@ -11,4 +14,22 @@
.replaceFirst("-+$","")
;
}
+
+ public static String streamToString (InputStream in) {
+ StringBuilder sb = new StringBuilder();
+
+ try {
+ BufferedReader br = new BufferedReader(new InputStreamReader(in));
+
+ String line;
+ while ((line = br.readLine()) != null) {
+ sb.append(line + System.lineSeparator());
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return sb.toString();
+ }
+
}
diff --git a/plugin/templates/export.html b/plugin/src/main/resources/assets/export.html
similarity index 89%
rename from plugin/templates/export.html
rename to plugin/src/main/resources/assets/export.html
index 9d404db..11adb85 100644
--- a/plugin/templates/export.html
+++ b/plugin/src/main/resources/assets/export.html
@@ -3,7 +3,7 @@
<html>
<head>
<meta charset="UTF-8">
- <title>Export Template</title>
+ <title>Export</title>
<!-- This is only for testing purposes -->
<script src="http://localhost:64543/js/korap-plugin-0.2.1.js"></script>
@@ -13,7 +13,7 @@
<div class="banner" data-note="Experimental"></div>
<h1>Export: <code id="export-query"></code></h1>
<section>
- <form id="frmid" class="form-table" action="http://localhost:7777/export" method="POST">
+ <form id="frmid" class="form-table" action="/export" method="POST">
<fieldset>
<input type="hidden" id="q" name="q">
<input type="hidden" id="ql" name="ql">
@@ -42,6 +42,6 @@
</fieldset>
</form>
</section>
- <script src="export.js" defer></script>
+ <script src="/export.js" defer></script>
</body>
</html>
diff --git a/plugin/templates/export.js b/plugin/src/main/resources/assets/export.js
similarity index 100%
rename from plugin/templates/export.js
rename to plugin/src/main/resources/assets/export.js
diff --git a/plugin/src/main/resources/exportPlugin.conf b/plugin/src/main/resources/exportPlugin.conf
index f2d907c..4e8ecbc 100644
--- a/plugin/src/main/resources/exportPlugin.conf
+++ b/plugin/src/main/resources/exportPlugin.conf
@@ -3,6 +3,8 @@
#Server Configuration
server.port=7777
server.host=localhost
+server.scheme=https
api.port=80
-api.host=korap.ids-mannheim.de
\ No newline at end of file
+api.host=korap.ids-mannheim.de
+api.scheme=https
\ 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 27a577a..cc9a96b 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
@@ -139,9 +139,30 @@
assertEquals("Request JSON: Http Response should be 400: ",
Status.BAD_REQUEST.getStatusCode(), responsejson.getStatus());
- };
+ }
@Test
+ public void testFormHTML () {
+ Response responsehtml = target("/export").request()
+ .get();
+ assertEquals("HTTP Code",
+ Status.OK.getStatusCode(), responsehtml.getStatus());
+ String str = responsehtml.readEntity(String.class);
+ assertTrue("HTTP Body", str.contains("<title>Export</title>"));
+ }
+
+ @Test
+ public void testJS () {
+ Response responsejs = target("/export.js").request()
+ .get();
+ assertEquals("HTTP Code",
+ Status.OK.getStatusCode(), responsejs.getStatus());
+ String str = responsejs.readEntity(String.class);
+
+ assertTrue("HTTP Body", str.contains("pluginit"));
+ }
+
+ @Test
public void testExportWsJsonEmpty () {
mockClient.reset().when(
request()