Début système de contexte + Ajout commande SignupRule.java

This commit is contained in:
Maximilien LEDOUX 2022-03-10 18:41:52 +01:00
parent 826ef9e2e4
commit 9326b09d0c
13 changed files with 186 additions and 39 deletions

View File

@ -0,0 +1,30 @@
package lightcontainer.domains.client;
import lightcontainer.storage.AppData;
import lightcontainer.storage.User;
import lightcontainer.utils.AES_GCM;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
public class Context {
private AppData appData;
public Context(AppData appData) {
this.appData = appData;
}
public boolean createUser(String login, String password) {
try {
String key = AES_GCM.generateSecretKey();
return this.appData.addUser(login, password, key);
} catch (NoSuchAlgorithmException e) {
return false;
}
}
}

View File

@ -1,15 +1,19 @@
package lightcontainer.protocol;
import lightcontainer.domains.client.Context;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class ProtocolReader {
private final String name;
// Variables
private final Pattern rulePattern;
// Constructor
protected ProtocolReader(String pattern) {
protected ProtocolReader(String name, String pattern) {
this.name = name;
this.rulePattern = Pattern.compile(pattern);
}
@ -42,6 +46,7 @@ public abstract class ProtocolReader {
/**
* Récupérer la commande à envoyer
*
* @return Commande
*/
public ProtocolWriter.ProtocolResult getResultCommand() {
@ -50,6 +55,7 @@ public abstract class ProtocolReader {
/**
* Mettre la commande à envoyer
*
* @param resultCommand Commande à envoyer
* @param receiver Le receveur de cette commande
*/
@ -61,32 +67,52 @@ public abstract class ProtocolReader {
/**
* Permet de lire un fichier. Cad reçevoir le contenu d'un fichier provenant du réseau.
* Redéfinissez cette méthode pour l'utiliser
*
* @param reader Buffer rempli du fichier
*/
public void read(InputStream reader) {}
public void read(InputStream reader) {
}
}
/**
* Permet de lancer la décomposition d'une commande pour en extraire les données
*
* @param data Contenu de la commande
*/
public <T extends ProtocolResult> T execute(String data) {
public <T extends ProtocolResult> T execute(Context context, String data) {
Matcher ruleMatcher = this.rulePattern.matcher(data);
// Vérifier que c'est le bon protocle
String name = ruleMatcher.group(1);
if (name != null && name.equals(this.name)) {
// Vérifier que la commande match
if (ruleMatcher.matches()) {
String[] groups = new String[ruleMatcher.groupCount()];
for (int i = 1; i <= groups.length; ++i)
groups[i - 1] = ruleMatcher.group(i);
groups[i - 1] = ruleMatcher.group(i + 1);
return onExecuted(groups);
return onExecuted(context, groups);
} else {
return onError();
}
}
return null;
}
/**
* Cette méthode est appelée lors de l'exécution de la règle
*
* @param data Paramètres pour créer la commande.
*/
protected abstract <T> T onExecuted(String... data);
protected abstract <T extends ProtocolResult> T onExecuted(Context context, String... data);
/**
* Cette méthode est appelée lors d'une erreur de la règle
*/
protected <T extends ProtocolResult> T onError() {
return null;
}
}

View File

@ -1,5 +1,6 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
@ -8,13 +9,15 @@ import lightcontainer.protocol.rules.writer.SignOkRule;
public class FilelistRule extends ProtocolReader {
// Constants
private static final String PATTERN = "^FILELIST\r\n$";
private static final String PATTERN = "^(FILELIST)\r\n$";
private static final String NAME = "FILELIST";
private ProtocolRepository protocolRep;
// Constructor
public FilelistRule(ProtocolRepository protocolRep) {
super(PATTERN);
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@ -25,7 +28,7 @@ public class FilelistRule extends ProtocolReader {
* @param data Paramètres pour créer la commande.
*/
@Override
protected FilelistRule.Result onExecuted(String... data) {
protected FilelistRule.Result onExecuted(Context context, String... data) {
FilelistRule.Result result = new Result();
result.setResultCommand(this.protocolRep.executeWriter(FilesRule.NAME, "endbenja.txt!500"), ResultCmdReceiver.CLIENT);
return result;

View File

@ -1,5 +1,6 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.protocol.ProtocolReader;
import java.io.BufferedReader;
@ -8,7 +9,9 @@ import java.util.List;
public class HelloRule extends ProtocolReader {
private static final String PATTERN = "^HELLO ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$";
private static final String PATTERN = "^(HELLO) ([A-Za-z0-9]{5,20}) ([0-9]{1,5})\r\n$";
private static final String NAME = "HELLO";
// Index du domain dans le tableau de donnée
private static final int DOMAIN = 0;
@ -17,7 +20,7 @@ public class HelloRule extends ProtocolReader {
private static final int PORT = 1;
public HelloRule() {
super(PATTERN);
super(NAME, PATTERN);
}
@ -39,12 +42,11 @@ public class HelloRule extends ProtocolReader {
return port;
}
}
@Override
protected HelloRule.Result onExecuted(String... data) {
protected HelloRule.Result onExecuted(Context context, String... data) {
String domain = data[DOMAIN];
int port = Integer.parseInt(data[PORT]);

View File

@ -1,5 +1,6 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
@ -12,7 +13,10 @@ import java.io.InputStream;
public class SavefileRule extends ProtocolReader {
// Constants
private static final String PATTERN = "^SAVEFILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
private static final String PATTERN = "^(SAVEFILE) ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
private static final String NAME = "SAVEFILE";
private static final int FILE_NAME = 0; // Index file name.
private static final int FILE_SIZE = 1; // Index file size.
@ -20,7 +24,7 @@ public class SavefileRule extends ProtocolReader {
// Constructor
public SavefileRule(ProtocolRepository protocolRep) {
super(PATTERN);
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@ -58,9 +62,18 @@ public class SavefileRule extends ProtocolReader {
* @param data Paramètres pour créer la commande.
*/
@Override
protected SavefileRule.Result onExecuted(String... data) {
protected SavefileRule.Result onExecuted(Context context, String... data) {
SavefileRule.Result result = new SavefileRule.Result(data[FILE_NAME], Integer.parseInt(data[FILE_SIZE]));
return result;
}
@Override
protected ProtocolReader.ProtocolResult onError() {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult();
// Commande renvoyée en cas d'erreur
result.setResultCommand(protocolRep.executeWriter(SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -1,5 +1,6 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.ProtocolWriter;
@ -9,19 +10,21 @@ public class SendOkRule extends ProtocolReader {
// Constants
private static final String PATTERN = "^SEND_OK\r\n$";
private static final String PATTERN = "^(SEND_OK)\r\n$";
private static final String NAME = "SEND_OK";
private ProtocolRepository protocolRep;
// Constructor
public SendOkRule(ProtocolRepository protocolRep) {
super(PATTERN);
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@Override
protected ProtocolReader.ProtocolResult onExecuted(String... data) {
protected ProtocolReader.ProtocolResult onExecuted(Context context, String... data) {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult();
result.setResultCommand(protocolRep.executeWriter(SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT);
return result;

View File

@ -1,7 +1,9 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
import lightcontainer.protocol.rules.writer.SignErrorRule;
import lightcontainer.protocol.rules.writer.SignOkRule;
@ -9,7 +11,10 @@ import java.io.InputStream;
public class SigninRule extends ProtocolReader {
// Constants
private static final String PATTERN = "^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$";
private static final String PATTERN = "^(SIGNIN) ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$";
private static final String NAME = "SIGNIN";
private static final int LOGIN = 0; // Index du domain dans le tableau de données
private static final int PASSWORD = 1; // Index du port dans le tableau de données
@ -17,7 +22,7 @@ public class SigninRule extends ProtocolReader {
// Constructor
public SigninRule(ProtocolRepository protocolRep) {
super(PATTERN);
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@ -41,17 +46,12 @@ public class SigninRule extends ProtocolReader {
}
public boolean checkCredentials() {
return getLogin().equals("aa") && getPassword().equals("aaaaa");
}
@Override
public void read(InputStream reader) {
return getLogin().equals("aaaaa") && getPassword().equals("aaaaa");
}
}
@Override
protected SigninRule.Result onExecuted(String... data) {
protected SigninRule.Result onExecuted(Context context, String... data) {
SigninRule.Result result = new SigninRule.Result(data[LOGIN], data[PASSWORD]);
// TODO : Création d'une règle d'écriture SIGN_OK et SIGN_ERROR proprement
@ -62,4 +62,12 @@ public class SigninRule extends ProtocolReader {
}
return result;
}
@Override
protected ProtocolReader.ProtocolResult onError() {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult();
result.setResultCommand(protocolRep.executeWriter(SignErrorRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -0,0 +1,58 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SignErrorRule;
public class SignupRule extends ProtocolReader {
// Constants
private static final String PATTERN = "^(SIGNUP) ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$";
private static final String NAME = "SIGNUP";
private static final int LOGIN = 0;
private static final int PASSWORD = 1;
private ProtocolRepository protocolRep;
public SignupRule(ProtocolRepository protocolRep) {
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
public class Result extends ProtocolResult {
//Variables
private final String login;
private final String password;
public Result(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
@Override
protected SignupRule.Result onExecuted(Context context, String... data) {
SignupRule.Result result = new SignupRule.Result(data[LOGIN], data[PASSWORD]);
if ()
return null;
}
@Override
protected ProtocolReader.ProtocolResult onError() {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult();
result.setResultCommand(protocolRep.executeWriter(SignErrorRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -67,13 +67,16 @@ public class AppData {
return this.users.get(userName);
}
/**
* Use this method when a user signs up.
*
* @param user The user to add.
* @return True if the user was added. False if a user with the same name already exists.
*/
public boolean addUser(User user) {
public boolean addUser(String login, String password, String key) {
User user = new User(login, password, key, new HashMap<>());
if (this.users.containsKey(user.getName())) {
return false;
} else {
@ -149,4 +152,5 @@ public class AppData {
return this.users.get(user.getName()).addStorage(file, storage);
}
}
}

View File

@ -98,7 +98,7 @@ public class JsonAdapter implements Adapter {
AppData appData = AppData.getInstance();
appData.setAppConfig(appConfig);
for (User user : users) {
appData.addUser(user);
appData.addUser(user.getName(), user.getPassword(), user.getAesKey());
}
this.appData = appData;
return this.appData;

View File

@ -29,8 +29,8 @@ ffe_retrievefile = ^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$
sbe_retrieveresult = ^(RETRIEVE_OK ([A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200})\r\n)|(RETRIEVE_ERROR\r\n)$
//Client to FileFrontEnd
client_signin = ^SIGNIN ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$
client_signup = ^SIGNUP ([A-Za-z0-9]{2,20}) ([^ !]{5,50})\r\n$
client_signin = ^SIGNIN ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$
client_signup = ^SIGNUP ([A-Za-z0-9]{5,20}) ([^ !]{5,50})\r\n$
ffe_signresult = ^(SIGN_OK|SIGN_ERROR)\r\n$
client_filelist = ^FILELIST\r\n$
ffe_filelistresult = ^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}$

View File

@ -29,7 +29,7 @@ public class JsonAdapterTests {
files.put(file1.getName(), file1);
User user1 = new User("User1", "Password", "djdjjdj", files);
appData.setAppConfig(appConfig);
appData.addUser(user1);
//appData.addUser(user1);
JsonAdapter jsonAdapter = new JsonAdapter(appData);
//WHEN the adapter converts AppData to Json
String jsonAppData = jsonAdapter.toString();

View File

@ -45,7 +45,7 @@ public class RepositoryTests {
files.put(file1.getName(), file1);
User user1 = new User("User1", "Password", "djdjjdj", files);
appData.setAppConfig(appConfig);
appData.addUser(user1);
//appData.addUser(user1);
JsonAdapter jsonAdapter = new JsonAdapter(appData);
//WHEN Repository calls save method
String filePath = "src/test/resources/test.json";