Use Tuple as response
This commit is contained in:
parent
097cce14e8
commit
b12de1cd2e
27
client/data/src/main/java/helper/Tuple.java
Normal file
27
client/data/src/main/java/helper/Tuple.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package helper;
|
||||||
|
|
||||||
|
public class Tuple<X, Y> {
|
||||||
|
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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ package res;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import helper.Tuple;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
@ -15,7 +16,6 @@ import java.util.*;
|
|||||||
public class DataController {
|
public class DataController {
|
||||||
|
|
||||||
public static long USER_ID = -1;
|
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 ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all";
|
||||||
private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add";
|
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 LOGIN_ENDPOINT = "http://localhost:8080/user/login";
|
||||||
private static final String ALL_USERS_ENDPOINT = "http://localhost:8080/user/all";
|
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;
|
private final HttpRequest httpRequest;
|
||||||
|
|
||||||
@ -32,19 +33,28 @@ public class DataController {
|
|||||||
|
|
||||||
public boolean login(String username, String password) {
|
public boolean login(String username, String password) {
|
||||||
try {
|
try {
|
||||||
String response = httpRequest.sendPostRequest(
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(
|
||||||
LOGIN_ENDPOINT,
|
LOGIN_ENDPOINT,
|
||||||
"login=" + username
|
"login=" + username
|
||||||
+ "&password=" + password,
|
+ "&password=" + password,
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
String[] data = response.getValue().split("\\s+");
|
||||||
|
|
||||||
USER_ID = Long.parseLong(response.split("\\s+")[1]);
|
USER_ID = Long.parseLong(data[1]);
|
||||||
TOKEN = response.split("\\s+")[0];
|
HttpRequest.TOKEN = data[1];
|
||||||
|
|
||||||
|
Tuple<Integer, String> auth = httpRequest.sendPostRequest(
|
||||||
|
HEADER_TEST_ENDPOINT,
|
||||||
|
"",
|
||||||
|
true
|
||||||
|
);
|
||||||
|
System.out.println("auth " + auth);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return USER_ID >= 0;
|
return USER_ID >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,11 +78,11 @@ public class DataController {
|
|||||||
ArrayList<Event> eventList = new ArrayList<>();
|
ArrayList<Event> eventList = new ArrayList<>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jsonResponse = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
|
Tuple<Integer, String> response = httpRequest.sendPostRequest(ALL_EVENTS_ENDPOINT, "userId=" + USER_ID, true);
|
||||||
|
String jsonResponse = response.getValue();
|
||||||
System.out.println(jsonResponse);
|
System.out.println(jsonResponse);
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
|
||||||
|
|
||||||
for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)) {
|
for (Object obj : objectMapper.readValue(jsonResponse, Object[].class)) {
|
||||||
ArrayList<Object> list = new ArrayList<>();
|
ArrayList<Object> list = new ArrayList<>();
|
||||||
@ -112,7 +122,6 @@ public class DataController {
|
|||||||
|
|
||||||
// Parse JSON
|
// Parse JSON
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
//String json = "{ \"color\" : \"Black\", \"type\" : \"BMW\" }";
|
|
||||||
|
|
||||||
return objectMapper.readValue(jsonString, Event[].class);
|
return objectMapper.readValue(jsonString, Event[].class);
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package res;
|
package res;
|
||||||
|
|
||||||
|
import helper.Tuple;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
@ -9,7 +11,9 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class HttpRequest {
|
public class HttpRequest {
|
||||||
public String sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception {
|
public static String TOKEN = "";
|
||||||
|
|
||||||
|
public Tuple<Integer, String> sendPostRequest(String urlString, String urlParameters, boolean sendAuth) throws Exception {
|
||||||
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
|
||||||
int postDataLength = postData.length;
|
int postDataLength = postData.length;
|
||||||
|
|
||||||
@ -39,7 +43,7 @@ public class HttpRequest {
|
|||||||
|
|
||||||
if(sendAuth){
|
if(sendAuth){
|
||||||
con.setRequestProperty("Accept", "application/json");
|
con.setRequestProperty("Accept", "application/json");
|
||||||
con.setRequestProperty("Authorization", "Bearer {token}");
|
con.setRequestProperty("Authorization", "Bearer " + TOKEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
|
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
|
||||||
@ -47,22 +51,24 @@ public class HttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int status = con.getResponseCode();
|
int status = con.getResponseCode();
|
||||||
if (status == 200) {
|
|
||||||
BufferedReader in = new BufferedReader(
|
|
||||||
new InputStreamReader(con.getInputStream()));
|
|
||||||
String inputLine;
|
String inputLine;
|
||||||
StringBuilder content = new StringBuilder();
|
StringBuilder content = new StringBuilder();
|
||||||
|
BufferedReader in;
|
||||||
|
|
||||||
|
if (status == 200) {
|
||||||
|
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||||
|
} else {
|
||||||
|
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
|
||||||
|
}
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
while ((inputLine = in.readLine()) != null) {
|
||||||
content.append(inputLine);
|
content.append(inputLine);
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
|
|
||||||
con.disconnect();
|
con.disconnect();
|
||||||
return content.toString();
|
|
||||||
} else {
|
return new Tuple<>(status, content.toString());
|
||||||
con.disconnect();
|
|
||||||
throw new Exception("Status: " + status);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String sendGetRequest(String urlString) throws Exception {
|
public String sendGetRequest(String urlString) throws Exception {
|
||||||
|
Loading…
Reference in New Issue
Block a user