You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/03/10 11:48:10 UTC

[causeway] branch master updated: CAUSEWAY-3304: [Commons] ensure Blob/Clob serialization is compatible between v2 and v3

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/master by this push:
     new e533c9d1c1 CAUSEWAY-3304: [Commons] ensure Blob/Clob serialization is compatible between v2 and v3
e533c9d1c1 is described below

commit e533c9d1c17a46815c7f1473e0b045678b862c55
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Mar 10 12:48:04 2023 +0100

    CAUSEWAY-3304: [Commons] ensure Blob/Clob serialization is compatible
    between v2 and v3
    
    - don't directly serialize the MimeType field, instead use its string
    representation instead
---
 .../org/apache/causeway/applib/value/Blob.java     | 47 ++++++++++++++++------
 .../org/apache/causeway/applib/value/Clob.java     | 46 +++++++++++++++------
 2 files changed, 69 insertions(+), 24 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
index 62688c1791..4a90aae5cb 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
@@ -21,7 +21,10 @@ package org.apache.causeway.applib.value;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.InvalidObjectException;
+import java.io.ObjectInputStream;
 import java.io.OutputStream;
+import java.io.Serializable;
 import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Objects;
@@ -81,18 +84,7 @@ import lombok.extern.log4j.Log4j2;
 @Log4j2
 public final class Blob implements NamedWithMimeType {
 
-    /**
-     * Computed for state:
-     *
-     * <p>
-     * <pre>
-     * private final MimeType mimeType;
-     * private final byte[] bytes;
-     * private final String name;
-     * </pre>
-     * </p>
-     */
-    private static final long serialVersionUID = 5659679806709601263L;
+    private static final long serialVersionUID = SerializationProxy.serialVersionUID;
 
     // -- FACTORIES
 
@@ -381,4 +373,35 @@ public final class Blob implements NamedWithMimeType {
 
     }
 
+    // -- SERIALIZATION PROXY
+
+    private Object writeReplace() {
+        return new SerializationProxy(this);
+    }
+
+    private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
+        throw new InvalidObjectException("Proxy required");
+    }
+
+    private static class SerializationProxy implements Serializable {
+        /**
+         * Generated, based on String, String, bytes[]
+         */
+        private static final long serialVersionUID = -950845631214162726L;
+        private final String name;
+        private final String mimeTypeBase;
+        private final byte[] bytes;
+
+        private SerializationProxy(final Blob blob) {
+            this.name = blob.getName();
+            this.mimeTypeBase = blob.getMimeType().getBaseType();
+            this.bytes = blob.getBytes();
+        }
+
+        private Object readResolve() {
+            return new Blob(name, mimeTypeBase, bytes);
+        }
+
+    }
+
 }
diff --git a/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java b/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
index 8128ff58c3..f0c18bc308 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/value/Clob.java
@@ -21,7 +21,10 @@ package org.apache.causeway.applib.value;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InvalidObjectException;
+import java.io.ObjectInputStream;
 import java.io.OutputStreamWriter;
+import java.io.Serializable;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.nio.charset.Charset;
@@ -75,18 +78,7 @@ import lombok.val;
 //@Log4j2
 public final class Clob implements NamedWithMimeType {
 
-    /**
-     * Computed for state:
-     *
-     * <p>
-     * <pre>
-     * private final MimeType mimeType;
-     * private final CharSequence chars;
-     * private final String name;
-     * </pre>
-     * </p>
-     */
-    private static final long serialVersionUID = 8694189924062378527L;
+    private static final long serialVersionUID = SerializationProxy.serialVersionUID;
 
     private final String name;
     private final MimeType mimeType;
@@ -331,5 +323,35 @@ public final class Clob implements NamedWithMimeType {
 
     }
 
+    // -- SERIALIZATION PROXY
+
+    private Object writeReplace() {
+        return new SerializationProxy(this);
+    }
+
+    private void readObject(final ObjectInputStream stream) throws InvalidObjectException {
+        throw new InvalidObjectException("Proxy required");
+    }
+
+    private static class SerializationProxy implements Serializable {
+        /**
+         * Generated, based on String, String, CharSequence
+         */
+        private static final long serialVersionUID = -3598440606899920566L;
+        private final String name;
+        private final String mimeTypeBase;
+        private final CharSequence chars;
+
+        private SerializationProxy(final Clob clob) {
+            this.name = clob.getName();
+            this.mimeTypeBase = clob.getMimeType().getBaseType();
+            this.chars = clob.getChars();
+        }
+
+        private Object readResolve() {
+            return new Clob(name, mimeTypeBase, chars);
+        }
+
+    }
 
 }