Move Envoy Common to common/ subdirectory

This commit is contained in:
2020-07-13 11:42:20 +02:00
parent 79cb8e821c
commit 34337c3838
58 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,84 @@
package envoy.data;
import java.io.Serializable;
/**
* This interface should be used for any type supposed to be a {@link Message}
* attachment (i.e. images or sound).
* <p>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Attachment.java</strong><br>
* Created: <strong>30 Dec 2019</strong><br>
*
* @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class Attachment implements Serializable {
/**
* Defines the type of the attachment.
*
* @since Envoy Common v0.1-beta
*/
public enum AttachmentType {
/**
* This attachment type denotes a picture.
*
* @since Envoy Common v0.1-beta
*/
PICTURE,
/**
* This attachment type denotes a video.
*
* @since Envoy Common v0.1-beta
*/
VIDEO,
/**
* This attachment type denotes a voice message.
*
* @since Envoy Common v0.1-beta
*/
VOICE,
/**
* This attachment type denotes a document.
*
* @since Envoy Common v0.1-beta
*/
DOCUMENT
}
private final byte[] data;
private final AttachmentType type;
private static final long serialVersionUID = 1L;
/**
* Constructs an attachment.
*
* @param data the data of the attachment
* @param type the type of the attachment
* @since Envoy Common v0.1-beta
*/
public Attachment(byte[] data, AttachmentType type) {
this.data = data;
this.type = type;
}
/**
* @return the data of the attachment
* @since Envoy Common v0.1-beta
*/
public byte[] getData() { return data; }
/**
* @return the type of the attachment
* @since Envoy Common v0.1-beta
*/
public AttachmentType getType() { return type; }
}

View File

@ -0,0 +1,86 @@
package envoy.data;
import java.util.*;
import envoy.exception.EnvoyException;
/**
* Manages all application settings that are set during application startup by
* either loading them from the {@link Properties} file
* {@code client.properties} or parsing them from the command line arguments of
* the application.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>Config.java</strong><br>
* Created: <strong>12 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.1-beta
*/
public class Config {
protected Map<String, ConfigItem<?>> items = new HashMap<>();
/**
* Parses config items from a properties object.
*
* @param properties the properties object to parse
* @since Envoy Common v0.1-beta
*/
public void load(Properties properties) {
items.entrySet()
.stream()
.filter(e -> properties.containsKey(e.getKey()))
.forEach(e -> e.getValue().parse(properties.getProperty(e.getKey())));
}
/**
* Parses config items from an array of command line arguments.
*
* @param args the command line arguments to parse
* @throws EnvoyException if the command line arguments contain an unknown token
* @since Envoy Common v0.1-beta
*/
public void load(String[] args) throws EnvoyException {
for (int i = 0; i < args.length; i++)
for (ConfigItem<?> item : items.values())
if (args[i].startsWith("--")) {
if (args[i].length() == 2) throw new EnvoyException("Malformed command line argument at position " + i);
final String commandLong = args[i].substring(2);
if (item.getCommandLong().equals(commandLong)) {
item.parse(args[++i]);
break;
}
} else if (args[i].startsWith("-")) {
if (args[i].length() == 1) throw new EnvoyException("Malformed command line argument at position " + i);
final String commandShort = args[i].substring(1);
if (item.getCommandShort().equals(commandShort)) {
item.parse(args[++i]);
break;
}
} else throw new EnvoyException("Malformed command line argument at position " + i);
}
/**
* Initializes config items from a map.
*
* @param items the items to include in this config
* @since Envoy Common v0.1-beta
*/
public void load(Map<String, ConfigItem<?>> items) { this.items.putAll(items); }
/**
* @return {@code true} if all mandatory config items are initialized
* @since Envoy Common v0.1-beta
*/
public boolean isInitialized() {
return items.values().stream().filter(ConfigItem::isMandatory).map(ConfigItem::get).noneMatch(Objects::isNull);
}
/**
* @param name the name of the config item to return
* @return the config item with the specified name
* @since Envoy Common v0.1-beta
*/
public ConfigItem<?> get(String name) { return items.get(name); }
}

View File

@ -0,0 +1,92 @@
package envoy.data;
import java.util.function.Function;
/**
* Contains a single {@link Config} value as well as the corresponding command
* line arguments and its default value.<br>
* <br>
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>ConfigItem.javaEvent.java</strong><br>
* Created: <strong>21.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of the config item's value
* @since Envoy Common v0.1-beta
*/
public class ConfigItem<T> {
private final String commandLong, commandShort;
private final Function<String, T> parseFunction;
private final boolean mandatory;
private T value;
/**
* Initializes a {@link ConfigItem}.
*
* @param commandLong the long command line argument to set this value
* @param commandShort the short command line argument to set this value
* @param parseFunction the {@code Function<String, T>} that parses the value
* from a string
* @param defaultValue the optional default value to set before parsing
* @param mandatory indicated that this config item must be initialized with
* a non-null value
* @since Envoy Common v0.1-beta
*/
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction, T defaultValue, boolean mandatory) {
this.commandLong = commandLong;
this.commandShort = commandShort;
this.parseFunction = parseFunction;
this.mandatory = mandatory;
value = defaultValue;
}
/**
* Initializes an optional {@link ConfigItem} without a default value.
*
* @param commandLong the long command line argument to set this value
* @param commandShort the short command line argument to set this value
* @param parseFunction the {@code Function<String, T>} that parses the value
* from a string
* @since Envoy Common v0.1-beta
*/
public ConfigItem(String commandLong, String commandShort, Function<String, T> parseFunction) {
this(commandLong, commandShort, parseFunction, null, false);
}
/**
* Parses this {@ConfigItem}'s value from a string.
*
* @param input the string to parse from
* @since Envoy Common v0.1-beta
*/
public void parse(String input) { value = parseFunction.apply(input); }
/**
* @return The long command line argument to set the value of this
* {@link ConfigItem}
* @since Envoy Common v0.1-beta
*/
public String getCommandLong() { return commandLong; }
/**
* @return The short command line argument to set the value of this
* {@link ConfigItem}
* @since Envoy Common v0.1-beta
*/
public String getCommandShort() { return commandShort; }
/**
* @return the value of this {@link ConfigItem}
* @since Envoy Common v0.1-beta
*/
public T get() { return value; }
/**
* @return {@code true} if this {@link ConfigItem} is mandatory for successful
* application initialization
* @since Envoy Common v0.1-beta
*/
public boolean isMandatory() { return mandatory; }
}

View File

