[java-opensaml] branch OSJ-246 updated: Introduce factory interface to make storage format pluggable.

Scott Cantor cantor.2 at osu.edu
Thu Apr 2 20:23:34 EDT 2020


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch OSJ-246
in repository java-opensaml.

View the commit online:
http://git.shibboleth.net/view/?p=java-opensaml.git;a=commit;h=88de686f18e23f15ab906df7892bcb0bc32d445c

The following commit(s) were added to refs/heads/OSJ-246 by this push:
       new  88de686   Introduce factory interface to make storage format pluggable.
88de686 is described below

commit 88de686f18e23f15ab906df7892bcb0bc32d445c
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Thu Apr 2 20:23:28 2020 -0400

    Introduce factory interface to make storage format pluggable.
---
 .../client/AbstractClientStorageServiceStore.java  |  2 ++
 .../storage/impl/client/ClientStorageService.java  | 27 ++++++++++++++++++----
 .../impl/client/ClientStorageServiceStore.java     | 20 ++++++++++++++++
 .../impl/client/JSONClientStorageServiceStore.java | 14 ++++++++---
 4 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java
index 6ef7d8b..58f3057 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/AbstractClientStorageServiceStore.java
@@ -36,6 +36,8 @@ import net.shibboleth.utilities.java.support.logic.Constraint;
 
 /**
  * Base class for the storage and reconstitution of data for a {@link ClientStorageService}.
+ * 
+ * @since 4.1.0
  */
 public abstract class AbstractClientStorageServiceStore implements ClientStorageServiceStore {
 
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
index b6d1c89..4781dac 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageService.java
@@ -56,6 +56,8 @@ import net.shibboleth.utilities.java.support.security.DataSealerKeyStrategy;
 import org.opensaml.storage.AbstractMapBackedStorageService;
 import org.opensaml.storage.MutableStorageRecord;
 import org.opensaml.storage.StorageCapabilitiesEx;
+import org.opensaml.storage.impl.client.ClientStorageServiceStore.Factory;
+import org.opensaml.storage.impl.client.JSONClientStorageServiceStore.JSONClientStorageServiceStoreFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,7 +68,7 @@ import org.slf4j.LoggerFactory;
  * <p>The data for this service is managed in a {@link ClientStorageServiceStore} object, which must
  * be created by some operation within the container for this implementation to function. Actual
  * load/store of the data to/from that object is driven via companion classes. The serialization
- * of data via JSON is inside the storage object class, but the encryption/decryption is here.</p>
+ * of data is inside the storage object class, but the encryption/decryption is here.</p>
  */
 public class ClientStorageService extends AbstractMapBackedStorageService implements Filter, StorageCapabilitiesEx {
 
@@ -110,6 +112,9 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
 
     /** KeyStrategy enabling us to detect whether data has been sealed with an older key. */
     @Nullable private DataSealerKeyStrategy keyStrategy;
+    
+    /** Factory for backing store. */
+    @Nonnull private Factory storeFactory;
 
     /** Constructor. */
     public ClientStorageService() {
@@ -117,6 +122,7 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
         capabilityMap = new HashMap<>(2);
         capabilityMap.put(ClientStorageSource.COOKIE, 4096);
         capabilityMap.put(ClientStorageSource.HTML_LOCAL_STORAGE, 1024 * 1024);
+        storeFactory = new JSONClientStorageServiceStoreFactory();
     }
 
     /** {@inheritDoc} */
@@ -239,6 +245,17 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
         
         keyStrategy = strategy;
     }
+    
+    /**
+     * Set the backing store {@link Factory} to use. 
+     * 
+     * @param factory factory to use
+     */
+    public void setClientStorageServiceStoreFactory(@Nonnull final Factory factory) {
+        ComponentSupport.ifInitializedThrowUnmodifiabledComponentException(this);
+        
+        storeFactory = Constraint.isNotNull(factory, "Factory cannot be null");
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -425,7 +442,7 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
                 
                 log.trace("{} Data after decryption: {}", getLogPrefix(), decrypted);
                 
-                storageObject = new JSONClientStorageServiceStore(decrypted, source);
+                storageObject = storeFactory.load(decrypted, source);
                 
                 if (keyStrategy != null) {
                     try {
@@ -441,16 +458,16 @@ public class ClientStorageService extends AbstractMapBackedStorageService implem
                 log.debug("{} Successfully decrypted and loaded storage state from client", getLogPrefix());
             } catch (final DataExpiredException e) {
                 log.debug("{} Secured data or key has expired", getLogPrefix());
-                storageObject = new JSONClientStorageServiceStore(null, source);
+                storageObject = storeFactory.load(null, source);
                 storageObject.setDirty(true);
             } catch (final DataSealerException e) {
                 log.error("{} Exception unwrapping secured data", getLogPrefix(), e);
-                storageObject = new JSONClientStorageServiceStore(null, source);
+                storageObject = storeFactory.load(null, source);
                 storageObject.setDirty(true);
             }
         } else {
             log.trace("{} Initializing empty storage state into session", getLogPrefix());
-            storageObject = new JSONClientStorageServiceStore(null, source);
+            storageObject = storeFactory.load(null, source);
         }
         
         // The object should be loaded, and marked "clean", or in the event of just about any failure
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java
index 764e982..0fadd3b 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/ClientStorageServiceStore.java
@@ -28,9 +28,12 @@ import org.opensaml.storage.impl.client.ClientStorageService.ClientStorageSource
 
 import net.shibboleth.utilities.java.support.annotation.constraint.Live;
 import net.shibboleth.utilities.java.support.annotation.constraint.NonnullElements;
+import net.shibboleth.utilities.java.support.annotation.constraint.NotEmpty;
 
 /**
  * Abstraction for the storage and reconstitution of data for a {@link ClientStorageService}.
+ * 
+ * @since 4.1.0
  */
 public interface ClientStorageServiceStore {
 
@@ -73,4 +76,21 @@ public interface ClientStorageServiceStore {
      */
     @Nullable ClientStorageServiceOperation save(@Nonnull final ClientStorageService storageService) throws IOException;
     
+    /**
+     * A factory for producing new {@link ClientStorageServiceStore} instances.
+     */
+    interface Factory {
+        
+        /**
+         * Load raw data into a new {@link ClientStorageServiceStore} instance.
+         * 
+         * @param raw data to load
+         * @param src data source
+         * 
+         * @return new store instance
+         */
+        @Nonnull ClientStorageServiceStore load(@Nullable @NotEmpty final String raw,
+                @Nonnull final ClientStorageSource src);
+    }
+    
 }
\ No newline at end of file
diff --git a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java
index 60ee2c5..0388f01 100644
--- a/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java
+++ b/opensaml-storage-impl/src/main/java/org/opensaml/storage/impl/client/JSONClientStorageServiceStore.java
@@ -101,10 +101,8 @@ public class JSONClientStorageServiceStore extends AbstractClientStorageServiceS
             }
             setDirty(false);
         } catch (final NullPointerException | ClassCastException | ArithmeticException | JsonException e) {
-            getContextMap().clear();
-            // Setting this should force corrupt data in the client to be overwritten.
-            setDirty(true);
             log.error("Found invalid data structure while parsing context map", e);
+            throw new IOException(e);
         }
     }
 
@@ -182,4 +180,14 @@ public class JSONClientStorageServiceStore extends AbstractClientStorageServiceS
     }
 //Checkstyle: CyclomaticComplexity ON
     
+    /** Factory for JSON-backed store. */
+    public static class JSONClientStorageServiceStoreFactory implements Factory {
+
+        /** {@inheritDoc} */
+        @Nonnull public ClientStorageServiceStore load(@Nullable @NotEmpty final String raw,
+                @Nonnull final ClientStorageSource src) {
+            return new JSONClientStorageServiceStore(raw, src);
+        }
+    }
+
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list