From 66ab0c2d43347a82dfb1fa89d7be5b941db2f174 Mon Sep 17 00:00:00 2001 From: delvh Date: Wed, 1 Jan 2020 19:16:40 +0100 Subject: [PATCH 1/5] Improved Message.java and User.java to remain compatible with database --- src/main/java/envoy/data/Message.java | 80 +++++++++++++++++++++++++-- src/main/java/envoy/data/User.java | 64 ++++++++++++++------- 2 files changed, 118 insertions(+), 26 deletions(-) diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index ddbfba2..5ec1c03 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -48,11 +48,11 @@ public class Message implements Serializable { READ } - private final long id, senderId, recipientId; - private final transient User sender, recipient; - private final Date date; - private final String text; - private final MessageAttachment attachment; + private long id, senderId, recipientId; + private transient User sender, recipient; + private Date date; + private String text; + private MessageAttachment attachment; private MessageStatus status; @@ -168,4 +168,74 @@ public class Message implements Serializable { * @since Envoy Common v0.2-alpha */ public MessageStatus getStatus() { return status; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param id the id to set + * @since Envoy Common v0.2-alpha + */ + public void setId(long id) { this.id = id; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param senderId the senderId to set + * @since Envoy Common v0.2-alpha + */ + public void setSenderId(long senderId) { this.senderId = senderId; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param recipientId the recipientId to set + * @since Envoy Common v0.2-alpha + */ + public void setRecipientId(long recipientId) { this.recipientId = recipientId; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param sender the sender to set + * @since Envoy Common v0.2-alpha + */ + public void setSender(User sender) { this.sender = sender; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param recipient the recipient to set + * @since Envoy Common v0.2-alpha + */ + public void setRecipient(User recipient) { this.recipient = recipient; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param date the date to set + * @since Envoy Common v0.2-alpha + */ + public void setDate(Date date) { this.date = date; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param text the text to set + * @since Envoy Common v0.2-alpha + */ + public void setText(String text) { this.text = text; } + + /** + * Only used for Hibernate. Please do not use. + * + * @param attachment the attachment to set + * @since Envoy Common v0.2-alpha + */ + public void setAttachment(MessageAttachment attachment) { this.attachment = attachment; } + + /** + * @param status the status to set + * @since Envoy Common v0.2-alpha + */ + public void setStatus(MessageStatus status) { this.status = status; } } \ No newline at end of file diff --git a/src/main/java/envoy/data/User.java b/src/main/java/envoy/data/User.java index d5c0bbc..2049e75 100644 --- a/src/main/java/envoy/data/User.java +++ b/src/main/java/envoy/data/User.java @@ -1,8 +1,7 @@ package envoy.data; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; +import java.util.Date; /** * Represents a unique user with a unique, numeric ID, a name and a current @@ -46,11 +45,10 @@ public class User implements Serializable { OFFLINE; } - private final long id; - private final String name; - private final List contacts = new ArrayList<>(); - - private UserStatus status; + private long id; + private String name; + private UserStatus status; + private Date lastOnline; private static final long serialVersionUID = 3530947374856708236L; @@ -60,46 +58,70 @@ public class User implements Serializable { * * @param id unique ID * @param name user name - * @since Envoy Client v0.2-alpha + * @since Envoy Common v0.2-alpha */ public User(long id, String name) { this.id = id; this.name = name; - status = UserStatus.OFFLINE; + status = UserStatus.ONLINE; + setLastOnline(new Date()); } + /** + * Used to let Hibernate modify the values of an {@link User} + * + * @since Envoy Common v0.2-alpha + */ + public User() {} + @Override public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); } /** * @return the ID of this {@link User} - * @since Envoy Client v0.2-alpha + * @since Envoy Common v0.2-alpha */ public long getId() { return id; } /** * @return the name of this {@link User} - * @since Envoy Client v0.2-alpha + * @since Envoy Common v0.2-alpha */ public String getName() { return name; } - /** - * @return a list of all users this user can send messages to - * @since Envoy Client v0.2-alpha - */ - public List getContacts() { return contacts; } - /** * @return the current status of this user - * @since Envoy Client v0.2-alpha + * @since Envoy Common v0.2-alpha */ public UserStatus getStatus() { return status; } /** - * Sets the current status of this user - * * @param status the status to set - * @since Envoy Client v0.2-alpha + * @since Envoy Common v0.2-alpha */ public void setStatus(UserStatus status) { this.status = status; } + + /** + * @param id the id to set + * @since Envoy Common v0.2-alpha + */ + public void setId(long id) { this.id = id; } + + /** + * @param name the name to set + * @since Envoy Common v0.2-alpha + */ + public void setName(String name) { this.name = name; } + + /** + * @return the lastOnline + * @since Envoy Common v0.2-alpha + */ + public Date getLastOnline() { return lastOnline; } + + /** + * @param lastOnline the lastOnline to set + * @since Envoy Common v0.2-alpha + */ + public void setLastOnline(Date lastOnline) { this.lastOnline = lastOnline; } } \ No newline at end of file From fe412fff66aa57f13ad18ba14d7bb6329c6d6f7a Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 2 Jan 2020 17:03:58 +0200 Subject: [PATCH 2/5] Added Contacts --- src/main/java/envoy/data/Contacts.java | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/envoy/data/Contacts.java diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java new file mode 100644 index 0000000..8374168 --- /dev/null +++ b/src/main/java/envoy/data/Contacts.java @@ -0,0 +1,31 @@ +package envoy.data; + +import java.io.Serializable; +import java.util.List; + +/** + * Project: envoy-common
+ * File: Contacts.java
+ * Created: 02.01.2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class Contacts implements Serializable { + + private List contacts; + + private static final long serialVersionUID = 136970804968152871L; + + /** + * @return a list of users messages can be sent to + * @since Envoy Common v0.2-alpha + */ + public List getContacts() { return contacts; } + + /** + * @param contacts the contact list to set + * @since Envoy Common v0.2-alpha + */ + public void setContacts(List contacts) { this.contacts = contacts; } +} From 8b4205f222b41e8f18d121257665e4fa9a955cf1 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 2 Jan 2020 17:46:20 +0200 Subject: [PATCH 3/5] Adder userId to Contacts --- src/main/java/envoy/data/Contacts.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java index 8374168..5c5314c 100644 --- a/src/main/java/envoy/data/Contacts.java +++ b/src/main/java/envoy/data/Contacts.java @@ -13,10 +13,26 @@ import java.util.List; */ public class Contacts implements Serializable { - private List contacts; + private long userId; + private List contacts; private static final long serialVersionUID = 136970804968152871L; + @Override + public String toString() { return String.format("Contacts[%s]", contacts); } + + /** + * @return the ID of the user this contacts belong to + * @since Envoy Common v0.2-alpha + */ + public long getUserId() { return userId; } + + /** + * @param userId the ID of the user this contacts belong to + * @since Envoy Common v0.2-alpha + */ + public void setUserId(long userId) { this.userId = userId; } + /** * @return a list of users messages can be sent to * @since Envoy Common v0.2-alpha From 3b808f49383d9016f73c181160631a815001c812 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 2 Jan 2020 18:50:04 +0200 Subject: [PATCH 4/5] Reverted some changes related to ORM --- src/main/java/envoy/data/Contacts.java | 28 ++-- src/main/java/envoy/data/Message.java | 165 ++++++------------- src/main/java/envoy/data/MessageBuilder.java | 16 +- src/main/java/envoy/data/User.java | 43 +---- 4 files changed, 80 insertions(+), 172 deletions(-) diff --git a/src/main/java/envoy/data/Contacts.java b/src/main/java/envoy/data/Contacts.java index 5c5314c..bd19102 100644 --- a/src/main/java/envoy/data/Contacts.java +++ b/src/main/java/envoy/data/Contacts.java @@ -13,11 +13,23 @@ import java.util.List; */ public class Contacts implements Serializable { - private long userId; - private List contacts; + private final long userId; + private final List contacts; private static final long serialVersionUID = 136970804968152871L; + /** + * Creates an instance of {@link Contacts}. + * + * @param userId the ID of the user this contacts belong to + * @param contacts the contact list + * @since Envoy Common v0.2-alpha + */ + public Contacts(long userId, List contacts) { + this.userId = userId; + this.contacts = contacts; + } + @Override public String toString() { return String.format("Contacts[%s]", contacts); } @@ -27,21 +39,9 @@ public class Contacts implements Serializable { */ public long getUserId() { return userId; } - /** - * @param userId the ID of the user this contacts belong to - * @since Envoy Common v0.2-alpha - */ - public void setUserId(long userId) { this.userId = userId; } - /** * @return a list of users messages can be sent to * @since Envoy Common v0.2-alpha */ public List getContacts() { return contacts; } - - /** - * @param contacts the contact list to set - * @since Envoy Common v0.2-alpha - */ - public void setContacts(List contacts) { this.contacts = contacts; } } diff --git a/src/main/java/envoy/data/Message.java b/src/main/java/envoy/data/Message.java index 5ec1c03..ff0414c 100644 --- a/src/main/java/envoy/data/Message.java +++ b/src/main/java/envoy/data/Message.java @@ -48,13 +48,13 @@ public class Message implements Serializable { READ } - private long id, senderId, recipientId; - private transient User sender, recipient; - private Date date; - private String text; - private MessageAttachment attachment; + private final long id, senderId, recipientId; + private final Date creationDate; + private final String text; + private final MessageAttachment attachment; - private MessageStatus status; + private Date receivedDate, readDate; + private MessageStatus status; private static final long serialVersionUID = -4393477412979594435L; @@ -64,26 +64,24 @@ public class Message implements Serializable { * this class provides {@code null} checks and default values for all * properties. * - * @param id unique ID - * @param sender the user who sends the message - * @param recipient 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 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 * @since Envoy Common v0.2-alpha */ - Message(long id, User sender, User recipient, Date date, String text, MessageAttachment attachment, MessageStatus status) { - this.id = id; - this.sender = sender; - this.recipient = recipient; - this.date = date; - this.text = text; - this.attachment = attachment; - this.status = status; + Message(long id, long senderId, long recipientId, Date date, String text, MessageAttachment attachment, MessageStatus status) { + this.id = id; + this.senderId = senderId; + this.recipientId = recipientId; + this.creationDate = date; + this.text = text; + this.attachment = attachment; + this.status = status; - senderId = sender.getId(); - recipientId = recipient.getId(); } /** @@ -106,13 +104,14 @@ public class Message implements Serializable { @Override public String toString() { - return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s]", + return String.format("TextMessage[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,hasAttachment=%b]", id, - sender, - recipient, - new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date), + senderId, + recipientId, + new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(creationDate), status, - text); + text, + attachment != null); } /** @@ -121,24 +120,12 @@ public class Message implements Serializable { */ public long getId() { return id; } - /** - * @return the sender of this message - * @since Envoy Common v0.2-alpha - */ - public User getSender() { return sender; } - /** * @return the sender ID of this message * @since Envoy Common v0.2-alpha */ public long getSenderId() { return senderId; } - /** - * @return the recipient of this message - * @since Envoy Common v0.2-alpha - */ - public User getRecipient() { return recipient; } - /** * @return the recipient ID of this message * @since Envoy Common v0.2-alpha @@ -149,7 +136,33 @@ public class Message implements Serializable { * @return the date at which this message was created * @since Envoy Common v0.2-alpha */ - public Date getDate() { return date; } + public Date getDate() { return creationDate; } + + /** + * @return the date at which the message has been received by the sender + * @since Envoy Common v0.2-alpha + */ + public Date getReceivedDate() { return receivedDate; } + + /** + * + * @param receivedDate the date at which the message has been received by the + * sender + * @since Envoy Common v0.2-alpha + */ + public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; } + + /** + * @return the date at which the message has been read by the sender + * @since Envoy Common v0.2-alpha + */ + public Date getReadDate() { return readDate; } + + /** + * @param readDate at which the message has been read by the sender + * @since Envoy Common v0.2-alpha + */ + public void setReadDate(Date readDate) { this.readDate = readDate; } /** * @return the text content of this message @@ -168,74 +181,4 @@ public class Message implements Serializable { * @since Envoy Common v0.2-alpha */ public MessageStatus getStatus() { return status; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param id the id to set - * @since Envoy Common v0.2-alpha - */ - public void setId(long id) { this.id = id; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param senderId the senderId to set - * @since Envoy Common v0.2-alpha - */ - public void setSenderId(long senderId) { this.senderId = senderId; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param recipientId the recipientId to set - * @since Envoy Common v0.2-alpha - */ - public void setRecipientId(long recipientId) { this.recipientId = recipientId; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param sender the sender to set - * @since Envoy Common v0.2-alpha - */ - public void setSender(User sender) { this.sender = sender; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param recipient the recipient to set - * @since Envoy Common v0.2-alpha - */ - public void setRecipient(User recipient) { this.recipient = recipient; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param date the date to set - * @since Envoy Common v0.2-alpha - */ - public void setDate(Date date) { this.date = date; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param text the text to set - * @since Envoy Common v0.2-alpha - */ - public void setText(String text) { this.text = text; } - - /** - * Only used for Hibernate. Please do not use. - * - * @param attachment the attachment to set - * @since Envoy Common v0.2-alpha - */ - public void setAttachment(MessageAttachment attachment) { this.attachment = attachment; } - - /** - * @param status the status to set - * @since Envoy Common v0.2-alpha - */ - public void setStatus(MessageStatus status) { this.status = status; } } \ No newline at end of file diff --git a/src/main/java/envoy/data/MessageBuilder.java b/src/main/java/envoy/data/MessageBuilder.java index 4217663..43e929e 100644 --- a/src/main/java/envoy/data/MessageBuilder.java +++ b/src/main/java/envoy/data/MessageBuilder.java @@ -17,7 +17,7 @@ import envoy.data.Message.MessageStatus; public class MessageBuilder { // Mandatory properties without default values - private final User sender, recipient; + private final long senderId, recipientId; // Properties with default values private long id; @@ -30,15 +30,13 @@ public class MessageBuilder { * Creates an instance of {@link MessageBuilder} with all mandatory values * without defaults for the {@link Message} class. * - * @param sender the user who sends the {@link Message} - * @param recipient the user who received the {@link Message} + * @param senderId the ID of the user who sends the {@link Message} + * @param recipientId the ID of the user who received the {@link Message} * @since Envoy Common v0.2-alpha */ - public MessageBuilder(User sender, User recipient) { - if (sender == null) throw new NullPointerException("Message sender is null"); - if (recipient == null) throw new NullPointerException("Message recipient is null"); - this.sender = sender; - this.recipient = recipient; + public MessageBuilder(long senderId, long recipientId) { + this.senderId = senderId; + this.recipientId = recipientId; } /** @@ -69,7 +67,7 @@ public class MessageBuilder { if (text == null) text = ""; if (status == null) status = MessageStatus.WAITING; - return new Message(id, sender, recipient, date, text, attachment, status); + return new Message(id, senderId, recipientId, date, text, attachment, status); } /** diff --git a/src/main/java/envoy/data/User.java b/src/main/java/envoy/data/User.java index 2049e75..dc33851 100644 --- a/src/main/java/envoy/data/User.java +++ b/src/main/java/envoy/data/User.java @@ -1,7 +1,6 @@ package envoy.data; import java.io.Serializable; -import java.util.Date; /** * Represents a unique user with a unique, numeric ID, a name and a current @@ -45,10 +44,10 @@ public class User implements Serializable { OFFLINE; } - private long id; - private String name; - private UserStatus status; - private Date lastOnline; + private final long id; + private final String name; + + private UserStatus status; private static final long serialVersionUID = 3530947374856708236L; @@ -64,16 +63,8 @@ public class User implements Serializable { this.id = id; this.name = name; status = UserStatus.ONLINE; - setLastOnline(new Date()); } - /** - * Used to let Hibernate modify the values of an {@link User} - * - * @since Envoy Common v0.2-alpha - */ - public User() {} - @Override public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); } @@ -96,32 +87,8 @@ public class User implements Serializable { public UserStatus getStatus() { return status; } /** - * @param status the status to set + * @param status the next status of this user * @since Envoy Common v0.2-alpha */ public void setStatus(UserStatus status) { this.status = status; } - - /** - * @param id the id to set - * @since Envoy Common v0.2-alpha - */ - public void setId(long id) { this.id = id; } - - /** - * @param name the name to set - * @since Envoy Common v0.2-alpha - */ - public void setName(String name) { this.name = name; } - - /** - * @return the lastOnline - * @since Envoy Common v0.2-alpha - */ - public Date getLastOnline() { return lastOnline; } - - /** - * @param lastOnline the lastOnline to set - * @since Envoy Common v0.2-alpha - */ - public void setLastOnline(Date lastOnline) { this.lastOnline = lastOnline; } } \ No newline at end of file From 5194e20a855d31e42065b8f838db7a9a03f637c2 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 3 Jan 2020 17:08:07 +0200 Subject: [PATCH 5/5] Added byte array serialization and deserialization methods. --- .../java/envoy/util/SerializationUtils.java | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/util/SerializationUtils.java b/src/main/java/envoy/util/SerializationUtils.java index c070733..b26aa49 100644 --- a/src/main/java/envoy/util/SerializationUtils.java +++ b/src/main/java/envoy/util/SerializationUtils.java @@ -1,5 +1,6 @@ package envoy.util; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; @@ -21,12 +22,14 @@ import java.io.Serializable; */ public class SerializationUtils { + private SerializationUtils() {} + private static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; } /** * Deserializes an arbitrary {@link Serializable} object from a file. * - * @param the type of the object to deserialize + * @param the type of the serialized object * @param file the file to deserialize from * @param serializedClass the class of the object to deserialize * @return the deserialized object @@ -42,7 +45,26 @@ public class SerializationUtils { } /** - * @param the deserialized object + * Deserializes an arbitrary {@link Serializable} object from a byte array. + * + * @param the type of the serialized object + * @param bytes the array in which the serialized object is stored + * @param serializedClass the class of the serialized object + * @return the deserialized object + * @throws IOException if something failed while deserializing the + * object + * @throws ClassNotFoundException if the deserialized object can not be linked + * to a class + * @since Envoy Common v0.2-alpha + */ + public static T read(byte[] bytes, Class serializedClass) throws IOException, ClassNotFoundException { + return read(new ByteArrayInputStream(bytes), serializedClass); + } + + /** + * Deserializes an arbitrary {@link Serializable} object from a stream. + * + * @param the type of the serialized object * @param in the {@link InputStream} of a serialized Object * @param serializedClass the object type to convert the deserialized object * into @@ -79,6 +101,22 @@ public class SerializationUtils { } } + /** + * Serializes an arbitrary object to a byte array. + * + * @param obj the object to serialize + * @return a byte array containing the serialized object + * @throws IOException if the serialization failed + * @since Envoy Common v0.2-alpha + */ + public static byte[] writeToByteArray(Object obj) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { + oout.writeObject(obj); + } + return baos.toByteArray(); + } + /** * Serializes an object and writes it into an output stream preceded by 4 bytes * containing the number of serialized bytes. @@ -86,14 +124,11 @@ public class SerializationUtils { * @param obj the object to serialize * @param out the output stream to serialize to * @throws IOException if an error occurred during serialization + * @since Envoy Common v0.2-alpha */ public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException { // Serialize object to byte array - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { - oout.writeObject(obj); - } - byte[] objBytes = baos.toByteArray(); + byte[] objBytes = writeToByteArray(obj); // Get length of byte array in bytes byte[] objLen = intToBytes(objBytes.length); @@ -102,6 +137,4 @@ public class SerializationUtils { out.write(objLen); out.write(objBytes); } - - private SerializationUtils() {} } \ No newline at end of file