application now resets online flag and num_sessions to 0 on startup

This commit is contained in:
Rohan Sircar 2019-11-16 14:39:01 +05:30
parent 2564fdca35
commit 5a79c7026e
8 changed files with 23 additions and 92 deletions

View File

@ -1,7 +1,6 @@
package org.ros.chatto;
import org.modelmapper.ModelMapper;
import org.ros.chatto.logged.ActiveUserStore;
import org.ros.chatto.security.AuthenticationSuccessHandlerImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
@ -45,11 +44,6 @@ public class BeanConfigurations {
return messageSource;
}
@Bean
ActiveUserStore activeUserStore() {
return new ActiveUserStore();
}
// @Bean
// public Connection connection() throws SQLException
// {

View File

@ -7,7 +7,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.logged.ActiveUserStore;
import org.ros.chatto.model.ChatMessage;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.MessageCipher;
@ -51,8 +50,6 @@ public class DemoRestController {
@Autowired
ChatMessageRepository chatMessageRepository;
@Autowired
ActiveUserStore activeUserStore;
@Autowired
private UserService userService;
@GetMapping("/users")
public List<ChatUser> getAllUsers() {
@ -128,11 +125,6 @@ public class DemoRestController {
return "redirect:/users";
}
@GetMapping(value = "/loggedUsers")
public ActiveUserStore getActiveUsers() {
return activeUserStore;
}
@GetMapping(value = "/loggedUsers2")
public List<ActiveUserDTO> getOtherActiveUsers(Principal principal)
{

View File

@ -1,12 +0,0 @@
package org.ros.chatto.logged;
import java.time.Instant;
import lombok.Data;
@Data
public class ActiveUser {
private String userName;
// private Instant lastActive;
private int numSessions = 0;
}

View File

@ -1,20 +0,0 @@
package org.ros.chatto.logged;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class ActiveUserStore {
public List<String> users;
public List<ActiveUser> activeUsers;
public ActiveUserStore() {
users = new ArrayList<String>();
activeUsers = new ArrayList<>();
}
}

View File

@ -26,14 +26,11 @@ import lombok.Setter;
public class LoggedUser implements HttpSessionBindingListener {
private String username;
private ActiveUserStore activeUserStore;
public LoggedUser(String username, ActiveUserStore activeUserStore) {
public LoggedUser(String username)
{
this.username = username;
this.activeUserStore = activeUserStore;
}
public LoggedUser() {
}
@ -41,27 +38,9 @@ public class LoggedUser implements HttpSessionBindingListener {
public void valueBound(HttpSessionBindingEvent event) {
UserService userService = getUserService(event);
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
List<String> users = activeUserStore.getUsers();
List<ActiveUser> activeUsers = activeUserStore.getActiveUsers();
LoggedUser user = (LoggedUser) event.getValue();
if (!users.contains(user.getUsername())) {
users.add(user.getUsername());
}
Instant instant = Instant.now();
// Optional<ActiveUser> activeUserOptional = activeUsers.stream()
// .filter(au -> au.getUserName().equals(user.getUsername()))
// .findFirst();
// ActiveUser activeUser2 = activeUserOptional.get();
// activeUser2.setNumSessions(activeUser2.getNumSessions() + 1);
// boolean found = activeUsers.stream().anyMatch(au -> au.getUserName().equals(user.getUsername()));
// if (!found) {
// System.out.println("Test found ");
// ActiveUser activeUser = new ActiveUser();
// activeUser.setUserName(user.getUsername());
//// activeUser.setLastActive(instant);
// activeUsers.add(activeUser);
// }
ChatUser chatUser = userService.findByUserName(user.getUsername());
UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
@ -79,27 +58,13 @@ public class LoggedUser implements HttpSessionBindingListener {
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
List<String> users = activeUserStore.getUsers();
LoggedUser user = (LoggedUser) event.getValue();
List<ActiveUser> activeUsers = activeUserStore.getActiveUsers();
UserService userService = getUserService(event);
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
Instant instant = Instant.now();
if (users.contains(user.getUsername())) {
users.remove(user.getUsername());
}
activeUsers.removeIf(au -> {
int numSessions = au.getNumSessions();
if (au.getUserName().equals(user.getUsername()) && --numSessions == 0) {
au.setNumSessions(numSessions);
return true;
}
return false;
});
ChatUser chatUser = userService.findByUserName(user.getUsername());

View File

@ -14,16 +14,13 @@ import org.springframework.stereotype.Component;
@Component("myAuthenticationSuccessHandler")
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Autowired
ActiveUserStore activeUserStore;
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException {
HttpSession session = request.getSession(false);
if (session != null) {
LoggedUser user = new LoggedUser(authentication.getName(), activeUserStore);
LoggedUser user = new LoggedUser(authentication.getName());
session.setAttribute("user", user);
}
response.sendRedirect("/chat");

View File

@ -13,11 +13,15 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.ros.chatto.model.ApplicationStatus;
import org.ros.chatto.model.UserSession;
import org.ros.chatto.repository.UserSessionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.PropertySource;
@ -59,6 +63,9 @@ public class DBInitializerService {
@PersistenceContext
EntityManager entityManager;
@Autowired
private UserSessionRepository userSessionRepository;
private final String tablesCreatedKey = "tables_created";
private final String rolesPopulatedKey = "roles_populated";
@ -162,6 +169,8 @@ public class DBInitializerService {
if (getNumTables() == 0)
populateDB();
closeConnection();
resetAllUserSessions(userSessionRepository.findAll());
}
public void populateDB() throws SQLException, IOException {
@ -243,4 +252,14 @@ public class DBInitializerService {
connection.close();
}
private void resetAllUserSessions(List<UserSession> userSessionsList)
{
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
us.setNumSessions(0);
us.setOnline(false);
return us;
}).collect(Collectors.toList());
userSessionRepository.saveAll(userSessionsResetList);
}
}

View File

@ -1,5 +1,6 @@
package org.ros.chatto.service;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
@ -10,7 +11,6 @@ import javax.transaction.Transactional;
import org.ros.chatto.dto.ActiveUserDTO;
import org.ros.chatto.dto.UserRegistrationDTO;
import org.ros.chatto.logged.ActiveUserStore;
import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.Role;
import org.ros.chatto.model.UserRole;
@ -23,8 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.time.Duration;
@Service
public class UserServiceImpl implements UserService {
@Autowired
@ -45,8 +43,6 @@ public class UserServiceImpl implements UserService {
@Autowired
private UserSessionRepository userSessionRepository;
@Autowired
private ActiveUserStore activeUserStore;
@Override
@Transactional