- EraseErrorRule : lecture de l'erreur envoyée par le SBE lors de la suppression d'un fichier. - EraseFileRule : commande créée par le FFE pour que le SBE puisse savoir quel fichier il doit supprimer. - EraseOkRule : lecture de la commande envoyée par le SBE lorsque la suppression d'un fichier s'est bien passée. - File : implémente désormais une interface ReadOnlyFile. - GetFileRule : utilise ReadOnlyFile plutôt que File. - ProtocolReader : reformatage. - ReadOnlyFile : interface pour récupérer les données de File. - RemoveFileErrorRule : commande envoyée au client lorsque la suppression d'un fichier a échoué. - RemoveFileOkRule : commande envoyée au client lorsque la suppression d'un fichier a réussi. - RemoveFileRule : lecture de la commande envoyée par le client lorsqu'il veut supprimer un fichier. - Repository : utilise ReadOnlyFile plutôt que File. - SendErrorRule et SendOkRule : modification des imports et attribut final.
162 lines
4.7 KiB
Java
162 lines
4.7 KiB
Java
package lightcontainer.storage;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.BufferedWriter;
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.List;
|
|
|
|
public class Repository {
|
|
// Variables
|
|
private final String filePath;
|
|
private final Adapter adapter;
|
|
private final AppData appData;
|
|
|
|
/**
|
|
* @param filePath The path to the configuration file
|
|
* @param adapter The adapter that converts objects to string and vice-versa
|
|
*/
|
|
public Repository(String filePath, AppData appData, Adapter adapter) {
|
|
this.filePath = filePath;
|
|
this.appData = appData;
|
|
this.adapter = adapter;
|
|
}
|
|
|
|
/**
|
|
* Saves configuration file
|
|
*/
|
|
public void save() {
|
|
if (filePath != null) {
|
|
String jsonAppData = adapter.toString(appData);
|
|
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
|
|
bufferedWriter.write(jsonAppData);
|
|
bufferedWriter.flush();
|
|
} catch (IOException e) {
|
|
System.out.println("Error while saving configuration file !");
|
|
}
|
|
}
|
|
}
|
|
|
|
public boolean addUser(String login, String password, String key, String passwordSalt) {
|
|
if (appData.addUser(login, password, key, passwordSalt)) {
|
|
save();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean addFileFor(File file, String userName) {
|
|
if (appData.addFileFor(file, userName)) {
|
|
save();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean canAddFile(String login) {
|
|
return appData.canAddFile(login);
|
|
}
|
|
|
|
public boolean deleteFileOf(String fileName, String userName) {
|
|
if (appData.deleteFileOf(fileName, userName)) {
|
|
save();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean addStorage(String userName, File file, String storage) {
|
|
if (appData.addStorage(userName, file, storage)) {
|
|
save();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Loads configuration file
|
|
*
|
|
* @return AppData
|
|
*/
|
|
public AppData load() {
|
|
String jsonString = readFile(filePath);
|
|
return adapter.fromString(jsonString);
|
|
}
|
|
|
|
private String readFile(String filePath) {
|
|
StringBuilder builder = new StringBuilder();
|
|
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath).toAbsolutePath(), StandardCharsets.UTF_8)) {
|
|
while (reader.ready()) {
|
|
builder.append(reader.readLine());
|
|
}
|
|
} catch (IOException e) {
|
|
System.out.println("Error while reading configuration file");
|
|
builder.setLength(0);
|
|
}
|
|
return builder.toString();
|
|
}
|
|
|
|
public int getUnicastPort() {
|
|
return appData.getAppConfig().getUnicastPort();
|
|
}
|
|
|
|
public String getMulticastIp() {
|
|
return appData.getAppConfig().getMulticastIp();
|
|
}
|
|
|
|
public int getMulticastPort() {
|
|
return appData.getAppConfig().getMulticastPort();
|
|
}
|
|
|
|
public boolean verifyUser(String login, String password) {
|
|
return appData.verifyUser(login, password);
|
|
}
|
|
|
|
public String getUserPasswordSalt(String login) {
|
|
return appData.getUserPasswordSalt(login);
|
|
}
|
|
|
|
/**
|
|
* Méthode permettant de récupérer le chemin de sauvegarde des fichiers
|
|
*
|
|
* @return Chemin de sauvegarde
|
|
*/
|
|
public String getStoragePath() {
|
|
return appData.getAppConfig().getStoragePath();
|
|
}
|
|
|
|
/**
|
|
* Méthode permettant de récupérer la clé AES d'un utilisateur
|
|
*
|
|
* @param login Login de l'utilisateur
|
|
* @return Clé AES
|
|
*/
|
|
public String getUserAesKey(String login) {
|
|
return appData.getUserAesKey(login);
|
|
}
|
|
|
|
/**
|
|
* Getter, allow to retrieve a specified file object.
|
|
*
|
|
* @param fileName The name of the file to retrieve.
|
|
* @param userName The name of the user who wants to retrieve the file.
|
|
* @return The requested file matching file name and username, or null
|
|
* if the user or file do not exist.
|
|
*
|
|
* @see AppData#getFileOf(String, String)
|
|
* @see File
|
|
*
|
|
* @author Unknown...
|
|
*/
|
|
public ReadOnlyFile getFileOf(String fileName, String userName) {
|
|
return this.appData.getFileOf(fileName, userName);
|
|
}
|
|
|
|
public List<String> getStringifiedFilesOf(String login) {
|
|
return this.appData.getStringifiedFilesOf(login);
|
|
}
|
|
}
|