@ -0,0 +1,91 @@
package envoy.data;
import java.io.Serializable;
import java.util.Objects;
import java.util.Set;
/**
* This class is the superclass for both {@link User} and {@link Group}.<br>
* It provides an id and a name for each user and group.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Contact.java</strong><br>
* Created: <strong>24 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy v0.1-beta
*/
public abstract class Contact implements Serializable {
private final long id;
private final transient Set<? extends Contact> contacts;
private String name;
private static final long serialVersionUID = 0L;
/**
* Creates a new instance of a {@link Contact}.
*
* @param id the ID of this contact
* @param name the name of this contact
* @param contacts the contacts of this {@link Contact}
* @since Envoy Common v0.1-beta
*/
public Contact(long id, String name, Set<? extends Contact> contacts) {
this.id = id;
this.name = name;
this.contacts = contacts;
}
/**
* @return the ID of this {@link Contact}
* @since Envoy Common v0.2-alpha
*/
public long getID() { return id; }
/**
* @return the name of this {@link Contact}
* @since Envoy Common v0.2-alpha
*/
public String getName() { return name; }
/**
* @param name the new name of this {@link Contact}
* @since Envoy Common v0.1-beta
*/
public void setName(String name) { this.name = name; }
/**
* {@inheritDoc}
*/
@Override
public String toString() { return String.format("Contact[id=%d,name=%s, contacts=%s]", id, name, contacts); }
/**
* Provides a hash code based on the ID of this contact.
*
* @since Envoy Common v0.1-beta
*/
@Override
public int hashCode() { return Objects.hash(id); }
/**
* Tests equality to another object. If that object is a contact as well,
* equality is determined by the ID.
*
* @param obj the object to test for equality to this contact
* @return {code true} if both objects are contacts and have identical IDs
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Contact)) return false;
return id == ((Contact) obj).id;
}
/**
* @return the contacts of this {@link Contact}
* @since Envoy Common v0.1-beta
*/
public Set<? extends Contact> getContacts() { return contacts; }
}

View File

@ -0,0 +1,55 @@
package envoy.data;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>Group.java</strong><br>
* Created: <strong>24 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public final class Group extends Contact {
private static final long serialVersionUID = 0L;
/**
* Creates a new instance of a {@link Group} without any members.
*
* @param id the ID of this group
* @param name the name of this group
* @since Envoy Common v0.1-beta
*/
public Group(long id, String name) { this(id, name, new HashSet<User>()); }
/**
* Creates an instance of a {@link Group}.
*
* @param id the ID of this group
* @param name the name of this group
* @param members all members that should be preinitialized
* @since Envoy Common v0.1-beta
*/
public Group(long id, String name, Set<User> members) { super(id, name, members); }
private void readObject(ObjectInputStream inputStream) throws Exception {
inputStream.defaultReadObject();
var contacts = Contact.class.getDeclaredField("contacts");
contacts.setAccessible(true);
contacts.set(this, inputStream.readObject());
}
private void writeObject(ObjectOutputStream outputStream) throws Exception {
outputStream.defaultWriteObject();
getContacts().forEach(user -> user.serializeContacts(false));
outputStream.writeObject(getContacts());
}
@SuppressWarnings("unchecked")
@Override
public Set<User> getContacts() { return (Set<User>) super.getContacts(); }
}

View File

@ -0,0 +1,71 @@
package envoy.data;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Map;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupMessage.java</strong><br>
* Created: <strong>26.03.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.1-beta
*/
public final class GroupMessage extends Message {
private final Map<Long, MessageStatus> memberStatuses;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link GroupMessage} with values for all of its properties. The
* use
* of this constructor is only intended for the {@link MessageBuilder} class, as
* 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 groupID the ID of the group which receives the message
* @param creationDate the creation date of the message
* @param receivedDate the received date of the message
* @param readDate the read 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 Message.MessageStatus} of the
* message
* @param forwarded whether this message was forwarded
* @param memberStatuses a map of all members and their status according to this
* {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
GroupMessage(long id, long senderID, long groupID, LocalDateTime creationDate, LocalDateTime receivedDate, LocalDateTime readDate, String text,
Attachment attachment, MessageStatus status, boolean forwarded, Map<Long, MessageStatus> memberStatuses) {
super(id, senderID, groupID, creationDate, receivedDate, readDate, text, attachment, status, forwarded);
this.memberStatuses = memberStatuses;
}
/**
* Sets the status to be the minimum of all members.
*
* @since Envoy Common v0.1-beta
*/
public void updateStatus() {
setStatus(Collections.min(memberStatuses.values()));
switch (getStatus()) {
case RECEIVED:
setReceivedDate(LocalDateTime.now());
break;
case READ:
setReadDate(LocalDateTime.now());
break;
}
}
/**
* @return the map of all statuses in this {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
public Map<Long, MessageStatus> getMemberStatuses() { return memberStatuses; }
}

View File

@ -0,0 +1,51 @@
package envoy.data;
import java.io.Serializable;
/**
* Generates increasing IDs between two numbers.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>IDGenerator.java</strong><br>
* Created: <strong>31.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class IDGenerator implements Serializable {
private final long end;
private long current;
private static final long serialVersionUID = 0L;
/**
* Creates an instance of {@link IDGenerator}.
*
* @param begin the first ID
* @param size the amount of IDs to provide
* @since Envoy Common v0.2-alpha
*/
public IDGenerator(long begin, long size) {
current = begin;
end = begin + size;
}
@Override
public String toString() { return String.format("IDGenerator[current=%d,end=%d]", current, end); }
/**
* @return {@code true} if there are unused IDs remaining
* @since Envoy Common v0.2-alpha
*/
public boolean hasNext() { return current < end; }
/**
* @return the next ID
* @since Envoy Common v0.2-alpha
*/
public long next() {
if (!hasNext()) throw new IllegalStateException("All IDs have been used");
return current++;
}
}

View File

