Skip to content

Commit

Permalink
Merge branch 'fix_client_checkstyle' into 'master'
Browse files Browse the repository at this point in the history
Fix client checkstyle

See merge request cse1105/2018-2019/oopp-group-12/template!104
  • Loading branch information
ivandim99 committed Apr 14, 2019
2 parents fbc0007 + e7f2f9c commit 867ff66
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 147 deletions.
110 changes: 56 additions & 54 deletions client/src/main/java/groupxii/client/TokenManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,71 @@
/**
* Holds the verification token and periodically updates it from the server.
*/

public class TokenManager {
public static TokenManager instance;
/**
* Holds the JSON request to be posted on login.
*/
private String loginReqBody;
public static TokenManager instance;

/**
* Holds the JWT from the server.
*/
private String jwt;
/**
* The daemon timeout should roughly match the validty of the token.
* TODO read the validity off the token.
*/
public static final int timeout = 86390000;

/**
* The daemon timeout should roughly match the validty of the token.
* TODO read the validity off the token.
*/
public final static int timeout = 86390000;
/**
* Holds the JSON request to be posted on login.
*/
private String loginReqBody;

public TokenManager(String username, String password) {
jwt = null;
/**
* Holds the JWT from the server.
*/
private String jwt;

loginReqBody = "{"
+ "\"username\":"
+ "\"" + username + "\", "
+"\"password\":"
+ "\"" + password + "\"}";
//TODO hash+salt the password
/**
* Constructs a token manager for the provided username and password.
*/
public TokenManager(String username, String password) {
jwt = null;

Thread daemon = new Thread(new Daemon());
daemon.setDaemon(true);
daemon.start();
}
loginReqBody = "{"
+ "\"username\":"
+ "\"" + username + "\", "
+ "\"password\":"
+ "\"" + password + "\"}";
//TODO hash+salt the password

/**
* Returns the currently held JWT
*/
public String getToken() {
return this.jwt;
}
Thread daemon = new Thread(new Daemon());
daemon.setDaemon(true);
daemon.start();
}

class Daemon implements Runnable {
/**
* Periodically requests a new token from the server.
* Should be a daemon thread.
*/
public void run() {
while (true) {
refreshToken();
/**
* Returns the currently held JWT.
*/
public String getToken() {
return this.jwt;
}

try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
//Die
return;
}
}
}
}
class Daemon implements Runnable {
/**
* Periodically requests a new token from the server.
* Should be a daemon thread.
*/
public void run() {
while (true) {
refreshToken();

try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
//Die
return;
}
}
}
}

public void refreshToken() {
jwt = LoginConnector.postCredentials(loginReqBody);
System.err.println("DEBUG: Recieved Token: " + jwt);
}
public void refreshToken() {
jwt = LoginConnector.postCredentials(loginReqBody);
System.err.println("DEBUG: Recieved Token: " + jwt);
}
}
23 changes: 4 additions & 19 deletions client/src/main/java/groupxii/client/connector/Connector.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package groupxii.client.connector;

