Sauvegarde et lecture du fichier de synchronisation -> AppData vers Json terminé

This commit is contained in:
Maximilien LEDOUX 2022-03-07 08:46:48 +01:00
parent a185a3ecb7
commit 57984456f1
8 changed files with 108 additions and 48 deletions

View File

@ -0,0 +1,16 @@
package lightcontainer.storage;
public interface Adapter {
/**
* @param appData The appData to stringify
* @return The string representation of AppData
*/
String toString(AppData appData);
/**
* @param appDataString The appData string to parse
* @return AppData
*/
AppData fromString(String appDataString);
}

View File

@ -1,6 +1,7 @@
package lightcontainer.storage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
@ -81,6 +82,10 @@ public class AppData {
}
}
public Iterator<User> usersIterator() {
return users.values().iterator();
}
/**
* @param fileName The name of the file
* @param user The user
@ -107,7 +112,8 @@ public class AppData {
if (!this.users.containsKey(user.getName())) {
return false;
} else {
return this.users.get(user.getName()).addFile(file);
this.users.get(user.getName()).addFile(file);
return true;
}
}

View File

@ -0,0 +1,70 @@
package lightcontainer.storage;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.Iterator;
public class JsonAdapter implements Adapter {
@Override
public String toString(AppData appData) {
return addData(appData);
}
private String addData(AppData appData) {
AppConfig appConfig = appData.getAppConfig();
JsonObject config = new JsonObject();
config.addProperty("unicast_port", appConfig.getUnicastPort());
config.addProperty("multicast_ip", appConfig.getMulticastIp());
config.addProperty("multicast_port", appConfig.getMulticastPort());
config.addProperty("network_interface", appConfig.getNetworkInterface());
config.addProperty("tls", appConfig.isTls());
JsonArray users = new JsonArray();
Iterator<User> userIterator = appData.usersIterator();
addUsers(users, userIterator);
config.addProperty("users", users.toString());
return config.toString();
}
private void addUsers(JsonArray users, Iterator<User> userIterator) {
while (userIterator.hasNext()) {
User current = userIterator.next();
JsonObject user = new JsonObject();
user.addProperty("name", current.getName());
user.addProperty("password", current.getPassword());
user.addProperty("aes_key", current.getAesKey());
JsonArray files = new JsonArray();
Iterator<File> fileIterator = current.fileIterator();
addFiles(fileIterator);
user.addProperty("files", files.toString());
users.add(user);
}
}
private void addFiles(Iterator<File> fileIterator) {
while (fileIterator.hasNext()) {
File currentFile = fileIterator.next();
JsonObject file = new JsonObject();
file.addProperty("name", currentFile.getName());
file.addProperty("size", currentFile.getSize());
file.addProperty("iv", currentFile.getIv());
JsonArray storage = new JsonArray();
Iterator<String> storageIterator = currentFile.getStorageIterator();
addStorage(storage, storageIterator);
file.addProperty("storage", storage.toString());
}
}
private void addStorage(JsonArray storage, Iterator<String> storageIterator) {
while (storageIterator.hasNext()) {
String storageString = storageIterator.next();
storage.add(storageString);
}
}
@Override
public AppData fromString(String appDataString) {
return null;
}
}

View File

@ -1,9 +0,0 @@
package lightcontainer.storage;
public class JsonReader implements FileReader {
@Override
public AppData readFile(String fileName) {
return null;
}
}

View File

@ -1,17 +0,0 @@
package lightcontainer.storage;
public class JsonRepository implements Repository {
@Override
public void save(FileWriter fileWriter) {
}
@Override
public AppData load(FileReader fileReader) {
return null;
}
}

View File

@ -1,9 +0,0 @@
package lightcontainer.storage;
public class JsonWriter implements FileWriter {
@Override
public void writeToFile(String fileName, AppData appData) {
}
}

View File

@ -1,8 +1,14 @@
package lightcontainer.storage;
public interface Repository {
public class Repository {
void save(FileWriter fileWriter);
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
void save(String filePath, Adapter adapter) {
AppData load(FileReader fileReader);
}
AppData load(String filePath, Adapter adapter) {
return null;
}
}

View File

@ -1,5 +1,7 @@
package lightcontainer.storage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -36,8 +38,8 @@ public class User {
return aesKey;
}
public Set<Map.Entry<String, File>> getFilesIterator() {
return files.entrySet();
public Iterator<File> fileIterator() {
return files.values().iterator();
}
public File getFile(String fileName) {
@ -48,13 +50,8 @@ public class User {
* @param file The file to add.
* @return False if a file with the same name already exists. Otherwise, adds the file and returns true.
*/
public boolean addFile(File file) {
if (this.files.containsKey(file.getName())) {
return false;
} else {
public void addFile(File file) {
this.files.put(file.getName(), file);
return true;
}
}
/**