@ -0,0 +1,69 @@
package envoy.data;
import java.io.Serializable;
/**
* Contains a {@link User}'s login / registration information as well as the
* client version.
* <p>
* Project: <strong>envoy-common</strong><br>
* File: <strong>LoginCredentials.java</strong><br>
* Created: <strong>29.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public final class LoginCredentials implements Serializable {
private final String identifier, password, clientVersion;
private final boolean registration;
private static final long serialVersionUID = 2;
/**
* Initializes login credentials for a handshake.
*
* @param identifier the identifier of the user
* @param password the password of the user
* @param registration signifies that these credentials are used for user
* registration instead of user login
* @param clientVersion the version of the client sending these credentials
* @since Envoy Common v0.1-beta
*/
public LoginCredentials(String identifier, String password, boolean registration, String clientVersion) {
this.identifier = identifier;
this.password = password;
this.registration = registration;
this.clientVersion = clientVersion;
}
@Override
public String toString() {
return String.format("LoginCredentials[identifier=%s,registration=%b,clientVersion=%s]", identifier, registration, clientVersion);
}
/**
* @return the identifier of the user performing the login
* @since Envoy Common v0.2-alpha
*/
public String getIdentifier() { return identifier; }
/**
* @return the password of the user performing the login
* @since Envoy Common v0.1-beta
*/
public String getPassword() { return password; }
/**
* @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; }
/**
* @return the version of the client sending these credentials
* @since Envoy Common v0.1-beta
*/
public String getClientVersion() { return clientVersion; }
}

View File

@ -0,0 +1,212 @@
package envoy.data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* Represents a unique message with a unique, numeric ID. Further metadata
* includes the sender and recipient {@link User}s, as well as the creation
* date and the current {@link MessageStatus}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Message.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Leon Hofmeister
* @since Envoy Common v0.2-alpha
*/
public class Message implements Serializable {
/**
* This enumeration defines all possible statuses a {link Message} can have.
*
* @since Envoy Common v0.2-alpha
*/
public enum MessageStatus {
/**
* is selected, if a message was sent but not received by the server yet.
*/
WAITING,
/**
* is selected, if a sent message was received by the server.
*/
SENT,
/**
* is selected, if a message was delivered from the server to the recipient, but
* has not been read yet.
*/
RECEIVED,
/**
* is selected, if a recipient opened the corresponding chat of said message.
*/
READ
}
private final long id, senderID, recipientID;
private final boolean forwarded;
private final LocalDateTime creationDate;
private final String text;
private final Attachment attachment;
private LocalDateTime receivedDate, readDate;
private MessageStatus status;
private static final long serialVersionUID = 1L;
/**
* Initializes a {@link Message} with values for all of its properties. The use
* of this constructor is only intended for the {@link MessageBuilder} class, as
* 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 creationDate the creation date of the message
* @param receivedDate the received date of the message
* @param readDate the read 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 forwarded whether this message was forwarded
* @since Envoy Common v0.2-alpha
*/
Message(long id, long senderID, long recipientID, LocalDateTime creationDate, LocalDateTime receivedDate, LocalDateTime readDate, String text,
Attachment attachment, MessageStatus status, boolean forwarded) {
this.id = id;
this.senderID = senderID;
this.recipientID = recipientID;
this.creationDate = creationDate;
this.receivedDate = receivedDate;
this.readDate = readDate;
this.text = text;
this.attachment = attachment;
this.status = status;
this.forwarded = forwarded;
}
/**
* Changes the current {@link MessageStatus} to the next logical status.<br>
* <br>
* The underlying order is as follows:
* <ol>
* <li>{@link MessageStatus#WAITING}
* <li>{@link MessageStatus#SENT}
* <li>{@link MessageStatus#RECEIVED}
* <li>{@link MessageStatus#READ}
* </ol>
*
* @since Envoy Common v0.2-alpha
*/
public void nextStatus() {
if (status == MessageStatus.READ) throw new IllegalStateException("Message status READ is already reached");
status = MessageStatus.values()[status.ordinal() + 1];
}
@Override
public String toString() {
return String.format("Message[id=%d,sender=%s,recipient=%s,date=%s,status=%s,text=%s,forwarded=%b,hasAttachment=%b]",
id,
senderID,
recipientID,
DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss").format(creationDate),
status,
text,
forwarded,
attachment != null);
}
/**
* @return the ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getID() { return id; }
/**
* @return the sender ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getSenderID() { return senderID; }
/**
* @return the recipient ID of this message
* @since Envoy Common v0.2-alpha
*/
public long getRecipientID() { return recipientID; }
/**
* @return the date at which this message was created
* @since Envoy Common v0.2-alpha
*/
public LocalDateTime getCreationDate() { return creationDate; }
/**
* @return the date at which the message has been received by the sender
* @since Envoy Common v0.2-alpha
*/
public LocalDateTime 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(LocalDateTime receivedDate) { this.receivedDate = receivedDate; }
/**
* @return the date at which the message has been read by the sender
* @since Envoy Common v0.2-alpha
*/
public LocalDateTime getReadDate() { return readDate; }
/**
* @param readDate at which the message has been read by the sender
* @since Envoy Common v0.2-alpha
*/
public void setReadDate(LocalDateTime readDate) { this.readDate = readDate; }
/**
* @return the text content of this message
* @since Envoy Common v0.2-alpha
*/
public String getText() { return text; }
/**
* @return the messageAttachment
* @since Envoy Common v0.2-alpha
*/
public Attachment getAttachment() { return attachment; }
/**
* @return {@code true} if an attachment is present
* @since Envoy Common v0.1-beta
*/
public boolean hasAttachment() { return attachment != null; }
/**
* @return the current status of this message
* @since Envoy Common v0.2-alpha
*/
public MessageStatus getStatus() { return status; }
/**
* @param status the new {@link MessageStatus}, if permitted
* @since Envoy Common v0.2-alpha
*/
public void setStatus(MessageStatus status) {
if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time");
this.status = status;
}
/**
* @return whether this message was forwarded
* @since Envoy common v0.1-beta
*/
public boolean isForwarded() { return forwarded; }
}

View File

