Merge pull request #7 from informatik-ag-ngl/f/login_or_registration

Adjusted data classes for user registration and id generation
This commit is contained in:
Kai S. K. Engelbart 2020-01-28 20:00:06 +01:00 committed by GitHub
commit f0ad54e670
12 changed files with 172 additions and 56 deletions

View File

@ -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/<project>=UTF-8

View File

@ -1,17 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="envoy-common">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module>
</project-modules>

View File

@ -18,6 +18,11 @@
<build>
<finalName>envoy-common</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -1,32 +1,39 @@
package envoy.data;
import java.io.Serializable;
/**
* Generates increasing IDs between two numbers.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>MessageIdGenerator.java</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 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
* @param size the amount of IDs to provide
* @since Envoy Common v0.2-alpha
*/
public MessageIdGenerator(long begin, long end) {
current = begin;
this.end = end;
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

View File

@ -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; }
}

View File

@ -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.
@ -64,24 +64,23 @@ 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;
this.creationDate = date;
this.creationDate = creationDate;
this.text = text;
this.attachment = attachment;
this.status = status;
}
/**
@ -187,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;
}
}

View File

@ -10,7 +10,7 @@ import envoy.data.Message.MessageStatus;
* 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
*/
@ -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 {
* <td>{@code MessageStatus.WAITING}</td>
* <tr>
* </table>
*
*
* @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;
}

View File

@ -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); }

View File

@ -1,5 +1,7 @@
package envoy.event;
import java.io.Serializable;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>Event.java</strong><br>
@ -9,11 +11,10 @@ package envoy.event;
* @param <T> the type of the Event
* @since Envoy v0.2-alpha
*/
public interface Event<T> {
public interface Event<T> extends Serializable {
/**
* @return the data associated with this event
*/
T get();
default T get() { return null; }
}

View File

@ -0,0 +1,41 @@
package envoy.event;
/**
* Signifies to the client that the handshake failed for the attached
* reason.<br>
* <br>
* Project: <strong>envoy-common</strong><br>
* File: <strong>HandshakeRejectionEvent.java</strong><br>
* Created: <strong>28 Jan 2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Common v0.3-alpha
*/
public class HandshakeRejectionEvent implements Event<String> {
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; }
}

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 implements Event<Void> {
private static final long serialVersionUID = 1431107413883364583L;
}

View File

@ -18,21 +18,31 @@ public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
private final Message.MessageStatus status;
private final Date date;
private static final long serialVersionUID = 4566145392192761313L;
/**
* Initializes a {@link MessageStatusChangeEvent}.
*
* @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;
}
/**
* Initializes 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