From d9175721cc1e7917271650a3f6cb06364f470302 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 14 Jun 2020 16:30:46 +0200 Subject: [PATCH 01/11] Apply suggestions from code review Co-authored-by: delvh --- drop_all_tables.sql | 2 +- src/main/java/envoy/server/Startup.java | 2 +- src/main/java/envoy/server/data/Contact.java | 2 +- .../java/envoy/server/data/PersistenceManager.java | 8 ++++---- .../java/envoy/server/net/ObjectWriteProxy.java | 1 - .../server/processors/GroupMessageProcessor.java | 1 - .../envoy/server/processors/MessageProcessor.java | 13 ++++--------- .../processors/MessageStatusChangeProcessor.java | 4 ++-- 8 files changed, 13 insertions(+), 20 deletions(-) diff --git a/drop_all_tables.sql b/drop_all_tables.sql index 71f8a44..9eb29c1 100644 --- a/drop_all_tables.sql +++ b/drop_all_tables.sql @@ -2,4 +2,4 @@ DROP SCHEMA public CASCADE; CREATE SCHEMA public; GRANT ALL ON SCHEMA public TO postgres; GRANT ALL ON SCHEMA public TO envoy; -GRANT ALL ON SCHEMA public TO public; \ No newline at end of file +GRANT ALL ON SCHEMA public TO public; diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 63415aa..9ad4bc3 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -38,7 +38,7 @@ public class Startup { final var items = new HashMap>(); items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", File::new, new File(System.getProperty("user.home"), ".envoy-server"), true)); - items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.SEVERE, true)); + items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", Level::parse, Level.WARNING, true)); items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", Level::parse, Level.FINEST, true)); final var config = new Config(); diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index b6bc402..475b3ef 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -40,7 +40,7 @@ public abstract class Contact { @Temporal(TemporalType.TIMESTAMP) private Date creationDate; - @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @ManyToMany(fetch = FetchType.EAGER) protected Set contacts; /** diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index d4c9929..26a7240 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -117,7 +117,7 @@ public class PersistenceManager { * Searches for a {@link User} with a specific ID. * * @param id the id to search for - * @return the user with the specified id + * @return the user with the specified ID or {@code null} if none was found * @since Envoy Server Standalone v0.1-alpha */ public User getUserByID(long id) { return entityManager.find(User.class, id); } @@ -126,7 +126,7 @@ public class PersistenceManager { * Searches for a {@link Group} with a specific ID. * * @param id the id to search for - * @return the group with the specific id + * @return the group with the specified ID or {@code null} if none was found * @since Envoy Server Standalone v0.1-beta */ public Group getGroupByID(long id) { return entityManager.find(Group.class, id); } @@ -135,7 +135,7 @@ public class PersistenceManager { * Searches for a {@link Contact} with a specific ID. * * @param id the id to search for - * @return the contact with the specific id + * @return the contact with the specified ID or {@code null} if none was found * @since Envoy Server Standalone v0.1-beta */ public Contact getContactByID(long id) { return entityManager.find(Contact.class, id); } @@ -166,7 +166,7 @@ public class PersistenceManager { * Searches for a {@link Message} with a specific id. * * @param id the id to search for - * @return the message with the specified id + * @return the message with the specified ID or {@code null} if none is found * @since Envoy Server Standalone v0.1-alpha */ public Message getMessageByID(long id) { return entityManager.find(Message.class, id); } diff --git a/src/main/java/envoy/server/net/ObjectWriteProxy.java b/src/main/java/envoy/server/net/ObjectWriteProxy.java index eea2908..1e1c76b 100755 --- a/src/main/java/envoy/server/net/ObjectWriteProxy.java +++ b/src/main/java/envoy/server/net/ObjectWriteProxy.java @@ -43,7 +43,6 @@ public class ObjectWriteProxy { // Create message targeted at the client final Message response = writeProxy.getMessage(); response.socketId = recipientSocketID; - logger.fine("Sending " + obj); // Serialize object to byte array diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 087e2c0..89b5527 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -38,7 +38,6 @@ public class GroupMessageProcessor implements ObjectProcessor { // Checks if all memberMessageStatuses are RECEIVED and if so sets the // groupMessage Status to RECEIVED. - if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) groupMessage.setStatus(MessageStatus.RECEIVED); members.forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index a326d8c..294a34d 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -31,19 +31,14 @@ public class MessageProcessor implements ObjectProcessor { @Override public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { + if (message.getStatus!=MessageStatus.WAITING) { + logger.warning("Received message with invalid status: " + message); + return; + } message.nextStatus(); ConnectionManager connectionManager = ConnectionManager.getInstance(); sendToUser(connectionManager, message, writeProxy); - if (message.getStatus() != MessageStatus.SENT) { - // Sending a messageStatusChangeEvent to the sender - try { - writeProxy.write(socketID, new MessageStatusChangeEvent(message)); - } catch (IOException e) { - logger.warning("Could not send messageStatusChangeEvent to the sender of this message with ID: " + message.getID()); - e.printStackTrace(); - } - } try { PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); } catch (EntityExistsException e) { diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index d88e120..242f627 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -33,8 +33,8 @@ public class MessageStatusChangeProcessor implements ObjectProcessor Date: Sun, 14 Jun 2020 16:40:51 +0200 Subject: [PATCH 02/11] Some refactorings based on suggestions from @delvh --- .../envoy/server/data/PersistenceManager.java | 20 +++++++++---------- .../envoy/server/net/ConnectionManager.java | 14 ++++--------- .../processors/ContactOperationProcessor.java | 6 +++--- .../processors/ContactSearchProcessor.java | 2 +- .../processors/GroupCreationProcessor.java | 6 +++--- .../processors/GroupMessageProcessor.java | 8 +++----- .../processors/GroupResizeProcessor.java | 2 +- .../processors/LoginCredentialProcessor.java | 2 +- .../server/processors/MessageProcessor.java | 5 ++--- .../MessageStatusChangeProcessor.java | 2 +- .../processors/UserStatusChangeProcessor.java | 2 +- src/main/resources/META-INF/persistence.xml | 2 +- 12 files changed, 31 insertions(+), 40 deletions(-) diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 26a7240..0221fe9 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -213,26 +213,26 @@ public class PersistenceManager { } /** - * Adds a user to the contact list of another user and vice versa. + * Adds a contact to the contact list of another contact and vice versa. * - * @param userId1 the ID of the first user - * @param userId2 the ID of the second user + * @param contactID1 the ID of the first contact + * @param contactID2 the ID of the second contact * @since Envoy Server Standalone v0.1-alpha */ - public void addUserContact(long userId1, long userId2) { + public void addContactBidirectional(long contactID1, long contactID2) { // Get users by ID - Contact u1 = getContactByID(userId1); - Contact u2 = getContactByID(userId2); + Contact c1 = getContactByID(contactID1); + Contact c2 = getContactByID(contactID2); // Add users to each others contact lists - u1.getContacts().add(u2); - u2.getContacts().add(u1); + c1.getContacts().add(c2); + c2.getContacts().add(c1); // Synchronize changes with the database transaction.begin(); - entityManager.merge(u1); - entityManager.merge(u2); + entityManager.merge(c1); + entityManager.merge(c2); transaction.commit(); } diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index 03f3810..6d9beea 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -1,10 +1,6 @@ package envoy.server.net; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import com.jenkov.nioserver.ISocketIdListener; @@ -53,7 +49,7 @@ public class ConnectionManager implements ISocketIdListener { public void socketCancelled(long socketID) { if (!pendingSockets.remove(socketID)) { // Notify contacts of this users offline-going - envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketId(socketID)); + envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketID(socketID)); user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); UserStatusChangeProcessor.updateUserStatus(user); @@ -83,14 +79,14 @@ public class ConnectionManager implements ISocketIdListener { * @return the ID of the socket * @since Envoy Server Standalone v0.1-alpha */ - public long getSocketId(long userID) { return sockets.get(userID); } + public long getSocketID(long userID) { return sockets.get(userID); } /** * @param socketID the id of the socket whose User is needed * @return the userId associated with this socketId * @since Envoy Server Standalone v0.1-alpha */ - public long getUserIdBySocketId(long socketID) { + public long getUserIdBySocketID(long socketID) { return sockets.entrySet().stream().filter(entry -> entry.getValue().equals(socketID)).findFirst().get().getKey(); } @@ -108,8 +104,6 @@ public class ConnectionManager implements ISocketIdListener { public Set getOnlineUsers() { return sockets.keySet(); } /** - * Returns all members of a group who are currently online. - * * @param group the group to search for * @return a set of all IDs of currently active members in this group * @since Envoy Server Standalone v0.1-beta diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index c47e516..9e181ae 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -27,15 +27,15 @@ public class ContactOperationProcessor implements ObjectProcessor()); input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); - group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketId(socketID))); + group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketID(socketID))); group.getContacts().forEach(c -> c.getContacts().add(group)); - group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketId(socketID))); + group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketID(socketID))); persistenceManager.addContact(group); group.getContacts() .stream() .map(Contact::getID) .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) + .map(connectionManager::getSocketID) .forEach(memberSocketID -> { try { writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD)); diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 89b5527..809df61 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -31,10 +31,8 @@ public class GroupMessageProcessor implements ObjectProcessor { ConnectionManager connectionManager = ConnectionManager.getInstance(); final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); - for (long i = 0; i < groupMessage.getMemberStatuses().size(); i++) { - groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT); - } - members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); }); + groupMessage.getMemberStatuses().replaceAll((id, oldStatus) -> MessageStatus.SENT); + members.forEach(user -> setMemberStatus(connectionManager, groupMessage, user.getID())); // Checks if all memberMessageStatuses are RECEIVED and if so sets the // groupMessage Status to RECEIVED. @@ -51,7 +49,7 @@ public class GroupMessageProcessor implements ObjectProcessor { private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) { if (connectionManager.isOnline(memberID)) try { // If recipient is online, send the groupMessage directly - writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); + writeProxy.write(connectionManager.getSocketID(memberID), groupMessage); } catch (IOException e) { logger.warning("Recipient online. Failed to send message" + groupMessage.getID()); e.printStackTrace(); diff --git a/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/src/main/java/envoy/server/processors/GroupResizeProcessor.java index dc99535..79c2be6 100644 --- a/src/main/java/envoy/server/processors/GroupResizeProcessor.java +++ b/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -46,7 +46,7 @@ public class GroupResizeProcessor implements ObjectProcessor { .stream() .map(Contact::getID) .filter(connectionManager::isOnline) - .map(connectionManager::getSocketId) + .map(connectionManager::getSocketID) .forEach(memberSocketID -> { try { writeProxy.write(memberSocketID, commonGroup); diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 7c12ec2..7e33f2b 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -69,7 +69,7 @@ public class LoginCredentialProcessor implements ObjectProcessor { @Override public void process(Message message, long socketID, ObjectWriteProxy writeProxy) { - if (message.getStatus!=MessageStatus.WAITING) { + if (message.getStatus() != MessageStatus.WAITING) { logger.warning("Received message with invalid status: " + message); return; } @@ -49,7 +48,7 @@ public class MessageProcessor implements ObjectProcessor { private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) { if (connectionManager.isOnline(message.getRecipientID())) try { // If recipient is online, send the message directly - writeProxy.write(connectionManager.getSocketId(message.getRecipientID()), message); + writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); // Update the message status to RECEIVED message.setReceivedDate(new Date()); message.nextStatus(); diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 242f627..2ca7c46 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -34,7 +34,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor - \ No newline at end of file + From 872b71fbe9791083f601ef8072c95aa0906b8529 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 15 Jun 2020 11:59:48 +0200 Subject: [PATCH 03/11] Fix JPA validator warnings with explicit column names Due to a bug in the JPA validator columns with camel case names are flagged as missing (probably due to the case-insensitive nature of SQL). This has been circumvented by assigning every column with a camel case name a new name with underscores. The inheritance strategy of the Contacts class has been changed to single table for performance reasons. --- src/main/java/envoy/server/data/Contact.java | 15 +++------------ src/main/java/envoy/server/data/Group.java | 8 -------- src/main/java/envoy/server/data/GroupMessage.java | 7 ++----- src/main/java/envoy/server/data/Message.java | 6 ++++-- src/main/java/envoy/server/data/User.java | 3 ++- 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index 475b3ef..7ad1d6b 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -3,17 +3,7 @@ package envoy.server.data; import java.util.Date; import java.util.Set; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.ManyToMany; -import javax.persistence.Table; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; /** * This class acts as a superclass for all contacts, being {@link User}s and @@ -29,7 +19,7 @@ import javax.persistence.TemporalType; @Entity @Table(name = "contacts") -@Inheritance(strategy = InheritanceType.JOINED) +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) public abstract class Contact { @Id @@ -37,6 +27,7 @@ public abstract class Contact { protected long id; protected String name; + @Column(name = "creation_date") @Temporal(TemporalType.TIMESTAMP) private Date creationDate; diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 5f3be22..bad4db9 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -5,7 +5,6 @@ import java.util.stream.Collectors; import javax.persistence.Entity; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; -import javax.persistence.Table; /** * Represents a group inside the database. Referred to as "server group" as @@ -19,7 +18,6 @@ import javax.persistence.Table; * @since Envoy Server Standalone v0.1-alpha */ @Entity -@Table(name = "groups") @NamedQueries({ @NamedQuery( name = Group.findByName, @@ -46,17 +44,11 @@ public class Group extends Contact { */ public static final String findPendingGroups = "Group.findPendingGroups"; - /** - * {@inheritDoc} - */ @Override public envoy.data.Group toCommon() { return new envoy.data.Group(id, name, contacts.parallelStream().map(User.class::cast).map(User::toFlatCommon).collect(Collectors.toSet())); } - /** - * {@inheritDoc} - */ @Override protected envoy.data.Group toFlatCommon() { return toCommon(); } } diff --git a/src/main/java/envoy/server/data/GroupMessage.java b/src/main/java/envoy/server/data/GroupMessage.java index 4c64cf7..13dcc49 100644 --- a/src/main/java/envoy/server/data/GroupMessage.java +++ b/src/main/java/envoy/server/data/GroupMessage.java @@ -3,10 +3,7 @@ package envoy.server.data; import java.util.Date; import java.util.Map; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.Temporal; -import javax.persistence.TemporalType; +import javax.persistence.*; import envoy.data.MessageBuilder; @@ -24,6 +21,7 @@ public class GroupMessage extends Message { @ElementCollection private Map memberMessageStatus; + @Column(name = "last_status_change_date") @Temporal(TemporalType.TIMESTAMP) protected Date lastStatusChangeDate; @@ -64,7 +62,6 @@ public class GroupMessage extends Message { .setForwarded(forwarded) .setStatus(status) .setText(text) - // .setAttachment(attachment) TODO make this work .buildGroupMessage((envoy.data.Group) recipient.toCommon(), memberMessageStatus); groupMessage.setReceivedDate(receivedDate); groupMessage.setReadDate(readDate); diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 9203a4f..8a0a0ca 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -49,12 +49,15 @@ public class Message { @JoinColumn protected Contact recipient; + @Column(name = "creation_date") @Temporal(TemporalType.TIMESTAMP) protected Date creationDate; + @Column(name = "received_date") @Temporal(TemporalType.TIMESTAMP) protected Date receivedDate; + @Column(name = "read_date") @Temporal(TemporalType.TIMESTAMP) protected Date readDate; @@ -88,7 +91,7 @@ public class Message { sender = persistenceManager.getUserByID(message.getSenderID()); recipient = persistenceManager.getContactByID(message.getRecipientID()); forwarded = message.isForwarded(); - // TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET + // TODO: Attachment } /** @@ -104,7 +107,6 @@ public class Message { .setDate(creationDate) .setStatus(status) .setForwarded(forwarded) - // .setAttachment(attachment) TODO make this work .build(); message.setReceivedDate(receivedDate); message.setReadDate(readDate); diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index f1076c1..99941f4 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -22,7 +22,6 @@ import envoy.data.User.UserStatus; * @since Envoy Server Standalone v0.1-alpha */ @Entity -@Table(name = "users") @NamedQueries({ @NamedQuery( name = User.findByName, @@ -63,8 +62,10 @@ public class User extends Contact { */ public static final String searchByName = "User.searchByName"; + @Column(name = "password_hash") private byte[] passwordHash; + @Column(name = "last_seen") @Temporal(TemporalType.TIMESTAMP) private Date lastSeen; From 5bc88634daac8c93d40cefdf00c248f3671c63cf Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 15 Jun 2020 12:17:26 +0200 Subject: [PATCH 04/11] Create CODEOWNERS --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..8ff7fe0 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @CyB3RC0nN0R From 3660dec8598d045d08b58672302e1a3304b6a7ce Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 20 Jun 2020 09:57:54 +0200 Subject: [PATCH 05/11] Shorten event names, refactor, prepare compatibility verification --- pom.xml | 2 +- src/main/java/envoy/server/Startup.java | 14 ++ src/main/java/envoy/server/data/Contact.java | 14 +- src/main/java/envoy/server/data/Message.java | 29 +-- .../envoy/server/data/PersistenceManager.java | 4 +- src/main/java/envoy/server/data/User.java | 15 +- .../envoy/server/net/ConnectionManager.java | 3 +- .../processors/ContactOperationProcessor.java | 10 +- .../processors/GroupCreationProcessor.java | 16 +- .../processors/GroupResizeProcessor.java | 16 +- .../IDGeneratorRequestProcessor.java | 8 +- .../processors/LoginCredentialProcessor.java | 190 ++++++++---------- .../server/processors/MessageProcessor.java | 4 +- .../MessageStatusChangeProcessor.java | 18 +- .../processors/NameChangeProcessor.java | 18 +- .../processors/UserStatusChangeProcessor.java | 20 +- 16 files changed, 179 insertions(+), 202 deletions(-) diff --git a/pom.xml b/pom.xml index 046ec55..0612482 100755 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~compatibility_verification-SNAPSHOT com.github.informatik-ag-ngl diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index 9ad4bc3..b88f1bd 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -29,6 +29,20 @@ import envoy.util.EnvoyLog; */ public class Startup { + /** + * The minimal client version compatible with this server. + * + * @since Envoy Server Standalone v0.1-beta + */ + public static final String MIN_CLIENT_VERSION = "0.1-beta"; + + /** + * The maximal client version compatible with this server. + * + * @since Envoy Server Standalone v0.1-beta + */ + public static final String MAX_CLIENT_VERSION = "0.1-beta"; + /** * Initializes the logger with a new config instance. * diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index 7ad1d6b..c802837 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -1,6 +1,6 @@ package envoy.server.data; -import java.util.Date; +import java.time.LocalDateTime; import java.util.Set; import javax.persistence.*; @@ -28,8 +28,7 @@ public abstract class Contact { protected String name; @Column(name = "creation_date") - @Temporal(TemporalType.TIMESTAMP) - private Date creationDate; + private LocalDateTime creationDate; @ManyToMany(fetch = FetchType.EAGER) protected Set contacts; @@ -95,17 +94,14 @@ public abstract class Contact { * @return the creationDate * @since Envoy Server Standalone v0.1-beta */ - public Date getCreationDate() { return creationDate; } + public LocalDateTime getCreationDate() { return creationDate; } /** * @param creationDate the creationDate to set * @since Envoy Server Standalone v0.1-beta */ - public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } + public void setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; } - /** - * {@inheritDoc} - */ @Override - public String toString() { return String.format("%s[id=%d,name=%s, contacts=%s]", getClass().getSimpleName(), id, name, contacts); } + public String toString() { return String.format("%s[id=%d,name=%s, %d contact(s)]", getClass().getSimpleName(), id, name, contacts.size()); } } diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 8a0a0ca..e53a7e5 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -1,9 +1,10 @@ package envoy.server.data; -import java.util.Date; +import java.time.LocalDateTime; import javax.persistence.*; +import envoy.data.Message.MessageStatus; import envoy.data.MessageBuilder; /** @@ -50,16 +51,13 @@ public class Message { protected Contact recipient; @Column(name = "creation_date") - @Temporal(TemporalType.TIMESTAMP) - protected Date creationDate; + protected LocalDateTime creationDate; @Column(name = "received_date") - @Temporal(TemporalType.TIMESTAMP) - protected Date receivedDate; + protected LocalDateTime receivedDate; @Column(name = "read_date") - @Temporal(TemporalType.TIMESTAMP) - protected Date readDate; + protected LocalDateTime readDate; protected String text; protected envoy.data.Message.MessageStatus status; @@ -113,6 +111,11 @@ public class Message { return message; } + public void received() { + receivedDate = LocalDateTime.now(); + status = MessageStatus.RECEIVED; + } + /** * @return the id of a {link envoy.data.Message} * @since Envoy Server Standalone v0.1-alpha @@ -156,41 +159,41 @@ public class Message { * @return the date at which a {link envoy.data.Message} has been created * @since Envoy Server Standalone v0.1-alpha */ - public Date getCreationDate() { return creationDate; } + public LocalDateTime getCreationDate() { return creationDate; } /** * @param creationDate the creation date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getCreationDate() */ - public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } + public void setCreationDate(LocalDateTime creationDate) { this.creationDate = creationDate; } /** * @return the date at which a {link envoy.data.Message} has been received by * the server * @since Envoy Server Standalone v0.1-alpha */ - public Date getReceivedDate() { return receivedDate; } + public LocalDateTime getReceivedDate() { return receivedDate; } /** * @param receivedDate the received date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getReceivedDate() */ - public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; } + public void setReceivedDate(LocalDateTime receivedDate) { this.receivedDate = receivedDate; } /** * @return the date at which a {link envoy.data.Message} has been read * @since Envoy Server Standalone v0.1-alpha */ - public Date getReadDate() { return readDate; } + public LocalDateTime getReadDate() { return readDate; } /** * @param readDate the read date to set * @since Envoy Server Standalone v0.1-alpha * @see Message#getReadDate() */ - public void setReadDate(Date readDate) { this.readDate = readDate; } + public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; } /** * @return the status of a {link envoy.data.Message} diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 0221fe9..1e99978 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -1,6 +1,6 @@ package envoy.server.data; -import java.util.Date; +import java.time.LocalDateTime; import java.util.List; import javax.persistence.EntityManager; @@ -38,7 +38,7 @@ public class PersistenceManager { .getOnlineUsers() .stream() .map(this::getUserByID) - .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); entityManager.merge(user); }); + .forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(LocalDateTime.now()); entityManager.merge(user); }); transaction.commit(); })); } diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 99941f4..9a1b03f 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -1,6 +1,6 @@ package envoy.server.data; -import java.util.Date; +import java.time.LocalDateTime; import java.util.Set; import java.util.stream.Collectors; @@ -66,22 +66,15 @@ public class User extends Contact { private byte[] passwordHash; @Column(name = "last_seen") - @Temporal(TemporalType.TIMESTAMP) - private Date lastSeen; + private LocalDateTime lastSeen; private UserStatus status; - /** - * {@inheritDoc} - */ @Override public envoy.data.User toCommon() { return new envoy.data.User(id, name, status, contacts.parallelStream().map(Contact::toFlatCommon).collect(Collectors.toSet())); } - /** - * {@inheritDoc} - */ @Override protected envoy.data.User toFlatCommon() { return new envoy.data.User(id, name, status, Set.of()); } @@ -101,13 +94,13 @@ public class User extends Contact { * @return the last date the user has been online * @since Envoy Server Standalone v0.1-alpha */ - public Date getLastSeen() { return lastSeen; } + public LocalDateTime getLastSeen() { return lastSeen; } /** * @param lastSeen the latest date at which the user has been online to set * @since Envoy Server Standalone v0.1-alpha */ - public void setLastSeen(Date lastSeen) { this.lastSeen = lastSeen; } + public void setLastSeen(LocalDateTime lastSeen) { this.lastSeen = lastSeen; } /** * @return the status diff --git a/src/main/java/envoy/server/net/ConnectionManager.java b/src/main/java/envoy/server/net/ConnectionManager.java index 6d9beea..cb2d818 100755 --- a/src/main/java/envoy/server/net/ConnectionManager.java +++ b/src/main/java/envoy/server/net/ConnectionManager.java @@ -1,5 +1,6 @@ package envoy.server.net; +import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; @@ -51,7 +52,7 @@ public class ConnectionManager implements ISocketIdListener { // Notify contacts of this users offline-going envoy.server.data.User user = PersistenceManager.getInstance().getUserByID(getUserIdBySocketID(socketID)); user.setStatus(UserStatus.OFFLINE); - user.setLastSeen(new Date()); + user.setLastSeen(LocalDateTime.now()); UserStatusChangeProcessor.updateUserStatus(user); // Remove the socket diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java index 9e181ae..febd409 100755 --- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.util.logging.Logger; import envoy.event.ElementOperation; -import envoy.event.contact.ContactOperationEvent; +import envoy.event.contact.ContactOperation; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; @@ -18,13 +18,13 @@ import envoy.util.EnvoyLog; * @author Kai S. K. Engelbart * @since Envoy Server Standalone v0.1-alpha */ -public class ContactOperationProcessor implements ObjectProcessor { +public class ContactOperationProcessor implements ObjectProcessor { private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final Logger logger = EnvoyLog.getLogger(ContactOperationProcessor.class); @Override - public void process(ContactOperationEvent evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { + public void process(ContactOperation evt, long socketId, ObjectWriteProxy writeProxy) throws IOException { switch (evt.getOperationType()) { case ADD: final long userID = ConnectionManager.getInstance().getUserIdBySocketID(socketId); @@ -36,7 +36,7 @@ public class ContactOperationProcessor implements ObjectProcessor getInputClass() { return ContactOperationEvent.class; } + public Class getInputClass() { return ContactOperation.class; } } diff --git a/src/main/java/envoy/server/processors/GroupCreationProcessor.java b/src/main/java/envoy/server/processors/GroupCreationProcessor.java index 9ea11fe..bf82ec4 100644 --- a/src/main/java/envoy/server/processors/GroupCreationProcessor.java +++ b/src/main/java/envoy/server/processors/GroupCreationProcessor.java @@ -4,8 +4,8 @@ import java.io.IOException; import java.util.HashSet; import envoy.event.ElementOperation; -import envoy.event.GroupCreationEvent; -import envoy.event.contact.ContactOperationEvent; +import envoy.event.GroupCreation; +import envoy.event.contact.ContactOperation; import envoy.server.data.Contact; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; @@ -19,17 +19,17 @@ import envoy.server.net.ObjectWriteProxy; * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-beta */ -public class GroupCreationProcessor implements ObjectProcessor { +public class GroupCreationProcessor implements ObjectProcessor { private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance(); @Override - public void process(GroupCreationEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(GroupCreation groupCreation, long socketID, ObjectWriteProxy writeProxy) throws IOException { envoy.server.data.Group group = new envoy.server.data.Group(); - group.setName(input.get()); + group.setName(groupCreation.get()); group.setContacts(new HashSet<>()); - input.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); + groupCreation.getInitialMemberIDs().stream().map(persistenceManager::getUserByID).forEach(group.getContacts()::add); group.getContacts().add(persistenceManager.getContactByID(connectionManager.getUserIdBySocketID(socketID))); group.getContacts().forEach(c -> c.getContacts().add(group)); group.getContacts().add(persistenceManager.getUserByID(connectionManager.getUserIdBySocketID(socketID))); @@ -41,7 +41,7 @@ public class GroupCreationProcessor implements ObjectProcessor { try { - writeProxy.write(memberSocketID, new ContactOperationEvent(group.toCommon(), ElementOperation.ADD)); + writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD)); } catch (IOException e) { e.printStackTrace(); } @@ -49,5 +49,5 @@ public class GroupCreationProcessor implements ObjectProcessor getInputClass() { return GroupCreationEvent.class; } + public Class getInputClass() { return GroupCreation.class; } } diff --git a/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/src/main/java/envoy/server/processors/GroupResizeProcessor.java index 79c2be6..e90b56f 100644 --- a/src/main/java/envoy/server/processors/GroupResizeProcessor.java +++ b/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -2,7 +2,7 @@ package envoy.server.processors; import java.io.IOException; -import envoy.event.GroupResizeEvent; +import envoy.event.GroupResize; import envoy.server.data.Contact; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; @@ -16,24 +16,24 @@ import envoy.server.net.ObjectWriteProxy; * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-beta */ -public class GroupResizeProcessor implements ObjectProcessor { +public class GroupResizeProcessor implements ObjectProcessor { private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); @Override - public void process(GroupResizeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(GroupResize groupResize, long socketID, ObjectWriteProxy writeProxy) throws IOException { // Acquire the group to resize from the database - var group = persistenceManager.getGroupByID(input.getGroupID()); + var group = persistenceManager.getGroupByID(groupResize.getGroupID()); // Perform the desired operation - switch (input.getOperation()) { + switch (groupResize.getOperation()) { case ADD: - group.getContacts().add(persistenceManager.getUserByID(input.get().getID())); + group.getContacts().add(persistenceManager.getUserByID(groupResize.get().getID())); break; case REMOVE: - group.getContacts().remove(persistenceManager.getUserByID(input.get().getID())); + group.getContacts().remove(persistenceManager.getUserByID(groupResize.get().getID())); break; } @@ -57,5 +57,5 @@ public class GroupResizeProcessor implements ObjectProcessor { } @Override - public Class getInputClass() { return GroupResizeEvent.class; } + public Class getInputClass() { return GroupResize.class; } } diff --git a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java index e30f18d..c2f423a 100755 --- a/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java +++ b/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java @@ -1,14 +1,12 @@ package envoy.server.processors; import java.io.IOException; -import java.util.logging.Logger; import envoy.data.IDGenerator; import envoy.event.IDGeneratorRequest; import envoy.server.data.ConfigItem; import envoy.server.data.PersistenceManager; import envoy.server.net.ObjectWriteProxy; -import envoy.util.EnvoyLog; /** * Project: envoy-server-standalone
@@ -22,17 +20,13 @@ import envoy.util.EnvoyLog; public class IDGeneratorRequestProcessor implements ObjectProcessor { private static final long ID_RANGE = 200; - private static final Logger logger = EnvoyLog.getLogger(IDGeneratorRequestProcessor.class); @Override public Class getInputClass() { return IDGeneratorRequest.class; } @Override public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { - logger.fine("Received id generation request."); - var generator = createIDGenerator(); - logger.info("Sending new id generator " + generator); - writeProxy.write(socketID, generator); + writeProxy.write(socketID, createIDGenerator()); } /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 7e33f2b..f837656 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,19 +1,23 @@ package envoy.server.processors; +import static envoy.data.User.UserStatus.ONLINE; +import static envoy.event.HandshakeRejection.*; + import java.io.IOException; -import java.util.*; +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.HashSet; import java.util.logging.Logger; import javax.persistence.NoResultException; import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; -import envoy.data.User; -import envoy.data.User.UserStatus; -import envoy.event.HandshakeRejectionEvent; -import envoy.event.MessageStatusChangeEvent; -import envoy.server.data.Message; +import envoy.event.HandshakeRejection; +import envoy.event.MessageStatusChange; +import envoy.server.Startup; import envoy.server.data.PersistenceManager; +import envoy.server.data.User; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; import envoy.util.EnvoyLog; @@ -29,126 +33,98 @@ import envoy.util.EnvoyLog; * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha */ -public class LoginCredentialProcessor implements ObjectProcessor { +public final class LoginCredentialProcessor implements ObjectProcessor { private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class); + // private static final Pattern versionPattern = + // Pattern.compile("v(?\\d).(?\\d)(?:-(?\\w+))?"); @Override - public void process(LoginCredentials input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { + + // Cache this write proxy for user-independant notifications UserStatusChangeProcessor.setWriteProxy(writeProxy); - logger.info(String.format("Received login credentials %s from socket ID %d", input, socketID)); - envoy.server.data.User user = getUser(input, socketID, writeProxy); - - // Not logged in successfully - if (user == null) { - logger.info("Rejecting handshake on socket " + socketID); - return; + // TODO: Verify compatibility + if (Startup.MIN_CLIENT_VERSION.compareTo(credentials.getClientVersion()) <= 0 + && Startup.MAX_CLIENT_VERSION.compareTo(credentials.getClientVersion()) >= 0) { + // writeProxy.write(socketID, new HandshakeRejectionEvent("Wrong version")); + // return; } + + // Acquire a user object (or reject the handshake if that's impossible) + User user = null; + if (!credentials.isRegistration()) { + try { + user = persistenceManager.getUserByName(credentials.getIdentifier()); + + // Checking if user is already online + if (connectionManager.isOnline(user.getID())) { + logger.warning(user + " is already online!"); + writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR)); + return; + } + // Evaluating the correctness of the password hash + if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) { + logger.info(user + " has entered the wrong password."); + writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER)); + return; + } + } catch (NoResultException e) { + logger.info("The requested user does not exist."); + writeProxy.write(socketID, new HandshakeRejection(WRONG_PASSWORD_OR_USER)); + return; + } + } else { + try { + // Checking that no user already has this identifier + PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); + + // This code only gets executed if this user already exists + logger.info("The requested user already exists."); + writeProxy.write(socketID, new HandshakeRejection(INTERNAL_ERROR)); + return; + } catch (NoResultException e) { + // Creation of a new user + user = new User(); + user.setName(credentials.getIdentifier()); + user.setLastSeen(LocalDateTime.now()); + user.setStatus(ONLINE); + user.setPasswordHash(credentials.getPasswordHash()); + user.setContacts(new HashSet<>()); + persistenceManager.addContact(user); + logger.info("Registered new " + user); + } + } + + logger.info(user + " successfully authenticated."); + connectionManager.registerUser(user.getID(), socketID); - // Notifies contacts of this users online-going and updates his status in the - // database - user.setStatus(UserStatus.ONLINE); + // Change status and notify contacts about it + user.setStatus(ONLINE); UserStatusChangeProcessor.updateUserStatus(user); - // Complete handshake - logger.fine("Sending user..."); + // Complete the handshake writeProxy.write(socketID, user.toCommon()); - logger.fine("Acquiring pending messages for the client..."); - List pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); - for (Message msg : pendingMessages) + + final var pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); + logger.fine("Sending " + pendingMessages.size() + " pending messages to " + user + "..."); + for (var msg : pendingMessages) { + final var msgCommon = msg.toCommon(); if (msg.getStatus() == MessageStatus.SENT) { - logger.info("Sending message " + msg.toCommon()); - writeProxy.write(socketID, msg.toCommon()); - msg.setReceivedDate(new Date()); - msg.setStatus(MessageStatus.RECEIVED); - if (connectionManager.isOnline(msg.getSender().getID())) { - var evt = new MessageStatusChangeEvent(msg.toCommon()); - logger.info("Sending messageStatusChangeEvent to sender " + evt); - writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), evt); - } + writeProxy.write(socketID, msgCommon); + msg.received(); + if (connectionManager.isOnline(msg.getSender().getID())) + writeProxy.write(connectionManager.getSocketID(msg.getSender().getID()), new MessageStatusChange(msgCommon)); PersistenceManager.getInstance().updateMessage(msg); - } else { - var evt = new MessageStatusChangeEvent(msg.toCommon()); - logger.info("Sending messageStatusChangeEvent " + evt); - writeProxy.write(socketID, evt); - } + } else writeProxy.write(socketID, new MessageStatusChange(msgCommon)); + } } @Override public Class 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 { - try { - 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.INTERNAL_ERROR)); - return null; - } - // Evaluating the correctness of the password hash - if (!Arrays.equals(credentials.getPasswordHash(), user.getPasswordHash())) { - writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD_OR_USER)); - return null; - } - return user; - } catch (NoResultException e) { - // Checking if user exists - writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.INTERNAL_ERROR)); - } catch (InputMismatchException e) { - // Checking if the given password hash is correct - writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.WRONG_PASSWORD_OR_USER)); - } - return null; - } - - /** - * @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 - */ - private envoy.server.data.User newUser(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { - try { - // Checking that no user already has this identifier - PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); - // this code only gets executed if this user already exists - writeProxy.write(socketID, new HandshakeRejectionEvent(HandshakeRejectionEvent.INTERNAL_ERROR)); - return null; - } catch (NoResultException e) { - // Creation of a new user - envoy.server.data.User user; - user = new envoy.server.data.User(); - user.setName(credentials.getIdentifier()); - user.setLastSeen(new Date()); - user.setStatus(User.UserStatus.ONLINE); - user.setPasswordHash(credentials.getPasswordHash()); - user.setContacts(new HashSet<>()); - persistenceManager.addContact(user); - return user; - } - } } diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 42c111a..8622cd8 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -1,7 +1,7 @@ package envoy.server.processors; import java.io.IOException; -import java.util.Date; +import java.time.LocalDateTime; import java.util.logging.Logger; import javax.persistence.EntityExistsException; @@ -50,7 +50,7 @@ public class MessageProcessor implements ObjectProcessor { // If recipient is online, send the message directly writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); // Update the message status to RECEIVED - message.setReceivedDate(new Date()); + message.setReceivedDate(LocalDateTime.now()); message.nextStatus(); } catch (IOException e) { logger.warning("Recipient online. Failed to send message" + message.getID()); diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 2ca7c46..a3dca3c 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -3,7 +3,7 @@ package envoy.server.processors; import java.io.IOException; import envoy.data.Message.MessageStatus; -import envoy.event.MessageStatusChangeEvent; +import envoy.event.MessageStatusChange; import envoy.exception.EnvoyException; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; @@ -17,26 +17,26 @@ import envoy.server.net.ObjectWriteProxy; * @author Leon Hofmeister * @since Envoy Server Standalone v0.1-alpha */ -public class MessageStatusChangeProcessor implements ObjectProcessor { +public class MessageStatusChangeProcessor implements ObjectProcessor { private final PersistenceManager persistenceManager = PersistenceManager.getInstance(); private final ConnectionManager connectionManager = ConnectionManager.getInstance(); @Override - public void process(MessageStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(MessageStatusChange statusChange, long socketID, ObjectWriteProxy writeProxy) throws IOException { // Any other status than READ is not supposed to be sent to the server - if (input.get() != MessageStatus.READ) throw new IOException(new EnvoyException("Message " + input + " has an invalid status")); + if (statusChange.get() != MessageStatus.READ) throw new IOException(new EnvoyException(statusChange + " has an invalid status")); - envoy.server.data.Message msg = persistenceManager.getMessageByID(input.getID()); - msg.setStatus(input.get()); - msg.setReadDate(input.getDate()); + final var msg = persistenceManager.getMessageByID(statusChange.getID()); + msg.setStatus(statusChange.get()); + msg.setReadDate(statusChange.getDate()); persistenceManager.updateMessage(msg); // Notifies the sender of the message about the status-update to READ final long senderID = msg.getSender().getID(); - if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), input); + if (connectionManager.isOnline(senderID)) writeProxy.write(connectionManager.getSocketID(senderID), statusChange); } @Override - public Class getInputClass() { return MessageStatusChangeEvent.class; } + public Class getInputClass() { return MessageStatusChange.class; } } diff --git a/src/main/java/envoy/server/processors/NameChangeProcessor.java b/src/main/java/envoy/server/processors/NameChangeProcessor.java index 991e7fb..4400c04 100644 --- a/src/main/java/envoy/server/processors/NameChangeProcessor.java +++ b/src/main/java/envoy/server/processors/NameChangeProcessor.java @@ -2,7 +2,7 @@ package envoy.server.processors; import java.io.IOException; -import envoy.event.NameChangeEvent; +import envoy.event.NameChange; import envoy.server.data.Contact; import envoy.server.data.PersistenceManager; import envoy.server.data.User; @@ -17,20 +17,20 @@ import envoy.server.net.ObjectWriteProxy; * @author Leon Hofmeister * @since Envoy Server Standalone v0.1-beta */ -public class NameChangeProcessor implements ObjectProcessor { +public class NameChangeProcessor implements ObjectProcessor { @Override - public void process(NameChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(NameChange nameChange, long socketID, ObjectWriteProxy writeProxy) throws IOException { PersistenceManager persistenceManager = PersistenceManager.getInstance(); ConnectionManager connectionManager = ConnectionManager.getInstance(); - Contact toUpdate = persistenceManager.getContactByID(input.getID()); - toUpdate.setName(input.get()); + Contact toUpdate = persistenceManager.getContactByID(nameChange.getID()); + toUpdate.setName(nameChange.get()); persistenceManager.updateContact(toUpdate); - // notifying online contacts of this client of his name change - toUpdate.getContacts().stream().filter(contact -> (contact instanceof User && connectionManager.isOnline(contact.getID()))).forEach(user -> { + // Notify online contacts of the name change + toUpdate.getContacts().stream().filter(User.class::isInstance).map(Contact::getID).filter(connectionManager::isOnline).forEach(userID -> { try { - writeProxy.write(user.getID(), input); + writeProxy.write(userID, nameChange); } catch (IOException e) { e.printStackTrace(); } @@ -38,5 +38,5 @@ public class NameChangeProcessor implements ObjectProcessor { } @Override - public Class getInputClass() { return NameChangeEvent.class; } + public Class getInputClass() { return NameChange.class; } } diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index f4d56a7..f090f42 100755 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.util.logging.Logger; import envoy.data.User.UserStatus; -import envoy.event.UserStatusChangeEvent; +import envoy.event.UserStatusChange; import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; @@ -12,7 +12,7 @@ import envoy.server.net.ObjectWriteProxy; import envoy.util.EnvoyLog; /** - * This processor handles incoming {@link UserStatusChangeEvent}.
+ * This processor handles incoming {@link UserStatusChange}.
*
* Project: envoy-server-standalone
* File: UserStatusChangeProcessor.java
@@ -21,7 +21,7 @@ import envoy.util.EnvoyLog; * @author Leon Hofmeister * @since Envoy Server Standalone v0.1-alpha */ -public class UserStatusChangeProcessor implements ObjectProcessor { +public class UserStatusChangeProcessor implements ObjectProcessor { private static ObjectWriteProxy writeProxy; private static PersistenceManager persistenceManager = PersistenceManager.getInstance(); @@ -29,13 +29,13 @@ public class UserStatusChangeProcessor implements ObjectProcessor getInputClass() { return UserStatusChangeEvent.class; } + public Class getInputClass() { return UserStatusChange.class; } @Override - public void process(UserStatusChangeEvent input, long socketID, ObjectWriteProxy writeProxy) throws IOException { + public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) throws IOException { // new status should not equal old status if (input.get().equals(persistenceManager.getUserByID(input.getID()).getStatus())) { - logger.warning("Received an unnecessary UserStatusChangeEvent"); + logger.warning("Received an unnecessary UserStatusChange"); return; } updateUserStatus(input); @@ -45,7 +45,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor Date: Sat, 20 Jun 2020 14:38:06 +0200 Subject: [PATCH 06/11] Add simple version comparison algorithm --- .../processors/LoginCredentialProcessor.java | 65 +++++++++++++++++-- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index f837656..5ab8cc6 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -8,6 +8,7 @@ import java.time.LocalDateTime; import java.util.Arrays; import java.util.HashSet; import java.util.logging.Logger; +import java.util.regex.Pattern; import javax.persistence.NoResultException; @@ -38,9 +39,8 @@ public final class LoginCredentialProcessor implements ObjectProcessor\\d).(?\\d)(?:-(?\\w+))?"); + private static final Logger logger = EnvoyLog.getLogger(LoginCredentialProcessor.class); + private static final Pattern versionPattern = Pattern.compile("(?\\d).(?\\d)(?:-(?\\w+))?"); @Override public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { @@ -49,10 +49,10 @@ public final class LoginCredentialProcessor implements ObjectProcessor= 0) { - // writeProxy.write(socketID, new HandshakeRejectionEvent("Wrong version")); - // return; + if (!verifyCompatibility(credentials.getClientVersion())) { + logger.info("The client has the wrong version."); + writeProxy.write(socketID, new HandshakeRejection("Wrong version")); + return; } // Acquire a user object (or reject the handshake if that's impossible) @@ -125,6 +125,57 @@ public final class LoginCredentialProcessor implements ObjectProcessor maxSuffix) return false; + } + + // Compare major + { + final var currentMajor = Integer.parseInt(currentMatcher.group("major")); + final var minMajor = Integer.parseInt(minMatcher.group("major")); + final var maxMajor = Integer.parseInt(maxMatcher.group("major")); + + if (currentMajor < minMajor || currentMajor > maxMajor) return false; + } + + // Compare minor + { + final var currentMinor = Integer.parseInt(currentMatcher.group("minor")); + final var minMinor = Integer.parseInt(minMatcher.group("minor")); + final var maxMinor = Integer.parseInt(maxMatcher.group("minor")); + + if (currentMinor < minMinor || currentMinor > maxMinor) return false; + } + + return true; + } + + private int convertSuffix(String suffix) { + switch (suffix == null ? "" : suffix) { + case "alpha": + return 0; + case "beta": + return 1; + default: + return 2; + } + } + @Override public Class getInputClass() { return LoginCredentials.class; } } From 0d7cb38b6d1de9b840ee6f2d8d838ac747ed20de Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 23 Jun 2020 09:08:57 +0200 Subject: [PATCH 07/11] Apply suggestions from code review --- pom.xml | 2 +- .../java/enovy/server/util/VersionUtils.java | 94 +++++++++++++++++++ .../java/enovy/server/util/package-info.java | 11 +++ src/main/java/envoy/server/Startup.java | 14 --- src/main/java/envoy/server/data/Message.java | 23 ++++- .../processors/LoginCredentialProcessor.java | 63 +------------ .../server/processors/MessageProcessor.java | 2 +- .../MessageStatusChangeProcessor.java | 3 +- 8 files changed, 133 insertions(+), 79 deletions(-) create mode 100644 src/main/java/enovy/server/util/VersionUtils.java create mode 100644 src/main/java/enovy/server/util/package-info.java diff --git a/pom.xml b/pom.xml index 0612482..046ec55 100755 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - f~compatibility_verification-SNAPSHOT + develop-SNAPSHOT com.github.informatik-ag-ngl diff --git a/src/main/java/enovy/server/util/VersionUtils.java b/src/main/java/enovy/server/util/VersionUtils.java new file mode 100644 index 0000000..1a66590 --- /dev/null +++ b/src/main/java/enovy/server/util/VersionUtils.java @@ -0,0 +1,94 @@ +package enovy.server.util; + +import java.util.regex.Pattern; + +/** + * Implements a comparison algorithm between Envoy versions and defines minimal + * and maximal client versions compatible with this server. + *

+ * Project: envoy-server-standalone
+ * File: VersionUtils.java
+ * Created: 23.06.2020
+ * + * @author KSKE + * @since Envoy Server Standalone v0.1-beta + */ +public class VersionUtils { + + /** + * The minimal client version compatible with this server. + * + * @since Envoy Server Standalone v0.1-beta + */ + public static final String MIN_CLIENT_VERSION = "0.1-beta"; + + /** + * The maximal client version compatible with this server. + * + * @since Envoy Server Standalone v0.1-beta + */ + public static final String MAX_CLIENT_VERSION = "0.1-beta"; + + private static final Pattern versionPattern = Pattern.compile("(?\\d).(?\\d)(?:-(?\\w+))?"); + + private VersionUtils() {} + + /** + * Parses an Envoy Client version string and checks whether that version is + * compatible with this server. + * + * @param version the version string to parse + * @return {@code true} if the given version is compatible with this server + * @since Envoy Server Standalone v0.1-beta + */ + public static boolean verifyCompatibility(String version) { + final var currentMatcher = versionPattern.matcher(version); + + if (!currentMatcher.matches()) return false; + + final var minMatcher = versionPattern.matcher(MIN_CLIENT_VERSION); + final var maxMatcher = versionPattern.matcher(MAX_CLIENT_VERSION); + + if (!minMatcher.matches() || !maxMatcher.matches()) throw new RuntimeException("Invalid min or max client version configured!"); + + // Compare suffixes + { + final var currentSuffix = convertSuffix(currentMatcher.group("suffix")); + final var minSuffix = convertSuffix(minMatcher.group("suffix")); + final var maxSuffix = convertSuffix(maxMatcher.group("suffix")); + + if (currentSuffix < minSuffix || currentSuffix > maxSuffix) return false; + } + + // Compare major + { + final var currentMajor = Integer.parseInt(currentMatcher.group("major")); + final var minMajor = Integer.parseInt(minMatcher.group("major")); + final var maxMajor = Integer.parseInt(maxMatcher.group("major")); + + if (currentMajor < minMajor || currentMajor > maxMajor) return false; + } + + // Compare minor + { + final var currentMinor = Integer.parseInt(currentMatcher.group("minor")); + final var minMinor = Integer.parseInt(minMatcher.group("minor")); + final var maxMinor = Integer.parseInt(maxMatcher.group("minor")); + + if (currentMinor < minMinor || currentMinor > maxMinor) return false; + } + + return true; + } + + private static int convertSuffix(String suffix) { + switch (suffix == null ? "" : suffix) { + case "alpha": + return 0; + case "beta": + return 1; + default: + return 2; + } + } +} diff --git a/src/main/java/enovy/server/util/package-info.java b/src/main/java/enovy/server/util/package-info.java new file mode 100644 index 0000000..f256f4c --- /dev/null +++ b/src/main/java/enovy/server/util/package-info.java @@ -0,0 +1,11 @@ +/** + * This package contains utility classes used in Envoy Server. + *

+ * Project: envoy-server-standalone
+ * File: package-info.java
+ * Created: 23.06.2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-beta + */ +package enovy.server.util; diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index b88f1bd..9ad4bc3 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -29,20 +29,6 @@ import envoy.util.EnvoyLog; */ public class Startup { - /** - * The minimal client version compatible with this server. - * - * @since Envoy Server Standalone v0.1-beta - */ - public static final String MIN_CLIENT_VERSION = "0.1-beta"; - - /** - * The maximal client version compatible with this server. - * - * @since Envoy Server Standalone v0.1-beta - */ - public static final String MAX_CLIENT_VERSION = "0.1-beta"; - /** * Initializes the logger with a new config instance. * diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index e53a7e5..5b81e7d 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -1,5 +1,7 @@ package envoy.server.data; +import static envoy.data.Message.MessageStatus.*; + import java.time.LocalDateTime; import javax.persistence.*; @@ -38,7 +40,7 @@ public class Message { * @since Envoy Server Standalone v0.1-beta */ public static final String getPending = "Message.getPending"; - + @Id protected long id; @@ -111,9 +113,26 @@ public class Message { return message; } + /** + * Sets the message status to {@link MessageStatus#RECEIVED} and sets the + * current time stamp as the received date. + * + * @since Envoy Server Standalone v0.1-beta + */ public void received() { receivedDate = LocalDateTime.now(); - status = MessageStatus.RECEIVED; + status = RECEIVED; + } + + /** + * Sets the message status to {@link MessageStatus#READ} and sets the + * current time stamp as the read date. + * + * @since Envoy Server Standalone v0.1-beta + */ + public void read() { + readDate = LocalDateTime.now(); + status = READ; } /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 5ab8cc6..9877f36 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -8,15 +8,14 @@ import java.time.LocalDateTime; import java.util.Arrays; import java.util.HashSet; import java.util.logging.Logger; -import java.util.regex.Pattern; import javax.persistence.NoResultException; +import enovy.server.util.VersionUtils; import envoy.data.LoginCredentials; import envoy.data.Message.MessageStatus; import envoy.event.HandshakeRejection; import envoy.event.MessageStatusChange; -import envoy.server.Startup; import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; @@ -40,7 +39,6 @@ public final class LoginCredentialProcessor implements ObjectProcessor\\d).(?\\d)(?:-(?\\w+))?"); @Override public void process(LoginCredentials credentials, long socketID, ObjectWriteProxy writeProxy) throws IOException { @@ -48,10 +46,9 @@ public final class LoginCredentialProcessor implements ObjectProcessor maxSuffix) return false; - } - - // Compare major - { - final var currentMajor = Integer.parseInt(currentMatcher.group("major")); - final var minMajor = Integer.parseInt(minMatcher.group("major")); - final var maxMajor = Integer.parseInt(maxMatcher.group("major")); - - if (currentMajor < minMajor || currentMajor > maxMajor) return false; - } - - // Compare minor - { - final var currentMinor = Integer.parseInt(currentMatcher.group("minor")); - final var minMinor = Integer.parseInt(minMatcher.group("minor")); - final var maxMinor = Integer.parseInt(maxMatcher.group("minor")); - - if (currentMinor < minMinor || currentMinor > maxMinor) return false; - } - - return true; - } - - private int convertSuffix(String suffix) { - switch (suffix == null ? "" : suffix) { - case "alpha": - return 0; - case "beta": - return 1; - default: - return 2; - } - } - @Override public Class getInputClass() { return LoginCredentials.class; } } diff --git a/src/main/java/envoy/server/processors/MessageProcessor.java b/src/main/java/envoy/server/processors/MessageProcessor.java index 8622cd8..c143d42 100755 --- a/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/src/main/java/envoy/server/processors/MessageProcessor.java @@ -35,7 +35,7 @@ public class MessageProcessor implements ObjectProcessor { return; } message.nextStatus(); - ConnectionManager connectionManager = ConnectionManager.getInstance(); + ConnectionManager connectionManager = ConnectionManager.getInstance(); sendToUser(connectionManager, message, writeProxy); try { diff --git a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index a3dca3c..8801d54 100755 --- a/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -28,8 +28,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor Date: Tue, 23 Jun 2020 16:02:12 +0000 Subject: [PATCH 08/11] Fix Javadoc author Co-authored-by: delvh --- src/main/java/enovy/server/util/VersionUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/enovy/server/util/VersionUtils.java b/src/main/java/enovy/server/util/VersionUtils.java index 1a66590..ab96890 100644 --- a/src/main/java/enovy/server/util/VersionUtils.java +++ b/src/main/java/enovy/server/util/VersionUtils.java @@ -10,7 +10,7 @@ import java.util.regex.Pattern; * File: VersionUtils.java
* Created: 23.06.2020
* - * @author KSKE + * @author Kai S. K. Engelbart * @since Envoy Server Standalone v0.1-beta */ public class VersionUtils { From e1bfab814c56c8a77615d82b24ba8f3e6db5a128 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 25 Jun 2020 17:00:41 +0200 Subject: [PATCH 09/11] Fix notifying the sender about a message delivery This addresses bugs in two instances of delivery notification: * the sender is online -> no event was sent * the sender comes online later -> wrong status (SENT) was sent --- .../processors/LoginCredentialProcessor.java | 11 ++++- .../server/processors/MessageProcessor.java | 47 +++++++++++-------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 9877f36..ad7addd 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -109,14 +109,21 @@ public final class LoginCredentialProcessor implements ObjectProcessor - *
+ * This {@link ObjectProcessor} handles incoming {@link Message}s. + *

* Project: envoy-server-standalone
* File: MessageProcessor.java
* Created: 30.12.2019
@@ -26,6 +27,8 @@ import envoy.util.EnvoyLog; */ public class MessageProcessor implements ObjectProcessor { + private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); + private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final Logger logger = EnvoyLog.getLogger(MessageProcessor.class); @Override @@ -35,26 +38,32 @@ public class MessageProcessor implements ObjectProcessor { return; } message.nextStatus(); - ConnectionManager connectionManager = ConnectionManager.getInstance(); - sendToUser(connectionManager, message, writeProxy); + // Convert to server message + final var serverMessage = new envoy.server.data.Message(message); + try { - PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(message)); - } catch (EntityExistsException e) { - logger.warning("Received a message with an id that already exists"); - } - } - private void sendToUser(ConnectionManager connectionManager, Message message, ObjectWriteProxy writeProxy) { - if (connectionManager.isOnline(message.getRecipientID())) try { - // If recipient is online, send the message directly - writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); - // Update the message status to RECEIVED - message.setReceivedDate(LocalDateTime.now()); - message.nextStatus(); + // Persist the message + persistenceManager.addMessage(serverMessage); + + // Send the message to the recipient if online + if (connectionManager.isOnline(message.getRecipientID())) { + writeProxy.write(connectionManager.getSocketID(message.getRecipientID()), message); + + // Increment status + message.nextStatus(); + serverMessage.received(); + persistenceManager.updateMessage(serverMessage); + + // Notify the sender about the delivery + // Note that the exact time stamp might differ slightly + writeProxy.write(socketID, new MessageStatusChange(message)); + } + } catch (EntityExistsException e) { + logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!"); } catch (IOException e) { - logger.warning("Recipient online. Failed to send message" + message.getID()); - e.printStackTrace(); + logger.log(Level.WARNING, "Failed to deliver " + message + ":", e); } } From a901d0af49db342ca810403e01279e0a44e56335 Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 26 Jun 2020 20:48:18 +0200 Subject: [PATCH 10/11] Fixed monstrosity not showing Envoy logo --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 79e92f3..a514d85 100755 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # envoy-server-standalone -Standalone server for the Envoy project +Standalone server for the +Envoy project From a4b1ad6d2275c0a5e90ac7eb3ed6a5c3331a7d95 Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 26 Jun 2020 22:04:40 +0200 Subject: [PATCH 11/11] Validate user name during registration --- .../processors/LoginCredentialProcessor.java | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index ad7addd..0a248c9 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -20,6 +20,7 @@ import envoy.server.data.PersistenceManager; import envoy.server.data.User; import envoy.server.net.ConnectionManager; import envoy.server.net.ObjectWriteProxy; +import envoy.util.Bounds; import envoy.util.EnvoyLog; /** @@ -38,7 +39,7 @@ public final class LoginCredentialProcessor implements ObjectProcessor()); - persistenceManager.addContact(user); - logger.info("Registered new " + user); } + try { + // Checking that no user already has this identifier + PersistenceManager.getInstance().getUserByName(credentials.getIdentifier()); + + // This code only gets executed if this user already exists + logger.info("The requested user already exists."); + writeProxy.write(socketID, new HandshakeRejection(USERNAME_TAKEN)); + return; + } catch (NoResultException e) { + // Creation of a new user + user = new User(); + user.setName(credentials.getIdentifier()); + user.setLastSeen(LocalDateTime.now()); + user.setStatus(ONLINE); + user.setPasswordHash(credentials.getPasswordHash()); + user.setContacts(new HashSet<>()); + persistenceManager.addContact(user); + logger.info("Registered new " + user); + } } logger.info(user + " successfully authenticated.");