@ -0,0 +1,240 @@
package envoy.data;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import envoy.data.Message.MessageStatus;
/**
* Provides a method of constructing the {@link Message} class.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>MessageBuilder.java</strong><br>
* Created: <strong>31.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class MessageBuilder {
// Mandatory properties without default values
private final long senderID, recipientID;
// Properties with default values
private long id;
private LocalDateTime creationDate, receivedDate, readDate;
private String text;
private Attachment attachment;
private Message.MessageStatus status;
private boolean forwarded;
/**
* 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 receives 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, 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 receives 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;
}
/**
* This constructor transforms a given {@link Message} into a new message for a
* new receiver.
* This makes it especially useful in the case of forwarding messages.
*
* @param msg the message to copy
* @param recipientID the ID of the user who receives the {@link Message}
* @param iDGenerator the ID generator used to generate a unique {@link Message}
* id
* @since Envoy v0.1-beta
*/
public MessageBuilder(Message msg, long recipientID, IDGenerator iDGenerator) {
this(msg.getRecipientID(), recipientID, iDGenerator.next());
this.attachment = msg.getAttachment();
this.creationDate = LocalDateTime.now();
this.forwarded = true;
this.text = msg.getText();
this.status = MessageStatus.WAITING;
}
/**
* Creates an instance of {@link Message} with the previously supplied values.
* If a mandatory value is not set, a default value will be used instead:<br>
* <br>
* <table border="1">
* <tr>
* <td>{@code date}</td>
* <td>{@code LocalDateTime.now()} and {@code null} for {@code receivedDate} and
* {@code readDate}</td>
* <tr>
* <tr>
* <td>{@code text}</td>
* <td>{@code ""}</td>
* <tr>
* <tr>
* <td>{@code status}</td>
* <td>{@code MessageStatus.WAITING}</td>
* <tr>
* </table>
*
* @return a new instance of {@link Message}
* @since Envoy Common v0.2-alpha
*/
public Message build() {
supplyDefaults();
return new Message(id, senderID, recipientID, creationDate, receivedDate, readDate, text, attachment, status, forwarded);
}
/**
* Creates an instance of {@link GroupMessage} with the previously supplied
* values. <br>
* <b> Sets all member statuses to {@link MessageStatus#WAITING}.</b><br>
* If a mandatory value is not set, a default value will be used
* instead:<br>
* <br>
* <table border="1">
* <tr>
* <td>{@code date}</td>
* <td>{@code new Date()}</td>
* <tr>
* <tr>
* <td>{@code text}</td>
* <td>{@code ""}</td>
* <tr>
* </table>
*
* @param group the {@link Group} that is used to fill the map of member
* statuses
* @return a new instance of {@link GroupMessage}
* @since Envoy Common v0.2-alpha
*/
public GroupMessage buildGroupMessage(Group group) {
var memberStatuses = new HashMap<Long, Message.MessageStatus>();
group.getContacts().forEach(user -> memberStatuses.put(user.getID(), MessageStatus.WAITING));
return buildGroupMessage(group, memberStatuses);
}
/**
* Creates an instance of {@link GroupMessage} with the previously supplied
* values. If a mandatory value is not set, a default value will be used
* instead:<br>
* <br>
* <table border="1">
* <tr>
* <td>{@code date}</td>
* <td>{@code new Date()}</td>
* <tr>
* <tr>
* <td>{@code text}</td>
* <td>{@code ""}</td>
* <tr>
* </table>
*
* @param group the {@link Group} that is used to fill the map of
* member statuses
* @param memberStatuses the map of all current statuses
* @return a new instance of {@link GroupMessage}
* @since Envoy Common v0.1-beta
*/
public GroupMessage buildGroupMessage(Group group, Map<Long, MessageStatus> memberStatuses) {
if (group == null || memberStatuses == null) throw new NullPointerException();
supplyDefaults();
return new GroupMessage(id, senderID, recipientID, creationDate, receivedDate, readDate, text, attachment, status, forwarded, memberStatuses);
}
private void supplyDefaults() {
if (creationDate == null) creationDate = LocalDateTime.now();
if (text == null) text = "";
if (status == null) status = MessageStatus.WAITING;
}
/**
* @param creationDate the creation date of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setCreationDate(LocalDateTime creationDate) {
this.creationDate = creationDate;
return this;
}
/**
* @param receivedDate the received date of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.1-beta
*/
public MessageBuilder setReceivedDate(LocalDateTime receivedDate) {
this.receivedDate = receivedDate;
return this;
}
/**
* @param readDate the read date of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.1-beta
*/
public MessageBuilder setReadDate(LocalDateTime readDate) {
this.readDate = readDate;
return this;
}
/**
* @param text the text of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setText(String text) {
this.text = text;
return this;
}
/**
* @param attachment the {@link Attachment} of the {@link Message} to
* create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setAttachment(Attachment attachment) {
this.attachment = attachment;
return this;
}
/**
* @param status the {@link MessageStatus} of the {@link Message} to create
* @return this {@link MessageBuilder}
* @since Envoy Common v0.2-alpha
*/
public MessageBuilder setStatus(Message.MessageStatus status) {
this.status = status;
return this;
}
/**
* @param forwarded sets whether this message is a forwarded message or not
* @return this {@link MessageBuilder}
* @since Envoy Common v0.1-beta
*/
public MessageBuilder setForwarded(boolean forwarded) {
this.forwarded = forwarded;
return this;
}
}

View File

@ -0,0 +1,137 @@
package envoy.data;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;
/**
* Represents a unique user with a unique, numeric ID, a name and a current
* {@link UserStatus}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>User.java</strong><br>
* Created: <strong>28.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public final class User extends Contact {
private UserStatus status;
/**
* Used to serialize contact list to a maximum depth of one.
*/
private transient boolean serializeContacts = true;
private static final long serialVersionUID = 1L;
/**
* This enumeration defines all possible statuses a user can have.
*
* @since Envoy Common v0.2-alpha
*/
public static enum UserStatus {
/**
* select this, if a user is online and can be interacted with
*/
ONLINE,
/**
* select this, if a user is online but unavailable at the moment (sudden
* interruption)
*/
AWAY,
/**
* select this, if a user is online but unavailable at the moment (polite way)
*/
BUSY,
/**
* select this, if a user is offline
*/
OFFLINE;
}
/**
* Initializes a {@link User}. <br>
* The {@link UserStatus} is set to {@link UserStatus#ONLINE}.
* No contacts are initialized.
*
* @param id unique ID
* @param name user name
* @since Envoy Common v0.2-alpha
*/
public User(long id, String name) {
super(id, name, new HashSet<>());
status = UserStatus.ONLINE;
}
/**
* Initializes a {@link User}. <br>
* The {@link UserStatus} is set to {@link UserStatus#ONLINE}.
*
* @param id unique ID
* @param name user name
* @param contacts the contacts of this user
* @since Envoy Common v0.2-alpha
*/
public User(long id, String name, Set<Contact> contacts) {
super(id, name, contacts);
status = UserStatus.ONLINE;
}
/**
* Initializes a {@link User}.
*
* @param id unique ID
* @param name user name
* @param status the status of this user
* @param contacts the contacts of this user
* @since Envoy Common v0.2-alpha
*/
public User(long id, String name, UserStatus status, Set<Contact> contacts) {
super(id, name, contacts);
this.status = status;
}
@Override
public String toString() { return String.format("User[id=%d,name=%s,status=%s,contacts=%s]", getID(), getName(), status, getContacts()); }
/**
* @return the current status of this user
* @since Envoy Common v0.2-alpha
*/
public UserStatus getStatus() { return status; }
/**
* @param status the next status of this user
* @since Envoy Common v0.2-alpha
*/
public void setStatus(UserStatus status) { this.status = status; }
private void readObject(ObjectInputStream inputStream) throws Exception {
inputStream.defaultReadObject();
var contacts = Contact.class.getDeclaredField("contacts");
contacts.setAccessible(true);
contacts.set(this, inputStream.readObject());
}
private void writeObject(ObjectOutputStream outputStream) throws Exception {
outputStream.defaultWriteObject();
if (serializeContacts) {
getContacts().stream().filter(User.class::isInstance).map(User.class::cast).forEach(user -> user.serializeContacts = false);
outputStream.writeObject(getContacts());
} else outputStream.writeObject(new HashSet<>());
}
/**
* @param serializeContacts whether the contacts of this {@link User} should be
* serialized
* @since Envoy Common v0.1-beta
*/
public void serializeContacts(boolean serializeContacts) { this.serializeContacts = serializeContacts; }
}

