From 0e3cd23d48edc89a1bf0aa40b1baec526b0a1d8d Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sun, 13 Mar 2022 17:05:08 +0100 Subject: [PATCH] Suppression du fichier temporaire du FFE lorsque le SBE nous renvoie le status (OK ou ERROR) --- app/src/main/java/lightcontainer/App.java | 64 ++++++++++++------- .../rules/reader/RetrieveErrorRule.java | 2 +- .../protocol/rules/reader/RetrieveOkRule.java | 2 +- .../protocol/rules/reader/SendErrorRule.java | 16 ++++- .../protocol/rules/reader/SendOkRule.java | 16 ++++- .../protocol/rules/writer/GetFileOkRule.java | 4 +- .../rules/writer/RetrieveFileRule.java | 2 +- .../main/java/lightcontainer/utils/SHA.java | 2 +- app/src/main/resources/appdata.json | 18 +----- 9 files changed, 76 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/lightcontainer/App.java b/app/src/main/java/lightcontainer/App.java index 9c28c9f..97607d2 100644 --- a/app/src/main/java/lightcontainer/App.java +++ b/app/src/main/java/lightcontainer/App.java @@ -28,30 +28,12 @@ public class App { // Create all repository ClientHandlerRepository clientRep = new ClientHandlerRepository(); StoreProcessorRepository storeRep = new StoreProcessorRepository(); + + // Initialisation des protocoles ProtocolRepository protocolRep = new ProtocolRepositoryImpl(); - protocolRep.addReader(new HelloRule()); - protocolRep.addReader(new SigninRule(protocolRep)); - protocolRep.addReader(new SignupRule(protocolRep)); - protocolRep.addReader(new SignoutRule()); - protocolRep.addReader(new FilelistRule(protocolRep)); - protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); - protocolRep.addReader(new SendOkRule(protocolRep)); - protocolRep.addReader(new GetFileRule(protocolRep)); - protocolRep.addReader(new EraseErrorRule(protocolRep)); - protocolRep.addReader(new EraseOkRule(protocolRep)); - protocolRep.addReader(new RemoveFileRule(protocolRep)); - - protocolRep.addWriter(new SignOkRule()); - protocolRep.addWriter(new SignErrorRule()); - protocolRep.addWriter(new FilesRule()); - protocolRep.addWriter(new SaveFileOkRule()); - protocolRep.addWriter(new SaveFileErrorRule()); - protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); - protocolRep.addWriter(new GetFileErrorRule()); - protocolRep.addWriter(new EraseFileRule()); - protocolRep.addWriter(new RemoveFileErrorRule()); - protocolRep.addWriter(new RemoveFileOkRule()); + initProtocols(repositoryStorage, protocolRep); + // Initialisation du dispatcher et des servers FileFrontEnd ffe = new FileFrontEnd(clientRep, storeRep, protocolRep); new UnicastServerListener(ffe, clientRep, protocolRep, repositoryStorage, repositoryStorage.getUnicastPort()); new MulticastServerListener(ffe, storeRep, protocolRep, repositoryStorage.getMulticastIp(), repositoryStorage.getMulticastPort()); @@ -64,6 +46,44 @@ public class App { // storeRep.close(); } + private static void initProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + initReadersProtocols(repositoryStorage, protocolRep); + + initWritersProtocols(repositoryStorage, protocolRep); + } + + private static void initReadersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + protocolRep.addReader(new HelloRule()); + protocolRep.addReader(new SigninRule(protocolRep)); + protocolRep.addReader(new SignupRule(protocolRep)); + protocolRep.addReader(new SignoutRule()); + protocolRep.addReader(new FilelistRule(protocolRep)); + protocolRep.addReader(new SavefileRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendOkRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new SendErrorRule(protocolRep, repositoryStorage.getStoragePath())); + protocolRep.addReader(new GetFileRule(protocolRep)); + protocolRep.addReader(new EraseErrorRule(protocolRep)); + protocolRep.addReader(new EraseOkRule(protocolRep)); + protocolRep.addReader(new RemoveFileRule(protocolRep)); + protocolRep.addReader(new RetrieveErrorRule()); + protocolRep.addReader(new RetrieveOkRule()); + } + + private static void initWritersProtocols(Repository repositoryStorage, ProtocolRepository protocolRep) { + protocolRep.addWriter(new SignOkRule()); + protocolRep.addWriter(new SignErrorRule()); + protocolRep.addWriter(new FilesRule()); + protocolRep.addWriter(new SaveFileOkRule()); + protocolRep.addWriter(new SaveFileErrorRule()); + protocolRep.addWriter(new SendfileRule(repositoryStorage.getStoragePath())); + protocolRep.addWriter(new GetFileErrorRule()); + protocolRep.addWriter(new EraseFileRule()); + protocolRep.addWriter(new RemoveFileErrorRule()); + protocolRep.addWriter(new RemoveFileOkRule()); + protocolRep.addWriter(new GetFileOkRule()); + protocolRep.addWriter(new RetrieveFileRule()); + } + private static Repository prepareStorage() { AppData appData = AppData.getInstance(); Repository repository = new Repository( diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java index 6483db2..551ea03 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveErrorRule.java @@ -9,7 +9,7 @@ public class RetrieveErrorRule extends ProtocolReader { private static final String NAME = "RETRIEVE_ERROR"; // Constructor - protected RetrieveErrorRule() { + public RetrieveErrorRule() { super(NAME, PATTERN); } diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java index 67a3149..39c4cb3 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -13,7 +13,7 @@ public class RetrieveOkRule extends ProtocolReader { private static final int HASHED_FILE_CONTENT = 2; // Index hashed file content // Variables - protected RetrieveOkRule() { + public RetrieveOkRule() { super(NAME, PATTERN); } 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 dbcbe28..3c77552 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle permettant de de confirmer la sauvegrade d'un fichier. */ @@ -17,11 +21,13 @@ public class SendErrorRule extends ProtocolReader { private static final String NAME = "SEND_ERROR"; private ProtocolRepository protocolRep; + private final String storagePath; // Constructor - public SendErrorRule(ProtocolRepository protocolRep) { + public SendErrorRule(ProtocolRepository protocolRep, String storagePath) { super(NAME, PATTERN); this.protocolRep = protocolRep; + this.storagePath = storagePath; } @@ -29,7 +35,13 @@ public class SendErrorRule extends ProtocolReader { protected ProtocolResult onExecuted(Context context, String... data) { ProtocolResult result = new ProtocolResult(context); result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT); - // TODO : Supprimer le fichier du FFE + + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", this.storagePath, hashedFileName))); + } catch (IOException e) {} + return result; } } 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 04522e4..7298bf0 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -5,6 +5,10 @@ import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileOkRule; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + /** * Règle permettant de de confirmer la sauvegrade d'un fichier. */ @@ -16,11 +20,13 @@ public class SendOkRule extends ProtocolReader { private static final String NAME = "SEND_OK"; private final ProtocolRepository protocolRep; + private final String storagePath; // Constructor - public SendOkRule(ProtocolRepository protocolRep) { + public SendOkRule(ProtocolRepository protocolRep, String storagePath) { super(NAME, PATTERN); this.protocolRep = protocolRep; + this.storagePath = storagePath; } @@ -30,10 +36,14 @@ public class SendOkRule extends ProtocolReader { result.setResultCommand(protocolRep.executeWriter(context, SaveFileOkRule.NAME), ResultCmdReceiver.CLIENT); // Sauvegarder dans JSON - System.out.println("Save en json du fichier"); context.addFile(context.getDataString("fileName"), context.getDataString("fileNameSalt"), context.getDataInt("size"), context.getDataString("iv"), context.getDomain()); - // TODO : Supprimer le fichier du FFE + // Suppression du fichier temporaire dans le stockage du FFE + String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); + } catch (IOException e) {} + return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java index 77bc6f5..39944db 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -4,14 +4,14 @@ import lightcontainer.protocol.ProtocolWriter; public class GetFileOkRule extends ProtocolWriter { // Constants - private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n)$"; + private static final String PATTERN = "^GETFILE_OK (^ !]{1,20}) ([0-9]{1,10})\r\n$"; public static final String NAME = "GETFILE_OK"; // -- params private static final int FILE_NAME = 0; // Index file name hashed private static final int FILE_SIZE = 1; // Index file size // Constructors - protected GetFileOkRule() { + public GetFileOkRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java index d99241c..65243c9 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java @@ -10,7 +10,7 @@ public class RetrieveFileRule extends ProtocolWriter { private static final int HASHED_FILE_NAME = 0; // Index hashed filename // Constructor - protected RetrieveFileRule() { + public RetrieveFileRule() { super(NAME, PATTERN); } } diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java index 4ffe472..6eb1c56 100644 --- a/app/src/main/java/lightcontainer/utils/SHA.java +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -44,7 +44,7 @@ public class SHA { * * @param in InputStream to the input, flux to hash. * @param fileSize Stream/file size. - * + *ichier, utilisé * @return Borrowing of the full current flux. * * @throws ShaException if an error occur. diff --git a/app/src/main/resources/appdata.json b/app/src/main/resources/appdata.json index f2068c3..bb67cfc 100644 --- a/app/src/main/resources/appdata.json +++ b/app/src/main/resources/appdata.json @@ -1,17 +1 @@ -{ - "unicast_port": 8000, - "multicast_ip": "226.66.66.1", - "multicast_port": 15502, - "network_interface": "My network interface", - "tls": true, - "storagePath": "C:\\Users\\ledou\\Documents\\ffe", - "users": [ - { - "name": "aaaaa", - "password": "5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617", - "aes_key": "qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=", - "passwordSalt": "Ns8Al6DpqPsIDlCSRBVTEg==", - "files": [] - } - ] -} \ No newline at end of file +{"unicast_port":8000,"multicast_ip":"226.66.66.1","multicast_port":15502,"network_interface":"My network interface","tls":true,"storagePath":"/home/benjamin/ffe","users":[{"name":"aaaaa","password":"5d628c274ebb008324f1e199d3bfff0a3fe839730a7f2355e82850d7acca5e5ca64db9071abf3d91034295695f84a617","aes_key":"qlTH6TijnfMRnrS0Qf+k6IPKGp5LoRMXGxCq16e+mF4=","passwordSalt":"Ns8Al6DpqPsIDlCSRBVTEg==","files":[{"name":"README.md","fileNameSalt":"/jo0zYyQs96gWI9OgBXiPQ==","size":17,"iv":"rvOFhgEvgFMISO44jqlSRg==","storage":["lightcontainerSB01"]}]}]} \ No newline at end of file