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); }