Module CSC7321—CSN: Middleware and software architecture for Internet distributed applications

Portail informatique

REST

REST: write a REST client and a REST server for the Vlib bike stations OpenAPI

For this lab, we use the Json representation (the marshalling and unmarshalling from java to json is done with the Jackson library), and the writing of REST services in Java with the JAX-RS specifications is achieved with the Jersey library.

VlibTour project, BikeStation client

Document yourself about the REST API offered for this open data

  1. Read carefully the Get ready page for this open data (NB there is a french and an english page)
  2. Create an account and get a key to access the OpenData (NB: it seems that some email addresses, such as gmail ones, never receive any email, use your telecom-sudparis email address).
  3. Get the static data of the bike stations from the city of Lyon in json. Save it in a file lyon.json.
  4. Read the documentation of the REST API
  5. For the sequel of this lab, we will use static data produced for the city of Paris 2 years ago. At this time, the API was in V2. Unfortunately the city of Paris does not use anymore this service. The OpenData is in V3.

Create the java classes for this open data

Each open Data manipulates its own data-types. For the Vlib OpenData the data-types manipulated are Station and Position. Those data-types are described in the server API: you have examples of a station in json. The good thing is that from an instance marshalled in json, we can produce the java classes For this example, from a station.json file, we can generate: the root class, e.g., Station and the contained classes, e.g., Position). That is the objective of this step.
  1. Use the file provided in the microproject basic structure: csc-mw-project-vlibtour/vlibtour-bikestation/src/main/resources/station.json
  2. From the station.json json document, create the Station and Position java classes and save them in the directories :
    • client side: csc-mw-project-vlibtour/vlibtour-bikestation/src/main/java/vlibtour/vlibtour_bikestation/client/generated_from_json
    • emulated server side: csc-mw-project-vlibtour/vlibtour-bikestation/src/main/java/vlibtour/vlibtour_bikestation/emulated_server/generated_from_json
    • We propose to use jsonschema2pojo web service. It uses the Jackson library to create the Station and Position classes. Be carefull, you have to select those choices:
      • package:vlibtour.vlibtour_bikestation.client.generated_from_json (client side)
      • class name: Station
      • language: Java
      • Source Type: JSON
      • Annotation:
        • Jackson2.x
        • primitive type
        • long integer (necessary for the last_update attribute)
        • double numbers
        • Use Commons-Lang3
        • include constructors
        • toString
      It will create an archive with Station.java and Position.java files.

Write a simple client

  1. In your vlibtour-bikestation module update the vlibtour.vlibtour_bikestation.client package: modify the BikeStationClient java class. The objective is to write a simple Java client, that gets the dynamic information for a given station ID in a Station instance, and prints it on the standard output.
    NB: it has to use the generated Station.toString() method.
    You can inspire yourself from the SkiersRestClient.java code, that
    • reads the address of server in the rest.properties file
    • makes some requests
  2. If you did not have the microproject skeleton, you could have create the structure of the module from the maven archetype, in this case you have to uncomment the Jackson dependency in the pom file. mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \ -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \ -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \ -DarchetypeVersion=2.26-b08
  3. Be aware that you need to adapt the rest.properties file (in the resources directory) in order to interact with your emulated server:
    • jcdecaux.rooturl: address of the server
    • jcdecaux.contract: Paris (for the emulated server). We have to change for the real server, because unfortunately the city of Paris does not use anymore this opendata)
    • jcdecaux.apiKey: your apikey
  4. Test your client with the real BikeStation server.

VlibTour project, BikeStation emulated server

  • The objective of this exercice is to write an emulation of the public BikeStation server. All the tests will be done with the emulated server. At deployment time, we should be able to choose between the public BikeStation server and the emulated one through the configuration file (rest.properties).

From the Paris.json file create the collection of Station for the city of Paris

  1. Write the Stations class You can inspire yourself from the Skiers class

Create the StationsRest class that implements the REST API of the real server

  • As in the real server, two REST methods have to be provided:
    • get all the stations (of a given city)
    • get a given station (of a given city)
    The SkiersRest class is again a good example. At each request, Skiers are read from a file.
  • At each request the Stations instance must be read from a file. To create a Stations instance from a json file you can use this code
    ObjectMapper mapper = new ObjectMapper(); //jackson class for converting from json to java List stationList; //JSON from file to Object stationList = Arrays.asList(mapper.readValue(new File(fileName), Station[].class)); // get the array of stations and convert to a list Stations stations=new Stations(stationList);// create a Stations instance

Write the Main class to run the emulated server

  • Write the Main class (the one that implements a GRIZZLY server). This class is a copy from the Main class of the Skier example. You have to :
    • get the URL from the property,
    • declare the packages for which it handles request
    new ResourceConfig().packages("vlibtour.vlibtour_bikestation.emulatedserver");

Test your client with the emulated server

  • Modify the rest.properties file to change the server url, and the city contract and test your client (unchanged) with your emulated server.
  • In your microproject, you will use your emulated server.

Next step for the microproject

In a next step, you can prepare some methods, that could be useful for the microproject. For example:

  • Find bikestations near a GPS position;
  • Find bikestations near a POI;
  • Get the availabilities of the bikestations near a POI
  • Find the bikestation the nearest from a POI with a minimum number of available bikes (for the group of tourists);
  • Find the bikestation the nearest from a POI with a minimum number of free positions to bring back the bikes (for a group of tourists);

 

 

 


$Date: 2019-09-09 00:31:57 +0200 (lun. 09 sept. 2019) $