Commit d3066303 authored by Joshua Priestley's avatar Joshua Priestley
Browse files

Files now download in markdown - formatting still to be done and cleanup

parent 65087d6e
Pipeline #171983 passed with stages
in 20 seconds
......@@ -2,6 +2,7 @@ package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -27,8 +28,11 @@ public class WebServer {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String isMarkdown = req.getParameter("markdown");
if (query == null) {
new IndexPage().writeTo(resp);
} else if("md".equals(isMarkdown)) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
......
......@@ -22,25 +22,31 @@ public class MarkdownResultPage implements Page {
@Override
public void writeTo(HttpServletResponse resp) throws IOException {
File markdown = new File("result.md");
File markdown = File.createTempFile("result", ".md");
resp.setContentType("file/markdown");
resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".md");
// Header
FileWriter writer = new FileWriter("result.md");
writer.write(query + "\n");
writer.write("======\n");
FileWriter writer = new FileWriter(markdown);
writer.write("<h1>" + query + "<h1>");
writer.write("-------------------------------------------------<br>");
// Content
if (answer == null || answer.isEmpty()) {
writer.write("Sorry, we did not understand your query.\n");
} else {
writer.write(answer);
writer.write("<p>" + answer + "</p>");
}
writer.flush();
System.out.println(markdown.length());
System.out.println(markdown.getName());
// Byte streams
InputStream inputStream = new FileInputStream(markdown);
OutputStream outputStream = new FileOutputStream(markdown);
inputStream.transferTo(outputStream);
inputStream.transferTo(resp.getOutputStream());
// Remove temporary file
markdown.delete();
markdown.deleteOnExit();
}
}
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