From a5e6b0b3e0dcb844eb1a540322a5fa879ceb4557 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sun, 13 Mar 2022 14:56:42 +0100 Subject: [PATCH] =?UTF-8?q?-=20Context=20:=20ajout=20de=20deleteFile=20pou?= =?UTF-8?q?r=20pouvoir=20supprimer=20un=20fichier=20du=20FFE=20=20=20=20qu?= =?UTF-8?q?and=20le=20SBE=20a=20confirm=C3=A9=20sa=20suppression.=20-=20Er?= =?UTF-8?q?aseErrorRule=20:=20lecture=20de=20l'erreur=20envoy=C3=A9e=20par?= =?UTF-8?q?=20le=20SBE=20lors=20de=20la=20=20=20=20=20=20=20=20suppression?= =?UTF-8?q?=20d'un=20fichier.=20-=20EraseFileRule=20:=20commande=20cr?= =?UTF-8?q?=C3=A9=C3=A9e=20par=20le=20FFE=20pour=20que=20le=20SBE=20puisse?= =?UTF-8?q?=20savoir=20=20=20quel=20fichier=20il=20doit=20supprimer.=20-?= =?UTF-8?q?=20EraseOkRule=20:=20lecture=20de=20la=20commande=20envoy=C3=A9?= =?UTF-8?q?e=20par=20le=20SBE=20lorsque=20la=20suppression=20d'un=20fichie?= =?UTF-8?q?r=20s'est=20bien=20pass=C3=A9e.=20-=20File=20:=20impl=C3=A9ment?= =?UTF-8?q?e=20d=C3=A9sormais=20une=20interface=20ReadOnlyFile.=20-=20GetF?= =?UTF-8?q?ileRule=20:=20utilise=20ReadOnlyFile=20plut=C3=B4t=20que=20File?= =?UTF-8?q?.=20-=20ProtocolReader=20:=20reformatage.=20-=20ReadOnlyFile=20?= =?UTF-8?q?:=20interface=20pour=20r=C3=A9cup=C3=A9rer=20les=20donn=C3=A9es?= =?UTF-8?q?=20de=20File.=20-=20RemoveFileErrorRule=20:=20commande=20envoy?= =?UTF-8?q?=C3=A9e=20au=20client=20lorsque=20la=20suppression=20=20=20d'un?= =?UTF-8?q?=20fichier=20a=20=C3=A9chou=C3=A9.=20-=20RemoveFileOkRule=20:?= =?UTF-8?q?=20commande=20envoy=C3=A9e=20au=20client=20lorsque=20la=20suppr?= =?UTF-8?q?ession=20d'un=20fichier=20a=20r=C3=A9ussi.=20-=20RemoveFileRule?= =?UTF-8?q?=20:=20lecture=20de=20la=20commande=20envoy=C3=A9e=20par=20le?= =?UTF-8?q?=20client=20lorsqu'il=20=20=20veut=20supprimer=20un=20fichier.?= =?UTF-8?q?=20-=20Repository=20:=20utilise=20ReadOnlyFile=20plut=C3=B4t=20?= =?UTF-8?q?que=20File.=20-=20SendErrorRule=20et=20SendOkRule=20:=20modific?= =?UTF-8?q?ation=20des=20imports=20et=20attribut=20final.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/client/Context.java | 20 ++++++- .../protocol/ProtocolReader.java | 3 +- .../protocol/rules/reader/EraseErrorRule.java | 28 +++++++++ .../protocol/rules/reader/EraseFileRule.java | 15 +++++ .../protocol/rules/reader/EraseOkRule.java | 31 ++++++++++ .../protocol/rules/reader/GetFileRule.java | 3 +- .../protocol/rules/reader/RemoveFileRule.java | 59 +++++++++++++++++++ .../protocol/rules/reader/SendErrorRule.java | 1 - .../protocol/rules/reader/SendOkRule.java | 4 +- .../rules/writer/RemoveFileErrorRule.java | 14 +++++ .../rules/writer/RemoveFileOkRule.java | 13 ++++ .../java/lightcontainer/storage/File.java | 7 ++- .../lightcontainer/storage/ReadOnlyFile.java | 16 +++++ .../lightcontainer/storage/Repository.java | 2 +- 14 files changed, 207 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java create mode 100644 app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java create mode 100644 app/src/main/java/lightcontainer/storage/ReadOnlyFile.java diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index 0df8787..c002c9f 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -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); + } } diff --git a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java index 133a820..0990f1a 100644 --- a/app/src/main/java/lightcontainer/protocol/ProtocolReader.java +++ b/app/src/main/java/lightcontainer/protocol/ProtocolReader.java @@ -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 onExecuted(Context context, String... data); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java new file mode 100644 index 0000000..6cf945a --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseErrorRule.java @@ -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; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java new file mode 100644 index 0000000..1573cf7 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseFileRule.java @@ -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); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java new file mode 100644 index 0000000..9ff6b44 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/EraseOkRule.java @@ -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; + } + + +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java index 6015a90..35323ca 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -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) { diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java new file mode 100644 index 0000000..4b1bdaa --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RemoveFileRule.java @@ -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; + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java index e4a589b..83653fc 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -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. diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java index d4c2b61..c0a9275 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -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); diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java new file mode 100644 index 0000000..4f91bfc --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java @@ -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); + } +} diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java new file mode 100644 index 0000000..964f5e6 --- /dev/null +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java @@ -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); + } +} diff --git a/app/src/main/java/lightcontainer/storage/File.java b/app/src/main/java/lightcontainer/storage/File.java index c610a07..4c3ab3d 100644 --- a/app/src/main/java/lightcontainer/storage/File.java +++ b/app/src/main/java/lightcontainer/storage/File.java @@ -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 getStorageIterator() { return storage.iterator(); } diff --git a/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java new file mode 100644 index 0000000..ae7bf29 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/ReadOnlyFile.java @@ -0,0 +1,16 @@ +package lightcontainer.storage; + +import java.util.Iterator; + +public interface ReadOnlyFile { + + String getName(); + + String getFileNameSalt(); + + int getSize(); + + String getIv(); + + Iterator getStorageIterator(); +} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 43dd38f..3561d73 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -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); }