91 lines
3.2 KiB
Java
91 lines
3.2 KiB
Java
package lightcontainer.protocol.rules.reader;
|
|
|
|
import lightcontainer.domains.client.Context;
|
|
import lightcontainer.interfaces.ProtocolRepository;
|
|
import lightcontainer.protocol.ProtocolReader;
|
|
import lightcontainer.protocol.rules.writer.SaveFileErrorRule;
|
|
import lightcontainer.protocol.rules.writer.SendfileRule;
|
|
import lightcontainer.utils.AES_GCM;
|
|
import lightcontainer.utils.FileReceiver;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
|
|
/**
|
|
* Règle permettant de sauvegarder un fichier sur le SBE.
|
|
* Celui-ci va chiffre le contenu du fichier à l'aide de AES.
|
|
*/
|
|
public class SavefileRule extends ProtocolReader {
|
|
// Constants
|
|
private static final String PATTERN = "^SAVEFILE ([^ !]{1,20}) ([0-9]{1,10})\r\n$";
|
|
|
|
private static final String NAME = "SAVEFILE";
|
|
|
|
private static final int FILE_NAME = 0; // Index file name.
|
|
private static final int FILE_SIZE = 1; // Index file size.
|
|
|
|
private ProtocolRepository protocolRep;
|
|
private String storagePath;
|
|
|
|
// Constructor
|
|
public SavefileRule(ProtocolRepository protocolRep, String storagePath) {
|
|
super(NAME, PATTERN);
|
|
this.protocolRep = protocolRep;
|
|
this.storagePath = storagePath;
|
|
}
|
|
|
|
public class Result extends ProtocolResult {
|
|
// Variables
|
|
private final String filename;
|
|
private final int size;
|
|
|
|
// Construct
|
|
public Result(Context context, String filename, int size) {
|
|
super(context);
|
|
this.filename = filename;
|
|
this.size = size;
|
|
}
|
|
|
|
@Override
|
|
public void read(InputStream reader) {
|
|
super.read(reader);
|
|
System.out.printf("Sauvegarde du fichier : %s %d\n", filename, size);
|
|
|
|
try {
|
|
FileReceiver fileReceiver = new FileReceiver(storagePath);
|
|
|
|
String key = AES_GCM.generateSecretKey();
|
|
String iv = AES_GCM.generateIV();
|
|
|
|
if (!fileReceiver.receiveFile(reader, this.filename, this.size, key, iv))
|
|
throw new IOException();
|
|
|
|
this.setResultCommand(protocolRep.executeWriter(getContext(), SendfileRule.NAME, this.filename, String.valueOf(this.size), ""), ResultCmdReceiver.STOREBACKEND);
|
|
} catch (IOException | AES_GCM.AesGcmException e) {
|
|
this.setResultCommand(protocolRep.executeWriter(getContext(), SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cette méthode est appelée lors de l'exécution de la règle
|
|
* @param data Paramètres pour créer la commande.
|
|
*/
|
|
@Override
|
|
protected SavefileRule.Result onExecuted(Context context, String... data) {
|
|
SavefileRule.Result result = new SavefileRule.Result(context, data[FILE_NAME], Integer.parseInt(data[FILE_SIZE]));
|
|
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
protected ProtocolReader.ProtocolResult onError(Context context) {
|
|
ProtocolReader.ProtocolResult result = new ProtocolReader.ProtocolResult(context);
|
|
// Commande renvoyée en cas d'erreur
|
|
result.setResultCommand(protocolRep.executeWriter(context, SaveFileErrorRule.NAME), ResultCmdReceiver.CLIENT);
|
|
|
|
return result;
|
|
}
|
|
}
|