From b12de1cd2ec1616fc46d85df2d73fceb76691e83 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Thu, 13 Jan 2022 23:14:36 +0100 Subject: [PATCH] Use Tuple as response --- client/data/src/main/java/helper/Tuple.java | 27 ++++++++++++++ .../src/main/java/res/DataController.java | 23 ++++++++---- .../data/src/main/java/res/HttpRequest.java | 36 +++++++++++-------- 3 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 client/data/src/main/java/helper/Tuple.java diff --git a/client/data/src/main/java/helper/Tuple.java b/client/data/src/main/java/helper/Tuple.java new file mode 100644 index 0000000..e342ad6 --- /dev/null +++ b/client/data/src/main/java/helper/Tuple.java @@ -0,0 +1,27 @@ +package helper; + +public class Tuple { + public final X key; + public final Y value; + + public Tuple(X key, Y value) { + this.key = key; + this.value = value; + } + + public X getKey() { + return key; + } + + public Y getValue() { + return value; + } + + @Override + public String toString() { + return "Tuple{" + + "key=" + key + + ", value=" + value + + '}'; + } +} diff --git a/client/data/src/main/java/res/DataController.java b/client/data/src/main/java/res/DataController.java index f4713ed..01e58bf 100644 --- a/client/data/src/main/java/res/DataController.java +++ b/client/data/src/main/java/res/DataController.java @@ -2,6 +2,7 @@ package res; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import helper.Tuple; import java.io.BufferedReader; import java.io.DataOutputStream; @@ -15,7 +16,6 @@ import java.util.*; public class DataController { public static long USER_ID = -1; - public static String TOKEN = ""; private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all"; private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add"; @@ -23,6 +23,7 @@ public class DataController { private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login"; private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/user/all"; + private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test"; private final HttpRequest httpRequest; @@ -32,19 +33,28 @@ public class DataController { public boolean login(String username, String password) { try { - String response = httpRequest.sendPostRequest( + Tuple response = httpRequest.sendPostRequest( LOGIN_ENDPOINT, "login=" + username + "&password=" + password, false ); + String[] data = response.getValue().split("\\s+"); - USER_ID = Long.parseLong(response.split("\\s+")[1]); - TOKEN = response.split("\\s+")[0]; + USER_ID = Long.parseLong(data[1]); + HttpRequest.TOKEN = data[1]; + + Tuple auth = httpRequest.sendPostRequest( + HEADER_TEST_ENDPOINT, + "", + true + ); + System.out.println("auth " + auth); } catch (Exception e) { e.printStackTrace(); return false; } + return USER_ID >= 0; } @@ -68,11 +78,11 @@ public class DataController { ArrayList eventList = new ArrayList<>(); try { - String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true); + Tuple response = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true); + String jsonResponse = response.getValue(); System.out.println(jsonResponse); ObjectMapper objectMapper = new ObjectMapper(); - //String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)) { ArrayList list = new ArrayList<>(); @@ -112,7 +122,6 @@ public class DataController { // Parse JSON ObjectMapper objectMapper = new ObjectMapper(); - //String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }"; return objectMapper.readValue(jsonString, Event[].class); } diff --git a/client/data/src/main/java/res/HttpRequest.java b/client/data/src/main/java/res/HttpRequest.java index 398f157..311fad4 100644 --- a/client/data/src/main/java/res/HttpRequest.java +++ b/client/data/src/main/java/res/HttpRequest.java @@ -1,5 +1,7 @@ package res; +import helper.Tuple; + import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; @@ -9,7 +11,9 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; public class HttpRequest { - public String sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception { + public static String TOKEN = ""; + + public Tuple sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception { byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; @@ -39,7 +43,7 @@ public class HttpRequest { if(sendAuth){ con.setRequestProperty("Accept", "application/json"); - con.setRequestProperty("Authorization", "Bearer {token}"); + con.setRequestProperty("Authorization", "Bearer " + TOKEN); } try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) { @@ -47,22 +51,24 @@ public class HttpRequest { } int status = con.getResponseCode(); - if (status == 200) { - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuilder content = new StringBuilder(); - while ((inputLine = in.readLine()) != null) { - content.append(inputLine); - } - in.close(); + String inputLine; + StringBuilder content = new StringBuilder(); + BufferedReader in; - con.disconnect(); - return content.toString(); + if (status == 200) { + in = new BufferedReader(new InputStreamReader(con.getInputStream())); } else { - con.disconnect(); - throw new Exception("Status: " + status); + in = new BufferedReader(new InputStreamReader(con.getErrorStream())); } + + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + con.disconnect(); + + return new Tuple<>(status, content.toString()); } public String sendGetRequest(String urlString) throws Exception {