]> WPIA git - gigi.git/commitdiff
add: Implement (de)serialisation support for CertificateOwner and derived classes
authorBenny Baumann <BenBE1987@gmx.net>
Sun, 7 Aug 2016 01:02:55 +0000 (03:02 +0200)
committerBenny Baumann <BenBE1987@gmx.net>
Sun, 7 Aug 2016 17:31:22 +0000 (19:31 +0200)
Change-Id: I7a82af006ada0a9001db36cbb354cdcef13f5b80

src/org/cacert/gigi/dbObjects/CertificateOwner.java
src/org/cacert/gigi/dbObjects/Organisation.java
src/org/cacert/gigi/dbObjects/User.java
src/org/cacert/gigi/util/AuthorizationContext.java
tests/org/cacert/gigi/TestUserSerialize.java [new file with mode: 0644]

index e1a9cca02a978abd041b148f95d0b3c84bc0ce0a..daff8bcb787866d631c83793f3794876c3beccf9 100644 (file)
@@ -1,12 +1,19 @@
 package org.cacert.gigi.dbObjects;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import java.io.Serializable;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
 
-public abstract class CertificateOwner implements IdCachable {
+public abstract class CertificateOwner implements IdCachable, Serializable {
+
+    private static final long serialVersionUID = -672580485730247314L;
 
     private static final ObjectCache<CertificateOwner> myCache = new ObjectCache<>();
 
@@ -125,4 +132,31 @@ public abstract class CertificateOwner implements IdCachable {
             return null;
         }
     }
+
+    private void writeObject(ObjectOutputStream oos) throws IOException {
+        oos.writeLong(getId());
+    }
+
+    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
+        id = (int) ois.readLong();
+    }
+
+    protected Object readResolve() throws ObjectStreamException {
+        /**
+         * Returning the Object by looking up its ID in the cache.
+         *
+         * @see http://www.javalobby.org/java/forums/t17491.html
+         * @see http://www.jguru.com/faq/view.jsp?EID=44039
+         * @see http://thecodersbreakfast.net/
+         *      ?post/2011/05/12/Serialization-and-magic-methods
+         */
+        CertificateOwner co = getById(this.getId());
+
+        if (null == co) {
+            throw new Error("Unknown Certificate Owner");
+        }
+
+        return co;
+    }
+
 }
index 7380381a50932e4efb08e4f42db59ee1cd279c7c..9581f815f170d1ad4801ccc702e540c0267fbe6f 100644 (file)
@@ -1,5 +1,8 @@
 package org.cacert.gigi.dbObjects;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -11,6 +14,8 @@ import org.cacert.gigi.dbObjects.wrappers.DataContainer;
 
 public class Organisation extends CertificateOwner {
 
+    private static final long serialVersionUID = -2386342985586320843L;
+
     @DataContainer
     public static class Affiliation {
 
@@ -253,4 +258,9 @@ public class Organisation extends CertificateOwner {
     public boolean isSelfOrganisation() {
         return SELF_ORG_NAME.equals(getName());
     }
+
+    private void writeObject(ObjectOutputStream oos) throws IOException {}
+
+    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
+
 }
index cf43f80f3182a4377a1578b3f11ac370375f8e53..009814762ec89ecb40df9cfecd045bf9374a0c57 100644 (file)
@@ -1,5 +1,8 @@
 package org.cacert.gigi.dbObjects;
 
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
@@ -29,6 +32,8 @@ import org.cacert.gigi.util.TimeConditions;
  */
 public class User extends CertificateOwner {
 
+    private static final long serialVersionUID = -7915843843752264176L;
+
     private DayDate dob;
 
     private String email;
@@ -603,4 +608,8 @@ public class User extends CertificateOwner {
         }
     }
 
+    private void writeObject(ObjectOutputStream oos) throws IOException {}
+
+    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
+
 }
index 8098eeed5aa742cebd3250554098bcb6ffc75ce6..892f1175d00d376d5dde56bf3ad25295281b644d 100644 (file)
@@ -1,6 +1,7 @@
 package org.cacert.gigi.util;
 
 import java.io.PrintWriter;
+import java.io.Serializable;
 import java.util.Arrays;
 import java.util.Map;
 
@@ -13,13 +14,15 @@ import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.template.Outputable;
 import org.cacert.gigi.output.template.SprintfCommand;
 
-public class AuthorizationContext implements Outputable {
+public class AuthorizationContext implements Outputable, Serializable {
 
-    CertificateOwner target;
+    private static final long serialVersionUID = -2596733469159940154L;
 
-    User actor;
+    private CertificateOwner target;
 
-    String supporterTicketId;
+    private User actor;
+
+    private String supporterTicketId;
 
     public AuthorizationContext(CertificateOwner target, User actor) {
         this.target = target;
diff --git a/tests/org/cacert/gigi/TestUserSerialize.java b/tests/org/cacert/gigi/TestUserSerialize.java
new file mode 100644 (file)
index 0000000..91c1dfd
--- /dev/null
@@ -0,0 +1,45 @@
+package org.cacert.gigi;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.cacert.gigi.dbObjects.User;
+import org.cacert.gigi.testUtils.BusinessTest;
+import org.junit.Test;
+
+public class TestUserSerialize extends BusinessTest {
+
+    private byte[] serialize(Object o) throws IOException {
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+            try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
+                oos.writeObject(o);
+                oos.flush();
+            }
+            baos.flush();
+            return baos.toByteArray();
+        }
+    }
+
+    private Object deserialize(byte[] ba) throws IOException, ClassNotFoundException {
+        try (ByteArrayInputStream bais = new ByteArrayInputStream(ba)) {
+            try (ObjectInputStream ois = new ObjectInputStream(bais)) {
+                Object o = ois.readObject();
+                return o;
+            }
+        }
+    }
+
+    @Test
+    public void testSerializeUser() throws GigiApiException, IOException, ClassNotFoundException {
+        User u = createVerifiedUser();
+        byte[] ba = serialize(u);
+        Object uo = deserialize(ba);
+        assertSame("Original user and the deserialized object must be the same", u, uo);
+    }
+
+}