SHARE
TWEET

ServerCommunication

a guest Feb 27th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package nl.tudelft.oopp.demo.communication;
  2.  
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5.  
  6. import java.net.URI;
  7. import java.net.http.HttpClient;
  8. import java.net.http.HttpRequest;
  9. import java.net.http.HttpResponse;
  10. import java.util.HashMap;
  11.  
  12. public class ServerCommunication {
  13.  
  14.     private static HttpClient client = HttpClient.newBuilder().build();
  15.  
  16.     /**
  17.      * Retrieves a quote from the server.
  18.      * @return the body of a get request to the server.
  19.      */
  20.     public static String getQuote() {
  21.         HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/quote")).build();
  22.         HttpResponse<String> response;
  23.         try {
  24.             response = client.send(request, HttpResponse.BodyHandlers.ofString());
  25.         } catch (Exception e) {
  26.             e.printStackTrace();
  27.             return "Communication with server failed";
  28.         }
  29.         if (response.statusCode() != 200) {
  30.             System.out.println("Status: " + response.statusCode());
  31.         }
  32.         return response.body();
  33.     }
  34.  
  35.     /**
  36.      * Retrieves a the list of buildings from the server.
  37.      * @return the body of a get request to the server.
  38.      */
  39.     public static String getBuildingList() {
  40.         HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/building")).build();
  41.         HttpResponse<String> response;
  42.         try {
  43.             response = client.send(request, HttpResponse.BodyHandlers.ofString());
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.             return "Communication with server failed";
  47.         }
  48.         if (response.statusCode() != 200) {
  49.             System.out.println("Status: " + response.statusCode());
  50.         }
  51.         return response.body();
  52.     }
  53.  
  54.     /**
  55.      * Creates a new building and adds it into the database
  56.      */
  57.     public static void addBuilding() throws JSONException {
  58.  
  59.         var values = new HashMap<String, String>() {{
  60.             put("name", "Library");
  61.             put("address", "Mekelweg 72");
  62.             put("numberOfBikes", "5");
  63.         }};
  64.  
  65.         JSONObject requestBody = new JSONObject(values);
  66.  
  67.         HttpRequest request = HttpRequest.newBuilder()
  68.                 .uri(URI.create("http://localhost:8080/building"))
  69.                 .POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
  70.                 .header("Content-Type", "application/json")
  71.                 .build();
  72.  
  73.         HttpResponse<String> response = null;
  74.         try {
  75.             response = client.send(request, HttpResponse.BodyHandlers.ofString());
  76.             }  catch (Exception e) {
  77.                 e.printStackTrace();
  78.                 System.out.println("Status: " + response.statusCode());
  79.             }
  80.  
  81.         if (response.statusCode() != 200) {
  82.             System.out.println("Status: " + response.statusCode());
  83.         }
  84.         System.out.println(response.body());
  85.     }
  86.  
  87.     /**
  88.      * Retrieves a the list of rooms from the server.
  89.      * @return the body of a get request to the server.
  90.      */
  91.     public static String getRoomList() {
  92.         HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/room")).build();
  93.         HttpResponse<String> response;
  94.         try {
  95.             response = client.send(request, HttpResponse.BodyHandlers.ofString());
  96.         } catch (Exception e) {
  97.             e.printStackTrace();
  98.             return "Communication with server failed";
  99.         }
  100.         if (response.statusCode() != 200) {
  101.             System.out.println("Status: " + response.statusCode());
  102.         }
  103.         return response.body();
  104.     }
  105.  
  106.     /**
  107.      * Retrieves a the list of users from the server.
  108.      * @return the body of a get request to the server.
  109.      */
  110.     public static String getUserList() {
  111.         HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/loginpage")).build();
  112.         HttpResponse<String> response;
  113.         try {
  114.             response = client.send(request, HttpResponse.BodyHandlers.ofString());
  115.         } catch (Exception e) {
  116.             e.printStackTrace();
  117.             return "Communication with server failed";
  118.         }
  119.         if (response.statusCode() != 200) {
  120.             System.out.println("Status: " + response.statusCode());
  121.         }
  122.         return response.body();
  123.     }
  124.  
  125.     public static String formatBuilding(JSONObject jsonObject) throws JSONException {
  126.         StringBuilder result = new StringBuilder();
  127.         int buildingBikes = jsonObject.getInt("buildingBikes");
  128.         String buildingAddress = jsonObject.getString("buildingAddress");
  129.         String buildingName = jsonObject.getString("buildingName");
  130.  
  131.         result.append("Name of building: ");
  132.         result.append(buildingName);
  133.         result.append("\nAddress of building: ");
  134.         result.append(buildingAddress);
  135.         result.append("\nAmount of bikes stored in building: ");
  136.         result.append(buildingBikes);
  137.         result.append("\n");
  138.         return result.toString();
  139.     }
  140.  
  141.     public static String formatRoom(JSONObject jsonObject) throws JSONException {
  142.         StringBuilder result = new StringBuilder();
  143.         String roomName = jsonObject.getString("name");
  144.         String roomDescription = jsonObject.getString("description");
  145.         int roomCapacity = jsonObject.getInt("capacity");
  146.  
  147.         result.append("Name of room: ");
  148.         result.append(roomName);
  149.         result.append("\nDescription of room: ");
  150.         result.append(roomDescription);
  151.         result.append("\nTotal capacity of room: ");
  152.         result.append(roomCapacity);
  153.         result.append("\n");
  154.         return result.toString();
  155.     }
  156.  
  157.  
  158. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top