View File

@ -0,0 +1,10 @@
/**
* This package contains all data objects that are used both by Envoy Client and
* by Envoy Server Standalone.
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy common v0.1-beta
*/
package envoy.data;

View File

@ -0,0 +1,28 @@
package envoy.event;
/**
* This enum declares all modification possibilities for a given container.<br>
* These can be: {@link ElementOperation#ADD} or
* {@link ElementOperation#REMOVE}.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ElementOperation.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public enum ElementOperation {
/**
* Select this element, if the given element should be added to the given
* container.
*/
ADD,
/**
* Select this element, if the given element should be removed from the given
* container.
*/
REMOVE
}

View File

@ -0,0 +1,49 @@
package envoy.event;
import java.io.Serializable;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>Event.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of the Event
* @since Envoy v0.2-alpha
*/
public abstract class Event<T> implements Serializable {
protected final T value;
private static final long serialVersionUID = 0L;
protected Event(T value) { this.value = value; }
/**
* @return the data associated with this event
*/
public T get() { return value; }
@Override
public String toString() { return String.format("%s[value=%s]", this.getClass().getSimpleName(), value); }
/**
* Serves as a super class for events that do not carry a value.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Event.java</strong><br>
* Created: <strong>11 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public static abstract class Valueless extends Event<Void> {
private static final long serialVersionUID = 0L;
protected Valueless() { super(null); }
@Override
public String toString() { return this.getClass().getSimpleName(); }
}
}

View File

@ -0,0 +1,83 @@
package envoy.event;
import java.util.*;
import java.util.function.Consumer;
/**
* This class handles events by allowing event handlers to register themselves
* and then be notified about certain events dispatched by the event bus.<br>
* <br>
* The event bus is a singleton and can be used across the entire application to
* guarantee the propagation of events.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>EventBus.java</strong><br>
* Created: <strong>04.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha
*/
public class EventBus {
/**
* Contains all event handler instances registered at this event bus as values
* mapped to by their supported event classes.
*/
private Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> handlers = new HashMap<>();
/**
* The singleton instance of this event bus that is used across the
* entire application.
*/
private static EventBus eventBus = new EventBus();
/**
* This constructor is not accessible from outside this class because a
* singleton instance of it is provided by the {@link EventBus#getInstance()}
* method.
*/
private EventBus() {}
/**
* @return the singleton instance of the event bus
* @since Envoy v0.2-alpha
*/
public static EventBus getInstance() { return eventBus; }
/**
* Registers an event handler to be notified when an
* event of a certain type is dispatched.
*
* @param <T> the type of event values to notify the handler about
* @param eventClass the class which the event handler is subscribing to
* @param handler the event handler to register
* @since Envoy v0.2-alpha
*/
@SuppressWarnings("unchecked")
public <T extends Event<?>> void register(Class<T> eventClass, Consumer<T> handler) {
if (!handlers.containsKey(eventClass)) handlers.put(eventClass, new ArrayList<>());
handlers.get(eventClass).add((Consumer<Event<?>>) handler);
}
/**
* Dispatches an event to every event handler subscribed to it.
*
* @param event the {@link Event} to dispatch
* @since Envoy v0.2-alpha
*/
public void dispatch(Event<?> event) {
handlers.keySet()
.stream()
.filter(event.getClass()::equals)
.map(handlers::get)
.flatMap(List::stream)
.forEach(h -> h.accept(event));
}
/**
* @return a map of all event handler instances currently registered at this
* event bus with the event classes they are subscribed to as keys
* @since Envoy v0.2-alpha
*/
public Map<Class<? extends Event<?>>, List<Consumer<Event<?>>>> getHandlers() { return handlers; }
}

View File

@ -0,0 +1,42 @@
package envoy.event;
import java.util.HashSet;
import java.util.Set;
import envoy.data.User;
/**
* This event creates a group with the given name.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupCreation.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public class GroupCreation extends Event<String> {
private final Set<Long> initialMemberIDs;
private static final long serialVersionUID = 0L;
/**
* @param value the name of this group at creation time
* @param initialMemberIDs the IDs of all {@link User}s that should be group
* members from the beginning on (excluding the creator
* of this group)
* @since Envoy Common v0.1-beta
*/
public GroupCreation(String value, Set<Long> initialMemberIDs) {
super(value);
this.initialMemberIDs = (initialMemberIDs != null) ? initialMemberIDs : new HashSet<>();
}
/**
* @return the IDs of all {@link User}s that are members from the beginning
* (excluding the creator of this group)
* @since Envoy Common v0.1-beta
*/
public Set<Long> getInitialMemberIDs() { return initialMemberIDs; }
}

View File

@ -0,0 +1,45 @@
package envoy.event;
import java.time.LocalDateTime;
import envoy.data.GroupMessage;
import envoy.data.Message.MessageStatus;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupMessageStatusChange.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.1-beta
*/
public class GroupMessageStatusChange extends MessageStatusChange {
private final long memberID;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link GroupMessageStatusChange}.
*
* @param id the ID of the {@link GroupMessage} this event is related to
* @param status the status of this specific members {@link GroupMessage}
* @param date the date at which the MessageStatus change occurred for
* this specific member
* @param memberID the ID of the group member that caused the status change
* @since Envoy Common v0.1-beta
*/
public GroupMessageStatusChange(long id, MessageStatus status, LocalDateTime date, long memberID) {
super(id, status, date);
this.memberID = memberID;
}
/**
* @return the memberID which the user who sends this event has
* @since Envoy Common v0.1-beta
*/
public long getMemberID() { return memberID; }
@Override
public String toString() { return String.format("GroupMessageStatusChange[meta=%s,memberID=%d]", super.toString(), memberID); }
}

