Make Filename and sanitization part of the exporter objects
Change-Id: I26834e80959b767907775e12c7b0ef0c377e3f95
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java
index 64cc843..8ed9007 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/Exporter.java
@@ -16,6 +16,8 @@
public JsonNode getQuery();
public JsonNode getCollection();
public void appendMatches (String s) throws IOException;
+ public String getFname ();
+ public void setFname (String s);
// Implemented by Exporter
public ResponseBuilder serve();
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 004de3f..3add327 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
@@ -201,11 +201,6 @@
);
}
- // set filename based on query (if not already set)
- if (fname == null) {
- fname = q;
- };
-
Exporter exp;
// Choose the correct exporter
@@ -216,6 +211,14 @@
exp = new RtfExporter();
};
+ // set filename based on query (if not already set)
+ if (fname != null) {
+ exp.setFname(fname);
+ }
+ else {
+ exp.setFname(q);
+ };
+
// Initialize exporter (with meta data and first matches)
exp.init(resp);
@@ -263,13 +266,6 @@
builder = exp.serve();
};
- builder.header(
- "Content-Disposition",
- "attachment; filename=" +
- sanitizeFileName(fname) +
- '.' +
- exp.getSuffix()
- );
return builder.build();
};
diff --git a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
index 944b729..6767680 100644
--- a/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
+++ b/plugin/src/main/java/de/ids_mannheim/korap/plkexport/MatchAggregator.java
@@ -22,6 +22,8 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
+import static de.ids_mannheim.korap.plkexport.Util.*;
+
/**
* Base class for collecting matches and header information
* for exporters implementing the Exporter interface.
@@ -38,6 +40,7 @@
private File file;
public JsonNode meta, query, collection;
+ private String fname;
public String getMimeType() {
return "text/plain";
@@ -47,6 +50,16 @@
return "txt";
};
+ public void setFname (String fname) {
+ this.fname = fname;
+ };
+
+ public String getFname () {
+ if (this.fname == null)
+ return "unknown";
+ return sanitizeFileName(this.fname);
+ };
+
public void setMeta (JsonNode meta) {
this.meta = meta;
};
@@ -177,7 +190,15 @@
rb = Response.ok(this.file);
};
- return rb.type(this.getMimeType());
+ return rb
+ .type(this.getMimeType())
+ .header(
+ "Content-Disposition",
+ "attachment; filename=" +
+ this.getFname() +
+ '.' +
+ this.getSuffix()
+ );
}
// Catch error
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java
index 8ab7acb..3784f2f 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/JsonExportTest.java
@@ -51,7 +51,17 @@
resp.close();
assertEquals(slurp(x),"{\"query\":\"cool\",\"meta\":\"ja\",\"collection\":\"hm\",\"matches\":[\"first\",\"second\",\"third\",\"fourth\"]}");
};
+
+ @Test
+ public void testAttributes () throws IOException {
+ JsonExporter json = new JsonExporter();
+ json.setFname("Beispiel");
+ assertEquals(json.getFname(),"Beispiel");
+ assertEquals(json.getMimeType(),"application/json");
+ assertEquals(json.getSuffix(),"json");
+ };
+
public static String slurp (File file) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String string;
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
index 80fe776..af21fd9 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/MatchAggregatorTest.java
@@ -45,4 +45,15 @@
assertNull(m.query);
assertNull(m.collection);
};
+
+ @Test
+ public void testAttributes () throws IOException {
+ MatchAggregator m = new MatchAggregator();
+ m.setFname("Beispiel");
+ assertEquals(m.getFname(),"Beispiel");
+ m.setFname("contains(<s name=\"okay\">,[orth='Test'])");
+ assertEquals(m.getFname(),"contains(s-name-okay-orth-Test)");
+ assertEquals(m.getMimeType(),"text/plain");
+ assertEquals(m.getSuffix(),"txt");
+ };
};
diff --git a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/RtfExportTest.java b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/RtfExportTest.java
index 956a33f..6192b5a 100644
--- a/plugin/src/test/java/de/ids_mannheim/korap/plkexport/RtfExportTest.java
+++ b/plugin/src/test/java/de/ids_mannheim/korap/plkexport/RtfExportTest.java
@@ -17,10 +17,10 @@
@Test
public void testInit () throws IOException {
- RtfExporter json = new RtfExporter();
- json.init("{\"query\":\"cool\"}");
+ RtfExporter rtf = new RtfExporter();
+ rtf.init("{\"query\":\"cool\"}");
- Response resp = json.serve().build();
+ Response resp = rtf.serve().build();
String x = (String) resp.getEntity();
resp.close();
assertTrue(x.contains("{\\pard\\ql @ Institut"));
@@ -29,8 +29,8 @@
@Test
public void testInitFull () throws IOException {
- RtfExporter json = new RtfExporter();
- json.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\"," +
+ RtfExporter rtf = new RtfExporter();
+ rtf.init("{\"meta\":\"ja\",\"collection\":\"hm\",\"query\":\"cool\"," +
"\"matches\":["+
"{\"author\":\"Goethe\","+
"\"title\":\"Title1\","+
@@ -47,7 +47,7 @@
"<span class=\\\"more\\\"></span></span>\"}"+
"]}");
- Response resp = json.serve().build();
+ Response resp = rtf.serve().build();
String x = (String) resp.getEntity();
resp.close();
assertTrue(x.contains("{\\b match1}"));
@@ -60,4 +60,13 @@
assertTrue(x.contains("Snippet\\par}"));
assertTrue(x.contains("Snippet [...]\\par}"));
};
+
+ @Test
+ public void testAttributes () throws IOException {
+ RtfExporter rtf = new RtfExporter();
+ rtf.setFname("Beispiel");
+ assertEquals(rtf.getFname(),"Beispiel");
+ assertEquals(rtf.getMimeType(),"application/rtf");
+ assertEquals(rtf.getSuffix(),"rtf");
+ };
};