diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java new file mode 100644 index 0000000..7ffb90d --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -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); +} diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java index 88b245e..2a693b2 100644 --- a/app/src/main/java/lightcontainer/storage/AppData.java +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -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 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; } } diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java new file mode 100644 index 0000000..d055c12 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -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 userIterator = appData.usersIterator(); + addUsers(users, userIterator); + config.addProperty("users", users.toString()); + return config.toString(); + } + + private void addUsers(JsonArray users, Iterator 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 fileIterator = current.fileIterator(); + addFiles(fileIterator); + user.addProperty("files", files.toString()); + users.add(user); + } + } + + private void addFiles(Iterator 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 storageIterator = currentFile.getStorageIterator(); + addStorage(storage, storageIterator); + file.addProperty("storage", storage.toString()); + } + } + + private void addStorage(JsonArray storage, Iterator storageIterator) { + while (storageIterator.hasNext()) { + String storageString = storageIterator.next(); + storage.add(storageString); + } + } + + @Override + public AppData fromString(String appDataString) { + return null; + } +} diff --git a/app/src/main/java/lightcontainer/storage/JsonReader.java b/app/src/main/java/lightcontainer/storage/JsonReader.java deleted file mode 100644 index 0b67603..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonReader.java +++ /dev/null @@ -1,9 +0,0 @@ -package lightcontainer.storage; - -public class JsonReader implements FileReader { - - @Override - public AppData readFile(String fileName) { - return null; - } -} diff --git a/app/src/main/java/lightcontainer/storage/JsonRepository.java b/app/src/main/java/lightcontainer/storage/JsonRepository.java deleted file mode 100644 index c2c693d..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonRepository.java +++ /dev/null @@ -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; - } - - -} diff --git a/app/src/main/java/lightcontainer/storage/JsonWriter.java b/app/src/main/java/lightcontainer/storage/JsonWriter.java deleted file mode 100644 index 56fceb9..0000000 --- a/app/src/main/java/lightcontainer/storage/JsonWriter.java +++ /dev/null @@ -1,9 +0,0 @@ -package lightcontainer.storage; - -public class JsonWriter implements FileWriter { - - @Override - public void writeToFile(String fileName, AppData appData) { - - } -} diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index b688fc7..6757ff5 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -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"; - AppData load(FileReader fileReader); + void save(String filePath, Adapter adapter) { + + } + + AppData load(String filePath, Adapter adapter) { + return null; + } } diff --git a/app/src/main/java/lightcontainer/storage/User.java b/app/src/main/java/lightcontainer/storage/User.java index 683fb07..9ba83f2 100644 --- a/app/src/main/java/lightcontainer/storage/User.java +++ b/app/src/main/java/lightcontainer/storage/User.java @@ -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> getFilesIterator() { - return files.entrySet(); + public Iterator 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 { - this.files.put(file.getName(), file); - return true; - } + public void addFile(File file) { + this.files.put(file.getName(), file); } /**