Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with the Payment Methods endpoint. Java : Simple Rest Server Instructions Create a simple server that uses simple design patterns Factory :

Please help me with the Payment Methods endpoint.

Java: Simple Rest Server

Instructions

Create a simple server that uses simple design patterns

  • Factory: Your server should parse the routes and create a new Processor class depending on the route
  • Builder: The process method of your Processor should use the Builder pattern to create a response.
  • DAO: Make a class that abstracts the data interactions away from the rest of the app
  • Singleton: Wrap your dao instance as a singleton class
  • Lazy Loading: DAOs should only load data when requested
  • DTO: Your JSON parsing, as well as response objects must be DTOs

First parse url, then use factory to instantiate correct Processor class. The factory will internally use the builder pattern to give the processor relevant data. Use builder to set the time, and input parameters.

Processor Class

  • Has a process() method that returns a ResponseDto object
  • Each endpoint is a subclass of Processor
  • Takes a hashmap of key value args
  • Use a factory to create these, based on the endpoint
  • Every processor uses a builder and returns the output of builder.build()

Response Class

  • This class gets stringified with GSON
  • Needs a ResponseBuilder helper
  • Needs Params Map (String, String)
  • Needs response dto
  • Needs Response Code (String)

Dao class

  • Singleton
  • Lazy load data
  • 1 DAO per type of endpoint to hold the data

Endpoints

  • Payment Methods
    • /addPaymentMethod?method=name
      • creates a new payment method and stores it in the paymentMethod DAO
      • assigns a machine code
      • needs Payment DTO + Payment DAO
      • fields: name, machineCode
    • /getAllPaymentMethods
      • Returns list of all payment methods
  • Items
    • /addItem?name=name&price=price
      • Creates a new item and gives it a price
      • Assigns machine readable code and returns it
      • needs ItemDAO + Item DTO
      • fields: name, price, machineCode
    • /listItems
      • returns all items
  • Transactions
    • /createTransaction?itemCode=code&paymentMethod=paymentCode
      • Must have valid payment method
      • fields: itemCode, paymentMethod
    • /listTransactions
      • returns all transaction
      • needs TransactionDAO + Transaction DTO

High level steps

  1. Parse request and extract the endpoint
  2. Parse the get args into a hashmap
  3. Use factory to instantiate correct processor class
  4. Call .process(args) and pass in the arguments on this class (args is a hash map of the get params)
  5. Inside the processor it can get or put data through the DAO
  6. Business Logic goes in processor Load/Store logic goes in DAO
  7. Returns a single ResponseDto
  8. Stringify this response dto and pass it into the output stream

Return Format

You will need a response class that looks like this before passing to JSON.

"{

"date": "2019-02-10T20:40:59.257Z", // time that this was generated

"params": {

"a": 2 // full list of all key value get params

},

responseCode: OK/ERROR

"response": {}, // put your responses here

}"

/******Main.java********/

import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Main { public static void main(String[] args) throws IOException { ServerSocket ding; Socket dong = null; try { ding = new ServerSocket(1299); System.out.println("Opened socket " + 1299); while (true) { // keeps listening for new clients, one at a time try { dong = ding.accept(); // waits for client here } catch (IOException e) { System.out.println("Error opening socket"); System.exit(1); } InputStream stream = dong.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(stream)); try { // read the first line to get the request method, URI and HTTP version String line = in.readLine(); System.out.println("----------REQUEST START---------"); System.out.println(line); // read only headers line = in.readLine(); while (line != null && line.trim().length() > 0) { int index = line.indexOf(": "); if (index > 0) { System.out.println(line); } else { break; } line = in.readLine(); } System.out.println("----------REQUEST END--------- "); } catch (IOException e) { System.out.println("Error reading"); System.exit(1); } BufferedOutputStream out = new BufferedOutputStream(dong.getOutputStream()); PrintWriter writer = new PrintWriter(out, true); // char output to the client // every response will always have the status-line, date, and server name writer.println("HTTP/1.1 200 OK"); writer.println("Server: TEST"); writer.println("Connection: close"); writer.println("Content-type: text/html"); writer.println(""); // Body of our response writer.println("

Hello World

"); dong.close(); } } catch (IOException e) { System.out.println("Error opening socket"); System.exit(1); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions

Question

Does the IRS acquiesce in decisions of U.S. district courts?

Answered: 1 week ago