Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rchatley committed Dec 24, 2015
0 parents commit 6024d6d
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
target
*.class
*.iml
*.ipr
.idea
.classpath
.settings
.project
.DS_Store
38 changes: 38 additions & 0 deletions pom.xml
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>
54 changes: 54 additions & 0 deletions src/main/java/com/develogical/WebServer.java
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();
}
}

13 changes: 13 additions & 0 deletions src/main/java/com/develogical/app/QueryProcessor.java
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 "";
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/develogical/web/ApiResponse.java
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);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/develogical/web/HtmlPage.java
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);

}
18 changes: 18 additions & 0 deletions src/main/java/com/develogical/web/IndexPage.java
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>");
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/develogical/web/ResultsPage.java
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>");
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/develogical/app/QueryProcessorTest.java
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"));
}
}

0 comments on commit 6024d6d

Please sign in to comment.