diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java index 89e1d2e0363e79d05e114e80c083547831f35797..db2fcdae9f5253d2738b45545a633fea0a45c165 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -5,11 +5,14 @@ import javax.swing.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import java.util.HashMap; +import java.util.Map; import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.frames.panels.LineChartPanel; import com.joelhelkala.watcherGui.frames.panels.ProgressBarCirclePanel; import com.joelhelkala.watcherGui.frames.panels.TemperaturePanel; +import com.joelhelkala.watcherGui.httpRequests.HttpRequests; public class WelcomePage implements MouseListener { @@ -28,8 +31,11 @@ public class WelcomePage implements MouseListener { private final JLabel nodeDataLabel; private final JLabel nodeSettingsLabel; private final JLabel friendsLabel; + private final JLabel helpLabel; + private final JLabel settingsLabel; - public WelcomePage(){ + public WelcomePage(){ + Map<String, String> temperatures = HttpRequests.getNodeData(1); JPanel topPanel = new JPanel(); topPanel.setBackground(dark); topPanel.setBounds(0,0,width, topPanelHeight); @@ -126,18 +132,22 @@ public class WelcomePage implements MouseListener { friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(friendsLabel); - JLabel helpLabel = new JLabel("Help"); + helpLabel = new JLabel("Help"); helpLabel.setHorizontalAlignment(SwingConstants.CENTER); helpLabel.setForeground(Color.WHITE); helpLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); helpLabel.setBounds(20, 615, 98, 16); + helpLabel.addMouseListener(this); + helpLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(helpLabel); - JLabel settingsLabel = new JLabel("Settings"); + settingsLabel = new JLabel("Settings"); settingsLabel.setHorizontalAlignment(SwingConstants.CENTER); settingsLabel.setForeground(Color.WHITE); settingsLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); settingsLabel.setBounds(123, 615, 98, 16); + settingsLabel.addMouseListener(this); + settingsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(settingsLabel); frame.getContentPane().add(cardPanel); frame.getContentPane().add(chartPanel); @@ -158,7 +168,8 @@ public class WelcomePage implements MouseListener { if (target == nodeDataLabel) nodeDataLabel.setForeground(lightgray); else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(lightgray); else if (target == friendsLabel) friendsLabel.setForeground(lightgray); - + else if (target == helpLabel) helpLabel.setForeground(lightgray); + else if (target == settingsLabel) settingsLabel.setForeground(lightgray); } @Override @@ -167,7 +178,8 @@ public class WelcomePage implements MouseListener { if (target == nodeDataLabel) nodeDataLabel.setForeground(Color.WHITE); else if (target == nodeSettingsLabel) nodeSettingsLabel.setForeground(Color.WHITE); else if (target == friendsLabel) friendsLabel.setForeground(Color.WHITE); - + else if (target == helpLabel) helpLabel.setForeground(Color.WHITE); + else if (target == settingsLabel) settingsLabel.setForeground(Color.WHITE); } @Override diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java index f56dcbb404b99cd35d093103719601b9556e642e..88d9d56786ad0802197f3cf6814a9f051b82ad79 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -9,6 +9,8 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; import org.json.JSONArray; import org.json.JSONException; @@ -135,8 +137,11 @@ public class HttpRequests { response.append(responseLine.trim()); } } + // Get the values from the response and set them to the user JSONObject object = new JSONObject(response.toString()); User.setToken(object.get("access_token").toString()); + User.setName(object.get("name").toString()); + User.setEmail(object.get("email").toString()); con.disconnect(); return true; } catch (MalformedURLException e) { @@ -149,6 +154,39 @@ public class HttpRequests { return false; } + /* + * Gets all the history data from the given node_id. + * TODO: Set a parameter that configures the amount of history data will be fetched + */ + public static Map<String, String> getNodeData(Integer node_id) { + Map<String, String> data = new HashMap<String, String>(); + try { + URL url = new URL(address + "/nodeData/" + node_id); + StringBuilder response = new StringBuilder(); + HttpURLConnection con = (HttpURLConnection)url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("Content-Type", "application/json; utf-8"); + con.setRequestProperty("Accept", "application/json"); + con.setDoOutput(true); + + try(BufferedReader br = new BufferedReader( + new InputStreamReader(con.getInputStream(), "utf-8"))) { + String responseLine = null; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + } + System.out.println(response); + con.disconnect(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + return data; + } + public static JSONArray parse(String responseBody) { JSONArray body = new JSONArray(responseBody); for(int i = 0; i < body.length(); i++) { diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthenticationFilter.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthenticationFilter.java index bbc0afc8155049ec5279f15ced064271251a77f7..81eb2c758e103d8ad46efc1c2f296d59438dd661 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthenticationFilter.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/filter/CustomAuthenticationFilter.java @@ -71,10 +71,11 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi .withClaim("role", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList())) .sign(algorithm); - Map<String, String> tokens = new HashMap<>(); - tokens.put("access_token", access_token); + Map<String, String> json_resp = new HashMap<>(); + json_resp.put("access_token", access_token); + json_resp.put("name", user.getName()); + json_resp.put("email", user.getEmail()); response.setContentType("application/json"); - // TODO: perhaps return user info and token, not just token - new ObjectMapper().writeValue(response.getOutputStream(), tokens); + new ObjectMapper().writeValue(response.getOutputStream(), json_resp); } }