View File

@ -0,0 +1,65 @@
package envoy.event;
import envoy.data.Contact;
import envoy.data.Group;
import envoy.data.User;
/**
* This event is used to communicate changes in the group size between client
* and server.<br>
* Possible actions are adding or removing certain {@link User}s to or from a
* certain {@link Group}.
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>GroupResize.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public class GroupResize extends Event<User> {
private final long groupID;
private final ElementOperation operation;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link GroupResize} through a Contact where the name has
* already been set.
*
* @param user the {@link User} who wants to join or leave a group
* @param group the {@link Group} he wants to join or leave
* @param operation describes what to do with the given user:<br>
* add him to this group or remove him from it
* @since Envoy Common v0.2-alpha
*/
public GroupResize(User user, Group group, ElementOperation operation) {
super(user);
if (group.getContacts().contains(user)) {
if (operation.equals(ElementOperation.ADD)) throw new IllegalArgumentException(
"Cannot add " + user + " to group " + group.getID() + " because he is already a member of this group");
} else if (operation.equals(ElementOperation.REMOVE))
throw new IllegalArgumentException("Cannot remove " + user + " from group " + group.getID() + " because he is no part of this group");
groupID = group.getID();
this.operation = operation;
}
/**
* @return the ID of the {@link Contact} this event is related to
* @since Envoy Common v0.2-alpha
*/
public long getGroupID() { return groupID; }
/**
* @return the operationType
* @since Envoy Common v0.1-beta
*/
public ElementOperation getOperation() { return operation; }
/**
* {@inheritDoc}
*/
@Override
public String toString() { return String.format("GroupResize[userid=%d,groupid=%d,operation=%s]", get(), groupID, operation); }
}

View File

@ -0,0 +1,63 @@
package envoy.event;
/**
* Signifies to the client that the handshake failed for the attached
* reason.
* <p>
* Project: <strong>envoy-common</strong><br>
* File: <strong>HandshakeRejection.java</strong><br>
* Created: <strong>28 Jan 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
public class HandshakeRejection extends Event<String> {
/**
* Select this value if a given password hash or user name was incorrect.
*
* @since Envoy Common v0.3-alpha
*/
public static final String WRONG_PASSWORD_OR_USER = "Incorrect user name or password.";
/**
* Select this value if a given user name for a registration is already taken.
*
* @since Envoy Common v0.1-beta
*/
public static final String USERNAME_TAKEN = "Incorrect user name or password.";
/**
* Select this value if the version of the client is incompatible with the
* server.
*
* @since Envoy Common v0.1-beta
*/
public static final String WRONG_VERSION = "Incompatible client version";
/**
* Select this value if the handshake could not be completed for some different
* reason.
*
* @since Envoy Common v0.3-alpha
*/
public static final String INTERNAL_ERROR = "An internal error occured.";
private static final long serialVersionUID = 0L;
/**
* Creates an instance of {@link HandshakeRejection} with the generic
* {@link HandshakeRejection#INTERNAL_ERROR} reason.
*
* @since Envoy Common v0.3-alpha
*/
public HandshakeRejection() { super(INTERNAL_ERROR); }
/**
* Creates an instance of {@link HandshakeRejection}.
*
* @param reason the reason why the handshake was rejected
* @since Envoy Common v0.3-alpha
*/
public HandshakeRejection(String reason) { super(reason); }
}

View File

@ -0,0 +1,17 @@
package envoy.event;
/**
* Signifies to the server that the client needs a new
* {@link envoy.data.IDGenerator} instance.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>IDGeneratorRequest.java</strong><br>
* Created: <strong>28 Jan 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
public class IDGeneratorRequest extends Event.Valueless {
private static final long serialVersionUID = 1431107413883364583L;
}

View File

@ -0,0 +1,59 @@
package envoy.event;
import java.time.LocalDateTime;
import envoy.data.Message;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>MessageStatusChange.java</strong><br>
* Created: <strong>6 Jan 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class MessageStatusChange extends Event<Message.MessageStatus> {
private final long id;
private final LocalDateTime date;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link MessageStatusChange}.
*
* @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
* @since Envoy Common v0.2-alpha
*/
public MessageStatusChange(long id, Message.MessageStatus status, LocalDateTime date) {
super(status);
this.id = id;
this.date = date;
}
/**
* Initializes a {@link MessageStatusChange} through a message.
*
* @param message the message from which to build the event
* @since Envoy Common v0.2-alpha
*/
public MessageStatusChange(Message message) { this(message.getID(), message.getStatus(), LocalDateTime.now()); }
/**
* @return the ID of the {@link Message} this event is related to
* @since Envoy Common v0.2-alpha
*/
public long getID() { return id; }
/**
* @return the date at which the status change occurred
* @since Envoy Common v0.2-alpha
*/
public LocalDateTime getDate() { return date; }
@Override
public String toString() { return String.format("MessageStatusChange[id=%d,status=%s,date=%s]", id, value, date); }
}

View File

@ -0,0 +1,52 @@
package envoy.event;
import envoy.data.Contact;
/**
* This event informs<br>
* a) the server of the name change of a user or a group.
* b) another user of this users name change.
*
* Project: <strong>envoy-common</strong><br>
* File: <strong>NameChange.java</strong><br>
* Created: <strong>25 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
public class NameChange extends Event<String> {
private final long id;
private static final long serialVersionUID = 0L;
/**
* Creates a new {@link NameChange} for a user or a group.
*
* @param contactID the id of the {@link Contact} who wishes to change his name
* @param newName the new name of this contact
* @since Envoy Common v0.1-beta
*/
public NameChange(long contactID, String newName) {
super(newName);
id = contactID;
}
/**
* Initializes a {@link NameChange} through a Contact where the name has
* already been set.
*
* @param contact the contact whose name was updated
* @since Envoy Common v0.2-alpha
*/
public NameChange(Contact contact) { this(contact.getID(), contact.getName()); }
/**
* @return the ID of the {@link Contact} this event is related to
* @since Envoy Common v0.2-alpha
*/
public long getID() { return id; }
@Override
public String toString() { return String.format("NameChange[id=%d,name=%s]", id, value); }
}

View File

