Commit 37a93024 authored by unknown's avatar unknown
Browse files

Merge branch 'markdown' of https://gitlab.doc.ic.ac.uk/ieo119/simplewebapp into markdown

parents d6842d38 971be045
......@@ -29,8 +29,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>8</source>
<target>8</target>
<source>11</source>
<target>11</target>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
......
......@@ -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();
}
}
java.runtime.version=11
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