forked from rchatley/SimpleWebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6024d6d
Showing
9 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
target | ||
*.class | ||
*.iml | ||
*.ipr | ||
.idea | ||
.classpath | ||
.settings | ||
.project | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.develogical</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
<artifactId>SimpleWebApp</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-servlet</artifactId> | ||
<version>9.2.14.v20151106</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.3</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ""; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<html>"); | ||
writer.println("<head></head>"); | ||
writer.println("<body>"); | ||
} | ||
|
||
private void writeFooterTo(PrintWriter writer) { | ||
writer.println("</body>"); | ||
writer.println("</html>"); | ||
} | ||
|
||
protected abstract void writeContentTo(PrintWriter writer); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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( | ||
"<h1>Welcome!</h1>" + | ||
"<p>Enter your query in the box below: " + | ||
"<form><input type=\"text\" name=\"q\" />" + | ||
"<input type=\"submit\">" + | ||
"</form>" + | ||
"</p>"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<h1>Sorry</h1>"); | ||
writer.print("<p>Sorry, we didn't understand <em>" + query + "</em></p>"); | ||
} else { | ||
writer.println("<h1>" + query + "</h1>"); | ||
writer.println("<p>" + answer + "</p>"); | ||
} | ||
|
||
writer.println("<p><a href=\"/\">Back to Search Page</a></p>"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
} | ||
} |