diff --git a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java index 4181b7b..36014e6 100644 --- a/app/src/main/java/lightcontainer/domains/client/ClientHandler.java +++ b/app/src/main/java/lightcontainer/domains/client/ClientHandler.java @@ -1,6 +1,5 @@ package lightcontainer.domains.client; -import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.interfaces.ClientHandlerFFE; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.interfaces.UnicastCHR; @@ -10,27 +9,23 @@ import lightcontainer.protocol.rules.reader.SigninRule; import lightcontainer.protocol.rules.reader.SignoutRule; import lightcontainer.protocol.rules.reader.SignupRule; import lightcontainer.protocol.rules.writer.SignErrorRule; -import lightcontainer.protocol.rules.writer.SignOkRule; -import javax.crypto.BadPaddingException; -import javax.net.ssl.SSLHandshakeException; import java.io.*; import java.net.Socket; import java.nio.charset.StandardCharsets; /** * ClientHandler - * + *

* UNICAST CLIENT * Class communicating with the client, and * intercepting and sending files to the client. * - * @version 1.1 - * @since 1.0 - * - * @see Runnable - * @see AutoCloseable - * @author Jérémi NIHART + * @author Jérémi NIHART + * @version 1.1 + * @see Runnable + * @see AutoCloseable + * @since 1.0 */ public class ClientHandler implements Runnable, AutoCloseable { // Variables @@ -60,21 +55,20 @@ public class ClientHandler implements Runnable, AutoCloseable { /** * Initialise the Client's Reader and Writer. * - * @since 1.0 - * - * @see BufferedReader - * @see PrintWriter + * @see BufferedReader + * @see PrintWriter + * @since 1.0 */ private void initClient() { // Start the thread try { this.reader = new BufferedReader(new InputStreamReader( - this.client.getInputStream(), - StandardCharsets.UTF_8 + this.client.getInputStream(), + StandardCharsets.UTF_8 )); this.writer = new PrintWriter(new OutputStreamWriter( - this.client.getOutputStream(), - StandardCharsets.UTF_8 + this.client.getOutputStream(), + StandardCharsets.UTF_8 ), true); } catch (IOException e) { e.printStackTrace(); @@ -85,7 +79,7 @@ public class ClientHandler implements Runnable, AutoCloseable { * Thread Function * Start the dialogue with the client. * - * @since 1.0 + * @since 1.0 */ @Override public void run() { @@ -153,11 +147,13 @@ public class ClientHandler implements Runnable, AutoCloseable { this.writer.close(); this.client.close(); System.out.printf("[CLIENT] %s s'est déconnecté\n", context.getLogin()); - } catch (IOException ignored) { } + } catch (IOException ignored) { + } } /** * Permet de vérifier si le client possède l'accès demandé + * * @param ruleResult La règle * @return TRUE si le client possède l'accès demandé */ @@ -175,7 +171,8 @@ public class ClientHandler implements Runnable, AutoCloseable { try { ruleResult.getClass().asSubclass(SigninRule.Result.class); return true; - } catch (ClassCastException e2) { } + } catch (ClassCastException e2) { + } } return false; @@ -193,23 +190,25 @@ public class ClientHandler implements Runnable, AutoCloseable { /** * Vérifie s'il s'âgit d'une demande de déconnexion + * * @param ruleResult */ private void checkSignout(ProtocolReader.ProtocolResult ruleResult) { try { ruleResult.getClass().asSubclass(SignoutRule.Result.class); repository.disconnect(this); - } catch (ClassCastException e2) { } + } catch (ClassCastException e2) { + } } /** * Vérifie s'il s'âgit d'une demande de déconnexion * @param ruleResult private void checkSignError(ProtocolWriter.ProtocolResult ruleResult) { - if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) { - System.out.println("Pas pu connecter"); - repository.disconnect(this); - } + if (ruleResult.getCommand().startsWith(SignErrorRule.NAME)) { + System.out.println("Pas pu connecter"); + repository.disconnect(this); + } } */ @@ -220,12 +219,15 @@ public class ClientHandler implements Runnable, AutoCloseable { synchronized (this) { try { this.wait(); - } catch (InterruptedException e) { e.printStackTrace(); } + } catch (InterruptedException e) { + e.printStackTrace(); + } } } /** * Permet d'envoyer la réponse au client. + * * @param response La réponse */ public void respond(ProtocolWriter.ProtocolResult response) { @@ -239,7 +241,7 @@ public class ClientHandler implements Runnable, AutoCloseable { * AutoClosable Function * Close the Client thread and resources. * - * @since 1.0 + * @since 1.0 */ @Override public void close() { diff --git a/app/src/main/java/lightcontainer/protocol/StandardizedDefinitions.java b/app/src/main/java/lightcontainer/protocol/StandardizedDefinitions.java index 5848f78..4bb0058 100644 --- a/app/src/main/java/lightcontainer/protocol/StandardizedDefinitions.java +++ b/app/src/main/java/lightcontainer/protocol/StandardizedDefinitions.java @@ -5,10 +5,10 @@ public class StandardizedDefinitions { //Parties de regex non-utilisées en dehors de cette classe. private final static String DIGIT = "[\\x30-\\x39]"; private final static String VISIBLECHAR = "[\\x20-\\xFF]"; - private final static String PASSCHAR = "[\\x20-\\xFF]"; + private final static String PASSCHAR = "[\\x22-\\xFF]"; private final static String BINARY = "[\\x00-\\xFF]"; private final static String LETTER = "[\\x41-\\x5A\\x61-\\x7A]"; - private final static String DIGIT_LETTER = DIGIT + "|" + LETTER; + private final static String DIGIT_LETTER = "[\\x30-\\x39\\x41-\\x5A\\x61-\\x7A]"; private final static String PORT = "(6553[\\x30-\\x35])|(655[\\x30-\\x32][\\x30-\\x39])|(65[\\x30-\\x34][\\x30-\\x32]{2})|(6[\\x30-\\x34][\\x30-\\x39]{3})|([\\x31-\\x35][\\x30-\\x39]{4})|([\\x30-\\x35]{0,5})|([\\x30-\\x39]{1,4})"; private final static String SIZE = DIGIT + "{1,10}"; private final static String LINE = "\\x0D\\x0A"; @@ -18,7 +18,7 @@ public class StandardizedDefinitions { private final static String DOMAIN = "[a-z-A-Z0-9\\.]{5,20}"; private final static String HASH_FILENAME = DIGIT_LETTER + "{50,200}"; private final static String HASH_FILECONTENT = DIGIT_LETTER + "{50,200}"; - private final static String FILE_INFO = HASH_FILENAME + BL + SIZE + BL + HASH_FILECONTENT; + private final static String FILE_INFO = "(" + HASH_FILENAME + ")" + BL + "(" + SIZE + ")" + BL + "(" + HASH_FILECONTENT + ")"; private final static String LOGIN = DIGIT_LETTER + "{5,20}"; //Regex à utiliser dans les différents protocoles. @@ -29,11 +29,11 @@ public class StandardizedDefinitions { public final static String FFE_SENDFILE = "^SENDFILE" + BL + FILE_INFO + LINE + "$"; public final static String SBE_SEND_RESULT_OK = "^SEND_OK" + LINE + "$"; public final static String SBE_SEND_RESULT_ERROR = "^SEND_ERROR" + LINE + "$"; - public final static String FFE_ERASE_FILE = "^ERASEFILE" + BL + HASH_FILENAME + LINE + "$"; + public final static String FFE_ERASE_FILE = "^ERASEFILE" + BL + "(" + HASH_FILENAME + ")" + LINE + "$"; public final static String SBE_ERASE_RESULT_OK = "^ERASE_OK" + LINE + "$"; public final static String SBE_ERASE_RESULT_ERROR = "^ERASE_ERROR" + LINE + "$"; - public final static String FFE_RETRIEVE_FILE = "^RETRIEVEFILE" + BL + HASH_FILENAME + LINE + "$"; - public final static String SBE_RETRIEVE_RESULT_OK = "^RETRIEVE_OK" + BL + "(" + FILE_INFO + ")" + LINE + "$"; + public final static String FFE_RETRIEVE_FILE = "^RETRIEVEFILE" + BL + "(" + HASH_FILENAME + ")" + LINE + "$"; + public final static String SBE_RETRIEVE_RESULT_OK = "^RETRIEVE_OK" + BL + FILE_INFO + LINE + "$"; public final static String SBE_RETRIEVE_RESULT_ERROR = "^RETRIEVE_ERROR" + LINE + "$"; //Client demande à FFE une tâche. FFE répond à client. 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 a667672..116fb6d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/GetFileRule.java @@ -4,7 +4,6 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.GetFileErrorRule; -import lightcontainer.protocol.rules.writer.RemoveFileErrorRule; import lightcontainer.protocol.rules.writer.RetrieveFileRule; import lightcontainer.storage.ReadOnlyFile; @@ -80,9 +79,11 @@ public class GetFileRule extends ProtocolReader { return result; } - /** TMP + /** + * TMP * TODO : But futur est de pouvoir en avoir plusieurs (sbe) * Cette méthode permet de choisir le domaine voulu. + * * @param storageIterator Les domaines * @return Le domain choisi */ diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java index b2b797d..4c0aa4b 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/HelloRule.java @@ -8,9 +8,9 @@ import lightcontainer.protocol.ProtocolReader; */ 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}) ([\\d]{0,5})\r\n$"; - private static final String NAME = "HELLO"; + private static final String NAME = "HELLO"; // Index du domain dans le tableau de donnée private static final int DOMAIN = 0; 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 226e2f5..3d7044d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/RetrieveOkRule.java @@ -43,6 +43,14 @@ public class RetrieveOkRule extends ProtocolReader { this.hashedFileContent = hashedFileContent; } + public String getFilename() { + return filename; + } + + public int getFilesize() { + return filesize; + } + /** * Bitch has bettern than my money * @param reader Buffer rempli du fichier @@ -70,7 +78,8 @@ public class RetrieveOkRule extends ProtocolReader { RetrieveOkRule.Result result = new RetrieveOkRule.Result(context, data[HASHED_FILE_NAME], Integer.parseInt(data[FILE_SIZE]), data[HASHED_FILE_CONTENT]); // save encrypted file size into bundle - context.putDataInt("encryptedFileSize", Integer.parseInt(data[FILE_SIZE])); // TODO to long ?! + context.putDataInt("encryptedFileSize", result.getFilesize()); // TODO to long ?! + context.putDataString("hashedFileName", result.getFilename()); // Set result command result.setResultCommand(protocolRep.executeWriter(context, GetFileOkRule.NAME, context.getDataString("fileName"), String.valueOf(context.getDataInt("fileSize"))), ResultCmdReceiver.CLIENT); diff --git a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java index 2d089d0..9128f97 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SavefileRule.java @@ -4,17 +4,14 @@ 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; import lightcontainer.protocol.rules.writer.SendfileRule; import lightcontainer.utils.AES_GCM; import lightcontainer.utils.FileReceiver; import lightcontainer.utils.SHA; import lightcontainer.utils.ShaHasher; -import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.security.NoSuchAlgorithmException; /** * Règle permettant de sauvegarder un fichier sur le SBE. @@ -22,7 +19,7 @@ import java.security.NoSuchAlgorithmException; */ 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 = "^SAVE_FILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$"; private static final String NAME = "SAVEFILE"; @@ -49,6 +46,7 @@ public class SavefileRule extends ProtocolReader { super(context); this.filename = filename; this.size = size; + System.out.println(size); } @Override @@ -76,6 +74,8 @@ public class SavefileRule extends ProtocolReader { int encryptedFileSize = fileReceiver.receiveFile(reader, this.filename, this.size, key, iv); if (encryptedFileSize < 0) throw new IOException(); + System.out.println(encryptedFileSize); + String fileHash = SHA.hashFile(storagePath, this.filename); // On met les données de la requête actuelle 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 1f395f1..520fa19 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendErrorRule.java @@ -4,11 +4,6 @@ import lightcontainer.domains.client.Context; import lightcontainer.interfaces.ProtocolRepository; import lightcontainer.protocol.ProtocolReader; import lightcontainer.protocol.rules.writer.SaveFileErrorRule; -import lightcontainer.utils.ShaHasher; - -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. 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 3c32bca..d7fb36d 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/reader/SendOkRule.java @@ -5,10 +5,6 @@ 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. */ diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java index 4fcba04..f0d9794 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/EraseFileRule.java @@ -4,6 +4,7 @@ import lightcontainer.protocol.ProtocolWriter; public class EraseFileRule extends ProtocolWriter { + //"^ERASEFILE ([A-Za-z0-9]{50,200})\r\n$" private static final String PATTERN = "^ERASEFILE ([A-Za-z0-9.]{50,200})\r\n$"; public static String NAME = "ERASEFILE"; @@ -11,5 +12,6 @@ public class EraseFileRule extends ProtocolWriter { public EraseFileRule() { super(NAME, PATTERN); + System.out.println(PATTERN); } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java index e0f50c4..457f978 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/FilesRule.java @@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter; * Règle permettant de construire une commande contenant la liste des fichiers d'un utilisateur */ public class FilesRule extends ProtocolWriter { - + //"^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$" private static final String PATTERN = "^FILES( ([^ !]{1,20})!([0-9]{1,10})){0,50}\r\n$"; public static final String NAME = "FILES"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java index 4a963d6..e43ec21 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileErrorRule.java @@ -8,6 +8,7 @@ import lightcontainer.protocol.ProtocolWriter; */ public class GetFileErrorRule extends ProtocolWriter { // Constants + //"^GETFILE_ERROR\r\n$" private static final String PATTERN = "^GETFILE_ERROR\r\n$"; public static final String NAME = "GETFILE_ERROR"; 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 87dc71d..8b5078a 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/GetFileOkRule.java @@ -6,6 +6,8 @@ import lightcontainer.utils.FileSender; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; public class GetFileOkRule extends ProtocolWriter { // Constants @@ -47,12 +49,16 @@ public class GetFileOkRule extends ProtocolWriter { System.out.println("Encrypted size for parsing: " + getContext().getDataInt("encryptedFileSize")+" normal: "+getContext().getDataInt("fileSize")); FileSender fileSender = new FileSender(storagePath); fileSender.sendFile( - getContext().getHashedFileName(this.filename), + getContext().getDataString("hashedFileName"), writer, getContext().getDataInt("encryptedFileSize"), // Encrypted file size (because data is parsing into AES system) getContext().getAesKey(), getContext().getDataString("fileIV") ); + + try { + Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, getContext().getDataString("hashedFileName")))); + } catch (IOException ignore) { } } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java index 5e7517f..c071683 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileErrorRule.java @@ -4,6 +4,7 @@ import lightcontainer.protocol.ProtocolWriter; public class RemoveFileErrorRule extends ProtocolWriter { + //"^REMOVEFILE_ERROR\r\n$" private static final String PATTERN = "^REMOVEFILE_ERROR\r\n$"; public static String NAME = "REMOVEFILE_ERROR"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java index a4ae784..bc9fc39 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RemoveFileOkRule.java @@ -3,7 +3,7 @@ package lightcontainer.protocol.rules.writer; import lightcontainer.protocol.ProtocolWriter; public class RemoveFileOkRule extends ProtocolWriter { - + //"^REMOVEFILE_OK\r\n$" private static final String PATTERN = "^REMOVEFILE_OK\r\n$"; public static String NAME = "REMOVEFILE_OK"; 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 c397129..b64d1bb 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/RetrieveFileRule.java @@ -4,7 +4,8 @@ import lightcontainer.protocol.ProtocolWriter; public class RetrieveFileRule extends ProtocolWriter { // Constants - private static final String PATTERN = "^RETRIEVEFILE ([A-Za-z0-9.]{50,200})\r\n$"; + //"^RETRIEVEFILE ([A-Za-z0-9]{50,200})\r\n$" + private static final String PATTERN = "^RETRIEVEFILE ([A-Za-z0-9]{50,200})\r\n$"; public static final String NAME = "RETRIEVEFILE"; // Constructor diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java index dca98d8..98a138c 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileErrorRule.java @@ -12,7 +12,7 @@ import java.nio.file.Path; * Règle signifiant que la sauvegarde d'un fichier a échoué */ public class SaveFileErrorRule extends ProtocolWriter { - + //"^SAVEFILE_ERROR\r\n$" private static final String PATTERN = "^SAVEFILE_ERROR\r\n$"; public static final String NAME = "SAVEFILE_ERROR"; @@ -38,7 +38,8 @@ public class SaveFileErrorRule extends ProtocolWriter { hasher.fromSalt(hasher.saltToByte(context.getDataString("fileNameSalt"))) ) )); - } catch (IOException e) {} + } catch (IOException e) { + } */ diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java index fac890f..13473f5 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SaveFileOkRule.java @@ -34,8 +34,7 @@ public class SaveFileOkRule extends ProtocolWriter { String hashedFileName = context.getHashedFileName(context.getDataString("fileName")); try { Files.deleteIfExists(Path.of(String.format("%s/%s", storagePath, hashedFileName))); - } catch (IOException e) {} - + } catch (IOException ignore) {} return result; } } diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java index 43616be..3eae4bd 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SendfileRule.java @@ -11,15 +11,15 @@ import java.io.OutputStream; * Règle envoyée au SBE, demandant la sauvegarde d'un fichier. */ public class SendfileRule extends ProtocolWriter { - - private static final String PATTERN = "^SENDFILE [A-Za-z0-9.]{50,200} [0-9]{1,10} [A-Za-z0-9.]{50,200}\r\n$"; + //"^SENDFILE [A-Za-z0-9]{50,200} [0-9]{1,10} [A-Za-z0-9]{50,200}\r\n$" + private static final String PATTERN = "^SENDFILE [A-Za-z0-9]{50,200} [0-9]{1,10} [A-Za-z0-9]{50,200}\\r\\n$"; public static final String NAME = "SENDFILE"; private static final int HASHED_FILE_NAME = 0; // Index file name hashed. private static final int FILE_SIZE = 1; // Index file size. private static final int HASHED_FILE_CONTENT = 2; // Index file content hashed. - + private String storagePath; public SendfileRule(String storagePath) { diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java index 096bb9a..ca02cc6 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignErrorRule.java @@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter; * Règle renvoyée au client lorsque l'authentification a échoué. */ public class SignErrorRule extends ProtocolWriter { - + //"^SIGN_ERROR\r\n$" private static final String PATTERN = "^SIGN_ERROR\r\n$"; public static final String NAME = "SIGN_ERROR"; diff --git a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java index 6d6ef04..05e0295 100644 --- a/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java +++ b/app/src/main/java/lightcontainer/protocol/rules/writer/SignOkRule.java @@ -6,7 +6,7 @@ import lightcontainer.protocol.ProtocolWriter; * Règle renvoyée au client lorsque l'authentification a réusie. */ public class SignOkRule extends ProtocolWriter { - + //"^SIGN_OK\r\n$" private static final String PATTERN = "^SIGN_OK\r\n$"; public static final String NAME = "SIGN_OK"; diff --git a/app/src/main/java/lightcontainer/utils/FileReceiver.java b/app/src/main/java/lightcontainer/utils/FileReceiver.java index 826bb1f..f852587 100644 --- a/app/src/main/java/lightcontainer/utils/FileReceiver.java +++ b/app/src/main/java/lightcontainer/utils/FileReceiver.java @@ -8,18 +8,17 @@ public class FileReceiver { public FileReceiver(String path) { this.path = path; } public int receiveFile(InputStream input, String fileName, int fileSize, String key, String iv) { - BufferedOutputStream bosFile = null; - try { - bosFile = new BufferedOutputStream(new FileOutputStream(String.format("%s/%s", path, fileName))); - - AES_GCM.encryptStream(input, bosFile, fileSize, key, iv); - - File f = new File(String.format("%s/%s", path, fileName)); - return (int)f.length(); // TODO change the size to LONG + File file = new File(String.format("%s/%s", path, fileName)); + if (file.createNewFile()) { + try (BufferedOutputStream bufferedStream = new BufferedOutputStream(new FileOutputStream(file))) { + // AES close l'outputstream mais on le try with ressources au cas ou ;) + AES_GCM.encryptStream(input, bufferedStream, fileSize, key, iv); + } + } + return (int)file.length(); // TODO change the size to LONG } catch(IOException | AES_GCM.AesGcmException ex) { ex.printStackTrace(); - if(bosFile != null) { try { bosFile.close(); } catch(Exception e) {} } return -1; } } diff --git a/app/src/main/java/lightcontainer/utils/FileSender.java b/app/src/main/java/lightcontainer/utils/FileSender.java index 008618a..227855d 100644 --- a/app/src/main/java/lightcontainer/utils/FileSender.java +++ b/app/src/main/java/lightcontainer/utils/FileSender.java @@ -27,13 +27,12 @@ public class FileSender { public boolean sendFile(String filename, OutputStream out) throws IOException { BufferedInputStream bisFile; int bytesReaded = 0; - try { - File f = new File(String.format("%s/%s", path, filename)); - long fileSize = f.length(); - if(f.exists()) { + File file = new File(String.format("%s/%s", path, filename)); + long fileSize = file.length(); + if(file.exists()) { byte[] buffer = new byte[DEFAULT_BUFFER]; - bisFile = new BufferedInputStream(new FileInputStream(f)); + bisFile = new BufferedInputStream(new FileInputStream(file)); long currentOffset = 0; while((currentOffset < fileSize) && (bytesReaded = bisFile.read(buffer)) > 0) { out.write(buffer, 0, bytesReaded); out.flush(); @@ -43,7 +42,7 @@ public class FileSender { return true; } else return false; - } catch(IOException ex) { + } catch(IOException ex) {// todo change throw ex; } } diff --git a/app/src/main/java/lightcontainer/utils/SHA.java b/app/src/main/java/lightcontainer/utils/SHA.java index 0f76efa..25cd806 100644 --- a/app/src/main/java/lightcontainer/utils/SHA.java +++ b/app/src/main/java/lightcontainer/utils/SHA.java @@ -87,7 +87,11 @@ public class SHA { public static String hashFile(String rootPath, String fileName) throws ShaException { try { File file = new File(String.format("%s/%s", rootPath, fileName)); - return hashStream(new FileInputStream(file), file.length()); + String hash; + try (InputStream inStream = new FileInputStream(file)) { + hash = hashStream(inStream, file.length()); + } + return hash; } catch (Exception e) { throw new ShaException(e); }