Workshop Lab3 - Tying it together
Introducing Lab 3
This workshop is to get you started on Lab 3, (the final of the three-part assignment Exposing data over HTTP).
In this sub-assignment, you will connect what you did in Lab 1 and Lab 2 in so far that the GUI client will actually connect to the Web API, get JSON, parse JSON and present products "Live" according to the user input. On the server-side (the web API), you will scrap the FakeProductLine and write a real product line which fetches all products from the database.
This means that there are basically two parts to this last assignment:
- Make the GUI connected to the Web API
- Make the Web API use JDBC to load all products (some 20 000 products) from a database
In this workshop we will focus on getting you started and familiar with the files you download.
Files to download
Download this zip file and unzip in a new folder where you will have all new files for lab3. You can put files from this folder into your folders for lab1 and lab2.
Note that there are a few new files for you in the download and you will use a lot of code from your lab1 and lab2 code base.
Overview of the files you just downloaded and unzipped
First of all, please note that there are two main folders:
- server-side (code for the Web API/Servlet/SystemetAPI part) - continuation of the Lab1
- client-side (code for the GUI part) - continuation of the Lab2
. ├── client-side │ ├── clean.sh │ ├── examples │ │ ├── ClientExample.java │ │ ├── Client.java │ │ └── ProductParser.java │ ├── lib │ │ └── org.json.jar │ └── se │ └── itu │ └── systemet │ └── domain │ ├── Product$Builder.class │ ├── Product.class │ ├── Product$Exporter.class │ └── Product.java ├── LICENSE ├── README.md └── server-side ├── compile_and_run_api_example.sh └── systemet-api ├── examples │ └── ApiExample.java └── se └── itu └── systemet └── storage └── DBHelper.java 14 directories, 14 files
In the server-side directory, you will find an example for how to test your web api from the command line using the database:
server-side/systemet-api/examples/ApiExample.java which can be run using the script
compile_and_run_api_example.sh found in the server-side directory. Replace the ApiExample.java from your lab1 with this file (overwrite the old ApiExample with this new file and the same with the script).
The new thing about this example/test program is that it tells the ProductLineFactory to use a database product line, and not the FakeProductLine you wrote. Your old ApiExample.java uses the FakeProductLine.
You will also find the file
server-side/systemet-api/se/itu/systemet/storage/DBHelper.java. Put this file in your old lab1 directory in
systemet-api/se/itu/systemet/storage/DBHelper.java. It is a file which helps you with the JDBC stuff, and you will call it from
systemet-api/se/itu/systemet/storage/SQLBasedProductLine.java, the class which will replace your old
systemet-api/se/itu/systemet/storage/FakeProductLine.java and talk to the database.
You will work with this file (
systemet-api/se/itu/systemet/storage/SQLBasedProductLine.java) and write code which uses DBHelper to send a big SQL query to the database for getting all products from the database.
Summing it up
Let's try to sum up what you need to do. In the zip file there are three files you should copy to the lab 1 directory:
systemet-api/examples/folder in the server-side directory (lab 1).
compile_and_run_api_example.shto the server-side directory (lab 1). There should be a couple of other scripts, used in lab 1, in the same folder.
systemet-api/se/itu/systemet/storage/folder in the server side directory (lab 1).
Hint: If you're not sure in what directory to place the files, check the package name in the files.
In the client-side directory, you will find three example classes in the examples directory, showing you how you can connect to a web API from Java and translate the JSON to a Java
List<Product>. You will also find a new third-party JAR, org.json.jar, which is needed in order to do the parsing. This must be on the classpath when compiling and running the GUI, since you will import stuff from the org.json package which resides in the JAR file.
These example files are the only thing you get and your task will be to look at them and figure out how to write the new class implementing ApiAccess and created by ApiAccessFactory (you need to make this happen by changing the factory). You can call your new class SystemetWebApiAccess if you like. Inside the class, implement the methods listed in the interface ApiAccess and make it fetch products from the Web API (JSON which you will parse to a list of products as in the example given).
You're SystemetWebApiAccess class will
- be created and returned to the GUI by ApiAccessFactory
- connect to the Web API (Servlet running in a separate terminal)
- read JSON from the Web API to a Java String
- parse the Java String with JSON to a
List<Product> using classes from <code>org.json
The examples directory
examples/ ├── ClientExample.java ├── Client.java └── ProductParser.java 0 directories, 3 files
Let's run the example
To run the example you need two terminals.
- Start a new terminal window and start your web api (winstone)
- Start another terminal window
- cd to the directory above examples in client-code
- compile all classes in examples/
javac -cp .:lib/org.json.jar examples/*.java
- Windows with Cygwin:
javac -cp ".;lib/org.json.jar" examples/*.java
- Run the example program:
java -cp lib/org.json.jar:. examples.ClientExample
- Windows with Cygwin:
java -cp "lib/org.json.jar;." examples.ClientExample
- Try giving the example program some argument (mac/linux), e.g.
$ java -cp lib/org.json.jar:. examples.ClientExample "min_alcohol=40&max_price=400"
- Try giving the example program some argument (Windows/Cygwin), e.g.
$ java -cp "lib/org.json.jar;." examples.ClientExample "min_alcohol=40&max_price=400"
What just happened?
Look at the code and we'll try to understand it together with the teachers (who will go through the code). The supervisors will help as well.
The se directory
This directory is incuded only to make the example work! The example needs a Product class!
. └── se └── itu └── systemet └── domain └── Product.java 6 directories, 7 files