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<>();
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;
+ }
+
}
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;
public class Organisation extends CertificateOwner {
+ private static final long serialVersionUID = -2386342985586320843L;
+
@DataContainer
public static class Affiliation {
public boolean isSelfOrganisation() {
return SELF_ORG_NAME.equals(getName());
}
+
+ private void writeObject(ObjectOutputStream oos) throws IOException {}
+
+ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
+
}
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;
*/
public class User extends CertificateOwner {
+ private static final long serialVersionUID = -7915843843752264176L;
+
private DayDate dob;
private String email;
}
}
+ private void writeObject(ObjectOutputStream oos) throws IOException {}
+
+ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {}
+
}
package org.cacert.gigi.util;
import java.io.PrintWriter;
+import java.io.Serializable;
import java.util.Arrays;
import java.util.Map;
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;
--- /dev/null
+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);
+ }
+
+}