import groupxii.client.TokenManager;
import groupxii.client.controllers.LoginController;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand All @@ -26,9 +25,6 @@ public static String request(String reqType, String resource, String body) {
HttpURLConnection connection;
try {
connection = OpenConnection.openConnection(Target.getHost() + resource);
if (reqType.equals("POST")) {
connection.setDoOutput(true);
}
} catch (MalformedURLException e) {
//TODO
return null;
Expand All @@ -37,9 +33,9 @@ public static String request(String reqType, String resource, String body) {
return null;
}

if (TokenManager.instance.getToken() != null) {
connection.setRequestProperty("Authorization", TokenManager.instance.getToken());
}
if (TokenManager.instance.getToken() != null) {
connection.setRequestProperty("Authorization", TokenManager.instance.getToken());
}

try {
connection.setRequestMethod(reqType);
Expand All @@ -49,6 +45,7 @@ public static String request(String reqType, String resource, String body) {
}

if (reqType.equals("POST")) {
connection.setDoOutput(true);
try {
int length = body.length();
connection.setFixedLengthStreamingMode(length);
Expand Down Expand Up @@ -105,17 +102,5 @@ public static String postRequest(String resource) {

public static String postRequest(String resource, String body) {
return request("POST", resource, body);
}

//WHY THE FUCK IS THIS HERE
/**
* communicates with the target server, and updates the
* total co2 reduction of the user in the database.
* @param amount of CO2 the user has reduced
* @return JSON String
*/
public static String updateReducedCo2(String amount) {
return postRequest("/increaseReducedCO2?Id="
+ LoginController.userId + "&ReducedCO2=" + amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import groupxii.client.localproducts.GetUserLocation;

public class LocalProductsConnector {

/**
* asks for the local shops from the server.
* Asks for the local shops from the server.
* @return list with the shops nearby
*/
public static String retrieveLocalShops() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package groupxii.client.connector;

public class LoginConnector {
/**
* Shortcut for requesting /login.
*/
public static String postCredentials(String reqBody) {
String resource = "/login";

return Connector.postRequest(resource, reqBody);
}

/**
* Shortcut for requesting /register with two variables.
*/
public static String register(String username, String password) {
String resource = "/register"
+ "?username=" + username
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* Dumb, but those two classes don't seem mokable.
*/
public class OpenConnection {
/**
* Creates HttpURLConnection.
*/
public static HttpURLConnection openConnection(String host)
throws MalformedURLException, IOException {
URL url = new URL(host);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package groupxii.client.connector;

public class TransportConnector {
public static String retrieveTransportList(){
/**
* Shortcut for /vehicleNameList.
*/
public static String retrieveTransportList() {
String resource = "/vehicleNameList";
return Connector.getRequest(resource);
}
public static String calculateCO2Reduction(String goodTransportName, String badTransportName , int goodConsumption , int badConsumption) {

/**
* Shortcut for /calculateTransprot.
*/
public static String calculateCO2Reduction(String goodTransportName,
String badTransportName,
int goodConsumption,
int badConsumption) {
String resource = "/calculateTransport"
+ "?" + goodTransportName
+ "&" + badTransportName
+ "&" + goodConsumption
+ "&" + badConsumption;
+ "?goodVehicleType=" + goodTransportName
+ "&badVehicleType" + badTransportName
+ "&goodAvgConsumption" + goodConsumption
+ "&badAvgConsumption" + badConsumption;

return Connector.getRequest(resource);
}

/**
* Shortcut for /usedTransportList.
*/
public static String retrieveUsedTransportList() {
String resource = "/usedTransportList";
return Connector.getRequest(resource);
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package groupxii.client.connector;

public class VegetarianMealConnector {
/**
* Shortcut for /mealNameList.
*/
public static String retrieveMealList() {
String resource = "/getMealList";
String resource = "/mealNameList";
return Connector.getRequest(resource);
}

Expand All @@ -25,6 +28,9 @@ public static String calculateCO2Reduction(String goodFoodName, int goodServingS
return Connector.getRequest(resource);
}

/**
* Shortcut for /saveMealData.
*/
public static String commitMeal(String goodFoodName, int goodServingSize,
String badFoodName, int badServingSize) {
String resource = "/saveMealData"
Expand All @@ -36,6 +42,9 @@ public static String commitMeal(String goodFoodName, int goodServingSize,
return Connector.postRequest(resource);
}

/**
* Shortcut for /getEatenMealList.
*/
public static String retrieveEatenMealList() {
String resource = "/getEatenMealList";
return Connector.getRequest(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,13 @@ public class LocalProductsController implements Initializable {
private ImageView mapsImage = new ImageView();

private int userId = 1;
//private List<String> listViewItems = new ArrayList<String>();
//private ObservableList<String> listViewObservable;
//private String listItemsStr = "";

@Override
public void initialize(URL location, ResourceBundle resources) {
String listItemsStr = LocalProductsConnector.retrieveLocalShops();
//listItemsStr = new Scanner(new URL("/localshops?location=" +
// getUserLocation.getUserLocation()).openStream(),"UTF-8").nextLine();
List<String> listViewItems = Arrays.asList(listItemsStr.split(", "));
ObservableList<String> listViewObservable =
FXCollections.observableArrayList(listViewItems);
//listViewObservable = FXCollections.observableArrayList(listViewItems);
localShops.setItems(listViewObservable);
}

Expand All @@ -62,8 +56,6 @@ public void getLocation(MouseEvent event) throws GeoIp2Exception, IOException {
if (url == null) {
textfield.setText("Please choose a shop first");
} else {
//GetUserLocation getUserLocation = new GetUserLocation();
//String location = getUserLocation.getUserLocation();
mapsImage.setImage(new Image("https://maps.googleapis.com/maps/api/staticmap?center=" + url + "&zoom=12&size=900x150&maptype=roadmap&markers=color:red%7Clabel:Shop%7C" + url + "&key=AIzaSyBvn_zZpLGUjLJBxIUoGHgJjzo2VlZm3jg"));
}
}
Expand All @@ -89,7 +81,7 @@ public void navigate(MouseEvent event) throws Exception {
*/
public String getShopLocation() {

if(localShops.getSelectionModel().getSelectedItem() == null){
if (localShops.getSelectionModel().getSelectedItem() == null) {
return "server is not running";
} else {
String locationStr = localShops.getSelectionModel().getSelectedItem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ public class LoginController {
@FXML
private Text errorMessage = new Text();

public static int userId = 1;

@FXML
public void loginButton(MouseEvent event) throws Exception {
if(txtUsername.getText().isEmpty() || txtPassword.getText().isEmpty()){
if (txtUsername.getText().isEmpty() || txtPassword.getText().isEmpty()) {
errorMessage.setText("Please enter a username and/or password.");
} else{
} else {
String username = txtUsername.getText();
String password = txtPassword.getText();
// Initializa TokenManager
Expand All @@ -45,9 +43,9 @@ public void loginButton(MouseEvent event) throws Exception {

@FXML
public void signUpButton(MouseEvent event) {
if(txtUsername.getText().isEmpty() || txtPassword.getText().isEmpty()){
if (txtUsername.getText().isEmpty() || txtPassword.getText().isEmpty()) {
errorMessage.setText("Please enter a username and/or password.");
} else{
} else {
String username = txtUsername.getText();
String password = txtPassword.getText();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
import java.net.URL;
import java.util.ResourceBundle;

import static groupxii.client.controllers.LoginController.userId;


public class MenuController implements Initializable {

private Main main = new Main();
Expand Down Expand Up @@ -73,8 +70,8 @@ public void btnTemperature(MouseEvent event) throws Exception {

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
String reducedCo2 = Connector.getRequest("/getReducedCo2OfUser?Id=" + userId);
//What are all this things?
String reducedCo2 = Connector.getRequest("/getReducedCo2OfUser");
//What are all this things?
reducedCo2Text.setText(reducedCo2);
double reducedCo2Number = Double.parseDouble(reducedCo2);
double savedTrees = reducedCo2Number / 24500;
Expand Down
Loading

0 comments on commit 867ff66

Please sign in to comment.