@ -0,0 +1,49 @@
package envoy.event;
import envoy.data.User;
import envoy.data.User.UserStatus;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>UserStatusChange.java</strong><br>
* Created: <strong>1 Feb 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.2-alpha
*/
public class UserStatusChange extends Event<UserStatus> {
private final long id;
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link UserStatusChange}.
*
* @param id the ID of the {@link User} this event is related to
* @param status the status of the {@link User} this event is related
* to
* @since Envoy Common v0.2-alpha
*/
public UserStatusChange(long id, User.UserStatus status) {
super(status);
this.id = id;
}
/**
* Initializes a {@link UserStatusChange} through a User.
*
* @param user the User from which to build the event
* @since Envoy Common v0.2-alpha
*/
public UserStatusChange(User user) { this(user.getID(), user.getStatus()); }
/**
* @return the ID of the {@link User} this event is related to
* @since Envoy Common v0.2-alpha
*/
public long getID() { return id; }
@Override
public String toString() { return String.format("UserStatusChange[id=%d,status=%s]", id, value); }
}

View File

@ -0,0 +1,40 @@
package envoy.event.contact;
import envoy.data.Contact;
import envoy.event.ElementOperation;
import envoy.event.Event;
/**
* Signifies the modification of a contact list.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactOperation.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public class ContactOperation extends Event<Contact> {
private final ElementOperation operationType;
private static final long serialVersionUID = 1L;
/**
* Initializes a {@link ContactOperation}.
*
* @param contact the user on which the operation is performed
* @param operationType the type of operation to perform
* @since Envoy Common v0.2-alpha
*/
public ContactOperation(Contact contact, ElementOperation operationType) {
super(contact);
this.operationType = operationType;
}
/**
* @return the type of operation to perform
* @since Envoy Common v0.2-alpha
*/
public ElementOperation getOperationType() { return operationType; }
}

View File

@ -0,0 +1,26 @@
package envoy.event.contact;
import envoy.event.Event;
/**
* Requests a contact search from the server.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactSearchRequest.java</strong><br>
* Created: <strong>05.02.2020</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Common v0.2-alpha
*/
public class ContactSearchRequest extends Event<String> {
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link ContactSearchRequest}.
*
* @param searchPhrase the search phrase to use in the contact search
* @since Envoy Common v0.2-alpha
*/
public ContactSearchRequest(String searchPhrase) { super(searchPhrase); }
}

View File

@ -0,0 +1,29 @@
package envoy.event.contact;
import java.util.List;
import envoy.data.Contact;
import envoy.event.Event;
/**
* Contains a list of {@link Contact}s for which a search was performed.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>ContactSearchResult.java</strong><br>
* Created: <strong>11 Feb 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class ContactSearchResult extends Event<List<Contact>> {
private static final long serialVersionUID = 0L;
/**
* Creates an instance of {@link ContactSearchResult}.
*
* @param users the users found during the search
* @since Envoy Common v0.2-alpha
*/
public ContactSearchResult(List<Contact> users) { super(users); }
}

View File

@ -0,0 +1,13 @@
/**
* This package contains all contact-related events.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>package-info.java</strong><br>
* Created: <strong>28 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy Common v0.1-beta
*/
package envoy.event.contact;

View File

@ -0,0 +1,10 @@
/**
* This package contains all events that can be sent or received by Envoy Client
* or Envoy Server Standalone.
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy common v0.1-beta
*/
package envoy.event;

View File

@ -0,0 +1,35 @@
package envoy.exception;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>EnvoyException.java</strong><br>
* Created: <strong>27 Oct 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy v0.1-alpha
*/
public class EnvoyException extends Exception {
private static final long serialVersionUID = 2096147309395387479L;
/**
* @param message the message to display once this Exception is thrown
* @since Envoy Common v0.2-alpha
*/
public EnvoyException(String message) { super(message); }
/**
* @param message the message to display once this Exception is thrown
* @param cause the {@link Throwable} which resulted in the throw of an
* EnvoyException
* @since Envoy Common v0.2-alpha
*/
public EnvoyException(String message, Throwable cause) { super(message, cause); }
/**
* @param cause the {@link Throwable} which resulted in the throw of an
* EnvoyException
* @since Envoy Common v0.2-alpha
*/
public EnvoyException(Throwable cause) { super(cause); }
}

View File

@ -0,0 +1,9 @@
/**
* This package contains all Envoy-specific exceptions.
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy common v0.1-beta
*/
package envoy.exception;

View File

@ -0,0 +1,40 @@
package envoy.util;
import java.util.regex.Pattern;
/**
* Implements contact name validation.
* <p>
* Project: <strong>envoy-common</strong><br>
* File: <strong>Bounds.java</strong><br>
* Created: <strong>25.06.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.1-beta
*/
public class Bounds {
private Bounds() {}
/**
* The regular expression against which contact names should be validated.
*
* @since Envoy Common v0.1-beta
*/
public static final Pattern CONTACT_NAME_PATTERN = Pattern.compile("^\\w[a-zA-Z0-9-]{2,15}$");
/**
* @param contactName the contact name to validate
* @return {@code true} if the given contact name is valid
* @since Envoy Common v0.1-beta
*/
public static boolean isValidContactName(String contactName) { return CONTACT_NAME_PATTERN.matcher(contactName).matches(); }
/**
* @return the maximum size allowed for a user/ group name.
* @apiNote has to be updated manually if {@link Bounds#CONTACT_NAME_PATTERN}
* gets updated.
* @since Envoy Common v0.1-beta
*/
public static int maximumUsernameSize() { return 16; }
}

View File

