Commit 9915e9c3 authored by Onadim, Izer's avatar Onadim, Izer
Browse files

[ieo119] Enable downloading of markdown as answer to query

parent cf40f4a4
Pipeline #171048 failed with stages
in 35 seconds
......@@ -2,6 +2,7 @@ package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownDownload;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -30,7 +31,8 @@ public class WebServer {
if (query == null) {
new IndexPage().writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
new MarkdownDownload(query, new QueryProcessor().process(query)).writeTo(resp);
//new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
}
}
......
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class MarkdownDownload implements Page {
private final String query;
private final String answer;
public MarkdownDownload(String query, String answer) {
this.query = query;
this.answer = answer;
}
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/markdown");
resp.setHeader("Content-disposition", String.format("attachment; filename=%s.md", query));
File temp = new File(query + ".txt");
if (!temp.createNewFile()) {
System.out.println("File already exists.");
return;
}
FileWriter fileWriter = new FileWriter(query + ".txt");
if (answer == null || answer.isEmpty()) {
fileWriter.write("# Sorry\n Sorry, we didn't understand " + query);
} else {
fileWriter.write("# " + query + "\n" + answer);
}
fileWriter.close();
InputStream inputStream = new FileInputStream(temp);
OutputStream outputStream = resp.getOutputStream();
inputStream.transferTo(outputStream);
temp.delete();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment