From d4cff6de094543f24ebe9d521f345d6c96d06966 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 09:05:03 +0100 Subject: [PATCH] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier=20de?= =?UTF-8?q?=20synchronisation=20->=20Json=20vers=20AppData=20termin=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lightcontainer/storage/JsonAdapter.java | 61 ++++++++++++++++++- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index d055c12..55a4792 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -1,9 +1,8 @@ package lightcontainer.storage; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; +import com.google.gson.*; -import java.util.Iterator; +import java.util.*; public class JsonAdapter implements Adapter { @@ -65,6 +64,62 @@ public class JsonAdapter implements Adapter { @Override public AppData fromString(String appDataString) { + try { + JsonElement jsonString = JsonParser.parseString(appDataString); + JsonObject jsonAppData = jsonString.getAsJsonObject(); + AppConfig appConfig = AppConfig.getInstance(); + appConfig.setUnicastPort(jsonAppData.get("unicast_port").getAsInt()); + appConfig.setMulticastIp(jsonAppData.get("multicast_ip").getAsString()); + appConfig.setMulticastPort(jsonAppData.get("multicast_port").getAsInt()); + appConfig.setNetworkInterface(jsonAppData.get("network_interface").getAsString()); + appConfig.setTls(jsonAppData.get("tls").getAsBoolean()); + JsonArray jsonUsers = jsonAppData.getAsJsonArray("users"); + List users = new ArrayList<>(); + getUsers(jsonUsers, users); + AppData appData = AppData.getInstance(); + appData.setAppConfig(appConfig); + for (User user : users) { + appData.addUser(user); + } + return appData; + } catch (JsonParseException parseException) { + System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log + } return null; } + + private void getUsers(JsonArray jsonUsers, List users) { + for (JsonElement element : jsonUsers) { + JsonObject jsonUser = element.getAsJsonObject(); + String name = jsonUser.get("username").getAsString(); + String password = jsonUser.get("password").getAsString(); + String aeskey = jsonUser.get("aes_key").getAsString(); + Map userFiles = new HashMap<>(); + JsonArray jsonFiles = jsonUser.getAsJsonArray("files"); + getFiles(userFiles, jsonFiles); + User user = new User(name, password, aeskey, userFiles); + users.add(user); + } + } + + private void getFiles(Map userFiles, JsonArray jsonFiles) { + for (JsonElement fileElement : jsonFiles) { + JsonObject jsonFile = fileElement.getAsJsonObject(); + String fileName = jsonFile.get("name").getAsString(); + int size = jsonFile.get("size").getAsInt(); + String iv = jsonFile.get("iv").getAsString(); + Set storage = new HashSet<>(); + JsonArray jsonStorage = jsonFile.getAsJsonArray("storage"); + getStorage(storage, jsonStorage); + File file = new File(fileName, size, iv, storage); + userFiles.put(file.getName(), file); + } + } + + private void getStorage(Set storage, JsonArray jsonStorage) { + for (JsonElement storageElement : jsonStorage) { + String storageName = storageElement.getAsString(); + storage.add(storageName); + } + } }