@ -0,0 +1,126 @@
package envoy.util;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.*;
import envoy.data.Config;
/**
* Configures the {@link java.util.logging} API to output the log into the
* console and a log file.
* <p>
* Call the {@link EnvoyLog#attach(String)} method to configure a part of the
* logger hierarchy.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
* @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy Common v0.1-beta
*/
public class EnvoyLog {
private static FileHandler fileHandler;
private static StreamHandler consoleHandler;
private static boolean initialized;
private EnvoyLog() {}
/**
* Initializes logging. Call this method before calling the
* {@link EnvoyLog#attach(String)} method.
*
* @param config the config providing the console and log file barriers
* @since Envoy Common v0.1-beta
*/
public static void initialize(Config config) {
if (initialized) throw new IllegalStateException("EnvoyLog is already initialized");
// Remove default console handler
LogManager.getLogManager().reset();
// Configure log file
final File logFile = new File((File) config.get("homeDirectory").get(),
"log/envoy_user_" + DateTimeFormatter.ofPattern("yyyy-MM-dd--hh-mm-mm").format(LocalDateTime.now()) + ".log");
logFile.getParentFile().mkdirs();
// Configure formatting
// Sample log entry: [2020-06-13 16:50:26] [INFORMATION] [envoy.client.ui.Startup] Closing connection...
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %6$s%n");
final SimpleFormatter formatter = new SimpleFormatter();
try {
fileHandler = new FileHandler(logFile.getAbsolutePath());
fileHandler.setLevel((Level) config.get("fileLevelBarrier").get());
fileHandler.setFormatter(formatter);
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
consoleHandler = new StreamHandler(System.out, formatter) {
@Override
public synchronized void publish(LogRecord record) {
super.publish(record);
flush();
}
};
consoleHandler.setLevel((Level) config.get("consoleLevelBarrier").get());
consoleHandler.setFormatter(formatter);
initialized = true;
}
/**
* Configures all loggers that are contained within the hierarchy of a specific
* path to use the console and file handlers.
*
* @param path the path to the loggers to configure
* @since Envoy Common v0.1-beta
*/
public static void attach(String path) {
if (!initialized) throw new IllegalStateException("EnvoyLog is not initialized");
// Get root logger
final Logger logger = Logger.getLogger(path);
// Add handlers
if (fileHandler != null) logger.addHandler(fileHandler);
logger.addHandler(consoleHandler);
// Delegate logger level filtering to the handlers
logger.setLevel(Level.ALL);
}
/**
* Creates a logger for a specified class, which output will be displayed inside
* the console and written to the log file.
*
* @param logClass the class in which the logger is used
* @return the created logger
* @since Envoy Common v0.1-beta
*/
public static Logger getLogger(Class<?> logClass) { return Logger.getLogger(logClass.getCanonicalName()); }
/**
* Defines the logger level required for a record to be written to the log file.
*
* @param fileLevelBarrier the log file level
* @since Envoy Common v0.1-beta
*/
public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); }
/**
* Defines the logger level required for a record to be written to the console.
*
* @param consoleLevelBarrier the console logger level
* @since Envoy Common v0.1-beta
*/
public static void setConsoleLevelBarrier(Level consoleLevelBarrier) {
if (consoleHandler != null) consoleHandler.setLevel(consoleLevelBarrier);
}
}

View File

@ -0,0 +1,153 @@
package envoy.util;
import java.io.*;
/**
* Defines utility methods related to serialization.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>SerializationUtils.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.2-alpha
*/
public class SerializationUtils {
private SerializationUtils() {}
/**
* Converts an integer into a byte array.
*
* @param n the integer to convert
* @return a byte array of length 4
* @since Envoy Common v0.2-alpha
*/
public static byte[] intToBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8), (byte) n }; }
/**
* Converts four bytes in byte array to an integer
*
* @param bytes the bytes to convert from
* @param offset the offset at which four bytes are read
* @return the converted integer
* @since Envoy Common v0.2-alpha
*/
public static int bytesToInt(byte[] bytes, int offset) {
return ((bytes[offset] & 0xFF) << 24) | ((bytes[offset + 1] & 0xFF) << 16) | ((bytes[offset + 2] & 0xFF) << 8)
| ((bytes[offset + 3] & 0xFF) << 0);
}
/**
* Deserializes an arbitrary {@link Serializable} object from a file.
*
* @param <T> 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
* @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 extends Serializable> T read(File file, Class<T> serializedClass) throws IOException, ClassNotFoundException {
if (file == null) throw new NullPointerException("File is null");
return read(new FileInputStream(file), serializedClass);
}
/**
* Deserializes an arbitrary {@link Serializable} object from a byte array.
*
* @param <T> 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 extends Serializable> T read(byte[] bytes, Class<T> serializedClass) throws IOException, ClassNotFoundException {
return read(new ByteArrayInputStream(bytes), serializedClass);
}
/**
* Deserializes an arbitrary {@link Serializable} object from a stream.
*
* @param <T> 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
* @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 extends Serializable> T read(InputStream in, Class<T> serializedClass) throws IOException, ClassNotFoundException {
try (ObjectInputStream oin = new ObjectInputStream(in)) {
return serializedClass.cast(oin.readObject());
}
}
/**
* Serializes arbitrary objects to a file.
*
* @param file the file to serialize to
* @param objs the objects to serialize
* @throws IOException if an error occurred during serialization
* @since Envoy Common v0.2-alpha
*/
public static void write(File file, Object... objs) throws IOException {
if (file == null) throw new NullPointerException("File is null");
if (objs == null) throw new NullPointerException("Null array passed to serialize");
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
for (var obj : objs)
out.writeObject(obj);
}
}
/**
* 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.
*
* @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
byte[] objBytes = writeToByteArray(obj);
// Get length of byte array in bytes
byte[] objLen = intToBytes(objBytes.length);
// Write length and byte array
out.write(objLen);
out.write(objBytes);
}
}

View File

@ -0,0 +1,11 @@
/**
* This package contains general useful classes that can be used by both Envoy
* Client and Envoy Server Standalone and that could not be assigned to any
* other package.
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy common v0.1-beta
*/
package envoy.util;

View File

@ -0,0 +1,19 @@
/**
* This module contains all packages that are used by Envoy Client and Envoy
* Server Standalone at the same time.
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S.K. Engelbart
* @since Envoy Common v0.1-beta
*/
module envoy.common {
exports envoy.data;
exports envoy.util;
exports envoy.exception;
exports envoy.event;
exports envoy.event.contact;
requires transitive java.logging;
}

View File

@ -0,0 +1,34 @@
package envoy.data;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
import java.util.Set;
import org.junit.jupiter.api.Test;
import envoy.data.User.UserStatus;
import envoy.util.SerializationUtils;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>UserTest.java</strong><br>
* Created: <strong>31 Mar 2020</strong><br>
*
* @author Leon Hofmeister
* @since Envoy Common v0.1-beta
*/
class UserTest {
@Test
void test() throws IOException, ClassNotFoundException {
User user2 = new User(2, "kai");
User user3 = new User(3, "ai");
User user4 = new User(4, "ki", Set.of(user2, user3));
User user5 = new User(5, "ka", Set.of(user2, user3, user4));
User user = new User(1, "maxi", UserStatus.AWAY, Set.of(user2, user3, user4, user5));
var serializedUser = SerializationUtils.writeToByteArray(user);
var deserializedUser = SerializationUtils.read(serializedUser, User.class);
assertEquals(user.getContacts(), deserializedUser.getContacts());
}
}