1 of 9

Chess Server Implementation Tips

CS – Advanced Programming Concepts

2 of 9

Server Class Structure

3 of 9

What do Web API handlers do?

  • If the web API requires an auth token, the handler can validate the auth token
      • Might put this logic in a handler base class so it can be shared by multiple handlers or in a Service class that can be shared by multiple services
  • Deserialize JSON request body to Java request object
  • Call service class to perform the requested function, passing it the Java request object
  • Receive Java response object from service
  • Serialize Java response object to JSON
  • Send HTTP response back to client with appropriate status code and response body

4 of 9

Example Web API handler logic

LoginRequest request = (LoginRequest)gson.fromJson(reqData, LoginRequest.class);

LoginService service = new LoginService();

LoginResult result = service.login(request);

return gson.toJson(result);

Best to create a separate class with fromJson and toJson methods instead of calling Gson directly from handlers

5 of 9

How are Handlers Created and Invoked?

post("/user", context ->

(new RegisterHandler()).handleRequest(context));

Note: There are multiple ways to avoid creating a new handler instance for every request:

  1. Create and keep them in instance variables of your Server class
  2. Use a static method (will make it difficult to avoid code duplication across handlers)
  3. Use the singleton pattern: Create a static getInstance() method that always returns the same instance

6 of 9

Tip – Avoid code duplication

  • Areas of potential code duplication
    • HTTP Handler classes
    • Service classes
    • Request/Result classes
    • DAO classes
  • Use inheritance to avoid this kind of code duplication
    • Put common code in bases classes that can be inherited

7 of 9

JSON Tips

  • Make sure that the field (variable) names in your Request and Response classes match exactly the names used in the specification (including capitalization)
  • Not doing this will cause bugs

  • GSON does not serialize null fields (they will be missing in the JSON)
  • This is helpful when generating Success and Failure JSON responses

class Response {

String message;

}

class LoginResponse extends Response {

String authtoken;

String username;

}

8 of 9

Generating an Auth Token

  • There are multiple ways. Here’s a simple way:

UUID.randomUUID().toString()

9 of 9

Server Implementation Approach

  • Review class structure diagram
  • Create Java packages in your project to contain your server classes
  • Create your Server class and get static file handling to work so you can see the test page (see the Server Web API lecture for details)
  • Pick one Web API and get it working end-to-end, and test it with the test Web page or curl (e.g., register)
  • As you go, write JUnit test cases for Service classes you create
  • Repeat for the other Web APIs until you have written all 7 and are passing all of the passoff tests