Commit c1b40e12 authored by Ivan Procaccini's avatar Ivan Procaccini

Initial commit

parents
target
*.class
*.iml
*.ipr
.idea
.classpath
.settings
.project
.DS_Store
<?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>ic.doc</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>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>ic.doc.WebServer</mainClass>
<name>simplewebapp</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package ic.doc;
public class QueryProcessor {
public String process(String query) {
StringBuilder results = new StringBuilder();
if (query.toLowerCase().contains("shakespeare")) {
results.append("William Shakespeare (26 April 1564 - 23 April 1616) was an\n" +
"English poet, playwright, and actor, widely regarded as the greatest\n" +
"writer in the English language and the world's pre-eminent dramatist. \n");
results.append(System.lineSeparator());
}
if (query.toLowerCase().contains("asimov")) {
results.append("Isaac Asimov (2 January 1920 - 6 April 1992) was an\n" +
"American writer and professor of Biochemistry, famous for\n" +
"his works of hard science fiction and popular science. \n");
results.append(System.lineSeparator());
}
return results.toString();
}
}
package ic.doc;
import ic.doc.web.HTMLResultPage;
import ic.doc.web.IndexPage;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
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(Integer.valueOf(System.getenv("PORT")));
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(new ServletHolder(new Website()), "/*");
server.setHandler(handler);
server.start();
}
static class Website extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String query = req.getParameter("q");
if (query == null) {
new IndexPage().writeTo(resp);
} else {
new HTMLResultPage(query, new QueryProcessor().process(query)).writeTo(resp);
}
}
}
public static void main(String[] args) throws Exception {
new WebServer();
}
}
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HTMLResultPage implements Page {
private final String query;
private final String answer;
public HTMLResultPage(String query, String answer) {
this.query = query;
this.answer = answer;
}
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>" + query + "</title></head>");
writer.println("<body>");
// Content
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.replace("\n", "<br>") + "</p>");
}
writer.println("<p><a href=\"/\">Back to Search Page</a></p>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
}
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class IndexPage implements Page {
public void writeTo(HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
// Header
writer.println("<html>");
writer.println("<head><title>Welcome</title></head>");
writer.println("<body>");
// Content
writer.println(
"<h1>Welcome!!</h1>" +
"<p>Enter your query in the box below: " +
"<form>" +
"<input type=\"text\" name=\"q\" />" +
"<br><br><input type=\"submit\">" +
"</form>" +
"</p>");
// Footer
writer.println("</body>");
writer.println("</html>");
}
}
package ic.doc.web;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public interface Page {
void writeTo(HttpServletResponse resp) throws IOException;
}
package ic.doc;
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 knowsAboutAsimov() throws Exception {
assertThat(queryProcessor.process("Asimov"), containsString("science fiction"));
}
@Test
public void isNotCaseSensitive() throws Exception {
assertThat(queryProcessor.process("shakespeare"), containsString("playwright"));
}
}
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