Skip to content
Snippets Groups Projects
Commit 58cbf3ec authored by joalhelk's avatar joalhelk
Browse files

timeout, data queries visible, dont scrab database on startup

parent ba0faaed
No related branches found
No related tags found
No related merge requests found
Showing
with 60 additions and 33 deletions
...@@ -73,11 +73,11 @@ public class WelcomePage implements MouseListener, ActionListener { ...@@ -73,11 +73,11 @@ public class WelcomePage implements MouseListener, ActionListener {
String selected = (String) comboBox.getSelectedItem(); String selected = (String) comboBox.getSelectedItem();
Node node = Nodes.findByLocation(selected); Node node = Nodes.findByLocation(selected);
if(node == null) return; if(node != null) {
node.updateData(HttpRequests.getNodeData(node.getId()));
node.updateData(HttpRequests.getNodeData(node.getId())); nodeDataFrame.updateData(node);
nodeDataFrame.updateData(node); nodeSettingFrame.updateInformation(node);
nodeSettingFrame.updateInformation(node); }
comboBox.addActionListener(this); comboBox.addActionListener(this);
comboBox.setBounds(500, 10, 200, 50); comboBox.setBounds(500, 10, 200, 50);
...@@ -188,8 +188,6 @@ public class WelcomePage implements MouseListener, ActionListener { ...@@ -188,8 +188,6 @@ public class WelcomePage implements MouseListener, ActionListener {
cl.show(stagePanel, "1"); cl.show(stagePanel, "1");
frame.add(stagePanel); frame.add(stagePanel);
nodeSettingFrame.updateInformation(Nodes.findByLocation(comboBox.getSelectedItem().toString()));
frame.setVisible(true); frame.setVisible(true);
} }
...@@ -333,4 +331,25 @@ public class WelcomePage implements MouseListener, ActionListener { ...@@ -333,4 +331,25 @@ public class WelcomePage implements MouseListener, ActionListener {
String node_name = ((JComboBox)e.getSource()).getSelectedItem().toString(); String node_name = ((JComboBox)e.getSource()).getSelectedItem().toString();
updateDataByName(node_name); updateDataByName(node_name);
} }
// Default error message dialog
public static void ErrorDialog() {
String message = "Something went wrong.";
String title = "ERROR!";
JOptionPane.showConfirmDialog(frame, message, title,
JOptionPane.ERROR_MESSAGE);
}
public static void TimedOutSession() {
String message = "\"The session has timed out.\"\n"
+ "You will be logged out...";
String title = "TIMEOUT!";
JOptionPane.showConfirmDialog(frame, message, title,
JOptionPane.ERROR_MESSAGE);
User.clear();
frame.dispose();
LoginPage login = new LoginPage();
}
} }
\ No newline at end of file
...@@ -91,12 +91,18 @@ public class NodeSettingsFrame extends JPanel implements ActionListener{ ...@@ -91,12 +91,18 @@ public class NodeSettingsFrame extends JPanel implements ActionListener{
// A confirmation window is prompted // A confirmation window is prompted
private void saveInformation() { private void saveInformation() {
boolean save = WelcomePage.handleSave(); boolean save = WelcomePage.handleSave();
if(save) HttpRequests.updateNode(new Node(descField.getText(), if(save && current_node != null) {
Float.parseFloat(latField.getText()), boolean success = HttpRequests.updateNode(new Node(descField.getText(),
Float.parseFloat(lonField.getText()), Float.parseFloat(latField.getText()),
locationField.getText(), Float.parseFloat(lonField.getText()),
current_node.getId())); locationField.getText(),
WelcomePage.updateComboBox(); current_node.getId()));
if(!success) {
WelcomePage.ErrorDialog();
return;
}
WelcomePage.updateComboBox();
}
} }
/* /*
......
...@@ -20,6 +20,7 @@ import org.json.JSONObject; ...@@ -20,6 +20,7 @@ import org.json.JSONObject;
import com.joelhelkala.watcherGui.Nodes.Node.Node; import com.joelhelkala.watcherGui.Nodes.Node.Node;
import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.User.User;
import com.joelhelkala.watcherGui.frames.WelcomePage;
/* /*
* Class to handle httpRequests to the server * Class to handle httpRequests to the server
...@@ -261,9 +262,13 @@ public class HttpRequests { ...@@ -261,9 +262,13 @@ public class HttpRequests {
} }
int status = con.getResponseCode(); int status = con.getResponseCode();
if(status < 300) success = true;
con.disconnect(); con.disconnect();
if(status < 300) success = true;
else if (status == 403) {
WelcomePage.TimedOutSession();
// Set success to true so error dialog wont be shown
success = true;
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
......
package com.joelhelkala.watcherServer; package com.joelhelkala.watcherServer;
import com.joelhelkala.watcherServer.appuser.AppUser;
import com.joelhelkala.watcherServer.appuser.AppUserRole;
import com.joelhelkala.watcherServer.appuser.AppUserService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication @SpringBootApplication
public class WatcherServerApplication { public class WatcherServerApplication {
...@@ -14,14 +9,4 @@ public class WatcherServerApplication { ...@@ -14,14 +9,4 @@ public class WatcherServerApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(WatcherServerApplication.class, args); SpringApplication.run(WatcherServerApplication.class, args);
} }
@Bean
CommandLineRunner run(AppUserService appUserService) {
return args -> {
appUserService.addNewUser(new AppUser("masa","masa@gmail.com","1234", AppUserRole.USER));
appUserService.addNewUser(new AppUser("pena","pena@gmail.com","1234", AppUserRole.USER));
appUserService.addNewUser(new AppUser("tero","tero@gmail.com","1234", AppUserRole.USER));
appUserService.addNewUser(new AppUser("arska","arska@gmail.com","1234", AppUserRole.ADMIN));
};
}
} }
\ No newline at end of file
...@@ -11,6 +11,7 @@ import java.util.Optional; ...@@ -11,6 +11,7 @@ import java.util.Optional;
@Repository @Repository
public interface AppUserRepository extends JpaRepository<AppUser, Long> { public interface AppUserRepository extends JpaRepository<AppUser, Long> {
@Query("SELECT u FROM AppUser u WHERE u.email = ?1")
Optional<AppUser> findByEmail(String email); Optional<AppUser> findByEmail(String email);
@Transactional @Transactional
......
...@@ -39,7 +39,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter { ...@@ -39,7 +39,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter {
if(request.getServletPath().equals("/api/v1/login") || request.getServletPath().equals("/api/v1/registration")) { if(request.getServletPath().equals("/api/v1/login") || request.getServletPath().equals("/api/v1/registration")) {
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} }
// Everything else does need // Everything else does
else { else {
String authorization_header = request.getHeader(AUTHORIZATION); String authorization_header = request.getHeader(AUTHORIZATION);
if (authorization_header != null && authorization_header.startsWith("Bearer ")) { if (authorization_header != null && authorization_header.startsWith("Bearer ")) {
...@@ -65,6 +65,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter { ...@@ -65,6 +65,7 @@ public class CustomAuthorizationFilter extends OncePerRequestFilter {
Map<String, String> error = new HashMap<>(); Map<String, String> error = new HashMap<>();
error.put("error_msg", e.getMessage()); error.put("error_msg", e.getMessage());
error.put("code", "401");
response.setContentType("application/json"); response.setContentType("application/json");
new ObjectMapper().writeValue(response.getOutputStream(), error); new ObjectMapper().writeValue(response.getOutputStream(), error);
} }
......
package com.joelhelkala.watcherServer.node; package com.joelhelkala.watcherServer.node;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.Optional; import java.util.Optional;
public interface NodeRepository extends JpaRepository<Node, Long> { public interface NodeRepository extends JpaRepository<Node, Long> {
@Query("SELECT n FROM Node n WHERE n.location = ?1")
Optional<Object> findByLocation(String location);
@Query("SELECT n FROM Node n WHERE n.id = ?1")
Optional<Node> findById(Long id); Optional<Node> findById(Long id);
} }
...@@ -17,6 +17,10 @@ public class NodeService { ...@@ -17,6 +17,10 @@ public class NodeService {
// Saves a given node to database // Saves a given node to database
public Node saveNode(Node node) { public Node saveNode(Node node) {
log.info("Adding new node with name : {}, to the database", node.getLocation());
boolean exists = nodeRepository.findByLocation(node.getLocation()).isPresent();
if (exists) throw new ApiRequestException("Node with name " + node.getLocation() + " already exists");
Node entity = nodeRepository.save(node); Node entity = nodeRepository.save(node);
return entity; return entity;
} }
......
spring.datasource.url=jdbc:postgresql://localhost:5432/person spring.datasource.url=jdbc:postgresql://localhost:5432/watcher
spring.datasource.username= spring.datasource.username=
spring.datasource.password= spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment