Commit 4e89cbc7 authored by Joshua Priestley's avatar Joshua Priestley
Browse files

Added comments to Dockerfile and reformatted code

parent 201fa8fc
Pipeline #173425 canceled with stages
in 6 minutes and 19 seconds
# FROM maven:3.6.0-jdk-11-slim
# Build stage
# # Install necessary dependencies
FROM maven:3.6.0-jdk-11-slim AS build RUN apt-get -y update
RUN apt-get -y update && apt-get -y install texlive-full pandoc RUN apt-get -y install texlive-full pandoc
# Copy source files
COPY src /src COPY src /src
COPY pom.xml / COPY pom.xml /
# Start application
RUN mvn package RUN mvn package
EXPOSE 8080 EXPOSE 8080
CMD ["sh", "target/bin/simplewebapp"] CMD ["sh", "target/bin/simplewebapp"]
\ No newline at end of file
...@@ -9,36 +9,46 @@ import java.io.PrintWriter; ...@@ -9,36 +9,46 @@ import java.io.PrintWriter;
public class HTMLResultPage implements Page { public class HTMLResultPage implements Page {
private final String query; private final String query;
private final String answer; private final String answer;
public HTMLResultPage(String query, String answer) { public HTMLResultPage(String query, String answer) {
this.query = query; this.query = query;
this.answer = answer; this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println(
"<html>\n"
+ "<head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n"
+ " <title>\n"
+ " Welcome\n"
+ " </title>\n"
+ "</head>");
// Body
writer.println("<body><div class=\"container\"><h1>");
if (answer == null || answer.isEmpty()) {
writer.println("Query Not Found");
} else {
writer.println(query.toUpperCase());
} }
public void writeTo(HttpServletResponse resp) throws IOException { writer.println("</h1><hr>");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter(); if (answer == null || answer.isEmpty()) {
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
writer.println( } else {
"<html>\n" + " <head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n" + " <title>\n" + " Welcome\n" + " </title>\n"); writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
writer.println("</head><body><div class=\"container\"><h1>");
if (answer == null || answer.isEmpty()) {
writer.println("Query Not Found");
} else {
writer.println(query.toUpperCase());
}
writer.println("</h1><hr>");
if (answer == null || answer.isEmpty()) {
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>");
} else {
writer.println("<p>" + answer.replace("\n", "<br>") + "</p>");
}
writer.println("<a href=\"/\"><button>Back to Search Page</button></a>");
writer.println("</div></body></html>");
} }
writer.println("<a href=\"/\"><button>Back to Search Page</button></a>");
// Footer
writer.println("</div></body></html>");
}
} }
...@@ -9,41 +9,45 @@ import java.io.PrintWriter; ...@@ -9,41 +9,45 @@ import java.io.PrintWriter;
public class IndexPage implements Page { public class IndexPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException { public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html"); resp.setContentType("text/html");
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
// Header // Header
writer.println( writer.println(
"<html>\n" + " <head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n" + " <title>\n" + " Welcome\n" + " </title>\n"); "<html>\n"
+ "<head><link rel = \"stylesheet\" href=\"https://drive.google.com/uc?export=view&id=1b-2_6zWL_6F360SBlZrdIGrpsuWgNkKz\" />\n"
+ " <title>\n"
+ " Welcome\n"
+ " </title>\n"
+ "</head>");
// Body
writer.println(
" <body>\n"
+ " <div class=\"container\">\n"
+ " <h1>\n"
+ " Search\n"
+ " </h1>\n"
+ " <hr>\n"
+ " <form>\n"
+ " <label>Enter your query in the box below.</label>\n"
+ " <input type=\"text\" name=\"q\" />\n"
+ " <label for=\"dl\">Choose a download type:</label>\n"
+ "\n"
+ " <select name=\"markdown\" id=\"dl\">\n"
+ " <option>No Download (Default)</option>\n"
+ " <option id=\"md\" name=\"markdown\" value=\"md\">Markdown File</option>\n"
+ " <option value=\"pdf\">PDF File</option>\n"
+ " </select>\n"
+ " <input type=\"submit\">\n"
+ " </form>\n"
+ " </div>\n"
+ " </body>\n"
+ "</html>");
writer.println( // Footer
" </head>\n" writer.println("</body>");
+ " <body>\n" writer.println("</html>");
+ " <div class=\"container\">\n" }
+ " <h1>\n"
+ " Search\n"
+ " </h1>\n"
+ " <hr>\n"
+ " <form>\n"
+ " <label>Enter your query in the box below.</label>\n"
+ " <input type=\"text\" name=\"q\" />\n"
+ " <label for=\"dl\">Choose a download type:</label>\n"
+ "\n"
+ " <select name=\"markdown\" id=\"dl\">\n"
+ " <option>No Download (Default)</option>\n"
+ " <option id=\"md\" name=\"markdown\" value=\"md\">Markdown File</option>\n"
+ " <option value=\"pdf\">PDF File</option>\n"
+ " </select>\n"
+ " <input type=\"submit\">\n"
+ " </form>\n"
+ " </div>\n"
+ " </body>\n"
+ "</html>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
} }
...@@ -7,7 +7,6 @@ import java.io.IOException; ...@@ -7,7 +7,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
public class MarkdownResultPage implements Page { public class MarkdownResultPage implements Page {
protected final String query; protected final String query;
...@@ -24,7 +23,7 @@ public class MarkdownResultPage implements Page { ...@@ -24,7 +23,7 @@ public class MarkdownResultPage implements Page {
resp.setContentType("file/markdown"); resp.setContentType("file/markdown");
resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".md"); resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".md");
outputStreams(markdown, resp); outputStreams(markdown, resp);
// Remove temporary file // Remove temporary file
markdown.delete(); markdown.delete();
...@@ -32,27 +31,26 @@ public class MarkdownResultPage implements Page { ...@@ -32,27 +31,26 @@ public class MarkdownResultPage implements Page {
void outputStreams(File markdown, HttpServletResponse resp) throws IOException { void outputStreams(File markdown, HttpServletResponse resp) throws IOException {
// Byte streams // Byte streams
InputStream inputStream = new FileInputStream(markdown); InputStream inputStream = new FileInputStream(markdown);
inputStream.transferTo(resp.getOutputStream()); inputStream.transferTo(resp.getOutputStream());
} }
File getFile() throws IOException { File getFile() throws IOException {
File markdown = new File((query +".md")); File markdown = new File(query + ".md");
// Header // Header
FileWriter writer = new FileWriter(markdown); FileWriter writer = new FileWriter(markdown);
writer.write("<h1>" + query + "<h1>"); writer.write("<h1>" + query + "<h1>");
writer.write("-------------------------------------------------<br>"); writer.write("-------------------------------------------------<br>");
// Content // Content
if (answer == null || answer.isEmpty()) { if (answer == null || answer.isEmpty()) {
writer.write("Sorry, we did not understand your query.\n"); writer.write("Sorry, we did not understand your query.\n");
} else { } else {
writer.write("<p>" + answer + "</p>"); writer.write("<p>" + answer + "</p>");
} }
writer.close(); writer.close();
return markdown; return markdown;
} }
} }
...@@ -12,15 +12,15 @@ public class PDFResultPage extends MarkdownResultPage { ...@@ -12,15 +12,15 @@ public class PDFResultPage extends MarkdownResultPage {
@Override @Override
public void writeTo(HttpServletResponse resp) throws IOException { public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("file/markdown"); resp.setContentType("file/pdf");
File markdown = getFile(); File markdown = getFile();
resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".pdf"); resp.setHeader("Content-Disposition", "Inline; filename=" + query + ".pdf");
ProcessBuilder pb = new ProcessBuilder("pandoc", markdown.getAbsolutePath(), "-o", "result.pdf"); ProcessBuilder pb = new ProcessBuilder("pandoc", markdown.getAbsolutePath(), "-o", "result.pdf");
Process p = pb.start(); Process p = pb.start();
try { try {
p.waitFor(); p.waitFor();
} catch (InterruptedException e) { } catch (InterruptedException e) {
...@@ -34,6 +34,5 @@ public class PDFResultPage extends MarkdownResultPage { ...@@ -34,6 +34,5 @@ public class PDFResultPage extends MarkdownResultPage {
// Remove temporary file // Remove temporary file
markdown.delete(); markdown.delete();
pdf.delete(); pdf.delete();
} }
} }
...@@ -5,5 +5,5 @@ import java.io.IOException; ...@@ -5,5 +5,5 @@ import java.io.IOException;
public interface Page { public interface Page {
void writeTo(HttpServletResponse resp) throws IOException; void writeTo(HttpServletResponse resp) throws IOException;
} }
<style>body {
font-family: Helvetica, sans-serif;
}
.container {
width: 500px;
height: 350px;
background-color: rgb(238, 238, 238);
position: fixed;
border: 2px solid rgb(190, 190, 190);
top: 50%;
left: 50%;
transform: translate(-50%, -90%);
border-radius: 20px;
padding:40px;
}
h1 {
font-family: Helvetica, sans-serif;
margin-top: 30px;
}
hr {
border: 3px solid rgb(190, 190, 190);
border-radius: 5px;
}
input[type=text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit], button {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
form {
margin-top: 30px;
}</style>
\ No newline at end of file
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