For this project, I have implemented an HTTP server, two web applications, and a full suite of tests. I have used following concepts:
- Concurrency and threads
- Sockets
- JUnit and testing
- Good design practices
For Part 1 of this Project I have implemented a HTTP server.
- I have used raw sockets for this Project. I have not useed Tomcat, Jetty, or any other existing server. I have also not used any external libraries for request parsing, etc.
- My server supports
GETandPOSTrequests. Any other HTTP method will result in a405 Method Not Allowedstatus code. See HTTP Status Code Definitions. - My server is flexible enough to support different web applications.
- My server is multithreaded. Each incoming request will be handled by a different threads.
Here the design is similar to the design supported by Jetty. My design does not follow the exact API nor does it is as extensive, however I have borrow similar ideas. Here are a few items to consider:
- Create a
Handlerinterface with ahandlemethod. Each web application will implement a different set of handlers, for example a search application will support aSearchHandlerand a chat application would have aChatHandler. - Support an
addMappingmethod that will map a URI path to a specificHandlerinstance. When a new request is made to the server the server will retrieve from the mapping theHandlerappropriate for the path in the request URI.
The examples below show how my API is used for the required applications below.
public class SearchApplication {
public static void main(String[] args) {
int port = 1024;
HTTPServer server = new HTTPServer(port);
//The request GET /reviewsearch will be dispatched to the
//handle method of the ReviewSearchHandler.
server.addMapping("/reviewsearch", new ReviewSearchHandler());
//The request GET /find will be dispatched to the
//handle method of the FindHandler.
server.addMapping("/find", new FindHandler());
server.startup();
}
}public class ChatApplication {
public static void main(String[] args) {
int port = 1024;
HTTPServer server = new HTTPServer(port);
server.addMapping("/slackbot", new ChatHandler());
server.startup();
}
}For Part 2 of this Project I have implemented an application that will allow users to search the InvertedIndex that was built for InvertedIndex project.
Below are the list of supported APIs:
This request will return a web page containing, at minimum, a text box and button. When the user enters a query in the text box and clicks the button the POST method described below will be called.
The body of this request will look as follows: query=term where the value is a URL-encoded string.
It will return a web page listing all of the review search results.
This request will return a web page containing, at minimum, a text box and button. When the user enters a message in the text box and clicks the button the POST method described below will be called.
The body of this request will look as follows: asin=123456789 where the value is a URL-encoded string.
It will return a web page listing all of the review and qa documents with the provided ASIN.
For Part 3 of this Project I have implemented an application that will allow a user to anonymously post a message to the Slack team. Refer to the Accessing the Slack API instructions for pointers.
Below are the list of supported APIs:
This request will return a web page containing, at minimum, a text box and button. When the user enters a message in the text box and clicks the button the POST method described below will be called.
The body of this request will look as follows: message=test+message where the value is a URL-encoded string.
This request will trigger a post of the message specified by the body to the Slack channel #project3. Regardless of the user who types the message it will always appear to have come from your application.
For Part 4 I have written test cases to test my solution. I have demonstrated unit tests, integration tests, and system tests.
Unit tests will test one method. It is not required that you have unit tests for every method you implement, however methods that perform complex processing or logic should have one or more associated unit tests. As an example, you may have a method that parses an HTTP request to validate that it has a correct format, valid method, etc. You should implement several unit tests to confirm that the method works correctly when the request is valid and in all scenarios when it is invalid.
Integration tests generally test a path of execution through your program. You may, for example, have a handler that takes as input a request and then calls another method to post to to Slack. In this case, you could use a mock request object and test that passing that mock object to your handler results in a correct post to Slack.
System tests will test the end-to-end execution of your program. A system test would use a client program to make a request (valid or invalid) of your deployed server and verify that the response is correct.
The only external libraries I am using for this project are GSON 2.8.5 and JUnit.