Commit bf70c0e5 authored by td1518's avatar td1518

Merge branch 'extended-java' into 'master'

Markdown downloads

See merge request !1
parents 5a1cfe19 9ae92636
Pipeline #112443 passed with stages
in 41 seconds
......@@ -7,6 +7,13 @@
<version>1.0-SNAPSHOT</version>
<artifactId>SimpleWebApp</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
......
......@@ -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;
......@@ -23,20 +24,26 @@ public class WebServer {
server.start();
}
public static void main(String[] args) throws Exception {
new WebServer();
}
static class Website extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
String resultFormat = req.getParameter("format");
if (query == null) {
new IndexPage().writeTo(resp);
} else if (resultFormat.toLowerCase().equals("markdown")) {
new MarkdownResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else if (resultFormat.toLowerCase().equals("pdf")) {
// new PDFResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
} else {
//Intentionally redirect to HTML on error to avoid cluttering with files
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
}
}
public static void main(String[] args) throws Exception {
new WebServer();
}
}
......@@ -22,6 +22,9 @@ public class IndexPage implements Page {
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<br><br><input type=\"submit\">" +
"<br><input type=\"radio\" checked=\"true\" name=\"format\" value=\"HTML\"/>HTML" +
"<br><input type=\"radio\" name=\"format\" value=\"Markdown\"/>Markdown" +
"<br><input type=\"radio\" name=\"format\" value=\"PDF\"/>PDF" +
"</form>" +
"</p>");
......
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MarkdownResultPage implements Page {
private final String query;
private final String answer;
public MarkdownResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/markdown");
resp.setHeader("Content-Disposition", "attachment;filename=result.md");
PrintWriter writer = resp.getWriter();
// Content
if (answer == null || answer.isEmpty()) {
writer.println("# Sorry");
writer.println("Sorry, we didn't understand: *" + query + "*");
writer.println("");
} else {
writer.println("# " + query);
writer.println(answer);
}
}
}
......@@ -2,8 +2,8 @@ package ic.doc;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
public class QueryProcessorTest {
......
package ic.doc;
import org.junit.Test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import static org.mockito.Mockito.*;
public class WebServerTest {
@Test
public void returnsCorrectFormat() throws Exception {
HttpServletRequest mockedReq = mock(HttpServletRequest.class);
HttpServletResponse mockedResp = mock(HttpServletResponse.class);
PrintWriter mockedWriter = mock(PrintWriter.class);
when(mockedReq.getParameter("q")).thenReturn("test search query");
when(mockedReq.getParameter("format")).thenReturn("Markdown");
when(mockedResp.getWriter()).thenReturn(mockedWriter);
WebServer.Website testSite = new WebServer.Website();
testSite.doGet(mockedReq, mockedResp);
verify(mockedResp, times(1)).setContentType("text/markdown");
verify(mockedResp, times(1)).setHeader("Content-Disposition", "attachment;filename=result.md");
verify(mockedResp, times(1)).getWriter();
}
}
\ 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