Commit a92375c7 authored by bc1417's avatar bc1417
Browse files

merged tests with new version of pdf and markdown page

parents 686ebdc5 ec95f3cd
Pipeline #65600 passed with stages
in 3 minutes and 42 seconds
package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import ic.doc.web.MarkdownResultPage;
import ic.doc.web.PDFResultPage;
import ic.doc.web.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
......@@ -35,10 +32,8 @@ public class WebServer {
} else {
if (type.equals("html")) {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else if (type.equals("markdown")) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
new PDFResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
new DownloadPage(query, new QueryProcessor().process(query), type).writeTo(resp);
}
}
}
......
package ic.doc.web;
import ic.doc.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class DownloadPage implements Page{
private final String query;
private final Query answer;
private final String format;
public DownloadPage(String query, Query answer, String format) {
this.query = query;
this.answer = answer;
this.format = format;
}
public void writeTo(HttpServletResponse resp) throws IOException{
if (format.equals("pdf")) {
resp.setContentType("application/pdf");
}
if (format.equals("md")) {
resp.setContentType("text/markdown");
}
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
resp.setHeader("Content-Disposition", "attachment;filename=\"sorry." + format + "\"");
PrintWriter writer = resp.getWriter();
writer.println("#Sorry");
writer.println("Sorry, we didn't understand " + query + ".");
} else {
resp.setHeader("Content-Disposition", "attachment;filename=\"" + query + "."+format+"\"");
try {
FileInputStream pdfInputStream = new FileInputStream(createFile());
OutputStream servletOutputStream = resp.getOutputStream();
servletOutputStream.write(pdfInputStream.readAllBytes());
} catch (IOException e) {
System.out.println("Unable to create file");
}
}
}
public File createFile() throws IOException{
File tmp = File.createTempFile(query, ".md");
tmp.deleteOnExit();
FileWriter fw = new FileWriter(tmp);
fw.write("#" + query + "\n");
fw.write(answer.getAnswer());
fw.write("![alt text]("+answer.getImgURL()+" \"Pic\")");
fw.write("[Learn More!]("+answer.getWikiURL()+")");
fw.close();
if (format.equals("pdf")) {
File pdf = File.createTempFile(query, ".pdf");
pdf.deleteOnExit();
ProcessBuilder processBuilder = new ProcessBuilder("pandoc",
tmp.getAbsolutePath(), "-f", "markdown", "-o", pdf.getAbsolutePath());
try {
Process process = processBuilder.start();
process.waitFor();
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
return pdf;
}
return tmp;
}
}
package ic.doc.web;
import ic.doc.Query;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class MarkdownResultPage implements Page {
private final String query;
private final Query answer;
public MarkdownResultPage(String query, Query answer) {
this.query = query;
this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/markdown"); // ME : THIS is the MIME Type? before said text/markdown?
// Content
if (answer == null || answer.getAnswer() == null || answer.getAnswer().isEmpty()) {
resp.setHeader("Content-Disposition", "attachment;filename=\"sorry.md\"");
PrintWriter writer = resp.getWriter();
writer.println("#Sorry");
writer.println("Sorry, we didn't understand " + query + ".");
} else {
resp.setHeader("Content-Disposition", "attachment;filename=\"" + query + ".md\"");
File tmp = File.createTempFile(query, ".tmp");
FileWriter fw = new FileWriter(tmp);
fw.write("#" + query + "\n");
fw.write(answer.getAnswer());
fw.write("![alt text]("+answer.getImgURL()+" \"Pic\")");
fw.write("[Learn More!]("+answer.getWikiURL()+")");
fw.close();
FileInputStream fileInputStream = new FileInputStream(tmp);
OutputStream servletOutputStream = resp.getOutputStream();
servletOutputStream.write(fileInputStream.readAllBytes());
// flush/close?
tmp.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