/HTTPSreport

HTTP server

Primary LanguageJava

# HT(TPS) Report

"Hi, Peter. What's happening? We need to talk about your TPS reports..."

HTTPSreport is an HTTP Server written in Java. It allows applications to plug in through the Client interface and be served HTTP requests.

Architecture

Receptionist

The Receptionist sits at the highest level and contains the main() method for the server. The Receptionist is also the holder of the ServerSocket and will generate a new thread for each connection. These connections are then passed to the Executive.

###Executive The executive is responsible for each Socket connection to the server. Each Executive will run on their own thread. The Executive will invoke the Analyst to generate a Memo for the incoming connection. The Executive will also send the Memo back to the user who generated the HTTP request. At the end of execution the Executive will close the Socket and the Executive's thread will die.

###Analyst The Analyst uses the Socket's InputStream to construct a usable Memo. The Analysts works only for the Executive.

###Memo The Memo is a data structure that encapsalates an HTTP packet. Three special fields are contained on the memo: "Verb", "Path", and "Version" which are special cases for the first line of an incoming HTTP Packet.

###Marketer The Marketer will call the Route to retrieve an instance of Client to invoke. It will then pass the Memo object down to the Client and await its response. When the response is given from the Client it passes it back up to the Executive.

###Route The Route will hold all of the routes specified. When a certain route is invoked it will create an instance of a Client for the Marketer to use.

###Client Client is the abstract interface used by an applications which wishes to use HTTPSreport. A Client will receive its Memo from the Marketer during the execute call. When the Client has finished it will invoke the Marketer and pass along the return Memo it has generated.

Usage

Creating a Client

Applications using HTTPSreport must make use of the Client Abstract Class provided. The package location of the Client Abstract Class is Server.src.Client. Clients are required to implement one public method: execute().

execute will take a Marketer instance and an inMemo as its paremeters. The Marketer instance is important because this is the instance the Client must contact after it has run to completion.

contactMarketer is inherited from the Client base class. This will take Marketer and Memo as parameters. The Marketer is the instance to be contacted (given to the Client by the execute method) and the Memo is the memo to pass back up. Marketer contains a method named clientResponse() which takes a Memo as a parameter. This is how a client contacts their Marketer.

Example Client

package Implementer.example;

import Server.src.Client;
import Server.src.Marketer;
import Server.src.Memo;

public class GetHelloWorldClient extends Client {

   public void execute(Marketer marketer, Memo _inMemo) {
      Memo outMemo = new Memo(200, "HTML", "<html>Hello World!</html>");
      contactMarketer(marketer, outMemo);
   }

}

Creating a Routes File

The routes file is a plaintext file for defining routes. Any route not found on the list will generate a 404 Not Found response. The syntax is simple, it expects one route per line with the verb in all caps first, followed by the path, followed the by package and class of the Client Implementation that will handle the request. For example:
GET / com.myapp.GetRootClient
POST /post/here com.yourapp.PostHereClient
PUT /example other.pacakge.PutExampleClient
DELETE /model com.myapp.DeleteModelClient
HEAD / other.package.HeadRootClient
How you decide to route incoming requests is completely up you. Feel free to be as modular as you like (or just make spaghetti).

Starting the Server

The httpsreport jar is an executable jar. There are three parameters to pass to the jar upon execution to link up the server correctly
[-p | --port] specifies the port number for the server to run on
[-r | --routes] an absolute path to the route text file for defined routes
[-j | --jar] an absolute path to your project's jar

  • HTTPSreport will find all client implementations in your jar file for you.

example: $ java -jar httpsreport.jar -p 3000 -r /path/to/routes.txt -j /path/to/myapp.jar