You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/11/03 18:50:04 UTC

(commons-io) branch master updated: Extract ObjectStreamClassPredicate for reuse outside an input stream,

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new f96918b06 Extract ObjectStreamClassPredicate for reuse outside an input stream,
f96918b06 is described below

commit f96918b064b599d8f06c92e2e7ba5a98cc6f6305
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 3 13:49:59 2024 -0500

    Extract ObjectStreamClassPredicate for reuse outside an input stream,
---
 .../serialization/ObjectStreamClassPredicate.java  | 185 +++++++++++++++++++++
 .../serialization/ValidatingObjectInputStream.java | 181 --------------------
 .../ValidatingObjectInputStreamTest.java           |   1 -
 3 files changed, 185 insertions(+), 182 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/serialization/ObjectStreamClassPredicate.java b/src/main/java/org/apache/commons/io/serialization/ObjectStreamClassPredicate.java
new file mode 100644
index 000000000..61b1aba93
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/serialization/ObjectStreamClassPredicate.java
@@ -0,0 +1,185 @@
+package org.apache.commons.io.serialization;
+
+import java.io.ObjectStreamClass;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+/**
+ * A predicate (boolean-valued function) of one argument to accept and reject classes.
+ * <p>
+ * The reject list takes precedence over the accept list.
+ * </p>
+ *
+ * @since 2.18.0
+ */
+public class ObjectStreamClassPredicate implements Predicate<ObjectStreamClass> {
+
+    // This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
+    private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
+
+    // This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
+    private final List<ClassNameMatcher> rejectMatchers = new ArrayList<>();
+
+    /**
+     * Constructs a new instance.
+     */
+    public ObjectStreamClassPredicate() {
+        // empty
+    }
+
+    /**
+     * Accepts the specified classes for deserialization, unless they are otherwise rejected.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param classes Classes to accept
+     * @return this object
+     */
+    public ObjectStreamClassPredicate accept(final Class<?>... classes) {
+        Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(acceptMatchers::add);
+        return this;
+    }
+
+    /**
+     * Accepts class names where the supplied ClassNameMatcher matches for deserialization, unless they are otherwise rejected.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param matcher a class name matcher to <em>accept</em> objects.
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate accept(final ClassNameMatcher matcher) {
+        acceptMatchers.add(matcher);
+        return this;
+    }
+
+    /**
+     * Accepts class names that match the supplied pattern for deserialization, unless they are otherwise rejected.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param pattern a Pattern for compiled regular expression.
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate accept(final Pattern pattern) {
+        acceptMatchers.add(new RegexpClassNameMatcher(pattern));
+        return this;
+    }
+
+    /**
+     * Accepts the wildcard specified classes for deserialization, unless they are otherwise rejected.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
+     *                 FilenameUtils.wildcardMatch}
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate accept(final String... patterns) {
+        Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(acceptMatchers::add);
+        return this;
+    }
+
+    /**
+     * Rejects the specified classes for deserialization, even if they are otherwise accepted.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param classes Classes to reject
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate reject(final Class<?>... classes) {
+        Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(rejectMatchers::add);
+        return this;
+    }
+
+    /**
+     * Rejects class names where the supplied ClassNameMatcher matches for deserialization, even if they are otherwise accepted.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param m the matcher to use
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate reject(final ClassNameMatcher m) {
+        rejectMatchers.add(m);
+        return this;
+    }
+
+    /**
+     * Rejects class names that match the supplied pattern for deserialization, even if they are otherwise accepted.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param pattern standard Java regexp
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate reject(final Pattern pattern) {
+        rejectMatchers.add(new RegexpClassNameMatcher(pattern));
+        return this;
+    }
+
+    /**
+     * Rejects the wildcard specified classes for deserialization, even if they are otherwise accepted.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
+     *                 FilenameUtils.wildcardMatch}
+     * @return this instance.
+     */
+    public ObjectStreamClassPredicate reject(final String... patterns) {
+        Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(rejectMatchers::add);
+        return this;
+    }
+
+    /**
+     * Tests that the ObjectStreamClass conforms to requirements.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param objectStreamClass The ObjectStreamClass to test.
+     * @return true if the input is accepted, false if rejected, false if neither.
+     */
+    @Override
+    public boolean test(final ObjectStreamClass objectStreamClass) {
+        return test(objectStreamClass.getName());
+    }
+
+    /**
+     * Tests that the class name conforms to requirements.
+     * <p>
+     * The reject list takes precedence over the accept list.
+     * </p>
+     *
+     * @param name The class name to test.
+     * @return true if the input is accepted, false if rejected, false if neither.
+     */
+    public boolean test(final String name) {
+        // The reject list takes precedence over the accept list.
+        for (final ClassNameMatcher m : rejectMatchers) {
+            if (m.matches(name)) {
+                return false;
+            }
+        }
+        for (final ClassNameMatcher m : acceptMatchers) {
+            if (m.matches(name)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java b/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java
index 6d701c91b..9c9317248 100644
--- a/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java
+++ b/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java
@@ -23,11 +23,7 @@ import java.io.InputStream;
 import java.io.InvalidClassException;
 import java.io.ObjectInputStream;
 import java.io.ObjectStreamClass;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Predicate;
 import java.util.regex.Pattern;
-import java.util.stream.Stream;
 
 import org.apache.commons.io.build.AbstractStreamBuilder;
 
@@ -247,183 +243,6 @@ public class ValidatingObjectInputStream extends ObjectInputStream {
 
     }
 
-    /**
-     * A predicate (boolean-valued function) of one argument to accept and reject classes.
-     * <p>
-     * The reject list takes precedence over the accept list.
-     * </p>
-     *
-     * @since 2.18.0
-     */
-    public static class ObjectStreamClassPredicate implements Predicate<ObjectStreamClass> {
-
-        // This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
-        private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
-
-        // This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
-        private final List<ClassNameMatcher> rejectMatchers = new ArrayList<>();
-
-        /**
-         * Constructs a new instance.
-         */
-        public ObjectStreamClassPredicate() {
-            // empty
-        }
-
-        /**
-         * Accepts the specified classes for deserialization, unless they are otherwise rejected.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param classes Classes to accept
-         * @return this object
-         */
-        public ObjectStreamClassPredicate accept(final Class<?>... classes) {
-            Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(acceptMatchers::add);
-            return this;
-        }
-
-        /**
-         * Accepts class names where the supplied ClassNameMatcher matches for deserialization, unless they are otherwise rejected.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param matcher a class name matcher to <em>accept</em> objects.
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate accept(final ClassNameMatcher matcher) {
-            acceptMatchers.add(matcher);
-            return this;
-        }
-
-        /**
-         * Accepts class names that match the supplied pattern for deserialization, unless they are otherwise rejected.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param pattern a Pattern for compiled regular expression.
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate accept(final Pattern pattern) {
-            acceptMatchers.add(new RegexpClassNameMatcher(pattern));
-            return this;
-        }
-
-        /**
-         * Accepts the wildcard specified classes for deserialization, unless they are otherwise rejected.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
-         *                 FilenameUtils.wildcardMatch}
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate accept(final String... patterns) {
-            Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(acceptMatchers::add);
-            return this;
-        }
-
-        /**
-         * Rejects the specified classes for deserialization, even if they are otherwise accepted.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param classes Classes to reject
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate reject(final Class<?>... classes) {
-            Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(rejectMatchers::add);
-            return this;
-        }
-
-        /**
-         * Rejects class names where the supplied ClassNameMatcher matches for deserialization, even if they are otherwise accepted.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param m the matcher to use
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate reject(final ClassNameMatcher m) {
-            rejectMatchers.add(m);
-            return this;
-        }
-
-        /**
-         * Rejects class names that match the supplied pattern for deserialization, even if they are otherwise accepted.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param pattern standard Java regexp
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate reject(final Pattern pattern) {
-            rejectMatchers.add(new RegexpClassNameMatcher(pattern));
-            return this;
-        }
-
-        /**
-         * Rejects the wildcard specified classes for deserialization, even if they are otherwise accepted.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
-         *                 FilenameUtils.wildcardMatch}
-         * @return this instance.
-         */
-        public ObjectStreamClassPredicate reject(final String... patterns) {
-            Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(rejectMatchers::add);
-            return this;
-        }
-
-        /**
-         * Tests that the ObjectStreamClass conforms to requirements.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param objectStreamClass The ObjectStreamClass to test.
-         * @return true if the input is accepted, false if rejected, false if neither.
-         */
-        @Override
-        public boolean test(final ObjectStreamClass objectStreamClass) {
-            return test(objectStreamClass.getName());
-        }
-
-        /**
-         * Tests that the class name conforms to requirements.
-         * <p>
-         * The reject list takes precedence over the accept list.
-         * </p>
-         *
-         * @param name The class name to test.
-         * @return true if the input is accepted, false if rejected, false if neither.
-         */
-        public boolean test(final String name) {
-            // The reject list takes precedence over the accept list.
-            for (final ClassNameMatcher m : rejectMatchers) {
-                if (m.matches(name)) {
-                    return false;
-                }
-            }
-            for (final ClassNameMatcher m : acceptMatchers) {
-                if (m.matches(name)) {
-                    return true;
-                }
-            }
-            return false;
-        }
-
-    }
-
     /**
      * Constructs a new {@link Builder}.
      *
diff --git a/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java b/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java
index 0d92908bf..36173d99e 100644
--- a/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java
@@ -34,7 +34,6 @@ import java.util.UUID;
 import java.util.regex.Pattern;
 
 import org.apache.commons.io.serialization.ValidatingObjectInputStream.Builder;
-import org.apache.commons.io.serialization.ValidatingObjectInputStream.ObjectStreamClassPredicate;
 import org.apache.commons.lang3.SerializationUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;