Clean-up, disabled SQL logging
This commit is contained in:
@ -31,10 +31,8 @@ import envoy.server.net.ObjectWriteProxy;
|
||||
*/
|
||||
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
|
||||
|
||||
private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
|
||||
|
||||
@Override
|
||||
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
||||
private final PersistenceManager persistenceManager = PersistenceManager.getInstance();
|
||||
private final ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||
|
||||
@Override
|
||||
public void process(LoginCredentials input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
@ -44,10 +42,13 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
envoy.server.data.User user = getUser(input, socketId, writeProxy);
|
||||
|
||||
// Not logged in successfully
|
||||
if (user == null) return;
|
||||
ConnectionManager.getInstance().registerUser(user.getId(), socketId);
|
||||
if (user == null) {
|
||||
System.out.println("Rejecting handshake on socket " + socketId);
|
||||
return;
|
||||
}
|
||||
connectionManager.registerUser(user.getId(), socketId);
|
||||
|
||||
// notifies contacts of this users online-going and updates his status in the
|
||||
// Notifies contacts of this users online-going and updates his status in the
|
||||
// database
|
||||
user.setStatus(UserStatus.ONLINE);
|
||||
UserStatusChangeProcessor.updateUserStatus(user);
|
||||
@ -62,42 +63,47 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
System.out.println("Sending contacts...");
|
||||
writeProxy.write(socketId, contacts);
|
||||
System.out.println("Acquiring pending messages for the client...");
|
||||
List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user);
|
||||
List<Message> pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user);
|
||||
for (Message msg : pendingMessages) {
|
||||
System.out.println("Sending message " + msg.toCommonMessage().toString());
|
||||
System.out.println("Sending message " + msg.toCommonMessage());
|
||||
writeProxy.write(socketId, msg.toCommonMessage());
|
||||
msg.setReceivedDate(new Date());
|
||||
msg.setStatus(MessageStatus.RECEIVED);
|
||||
PersistenceManager.getPersistenceManager().updateMessage(msg);
|
||||
PersistenceManager.getInstance().updateMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
|
||||
|
||||
private envoy.server.data.User getUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
return credentials.isRegistration() ? newUser(credentials, socketId, writeProxy) : checkForExistingUser(credentials, socketId, writeProxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param credentials the input to evaluate
|
||||
* @param socketId the socket ID at which the client performing the handshake
|
||||
* is connected
|
||||
* @param writeProxy the {@link ObjectWriteProxy} to use if login was not
|
||||
* successful
|
||||
* @return the database user matching the login credentials
|
||||
* @throws IOException if sending the failed login back to the client failed
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
*/
|
||||
private envoy.server.data.User checkForExistingUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
envoy.server.data.User user;
|
||||
ConnectionManager connectionManager = ConnectionManager.getInstance();
|
||||
String userIdentifier = credentials.getIdentifier();
|
||||
try {
|
||||
// TODO will need to be replaced with the Identifier once implemented
|
||||
user = persistenceManager.getUserByName(userIdentifier);
|
||||
envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
|
||||
|
||||
// Checking if user is already online
|
||||
if (connectionManager.isOnline(user.getId())) {
|
||||
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
|
||||
return null;
|
||||
}
|
||||
// Evaluating the correctness of the password hash
|
||||
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash()))
|
||||
throw new InputMismatchException("User " + credentials.getIdentifier() + "tried logging in using a wrong password");
|
||||
if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) {
|
||||
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD));
|
||||
return null;
|
||||
}
|
||||
return user;
|
||||
} catch (NoResultException e) {
|
||||
// Checking if user exists
|
||||
@ -111,6 +117,10 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
|
||||
/**
|
||||
* @param credentials the credentials upon which to create the new {@link User}
|
||||
* @param socketId the socketID at which the client performing the handshake
|
||||
* is connected
|
||||
* @param writeProxy the write proxy used to notify the client about handshake
|
||||
* rejection
|
||||
* @return the newly created {@link User}
|
||||
* @throws IOException if sending the failed login back to the client failed
|
||||
* @since Envoy Server Standalone v0.1-alpha
|
||||
@ -118,7 +128,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
private envoy.server.data.User newUser(LoginCredentials credentials, long socketId, ObjectWriteProxy writeProxy) throws IOException {
|
||||
try {
|
||||
// Checking that no user already has this identifier
|
||||
PersistenceManager.getPersistenceManager().getUserByName(credentials.getIdentifier());
|
||||
PersistenceManager.getInstance().getUserByName(credentials.getIdentifier());
|
||||
// this code only gets executed if this user already exists
|
||||
writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.USER_EXISTS_ALREADY));
|
||||
return null;
|
||||
@ -130,6 +140,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
|
||||
user.setLastSeen(new Date());
|
||||
user.setStatus(User.UserStatus.ONLINE);
|
||||
user.setPasswordHash(credentials.getPasswordHash());
|
||||
user.setContacts(new ArrayList<>());
|
||||
persistenceManager.addUser(user);
|
||||
return user;
|
||||
}
|
||||
|
Reference in New Issue
Block a user