Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.tudelft.oopp.demo.communication;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.net.URI;
- import java.net.http.HttpClient;
- import java.net.http.HttpRequest;
- import java.net.http.HttpResponse;
- import java.util.HashMap;
- public class ServerCommunication {
- private static HttpClient client = HttpClient.newBuilder().build();
- /**
- * Retrieves a quote from the server.
- * @return the body of a get request to the server.
- */
- public static String getQuote() {
- HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/quote")).build();
- HttpResponse<String> response;
- try {
- response = client.send(request, HttpResponse.BodyHandlers.ofString());
- } catch (Exception e) {
- e.printStackTrace();
- return "Communication with server failed";
- }
- if (response.statusCode() != 200) {
- System.out.println("Status: " + response.statusCode());
- }
- return response.body();
- }
- /**
- * Retrieves a the list of buildings from the server.
- * @return the body of a get request to the server.
- */
- public static String getBuildingList() {
- HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/building")).build();
- HttpResponse<String> response;
- try {
- response = client.send(request, HttpResponse.BodyHandlers.ofString());
- } catch (Exception e) {
- e.printStackTrace();
- return "Communication with server failed";
- }
- if (response.statusCode() != 200) {
- System.out.println("Status: " + response.statusCode());
- }
- return response.body();
- }
- /**
- * Creates a new building and adds it into the database
- */
- public static void addBuilding() throws JSONException {
- var values = new HashMap<String, String>() {{
- put("name", "Library");
- put("address", "Mekelweg 72");
- put("numberOfBikes", "5");
- }};
- JSONObject requestBody = new JSONObject(values);
- HttpRequest request = HttpRequest.newBuilder()
- .uri(URI.create("http://localhost:8080/building"))
- .POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
- .header("Content-Type", "application/json")
- .build();
- HttpResponse<String> response = null;
- try {
- response = client.send(request, HttpResponse.BodyHandlers.ofString());
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("Status: " + response.statusCode());
- }
- if (response.statusCode() != 200) {
- System.out.println("Status: " + response.statusCode());
- }
- System.out.println(response.body());
- }
- /**
- * Retrieves a the list of rooms from the server.
- * @return the body of a get request to the server.
- */
- public static String getRoomList() {
- HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/room")).build();
- HttpResponse<String> response;
- try {
- response = client.send(request, HttpResponse.BodyHandlers.ofString());
- } catch (Exception e) {
- e.printStackTrace();
- return "Communication with server failed";
- }
- if (response.statusCode() != 200) {
- System.out.println("Status: " + response.statusCode());
- }
- return response.body();
- }
- /**
- * Retrieves a the list of users from the server.
- * @return the body of a get request to the server.
- */
- public static String getUserList() {
- HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create("http://localhost:8080/loginpage")).build();
- HttpResponse<String> response;
- try {
- response = client.send(request, HttpResponse.BodyHandlers.ofString());
- } catch (Exception e) {
- e.printStackTrace();
- return "Communication with server failed";
- }
- if (response.statusCode() != 200) {
- System.out.println("Status: " + response.statusCode());
- }
- return response.body();
- }
- public static String formatBuilding(JSONObject jsonObject) throws JSONException {
- StringBuilder result = new StringBuilder();
- int buildingBikes = jsonObject.getInt("buildingBikes");
- String buildingAddress = jsonObject.getString("buildingAddress");
- String buildingName = jsonObject.getString("buildingName");
- result.append("Name of building: ");
- result.append(buildingName);
- result.append("\nAddress of building: ");
- result.append(buildingAddress);
- result.append("\nAmount of bikes stored in building: ");
- result.append(buildingBikes);
- result.append("\n");
- return result.toString();
- }
- public static String formatRoom(JSONObject jsonObject) throws JSONException {
- StringBuilder result = new StringBuilder();
- String roomName = jsonObject.getString("name");
- String roomDescription = jsonObject.getString("description");
- int roomCapacity = jsonObject.getInt("capacity");
- result.append("Name of room: ");
- result.append(roomName);
- result.append("\nDescription of room: ");
- result.append(roomDescription);
- result.append("\nTotal capacity of room: ");
- result.append(roomCapacity);
- result.append("\n");
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement