aboutsummaryrefslogtreecommitdiff
path: root/src/main/ui/JCA.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/ui/JCA.java')
-rw-r--r--src/main/ui/JCA.java95
1 files changed, 76 insertions, 19 deletions
diff --git a/src/main/ui/JCA.java b/src/main/ui/JCA.java
index 882c546..420ec10 100644
--- a/src/main/ui/JCA.java
+++ b/src/main/ui/JCA.java
@@ -1,9 +1,14 @@
package ui;
+import model.asn1.exceptions.InvalidDBException;
import model.asn1.exceptions.ParseException;
import model.ca.CertificationAuthority;
+import persistence.Decoder;
+import persistence.FS;
+import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Scanner;
@@ -13,6 +18,11 @@ import java.util.Scanner;
*/
public class JCA {
/**
+ * Default db file (./data/ca.json)
+ */
+ private static final Path PATH_DEFAULT = Path.of("data", "ca.json");
+
+ /**
* Instances of the five screens;
*/
private final UIHandler mainScreen;
@@ -23,13 +33,18 @@ public class JCA {
/**
* The CA
*/
- private final CertificationAuthority ca;
+ private CertificationAuthority ca;
/**
* The current screen.
*/
private UIHandler screen;
/**
+ * There are unsaved changes.
+ */
+ private boolean unsaved = false;
+
+ /**
* EFFECTS: Init with main screen and empty CA. No private key and no CA cert.
* Throws {@link NoSuchAlgorithmException} when crypto issue happens.
*/
@@ -106,34 +121,72 @@ public class JCA {
screen.enter(args);
}
+ /**
+ * EFFECTS: Read the database file and replace all local states.
+ * MODIFIES: this
+ */
+ private void load() {
+ if (unsaved) {
+ System.out.println("Current database is not saved yet.");
+ return;
+ }
+ try {
+ this.ca = Decoder.decodeCA(FS.read(PATH_DEFAULT));
+ } catch (InvalidDBException | NoSuchAlgorithmException e) {
+ System.out.println(e.getMessage());
+ if (e.getCause() != null) {
+ System.out.println(e.getCause().getMessage());
+ e.getCause().printStackTrace();
+ }
+ }
+ }
+
private void handleLine(String... args) {
if (args[0].equals("log")) {
ca.getLogs().forEach(System.out::println);
return;
}
- switch (args[0]) {
- case "help":
- screen.help();
- System.out.println("log\tView audit logs");
- break;
- case "show":
- screen.show();
- break;
- case "commit":
- screen.commit();
- break;
- case "exit":
- setScreen(screen.exit());
- break;
- default:
- screen.command(args);
- break;
+ if ("help".equals(args[0])) {
+ screen.help();
+ System.out.println("log\tView audit logs");
+ System.out.println("load\tLoad database");
+ System.out.println("save\tSave database");
+ } else if ("commit".equals(args[0])) {
+ screen.commit();
+ } else if ("show".equals(args[0])) {
+ screen.show();
+ } else if ("exit".equals(args[0])) {
+ setScreen(screen.exit());
+ } else if ("save".equals(args[0])) {
+ save();
+ } else if ("load".equals(args[0])) {
+ load();
+ } else {
+ screen.command(args);
}
printPS1();
}
+ /**
+ * EFFECTS: Print the '*user@JCA PS1' line
+ */
private void printPS1() {
- System.out.printf("%s@JCA %s ", ca.getUser(), screen.getPS1());
+ System.out.printf("%s%s@JCA %s ",
+ unsaved ? "*" : "",
+ ca.getUser(),
+ screen.getPS1());
+ }
+
+ /**
+ * EFFECTS: Save the DB
+ */
+ public void save() {
+ try {
+ FS.write(PATH_DEFAULT, Decoder.encodeCA(ca));
+ unsaved = false;
+ } catch (IOException e) {
+ System.out.println(e.getMessage());
+ }
}
/**
@@ -152,6 +205,10 @@ public class JCA {
}
}
+ public void setUnsaved(boolean unsaved) {
+ this.unsaved = unsaved;
+ }
+
public CertificationAuthority getCa() {
return ca;
}