From 6024d6dfce2e6d5e547b73bf5745050de423487b Mon Sep 17 00:00:00 2001
From: Robert Chatley
Date: Thu, 24 Dec 2015 13:07:42 +0000
Subject: [PATCH] Initial commit
---
.gitignore | 9 ++++
pom.xml | 38 +++++++++++++
src/main/java/com/develogical/WebServer.java | 54 +++++++++++++++++++
.../com/develogical/app/QueryProcessor.java | 13 +++++
.../java/com/develogical/web/ApiResponse.java | 20 +++++++
.../java/com/develogical/web/HtmlPage.java | 30 +++++++++++
.../java/com/develogical/web/IndexPage.java | 18 +++++++
.../java/com/develogical/web/ResultsPage.java | 28 ++++++++++
.../develogical/app/QueryProcessorTest.java | 27 ++++++++++
9 files changed, 237 insertions(+)
create mode 100644 .gitignore
create mode 100644 pom.xml
create mode 100644 src/main/java/com/develogical/WebServer.java
create mode 100644 src/main/java/com/develogical/app/QueryProcessor.java
create mode 100644 src/main/java/com/develogical/web/ApiResponse.java
create mode 100644 src/main/java/com/develogical/web/HtmlPage.java
create mode 100644 src/main/java/com/develogical/web/IndexPage.java
create mode 100644 src/main/java/com/develogical/web/ResultsPage.java
create mode 100644 src/test/java/com/develogical/app/QueryProcessorTest.java
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..efdbd2c
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,9 @@
+target
+*.class
+*.iml
+*.ipr
+.idea
+.classpath
+.settings
+.project
+.DS_Store
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..a6bf8df
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,38 @@
+
+
+ 4.0.0
+ com.develogical
+ 1.0-SNAPSHOT
+ SimpleWebApp
+
+
+ org.eclipse.jetty
+ jetty-servlet
+ 9.2.14.v20151106
+
+
+ javax.servlet
+ servlet-api
+ 2.5
+
+
+ junit
+ junit
+ 4.12
+
+
+
+
+
+ maven-compiler-plugin
+ 3.3
+
+
+ 1.7
+
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/develogical/WebServer.java b/src/main/java/com/develogical/WebServer.java
new file mode 100644
index 0000000..89c5a7e
--- /dev/null
+++ b/src/main/java/com/develogical/WebServer.java
@@ -0,0 +1,54 @@
+package com.develogical;
+
+import com.develogical.app.QueryProcessor;
+import com.develogical.web.ApiResponse;
+import com.develogical.web.IndexPage;
+import com.develogical.web.ResultsPage;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.ServletHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class WebServer {
+
+ public WebServer() throws Exception {
+ Server server = new Server(8080);
+
+ ServletHandler handler = new ServletHandler();
+ handler.addServletWithMapping(new ServletHolder(new Website()), "/*");
+ handler.addServletWithMapping(new ServletHolder(new Api()), "/api/*");
+ server.setHandler(handler);
+
+ server.start();
+ }
+
+ static class Website extends HttpServlet {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ String query = req.getParameter("q");
+ if (query == null) {
+ new IndexPage().writeTo(resp);
+ } else {
+ new ResultsPage(query, new QueryProcessor().process(query)).writeTo(resp);
+ }
+ }
+ }
+
+ static class Api extends HttpServlet {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ String query = req.getParameter("q");
+ new ApiResponse(new QueryProcessor().process(query)).writeTo(resp);
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ new WebServer();
+ }
+}
+
diff --git a/src/main/java/com/develogical/app/QueryProcessor.java b/src/main/java/com/develogical/app/QueryProcessor.java
new file mode 100644
index 0000000..5027093
--- /dev/null
+++ b/src/main/java/com/develogical/app/QueryProcessor.java
@@ -0,0 +1,13 @@
+package com.develogical.app;
+
+public class QueryProcessor {
+
+ public String process(String query) {
+ if (query.toLowerCase().contains("shakespeare")) {
+ return "William Shakespeare (26 April 1564 - 23 April 1616) was an " +
+ "English poet, playwright, and actor, widely regarded as the greatest " +
+ "writer in the English language and the world's pre-eminent dramatist.";
+ }
+ return "";
+ }
+}
diff --git a/src/main/java/com/develogical/web/ApiResponse.java b/src/main/java/com/develogical/web/ApiResponse.java
new file mode 100644
index 0000000..832819f
--- /dev/null
+++ b/src/main/java/com/develogical/web/ApiResponse.java
@@ -0,0 +1,20 @@
+package com.develogical.web;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public class ApiResponse {
+
+ private final String answer;
+
+ public ApiResponse(String answer) {
+ this.answer = answer;
+ }
+
+ public void writeTo(HttpServletResponse resp) throws IOException {
+ resp.setContentType("text/plain");
+ PrintWriter writer = resp.getWriter();
+ writer.println(answer);
+ }
+}
diff --git a/src/main/java/com/develogical/web/HtmlPage.java b/src/main/java/com/develogical/web/HtmlPage.java
new file mode 100644
index 0000000..19bceee
--- /dev/null
+++ b/src/main/java/com/develogical/web/HtmlPage.java
@@ -0,0 +1,30 @@
+package com.develogical.web;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+public abstract class HtmlPage {
+
+ public void writeTo(HttpServletResponse resp) throws IOException {
+ resp.setContentType("text/html");
+ PrintWriter writer = resp.getWriter();
+ writeHeaderTo(writer);
+ writeContentTo(writer);
+ writeFooterTo(writer);
+ }
+
+ private void writeHeaderTo(PrintWriter writer) {
+ writer.println("");
+ writer.println("");
+ writer.println("");
+ }
+
+ private void writeFooterTo(PrintWriter writer) {
+ writer.println("");
+ writer.println("");
+ }
+
+ protected abstract void writeContentTo(PrintWriter writer);
+
+}
diff --git a/src/main/java/com/develogical/web/IndexPage.java b/src/main/java/com/develogical/web/IndexPage.java
new file mode 100644
index 0000000..7de3044
--- /dev/null
+++ b/src/main/java/com/develogical/web/IndexPage.java
@@ -0,0 +1,18 @@
+package com.develogical.web;
+
+import java.io.PrintWriter;
+
+public class IndexPage extends HtmlPage {
+
+ @Override
+ protected void writeContentTo(PrintWriter writer) {
+ writer.println(
+ "Welcome!
" +
+ "Enter your query in the box below: " +
+ "
" +
+ "
");
+ }
+
+}
diff --git a/src/main/java/com/develogical/web/ResultsPage.java b/src/main/java/com/develogical/web/ResultsPage.java
new file mode 100644
index 0000000..d9b3236
--- /dev/null
+++ b/src/main/java/com/develogical/web/ResultsPage.java
@@ -0,0 +1,28 @@
+package com.develogical.web;
+
+import java.io.PrintWriter;
+
+public class ResultsPage extends HtmlPage {
+
+ private final String query;
+ private final String answer;
+
+ public ResultsPage(String query, String answer) {
+ this.query = query;
+ this.answer = answer;
+ }
+
+ @Override
+ protected void writeContentTo(PrintWriter writer) {
+
+ if (answer == null || answer.isEmpty()) {
+ writer.println("Sorry
");
+ writer.print("Sorry, we didn't understand " + query + "
");
+ } else {
+ writer.println("" + query + "
");
+ writer.println("" + answer + "
");
+ }
+
+ writer.println("Back to Search Page
");
+ }
+}
diff --git a/src/test/java/com/develogical/app/QueryProcessorTest.java b/src/test/java/com/develogical/app/QueryProcessorTest.java
new file mode 100644
index 0000000..744dc0b
--- /dev/null
+++ b/src/test/java/com/develogical/app/QueryProcessorTest.java
@@ -0,0 +1,27 @@
+package com.develogical.app;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class QueryProcessorTest {
+
+ QueryProcessor queryProcessor = new QueryProcessor();
+
+ @Test
+ public void returnsEmptyStringIfCannotProcessQuery() throws Exception {
+ assertThat(queryProcessor.process("test"), is(""));
+ }
+
+ @Test
+ public void knowsAboutShakespeare() throws Exception {
+ assertThat(queryProcessor.process("Shakespeare"), containsString("playwright"));
+ }
+
+ @Test
+ public void isNotCaseSensitive() throws Exception {
+ assertThat(queryProcessor.process("shakespeare"), containsString("playwright"));
+ }
+}