From 1a4523db54e385d543ade51ee03576e3296dc385 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 18 Jan 2020 09:37:35 +0100 Subject: [PATCH 1/8] Added a constructor for User as well as for MessageStatusChangeEvent --- src/main/java/envoy/data/Message.java | 1 - src/main/java/envoy/data/User.java | 15 ++++++++++++++- .../envoy/event/MessageStatusChangeEvent.java | 14 +++++++++++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index 5d7ee33..50a2f09 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -81,7 +81,6 @@ public class Message implements Serializable { this.text = text; this.attachment = attachment; this.status = status; - } /** diff --git a/src/main/java/envoy/data/User.java b/src/main/java/envoy/data/User.java index dc33851..bc9cf0f 100644 --- a/src/main/java/envoy/data/User.java +++ b/src/main/java/envoy/data/User.java @@ -53,7 +53,7 @@ public class User implements Serializable { /** * Initializes a {@link User}. The {@link UserStatus} is set to - * {@link UserStatus#OFFLINE}. + * {@link UserStatus#ONLINE}. * * @param id unique ID * @param name user name @@ -65,6 +65,19 @@ public class User implements Serializable { status = UserStatus.ONLINE; } + /** + * Initializes a {@link User}. + * + * @param id unique ID + * @param name user name + * @param status the status of the user + * @since Envoy Common v0.2-alpha + */ + public User(long id, String name, UserStatus status) { + this(id, name); + this.status = status; + } + @Override public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); } diff --git a/src/main/java/envoy/event/MessageStatusChangeEvent.java b/src/main/java/envoy/event/MessageStatusChangeEvent.java index b0aea1f..ebe818b 100644 --- a/src/main/java/envoy/event/MessageStatusChangeEvent.java +++ b/src/main/java/envoy/event/MessageStatusChangeEvent.java @@ -23,16 +23,24 @@ public class MessageStatusChangeEvent implements Event { * * @param id the ID of the {@link Message} this event is related to * @param status the status of the {@link Message} this event is related - * to - * @param date the date at which the MessageStatus change occurred + * to + * @param date the date at which the MessageStatus change occurred * @since Envoy Common v0.2-alpha */ public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) { this.id = id; this.status = status; - this.date = date; + this.date = date; } + /** + * Initialises a {@link MessageStatusChangeEvent} through a message. + * + * @param message the message from which to build the event + * @since Envoy Common v0.2-alpha + */ + public MessageStatusChangeEvent(Message message) { this(message.getId(), message.getStatus(), new Date()); } + /** * @return the status of the {@link Message} this event is related to * @since Envoy Common v0.2-alpha From e0fef6af54dd3721ebb2a4e13cc3d271d3c00e2a Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 18 Jan 2020 10:27:29 +0100 Subject: [PATCH 2/8] Added boolean registration to LoginCredentials --- .classpath | 5 ++++ .settings/org.eclipse.core.resources.prefs | 1 + .settings/org.eclipse.wst.common.component | 13 +++++++---- .../java/envoy/data/LoginCredentials.java | 23 ++++++++++++++----- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index 907ff21..88d37ef 100644 --- a/.classpath +++ b/.classpath @@ -24,5 +24,10 @@ + + + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 8bc0e1c..04cfa2c 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,5 +1,6 @@ eclipse.preferences.version=1 encoding//src/main/java=UTF-8 +encoding//src/main/resources=UTF-8 encoding//src/test/java=UTF-8 encoding//src/test/resources=UTF-8 encoding/=UTF-8 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index 44f2db9..08f7658 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,17 +1,22 @@ - + + - + + - + + + - + + diff --git a/src/main/java/envoy/data/LoginCredentials.java b/src/main/java/envoy/data/LoginCredentials.java index 58d14d4..9b29c07 100644 --- a/src/main/java/envoy/data/LoginCredentials.java +++ b/src/main/java/envoy/data/LoginCredentials.java @@ -19,20 +19,24 @@ public class LoginCredentials implements Serializable { private final String name; private final byte[] passwordHash; + private final boolean registration; private static final long serialVersionUID = -7395245059059523314L; /** * Creates an in stance of {@link LoginCredentials}. * - * @param name the name of the user - * @param password the password of the user (will be converted to a hash) + * @param name the name of the user + * @param password the password of the user (will be converted to a hash) + * @param registration signifies that these credentials are used for user + * registration instead of user login * @throws NoSuchAlgorithmException if the algorithm used is unknown * @since Envoy Common v0.2-alpha */ - public LoginCredentials(String name, char[] password) throws NoSuchAlgorithmException { - this.name = name; - passwordHash = getSha256(toByteArray(password)); + public LoginCredentials(String name, char[] password, boolean registration) throws NoSuchAlgorithmException { + this.name = name; + passwordHash = getSha256(toByteArray(password)); + this.registration = registration; } private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); } @@ -52,7 +56,7 @@ public class LoginCredentials implements Serializable { form.format("LoginCredentials[name=%s,passwordHash=", name); for (byte element : passwordHash) form.format("%02x", element); - return form.format("]").toString(); + return form.format(",registration=%b]", registration).toString(); } } @@ -67,4 +71,11 @@ public class LoginCredentials implements Serializable { * @since Envoy Common v0.2-alpha */ public byte[] getPasswordHash() { return passwordHash; } + + /** + * @return {@code true} if these credentials are used for user registration + * instead of user login + * @since Envoy Common v0.2-alpha + */ + public boolean isRegistration() { return registration; } } \ No newline at end of file From 09b803ed4b0f340121791ee1736cd3ed1420a164 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 17:01:02 +0100 Subject: [PATCH 3/8] Renamed MessageIdGenerator to IdGenerator, added IdGeneratorRequest Also removed src/test/resources from pom.xml. --- .classpath | 5 ----- pom.xml | 5 +++++ ...MessageIdGenerator.java => IdGenerator.java} | 17 ++++++++++++----- src/main/java/envoy/data/Message.java | 6 +++--- src/main/java/envoy/event/Event.java | 3 +-- .../java/envoy/event/IdGeneratorRequest.java | 14 ++++++++++++++ 6 files changed, 35 insertions(+), 15 deletions(-) rename src/main/java/envoy/data/{MessageIdGenerator.java => IdGenerator.java} (65%) create mode 100644 src/main/java/envoy/event/IdGeneratorRequest.java diff --git a/.classpath b/.classpath index 88d37ef..907ff21 100644 --- a/.classpath +++ b/.classpath @@ -24,10 +24,5 @@ - - - - - diff --git a/pom.xml b/pom.xml index eee2dcf..1feb64c 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,11 @@ envoy-common + + + src/main/resources + + org.apache.maven.plugins diff --git a/src/main/java/envoy/data/MessageIdGenerator.java b/src/main/java/envoy/data/IdGenerator.java similarity index 65% rename from src/main/java/envoy/data/MessageIdGenerator.java rename to src/main/java/envoy/data/IdGenerator.java index dbd27a2..9b61a20 100644 --- a/src/main/java/envoy/data/MessageIdGenerator.java +++ b/src/main/java/envoy/data/IdGenerator.java @@ -1,32 +1,39 @@ package envoy.data; +import java.io.Serializable; + /** * Generates increasing IDs between two numbers.
*
* Project: envoy-common
- * File: MessageIdGenerator.java
+ * File: IdGenerator.java
* Created: 31.12.2019
- * + * * @author Kai S. K. Engelbart * @since Envoy Common v0.2-alpha */ -public class MessageIdGenerator { +public class IdGenerator implements Serializable { private final long end; private long current; + private static final long serialVersionUID = -1517378307055845147L; + /** - * Creates an instance of {@link MessageIdGenerator}. + * Creates an instance of {@link IdGenerator}. * * @param begin the first ID * @param end the last ID * @since Envoy Common v0.2-alpha */ - public MessageIdGenerator(long begin, long end) { + public IdGenerator(long begin, long end) { current = begin; this.end = end; } + @Override + public String toString() { return String.format("MessageIdGenerator[current=%d,end=%d]", current, end); } + /** * @return {@code true} if there are unused IDs remaining * @since Envoy Common v0.2-alpha diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index 50a2f09..dea58cf 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -24,7 +24,7 @@ public class Message implements Serializable { * * @since Envoy Common v0.2-alpha */ - public static enum MessageStatus { + public enum MessageStatus { /** * is selected, if a message was sent but not received by the server yet. @@ -77,7 +77,7 @@ public class Message implements Serializable { this.id = id; this.senderId = senderId; this.recipientId = recipientId; - this.creationDate = date; + creationDate = date; this.text = text; this.attachment = attachment; this.status = status; @@ -186,6 +186,6 @@ public class Message implements Serializable { */ public void setStatus(MessageStatus status) { if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time"); - else this.status = status; + this.status = status; } } \ No newline at end of file diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java index f17b95b..1aee5a4 100644 --- a/src/main/java/envoy/event/Event.java +++ b/src/main/java/envoy/event/Event.java @@ -14,6 +14,5 @@ public interface Event { /** * @return the data associated with this event */ - T get(); + default T get() { return null; } } - diff --git a/src/main/java/envoy/event/IdGeneratorRequest.java b/src/main/java/envoy/event/IdGeneratorRequest.java new file mode 100644 index 0000000..ab80515 --- /dev/null +++ b/src/main/java/envoy/event/IdGeneratorRequest.java @@ -0,0 +1,14 @@ +package envoy.event; + +/** + * Signifies to the server that the client needs a new + * {@link envoy.data.IdGenerator} instance.
+ *
+ * Project: envoy-common
+ * File: IdGeneratorRequest.java
+ * Created: 28 Jan 2020
+ * + * @author Kai S. K. Engelbart + */ +public class IdGeneratorRequest implements Event { +} From dd3f920e94e3b3ee31661a93bdb8acb78053a49b Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 17:09:28 +0100 Subject: [PATCH 4/8] Changed IdGenerator constructor to accept size instead of end index --- src/main/java/envoy/data/IdGenerator.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/data/IdGenerator.java b/src/main/java/envoy/data/IdGenerator.java index 9b61a20..1d5803d 100644 --- a/src/main/java/envoy/data/IdGenerator.java +++ b/src/main/java/envoy/data/IdGenerator.java @@ -23,12 +23,12 @@ public class IdGenerator implements Serializable { * Creates an instance of {@link IdGenerator}. * * @param begin the first ID - * @param end the last ID + * @param size the amount of IDs to provide * @since Envoy Common v0.2-alpha */ - public IdGenerator(long begin, long end) { - current = begin; - this.end = end; + public IdGenerator(long begin, long size) { + current = begin; + end = begin + size; } @Override From aed8016485adb78a1b1af768e1eb3ca1385a087d Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 17:32:33 +0100 Subject: [PATCH 5/8] Made id a mandatory field in MessageBuilder Added support for IdGenerator in the MessageBuilder constructor. --- src/main/java/envoy/data/Message.java | 18 ++++---- src/main/java/envoy/data/MessageBuilder.java | 44 +++++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index dea58cf..85702ca 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -64,20 +64,20 @@ public class Message implements Serializable { * this class provides {@code null} checks and default values for all * properties. * - * @param id unique ID - * @param senderId the ID of the user who sends the message - * @param recipientId the ID of the user who receives the message - * @param date the creation date of the message - * @param text the text content of the message - * @param attachment the attachment of the message, if present - * @param status the current {@link MessageStatus} of the message + * @param id unique ID + * @param senderId the ID of the user who sends the message + * @param recipientId the ID of the user who receives the message + * @param creationDate the creation date of the message + * @param text the text content of the message + * @param attachment the attachment of the message, if present + * @param status the current {@link MessageStatus} of the message * @since Envoy Common v0.2-alpha */ - Message(long id, long senderId, long recipientId, Date date, String text, MessageAttachment attachment, MessageStatus status) { + Message(long id, long senderId, long recipientId, Date creationDate, String text, MessageAttachment attachment, MessageStatus status) { this.id = id; this.senderId = senderId; this.recipientId = recipientId; - creationDate = date; + this.creationDate = creationDate; this.text = text; this.attachment = attachment; this.status = status; diff --git a/src/main/java/envoy/data/MessageBuilder.java b/src/main/java/envoy/data/MessageBuilder.java index 43e929e..5166352 100644 --- a/src/main/java/envoy/data/MessageBuilder.java +++ b/src/main/java/envoy/data/MessageBuilder.java @@ -10,7 +10,7 @@ import envoy.data.Message.MessageStatus; * Project: envoy-common
* File: MessageBuilder.java
* Created: 31.12.2019
- * + * * @author Kai S. K. Engelbart * @since Envoy Common v0.2-alpha */ @@ -21,7 +21,7 @@ public class MessageBuilder { // Properties with default values private long id; - private Date date; + private Date creationDate; private String text; private MessageAttachment attachment; private Message.MessageStatus status; @@ -32,11 +32,27 @@ public class MessageBuilder { * * @param senderId the ID of the user who sends the {@link Message} * @param recipientId the ID of the user who received the {@link Message} + * @param idGenerator the ID generator used to generate a unique {@link Message} + * id * @since Envoy Common v0.2-alpha */ - public MessageBuilder(long senderId, long recipientId) { + public MessageBuilder(long senderId, long recipientId, IdGenerator idGenerator) { + this(senderId, recipientId, idGenerator.next()); + } + + /** + * Creates an instance of {@link MessageBuilder} with all mandatory values + * without defaults for the {@link Message} class. + * + * @param senderId the ID of the user who sends the {@link Message} + * @param recipientId the ID of the user who received the {@link Message} + * @param messageId the ID of the {@link Message} + * @since Envoy Common v0.2-alpha + */ + public MessageBuilder(long senderId, long recipientId, long messageId) { this.senderId = senderId; this.recipientId = recipientId; + id = messageId; } /** @@ -57,36 +73,26 @@ public class MessageBuilder { * {@code MessageStatus.WAITING} * * - * + * * @return a new instance of {@link Message} * @since Envoy Common v0.2-alpha */ public Message build() { // Supplement default values - if (date == null) date = new Date(); + if (creationDate == null) creationDate = new Date(); if (text == null) text = ""; if (status == null) status = MessageStatus.WAITING; - return new Message(id, senderId, recipientId, date, text, attachment, status); + return new Message(id, senderId, recipientId, creationDate, text, attachment, status); } /** - * @param id the unique ID of the {@link Message} to create + * @param creationDate the creation date of the {@link Message} to create * @return this {@link MessageBuilder} * @since Envoy Common v0.2-alpha */ - public MessageBuilder setId(long id) { - this.id = id; - return this; - } - - /** - * @param date the creation date of the {@link Message} to create - * @return this {@link MessageBuilder} - * @since Envoy Common v0.2-alpha - */ - public MessageBuilder setDate(Date date) { - this.date = date; + public MessageBuilder setDate(Date creationDate) { + this.creationDate = creationDate; return this; } From 8ab72b7a7637d1ea96c55a4907e310f0227b87d5 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 17:50:09 +0100 Subject: [PATCH 6/8] Made Event and its implementations serializable --- src/main/java/envoy/event/Event.java | 4 +++- src/main/java/envoy/event/IdGeneratorRequest.java | 2 ++ src/main/java/envoy/event/MessageStatusChangeEvent.java | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/event/Event.java b/src/main/java/envoy/event/Event.java index 1aee5a4..3be9d9c 100644 --- a/src/main/java/envoy/event/Event.java +++ b/src/main/java/envoy/event/Event.java @@ -1,5 +1,7 @@ package envoy.event; +import java.io.Serializable; + /** * Project: envoy-common
* File: Event.java
@@ -9,7 +11,7 @@ package envoy.event; * @param the type of the Event * @since Envoy v0.2-alpha */ -public interface Event { +public interface Event extends Serializable { /** * @return the data associated with this event diff --git a/src/main/java/envoy/event/IdGeneratorRequest.java b/src/main/java/envoy/event/IdGeneratorRequest.java index ab80515..ec3ae34 100644 --- a/src/main/java/envoy/event/IdGeneratorRequest.java +++ b/src/main/java/envoy/event/IdGeneratorRequest.java @@ -11,4 +11,6 @@ package envoy.event; * @author Kai S. K. Engelbart */ public class IdGeneratorRequest implements Event { + + private static final long serialVersionUID = 1431107413883364583L; } diff --git a/src/main/java/envoy/event/MessageStatusChangeEvent.java b/src/main/java/envoy/event/MessageStatusChangeEvent.java index ebe818b..88b6cad 100644 --- a/src/main/java/envoy/event/MessageStatusChangeEvent.java +++ b/src/main/java/envoy/event/MessageStatusChangeEvent.java @@ -18,6 +18,8 @@ public class MessageStatusChangeEvent implements Event { private final Message.MessageStatus status; private final Date date; + private static final long serialVersionUID = 4566145392192761313L; + /** * Initializes a {@link MessageStatusChangeEvent}. * From 5c19fe28492950c61068c8d129b293cb2e2d2b74 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 18:58:44 +0100 Subject: [PATCH 7/8] Added HandshakeRejectionEvent and fixed some Javadoc --- .../envoy/event/HandshakeRejectionEvent.java | 41 +++++++++++++++++++ .../java/envoy/event/IdGeneratorRequest.java | 1 + .../envoy/event/MessageStatusChangeEvent.java | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/main/java/envoy/event/HandshakeRejectionEvent.java diff --git a/src/main/java/envoy/event/HandshakeRejectionEvent.java b/src/main/java/envoy/event/HandshakeRejectionEvent.java new file mode 100644 index 0000000..aca655e --- /dev/null +++ b/src/main/java/envoy/event/HandshakeRejectionEvent.java @@ -0,0 +1,41 @@ +package envoy.event; + +/** + * Signifies to the client that the handshake failed for the attached + * reason.
+ *
+ * Project: envoy-common
+ * File: HandshakeRejectionEvent.java
+ * Created: 28 Jan 2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.3-alpha + */ +public class HandshakeRejectionEvent implements Event { + + private final String reason; + + private static final long serialVersionUID = -8723270093452609197L; + + /** + * Creates an instance of {@link HandshakeRejectionEvent} with an empty reason. + * + * @since Envoy Common v0.3-alpha + */ + public HandshakeRejectionEvent() { this(""); } + + /** + * Creates an instance of {@link HandshakeRejectionEvent}. + * + * @param reason the reason why the handshake was rejected + * @since Envoy Common v0.3-alpha + */ + public HandshakeRejectionEvent(String reason) { this.reason = reason; } + + /** + * @return the reason why the handshake was rejected + * @since Envoy Common v0.3-alpha + */ + @Override + public String get() { return reason; } +} diff --git a/src/main/java/envoy/event/IdGeneratorRequest.java b/src/main/java/envoy/event/IdGeneratorRequest.java index ec3ae34..885f40d 100644 --- a/src/main/java/envoy/event/IdGeneratorRequest.java +++ b/src/main/java/envoy/event/IdGeneratorRequest.java @@ -9,6 +9,7 @@ package envoy.event; * Created: 28 Jan 2020
* * @author Kai S. K. Engelbart + * @since Envoy Common v0.3-alpha */ public class IdGeneratorRequest implements Event { diff --git a/src/main/java/envoy/event/MessageStatusChangeEvent.java b/src/main/java/envoy/event/MessageStatusChangeEvent.java index 88b6cad..c56f7a0 100644 --- a/src/main/java/envoy/event/MessageStatusChangeEvent.java +++ b/src/main/java/envoy/event/MessageStatusChangeEvent.java @@ -36,7 +36,7 @@ public class MessageStatusChangeEvent implements Event { } /** - * Initialises a {@link MessageStatusChangeEvent} through a message. + * Initializes a {@link MessageStatusChangeEvent} through a message. * * @param message the message from which to build the event * @since Envoy Common v0.2-alpha From 5999d4a817eec74e52672a9efa173a6c04ab9f1c Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 28 Jan 2020 19:58:23 +0100 Subject: [PATCH 8/8] Fixed typo --- src/main/java/envoy/data/IdGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/data/IdGenerator.java b/src/main/java/envoy/data/IdGenerator.java index 1d5803d..26b7131 100644 --- a/src/main/java/envoy/data/IdGenerator.java +++ b/src/main/java/envoy/data/IdGenerator.java @@ -32,7 +32,7 @@ public class IdGenerator implements Serializable { } @Override - public String toString() { return String.format("MessageIdGenerator[current=%d,end=%d]", current, end); } + public String toString() { return String.format("IdGenerator[current=%d,end=%d]", current, end); } /** * @return {@code true} if there are unused IDs remaining