辅导案例-CSE3OAD-Assignment 2
CSE3OAD Assignment 2
Due Date: 5 June 2020, Friday, 10:00 AM

Assessment: This assignment 2 is worth 30% of the final mark for CSE3OAD.
This is an individual assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a
manner that gives the impression that the work is your own. The Department
of Computer Science and Information Technology treats plagiarism very
seriously. When it is detected, penalties are strictly imposed. Students are
referred to the Department of Computer Science and Information
Technology’s Handbook and policy documents with regard to plagiarism.
No extensions will be given: Penalties are applied to late assignments (5% of
your total assignment mark given is deducted per day, accepted up to 5 days
after the due date only). If there are circumstances that prevent the
assignment being submitted on time, an application for special consideration
may be made.
See the departmental Student Handbook for details. Note that delays caused
by computer downtime cannot be accepted as a valid reason for a late
submission without penalty. Students must plan their work to allow
for both scheduled and unscheduled downtime. Assignments submitted more
than 5 days late will receive the mark of 0.
Return of Assignments: Students are referred to the departmental Student
Handbook for details.
Objectives: To design and implement a RESTful API that will provide an access
endpoint to a data source controller similar to the one you implemented using
JDBC in Assignment 1.

Introduction

The aims of the assignment are:
• To implement web services for making available resources regarding
your fridge groceries and items. The groceries and items are those that
you have worked with in Assignment 1.
o Using Java Reflection and Servlet API
• To apply the relevant validation strategies to the grocery, using the
Validation Framework we built
in Lab 07.


Files Provided:

The following are provided in the fridge directory (zip file)

• The servlet descriptor file web.xml (directory fridge/WEB-INF)
• The jar files for JSON conversion and JDBC (MySQL) driver (directory
fridge/WEB-INF/lib)
• The following in the directory fridge/WEB-INF/classes
o The Java files for the Validation framework (complete files):
▪ Min.java, MinValidator.java
▪ Max.java, MaValidator.java
▪ NotNull.java, NotNullValidator.java
▪ CharCount.java, CharCountValidator.java
▪ Validator.java, ValidationException.java
o The Data Source Controller, FridgeDSC.java
▪ Grocery.java (excluding the validation annotations required for task XXX)
▪ Item.java
o The Controllers (incomplete)
▪ GroceryController.java
▪ ItemController.java
o The Router Servlet (incomplete)
▪ FridgeRouterServlet.java
o A few custom Exception sub-classes to be used throughout this Assignment 2
(complete)
▪ MissingArgumentException.java
▪ ResourceNotFoundException.java
▪ UpdateNotAllowedException.java

• The SQL Script to create and populate your database

o CreateDatabaseScript.sql

Task 1 - Optional

1. Study the Validation Framework provided (complete code provided) as well
as the lecture and lab materials that covered Java Reflection, Annotations and
the Validation Framework.

2. TODO: Clearly annotate the relevant fields of the Grocery.java model.
o You may want to test if your validation on the model is working (see how we
did that in the labs) – a static main has been provided in Grocery.java for you
to add your testing code.

Task 2

The controllers are the bridge between the FridgeDSC class and the
FridgeRouterServlet class.

• Study the complete FridgeDSC.java file provided, paying attention to
its constructor (some changes have been made, which differs from
Assignment 1)
o NOTE: the constructor requires database host, username and
password as arguments For each controller (GroceryController.java and
ItemController.java)

• TODO: Complete each of the method stubs provided. Each method
stubs needs to make a call to a relevant FridgeDSC.java method. Identify
which one and code it in the controllers.
o You may want to test if the controllers are responding properly when
calling each FridgeDSC method – a static main method has been
provided in each class for you to add your testing code.

Task 3

Implement the servlet (FridgeRouterServlet class) - The purpose of this servlet
class is to:

1. Identify which resource the URL is requesting, using HttpServletRequest
• example, the resource from a browser call to
http://localhost:8080/fridge/api/grocery
will then be “grocery” (the same applies for a call to
http://localhost:8080/fridge/api/GroCerY)
• the HTTP method can also be found here (in this example, a browser
call means using HTTP GET method)

2. Using the Servlet API,
• retrieve your database host, username and password from the servlet
descriptor file web.xml. (add your MySQL username & password where
needed in file web.xml)

3. Using the Reflection API,
a. Find if the resource controller exists - from the above example,
resource “grocery” should have a matching controller class
GroceryController [hint: using Class.forName(…)]
b. Create an instance of such controller class
NOTE:
• HTTP GET (with no parameters identified) maps to controller method
get()
• HTTP GET (with parameter identified) maps to controller method
get(id)
• HTTP POST (with parameter identified) maps to controller method
add(…)
• HTTP PUT (with parameter identified) maps to controller method
update(id)
• HTTP DELETE (with parameter identified) maps to controller method
delete(id)

c. If we have an HTTP GET method, with no identifiable parameters (from the
above example), find if GroceryController class has a no-argument method
named get()
• then, make a call to this method, and store the returned data

d. Do the same (a, b, c above) if the resource identified is “item”
• NOTE: clearly distinguish the difference between controller classes
GroceryController and ItemController

4. Using the Gson package provided and HttpServletResponse
a. Convert any returned data (from controller method call) to JSON data
(using Gson)
b. Convert any valid case messages (if any) to JSON data (using Gson)
c. Send the any JSON data back to the
d. Making sure any potential errors (throughout steps 1-4
described for this Task 3) are caught, identified and send back to
the browser (using GSON to convert messages to JSON) and using
appropriate HTTP Status Codes and adequate (descriptive) error
messages.

You will use Postman to test your API (https://www.getpostman.com/) – More
on Postman will be covered in your lab sessions.

Testing your API (Using Postman)

The resources to access (using Postman for example) for the assignment 2 are:
• http://localhost:8080/fridge/api/grocery
on the above you can
▪ get all (GET http method with the above URL)
▪ get one (GET http method with id 10:
http://localhost:8080/fridge/api/grocery/10 )
▪ add one (POST http method with above URL, with additional grocery info in
JSON format sent as POST data)
▪ delete one (DELETE http method with id 11:
http://localhost:8080/fridge/api/grocery/11)
▪ update one (PUT http method with id 9:
http://localhost:8080/fridge/api/grocery/9)
• http://localhost:8080/fridge/api/item
▪ get all items (GET http method with the above url)

NOTE:

The update grocery requirement is different from usual updates (only one field
is updated, that is, reduced by 1) - so the PUT method does not contain any
data. and the action of reducing grocery quantity is done on the grocery found
(if any) by the provided id the id (10, 11, 9) provided above are examples -
groceries with those id should exist in your database for the relevant action to
be possible. (Else it should return a well-defined and meaningful error
message)

What to submit

Electronic copy of all of the classes required to run the application, including
those that are provided, are to be submitted to LMS.
The only requirement is that your program should work on the database tables
grocery and item which must have the same structure as the one in the
provided MySQL script (CreateDatabaseScript.sql )

For each class that you submit, you must include, as part of the comments, at
the beginning of each file,

o your full name (with surname in capital),
o your student ID,
o your username (if different from student ID), and
o the subject code (CSE3OAD)

51作业君 51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: ITCSdaixie