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:
commit
f0ad54e670
@ -1,5 +1,6 @@
|
|||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
encoding//src/main/java=UTF-8
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/main/resources=UTF-8
|
||||||
encoding//src/test/java=UTF-8
|
encoding//src/test/java=UTF-8
|
||||||
encoding//src/test/resources=UTF-8
|
encoding//src/test/resources=UTF-8
|
||||||
encoding/<project>=UTF-8
|
encoding/<project>=UTF-8
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<wb-module deploy-name="envoy-common">
|
<wb-module deploy-name="envoy-common">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
<wb-resource deploy-path="/" source-path="/src/main/java"/>
|
||||||
|
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</wb-module>
|
</wb-module>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</project-modules>
|
</project-modules>
|
||||||
|
5
pom.xml
5
pom.xml
@ -18,6 +18,11 @@
|
|||||||
|
|
||||||
<build>
|
<build>
|
||||||
<finalName>envoy-common</finalName>
|
<finalName>envoy-common</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
@ -1,32 +1,39 @@
|
|||||||
package envoy.data;
|
package envoy.data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates increasing IDs between two numbers.<br>
|
* Generates increasing IDs between two numbers.<br>
|
||||||
* <br>
|
* <br>
|
||||||
* Project: <strong>envoy-common</strong><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>
|
* Created: <strong>31.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public class MessageIdGenerator {
|
public class IdGenerator implements Serializable {
|
||||||
|
|
||||||
private final long end;
|
private final long end;
|
||||||
private long current;
|
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 begin the first ID
|
||||||
* @param end the last ID
|
* @param size the amount of IDs to provide
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public MessageIdGenerator(long begin, long end) {
|
public IdGenerator(long begin, long size) {
|
||||||
current = begin;
|
current = begin;
|
||||||
this.end = end;
|
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
|
* @return {@code true} if there are unused IDs remaining
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
@ -19,20 +19,24 @@ public class LoginCredentials implements Serializable {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final byte[] passwordHash;
|
private final byte[] passwordHash;
|
||||||
|
private final boolean registration;
|
||||||
|
|
||||||
private static final long serialVersionUID = -7395245059059523314L;
|
private static final long serialVersionUID = -7395245059059523314L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an in stance of {@link LoginCredentials}.
|
* Creates an in stance of {@link LoginCredentials}.
|
||||||
*
|
*
|
||||||
* @param name the name of the user
|
* @param name the name of the user
|
||||||
* @param password the password of the user (will be converted to a hash)
|
* @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
|
* @throws NoSuchAlgorithmException if the algorithm used is unknown
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public LoginCredentials(String name, char[] password) throws NoSuchAlgorithmException {
|
public LoginCredentials(String name, char[] password, boolean registration) throws NoSuchAlgorithmException {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
passwordHash = getSha256(toByteArray(password));
|
passwordHash = getSha256(toByteArray(password));
|
||||||
|
this.registration = registration;
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); }
|
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);
|
form.format("LoginCredentials[name=%s,passwordHash=", name);
|
||||||
for (byte element : passwordHash)
|
for (byte element : passwordHash)
|
||||||
form.format("%02x", element);
|
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
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public byte[] getPasswordHash() { return passwordHash; }
|
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; }
|
||||||
}
|
}
|
@ -24,7 +24,7 @@ public class Message implements Serializable {
|
|||||||
*
|
*
|
||||||
* @since Envoy Common v0.2-alpha
|
* @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.
|
* 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
|
* this class provides {@code null} checks and default values for all
|
||||||
* properties.
|
* properties.
|
||||||
*
|
*
|
||||||
* @param id unique ID
|
* @param id unique ID
|
||||||
* @param senderId the ID of the user who sends the message
|
* @param senderId the ID of the user who sends the message
|
||||||
* @param recipientId the ID of the user who receives the message
|
* @param recipientId the ID of the user who receives the message
|
||||||
* @param date the creation date of the message
|
* @param creationDate the creation date of the message
|
||||||
* @param text the text content of the message
|
* @param text the text content of the message
|
||||||
* @param attachment the attachment of the message, if present
|
* @param attachment the attachment of the message, if present
|
||||||
* @param status the current {@link MessageStatus} of the message
|
* @param status the current {@link MessageStatus} of the message
|
||||||
* @since Envoy Common v0.2-alpha
|
* @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.id = id;
|
||||||
this.senderId = senderId;
|
this.senderId = senderId;
|
||||||
this.recipientId = recipientId;
|
this.recipientId = recipientId;
|
||||||
this.creationDate = date;
|
this.creationDate = creationDate;
|
||||||
this.text = text;
|
this.text = text;
|
||||||
this.attachment = attachment;
|
this.attachment = attachment;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,6 +186,6 @@ public class Message implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public void setStatus(MessageStatus status) {
|
public void setStatus(MessageStatus status) {
|
||||||
if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time");
|
if (status.ordinal() < this.status.ordinal()) throw new IllegalStateException("This message is moving backwards in time");
|
||||||
else this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,7 +10,7 @@ import envoy.data.Message.MessageStatus;
|
|||||||
* Project: <strong>envoy-common</strong><br>
|
* Project: <strong>envoy-common</strong><br>
|
||||||
* File: <strong>MessageBuilder.java</strong><br>
|
* File: <strong>MessageBuilder.java</strong><br>
|
||||||
* Created: <strong>31.12.2019</strong><br>
|
* Created: <strong>31.12.2019</strong><br>
|
||||||
*
|
*
|
||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
@ -21,7 +21,7 @@ public class MessageBuilder {
|
|||||||
|
|
||||||
// Properties with default values
|
// Properties with default values
|
||||||
private long id;
|
private long id;
|
||||||
private Date date;
|
private Date creationDate;
|
||||||
private String text;
|
private String text;
|
||||||
private MessageAttachment<?> attachment;
|
private MessageAttachment<?> attachment;
|
||||||
private Message.MessageStatus status;
|
private Message.MessageStatus status;
|
||||||
@ -32,11 +32,27 @@ public class MessageBuilder {
|
|||||||
*
|
*
|
||||||
* @param senderId the ID of the user who sends 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}
|
* @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
|
* @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.senderId = senderId;
|
||||||
this.recipientId = recipientId;
|
this.recipientId = recipientId;
|
||||||
|
id = messageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,36 +73,26 @@ public class MessageBuilder {
|
|||||||
* <td>{@code MessageStatus.WAITING}</td>
|
* <td>{@code MessageStatus.WAITING}</td>
|
||||||
* <tr>
|
* <tr>
|
||||||
* </table>
|
* </table>
|
||||||
*
|
*
|
||||||
* @return a new instance of {@link Message}
|
* @return a new instance of {@link Message}
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public Message build() {
|
public Message build() {
|
||||||
// Supplement default values
|
// Supplement default values
|
||||||
if (date == null) date = new Date();
|
if (creationDate == null) creationDate = new Date();
|
||||||
if (text == null) text = "";
|
if (text == null) text = "";
|
||||||
if (status == null) status = MessageStatus.WAITING;
|
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}
|
* @return this {@link MessageBuilder}
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public MessageBuilder setId(long id) {
|
public MessageBuilder setDate(Date creationDate) {
|
||||||
this.id = id;
|
this.creationDate = creationDate;
|
||||||
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;
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ public class User implements Serializable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a {@link User}. The {@link UserStatus} is set to
|
* Initializes a {@link User}. The {@link UserStatus} is set to
|
||||||
* {@link UserStatus#OFFLINE}.
|
* {@link UserStatus#ONLINE}.
|
||||||
*
|
*
|
||||||
* @param id unique ID
|
* @param id unique ID
|
||||||
* @param name user name
|
* @param name user name
|
||||||
@ -65,6 +65,19 @@ public class User implements Serializable {
|
|||||||
status = UserStatus.ONLINE;
|
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
|
@Override
|
||||||
public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); }
|
public String toString() { return String.format("User[id=%d,name=%s,status=%s]", id, name, status); }
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package envoy.event;
|
package envoy.event;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-common</strong><br>
|
* Project: <strong>envoy-common</strong><br>
|
||||||
* File: <strong>Event.java</strong><br>
|
* File: <strong>Event.java</strong><br>
|
||||||
@ -9,11 +11,10 @@ package envoy.event;
|
|||||||
* @param <T> the type of the Event
|
* @param <T> the type of the Event
|
||||||
* @since Envoy v0.2-alpha
|
* @since Envoy v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public interface Event<T> {
|
public interface Event<T> extends Serializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the data associated with this event
|
* @return the data associated with this event
|
||||||
*/
|
*/
|
||||||
T get();
|
default T get() { return null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
41
src/main/java/envoy/event/HandshakeRejectionEvent.java
Normal file
41
src/main/java/envoy/event/HandshakeRejectionEvent.java
Normal 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; }
|
||||||
|
}
|
17
src/main/java/envoy/event/IdGeneratorRequest.java
Normal file
17
src/main/java/envoy/event/IdGeneratorRequest.java
Normal 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;
|
||||||
|
}
|
@ -18,21 +18,31 @@ public class MessageStatusChangeEvent implements Event<Message.MessageStatus> {
|
|||||||
private final Message.MessageStatus status;
|
private final Message.MessageStatus status;
|
||||||
private final Date date;
|
private final Date date;
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 4566145392192761313L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a {@link MessageStatusChangeEvent}.
|
* Initializes a {@link MessageStatusChangeEvent}.
|
||||||
*
|
*
|
||||||
* @param id the ID of the {@link Message} this event is related to
|
* @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
|
* @param status the status of the {@link Message} this event is related
|
||||||
* to
|
* to
|
||||||
* @param date the date at which the MessageStatus change occurred
|
* @param date the date at which the MessageStatus change occurred
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
*/
|
*/
|
||||||
public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) {
|
public MessageStatusChangeEvent(long id, Message.MessageStatus status, Date date) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.status = status;
|
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
|
* @return the status of the {@link Message} this event is related to
|
||||||
* @since Envoy Common v0.2-alpha
|
* @since Envoy Common v0.2-alpha
|
||||||
|
Reference in New Issue
Block a user