- Context : ajout de deleteFile pour pouvoir supprimer un fichier du FFE quand le SBE a confirmé sa suppression.

- EraseErrorRule : lecture de l'erreur envoyée par le SBE lors de la        suppression d'un fichier.
- EraseFileRule : commande créée par le FFE pour que le SBE puisse savoir   quel fichier il doit supprimer.
- EraseOkRule : lecture de la commande envoyée par le SBE lorsque la suppression d'un fichier s'est bien passée.
- File : implémente désormais une interface ReadOnlyFile.
- GetFileRule : utilise ReadOnlyFile plutôt que File.
- ProtocolReader : reformatage.
- ReadOnlyFile : interface pour récupérer les données de File.
- RemoveFileErrorRule : commande envoyée au client lorsque la suppression   d'un fichier a échoué.
- RemoveFileOkRule : commande envoyée au client lorsque la suppression d'un fichier a réussi.
- RemoveFileRule : lecture de la commande envoyée par le client lorsqu'il   veut supprimer un fichier.
- Repository : utilise ReadOnlyFile plutôt que File.
- SendErrorRule et SendOkRule : modification des imports et attribut final.
This commit is contained in:
Maximilien LEDOUX 2022-03-13 14:56:42 +01:00
parent a9a620275d
commit a5e6b0b3e0
14 changed files with 207 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package lightcontainer.domains.client;
import lightcontainer.storage.File;
import lightcontainer.storage.ReadOnlyFile;
import lightcontainer.storage.Repository;
import lightcontainer.utils.AES_GCM;
import lightcontainer.utils.ShaHasher;
@ -52,7 +53,8 @@ public class Context {
this.login = login;
return true;
}
} catch (AES_GCM.AesGcmException ignored) { }
} catch (AES_GCM.AesGcmException ignored) {
}
return false;
}
@ -166,7 +168,17 @@ public class Context {
return this.repository.addFileFor(new File(fileName, fileNameSalt, size, iv, Set.of(domain)), getLogin());
}
public File getFileOf(String fileName, String userName) {
public String getHashedFileName(String fileName) {
ReadOnlyFile file = repository.getFileOf(fileName, login);
if (file != null) {
String completeFileName = login + "_" + fileName;
ShaHasher hasher = new ShaHasher(completeFileName);
return hasher.fromSalt(hasher.saltToByte(file.getFileNameSalt()));
}
return null;
}
public ReadOnlyFile getFileOf(String fileName, String userName) {
return this.repository.getFileOf(fileName, userName);
}
@ -185,4 +197,8 @@ public class Context {
public void setDomain(String domain) {
this.domain = domain;
}
public void deleteFile(String deletedFileName) {
repository.deleteFileOf(deletedFileName, login);
}
}

View File

@ -93,6 +93,7 @@ public abstract class ProtocolReader {
/**
* Permet de récupérer le context courant
*
* @return Context courant
*/
protected Context getContext() {
@ -140,7 +141,7 @@ public abstract class ProtocolReader {
* Cette méthode est appelée lors de l'exécution de la règle
*
* @param context Utilisateur context.
* @param data Paramètres pour créer la commande.
* @param data Paramètres pour créer la commande.
*/
protected abstract <T extends ProtocolResult> T onExecuted(Context context, String... data);

View File

@ -0,0 +1,28 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.RemoveFileErrorRule;
public class EraseErrorRule extends ProtocolReader {
private static final String PATTERN = "^ERASE_ERROR\r\n$";
private static final String NAME = "ERASE_ERROR";
private final ProtocolRepository protocolRep;
protected EraseErrorRule(ProtocolRepository protocolRep) {
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@Override
protected ProtocolResult onExecuted(Context context, String... data) {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -0,0 +1,15 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.protocol.ProtocolWriter;
public class EraseFileRule extends ProtocolWriter {
private static final String PATTERN = "^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$";
public static String NAME = "ERASEFILE";
protected EraseFileRule() {
super(NAME, PATTERN);
}
}

View File

@ -0,0 +1,31 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.RemoveFileOkRule;
public class EraseOkRule extends ProtocolReader {
private static final String PATTERN = "^ERASE_OK\r\n$";
private static final String NAME = "ERASE_OK";
private final ProtocolRepository protocolRep;
protected EraseOkRule(ProtocolRepository protocolRep) {
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
@Override
protected ProtocolReader.ProtocolResult onExecuted(Context context, String... data) {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
String deletedFileName = context.getDataString("fileName");
context.deleteFile(deletedFileName);
result.setResultCommand(protocolRep.executeWriter(context, RemoveFileOkRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -5,6 +5,7 @@ import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.GetFileErrorRule;
import lightcontainer.storage.File;
import lightcontainer.storage.ReadOnlyFile;
public class GetFileRule extends ProtocolReader {
// Constants
@ -47,7 +48,7 @@ public class GetFileRule extends ProtocolReader {
protected GetFileRule.Result onExecuted(Context context, String... data) {
GetFileRule.Result result = new GetFileRule.Result(context, data[FILE_NAME]);
File file = context.getFileOf(data[FILE_NAME], context.getLogin());
ReadOnlyFile file = context.getFileOf(data[FILE_NAME], context.getLogin());
if (true) {

View File

@ -0,0 +1,59 @@
package lightcontainer.protocol.rules.reader;
import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.RemoveFileErrorRule;
public class RemoveFileRule extends ProtocolReader {
private static final String PATTERN = "^REMOVEFILE ([^ !]{1,20})\r\n$";
private static final String NAME = "REMOVEFILE";
private static final int FILENAME = 0;
private final ProtocolRepository protocolRep;
protected RemoveFileRule(ProtocolRepository protocolRep) {
super(NAME, PATTERN);
this.protocolRep = protocolRep;
}
public class Result extends ProtocolResult {
private final String fileName;
public Result(Context context, String fileName) {
super(context);
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
}
@Override
protected RemoveFileRule.Result onExecuted(Context context, String... data) {
RemoveFileRule.Result result = new RemoveFileRule.Result(context, data[FILENAME]);
String extendedFileName = context.getHashedFileName(result.getFileName());
if (extendedFileName != null) {
context.putDataString("fileName", result.getFileName());
result.setResultCommand(protocolRep.executeWriter(context, EraseFileRule.NAME, extendedFileName), ResultCmdReceiver.STOREBACKEND);
} else {
result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
}
return result;
}
@Override
protected ProtocolReader.ProtocolResult onError(Context context) {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
result.setResultCommand(protocolRep.executeWriter(context, RemoveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
return result;
}
}

View File

@ -4,7 +4,6 @@ import lightcontainer.domains.client.Context;
import lightcontainer.interfaces.ProtocolRepository;
import lightcontainer.protocol.ProtocolReader;
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
import lightcontainer.protocol.rules.writer.SaveFileOkRule;
/**
* Règle permettant de de confirmer la sauvegrade d'un fichier.

View File

@ -15,7 +15,7 @@ public class SendOkRule extends ProtocolReader {
private static final String NAME = "SEND_OK";
private ProtocolRepository protocolRep;
private final ProtocolRepository protocolRep;
// Constructor
public SendOkRule(ProtocolRepository protocolRep) {
@ -25,7 +25,7 @@ public class SendOkRule extends ProtocolReader {
@Override
protected ProtocolReader.ProtocolResult onExecuted(Context context, String... data) {
protected ProtocolResult onExecuted(Context context, String... data) {
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT);

View File

@ -0,0 +1,14 @@
package lightcontainer.protocol.rules.writer;
import lightcontainer.protocol.ProtocolWriter;
public class RemoveFileErrorRule extends ProtocolWriter {
private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$";
public static String NAME = "REMOVEFILE_ERROR";
protected RemoveFileErrorRule() {
super(NAME, PATTERN);
}
}

View File

@ -0,0 +1,13 @@
package lightcontainer.protocol.rules.writer;
import lightcontainer.protocol.ProtocolWriter;
public class RemoveFileOkRule extends ProtocolWriter {
private static final String PATTERN = "^REMOVEFILE_OK\r\n$";
public static String NAME = "REMOVEFILE_OK";
protected RemoveFileOkRule() {
super(NAME, PATTERN);
}
}

View File

@ -6,7 +6,7 @@ import java.util.Set;
/**
* File represents all information related to a file
*/
public class File {
public class File implements ReadOnlyFile {
// Variables
private final String name;
private final String fileNameSalt;
@ -23,22 +23,27 @@ public class File {
this.storage = storage;
}
@Override
public String getName() {
return name;
}
@Override
public String getFileNameSalt() {
return fileNameSalt;
}
@Override
public int getSize() {
return size;
}
@Override
public String getIv() {
return iv;
}
@Override
public Iterator<String> getStorageIterator() {
return storage.iterator();
}

View File

@ -0,0 +1,16 @@
package lightcontainer.storage;
import java.util.Iterator;
public interface ReadOnlyFile {
String getName();
String getFileNameSalt();
int getSize();
String getIv();
Iterator<String> getStorageIterator();
}

View File

@ -151,7 +151,7 @@ public class Repository {
*
* @author Unknown...
*/
public File getFileOf(String fileName, String userName) {
public ReadOnlyFile getFileOf(String fileName, String userName) {
return this.appData.getFileOf(fileName, userName);
}