Skip to content

Commit f96918b

Browse files
committed
Extract ObjectStreamClassPredicate for reuse outside an input stream,
1 parent 7245fbd commit f96918b

File tree

3 files changed

+185
-182
lines changed

3 files changed

+185
-182
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package org.apache.commons.io.serialization;
2+
3+
import java.io.ObjectStreamClass;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.function.Predicate;
7+
import java.util.regex.Pattern;
8+
import java.util.stream.Stream;
9+
10+
/**
11+
* A predicate (boolean-valued function) of one argument to accept and reject classes.
12+
* <p>
13+
* The reject list takes precedence over the accept list.
14+
* </p>
15+
*
16+
* @since 2.18.0
17+
*/
18+
public class ObjectStreamClassPredicate implements Predicate<ObjectStreamClass> {
19+
20+
// This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
21+
private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
22+
23+
// This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
24+
private final List<ClassNameMatcher> rejectMatchers = new ArrayList<>();
25+
26+
/**
27+
* Constructs a new instance.
28+
*/
29+
public ObjectStreamClassPredicate() {
30+
// empty
31+
}
32+
33+
/**
34+
* Accepts the specified classes for deserialization, unless they are otherwise rejected.
35+
* <p>
36+
* The reject list takes precedence over the accept list.
37+
* </p>
38+
*
39+
* @param classes Classes to accept
40+
* @return this object
41+
*/
42+
public ObjectStreamClassPredicate accept(final Class<?>... classes) {
43+
Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(acceptMatchers::add);
44+
return this;
45+
}
46+
47+
/**
48+
* Accepts class names where the supplied ClassNameMatcher matches for deserialization, unless they are otherwise rejected.
49+
* <p>
50+
* The reject list takes precedence over the accept list.
51+
* </p>
52+
*
53+
* @param matcher a class name matcher to <em>accept</em> objects.
54+
* @return this instance.
55+
*/
56+
public ObjectStreamClassPredicate accept(final ClassNameMatcher matcher) {
57+
acceptMatchers.add(matcher);
58+
return this;
59+
}
60+
61+
/**
62+
* Accepts class names that match the supplied pattern for deserialization, unless they are otherwise rejected.
63+
* <p>
64+
* The reject list takes precedence over the accept list.
65+
* </p>
66+
*
67+
* @param pattern a Pattern for compiled regular expression.
68+
* @return this instance.
69+
*/
70+
public ObjectStreamClassPredicate accept(final Pattern pattern) {
71+
acceptMatchers.add(new RegexpClassNameMatcher(pattern));
72+
return this;
73+
}
74+
75+
/**
76+
* Accepts the wildcard specified classes for deserialization, unless they are otherwise rejected.
77+
* <p>
78+
* The reject list takes precedence over the accept list.
79+
* </p>
80+
*
81+
* @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
82+
* FilenameUtils.wildcardMatch}
83+
* @return this instance.
84+
*/
85+
public ObjectStreamClassPredicate accept(final String... patterns) {
86+
Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(acceptMatchers::add);
87+
return this;
88+
}
89+
90+
/**
91+
* Rejects the specified classes for deserialization, even if they are otherwise accepted.
92+
* <p>
93+
* The reject list takes precedence over the accept list.
94+
* </p>
95+
*
96+
* @param classes Classes to reject
97+
* @return this instance.
98+
*/
99+
public ObjectStreamClassPredicate reject(final Class<?>... classes) {
100+
Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(rejectMatchers::add);
101+
return this;
102+
}
103+
104+
/**
105+
* Rejects class names where the supplied ClassNameMatcher matches for deserialization, even if they are otherwise accepted.
106+
* <p>
107+
* The reject list takes precedence over the accept list.
108+
* </p>
109+
*
110+
* @param m the matcher to use
111+
* @return this instance.
112+
*/
113+
public ObjectStreamClassPredicate reject(final ClassNameMatcher m) {
114+
rejectMatchers.add(m);
115+
return this;
116+
}
117+
118+
/**
119+
* Rejects class names that match the supplied pattern for deserialization, even if they are otherwise accepted.
120+
* <p>
121+
* The reject list takes precedence over the accept list.
122+
* </p>
123+
*
124+
* @param pattern standard Java regexp
125+
* @return this instance.
126+
*/
127+
public ObjectStreamClassPredicate reject(final Pattern pattern) {
128+
rejectMatchers.add(new RegexpClassNameMatcher(pattern));
129+
return this;
130+
}
131+
132+
/**
133+
* Rejects the wildcard specified classes for deserialization, even if they are otherwise accepted.
134+
* <p>
135+
* The reject list takes precedence over the accept list.
136+
* </p>
137+
*
138+
* @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
139+
* FilenameUtils.wildcardMatch}
140+
* @return this instance.
141+
*/
142+
public ObjectStreamClassPredicate reject(final String... patterns) {
143+
Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(rejectMatchers::add);
144+
return this;
145+
}
146+
147+
/**
148+
* Tests that the ObjectStreamClass conforms to requirements.
149+
* <p>
150+
* The reject list takes precedence over the accept list.
151+
* </p>
152+
*
153+
* @param objectStreamClass The ObjectStreamClass to test.
154+
* @return true if the input is accepted, false if rejected, false if neither.
155+
*/
156+
@Override
157+
public boolean test(final ObjectStreamClass objectStreamClass) {
158+
return test(objectStreamClass.getName());
159+
}
160+
161+
/**
162+
* Tests that the class name conforms to requirements.
163+
* <p>
164+
* The reject list takes precedence over the accept list.
165+
* </p>
166+
*
167+
* @param name The class name to test.
168+
* @return true if the input is accepted, false if rejected, false if neither.
169+
*/
170+
public boolean test(final String name) {
171+
// The reject list takes precedence over the accept list.
172+
for (final ClassNameMatcher m : rejectMatchers) {
173+
if (m.matches(name)) {
174+
return false;
175+
}
176+
}
177+
for (final ClassNameMatcher m : acceptMatchers) {
178+
if (m.matches(name)) {
179+
return true;
180+
}
181+
}
182+
return false;
183+
}
184+
185+
}

src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java

Lines changed: 0 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
import java.io.InvalidClassException;
2424
import java.io.ObjectInputStream;
2525
import java.io.ObjectStreamClass;
26-
import java.util.ArrayList;
27-
import java.util.List;
28-
import java.util.function.Predicate;
2926
import java.util.regex.Pattern;
30-
import java.util.stream.Stream;
3127

3228
import org.apache.commons.io.build.AbstractStreamBuilder;
3329

@@ -247,183 +243,6 @@ public Builder setPredicate(final ObjectStreamClassPredicate predicate) {
247243

248244
}
249245

250-
/**
251-
* A predicate (boolean-valued function) of one argument to accept and reject classes.
252-
* <p>
253-
* The reject list takes precedence over the accept list.
254-
* </p>
255-
*
256-
* @since 2.18.0
257-
*/
258-
public static class ObjectStreamClassPredicate implements Predicate<ObjectStreamClass> {
259-
260-
// This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
261-
private final List<ClassNameMatcher> acceptMatchers = new ArrayList<>();
262-
263-
// This is not a Set for now to avoid ClassNameMatchers requiring proper implementations of hashCode() and equals().
264-
private final List<ClassNameMatcher> rejectMatchers = new ArrayList<>();
265-
266-
/**
267-
* Constructs a new instance.
268-
*/
269-
public ObjectStreamClassPredicate() {
270-
// empty
271-
}
272-
273-
/**
274-
* Accepts the specified classes for deserialization, unless they are otherwise rejected.
275-
* <p>
276-
* The reject list takes precedence over the accept list.
277-
* </p>
278-
*
279-
* @param classes Classes to accept
280-
* @return this object
281-
*/
282-
public ObjectStreamClassPredicate accept(final Class<?>... classes) {
283-
Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(acceptMatchers::add);
284-
return this;
285-
}
286-
287-
/**
288-
* Accepts class names where the supplied ClassNameMatcher matches for deserialization, unless they are otherwise rejected.
289-
* <p>
290-
* The reject list takes precedence over the accept list.
291-
* </p>
292-
*
293-
* @param matcher a class name matcher to <em>accept</em> objects.
294-
* @return this instance.
295-
*/
296-
public ObjectStreamClassPredicate accept(final ClassNameMatcher matcher) {
297-
acceptMatchers.add(matcher);
298-
return this;
299-
}
300-
301-
/**
302-
* Accepts class names that match the supplied pattern for deserialization, unless they are otherwise rejected.
303-
* <p>
304-
* The reject list takes precedence over the accept list.
305-
* </p>
306-
*
307-
* @param pattern a Pattern for compiled regular expression.
308-
* @return this instance.
309-
*/
310-
public ObjectStreamClassPredicate accept(final Pattern pattern) {
311-
acceptMatchers.add(new RegexpClassNameMatcher(pattern));
312-
return this;
313-
}
314-
315-
/**
316-
* Accepts the wildcard specified classes for deserialization, unless they are otherwise rejected.
317-
* <p>
318-
* The reject list takes precedence over the accept list.
319-
* </p>
320-
*
321-
* @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
322-
* FilenameUtils.wildcardMatch}
323-
* @return this instance.
324-
*/
325-
public ObjectStreamClassPredicate accept(final String... patterns) {
326-
Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(acceptMatchers::add);
327-
return this;
328-
}
329-
330-
/**
331-
* Rejects the specified classes for deserialization, even if they are otherwise accepted.
332-
* <p>
333-
* The reject list takes precedence over the accept list.
334-
* </p>
335-
*
336-
* @param classes Classes to reject
337-
* @return this instance.
338-
*/
339-
public ObjectStreamClassPredicate reject(final Class<?>... classes) {
340-
Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(rejectMatchers::add);
341-
return this;
342-
}
343-
344-
/**
345-
* Rejects class names where the supplied ClassNameMatcher matches for deserialization, even if they are otherwise accepted.
346-
* <p>
347-
* The reject list takes precedence over the accept list.
348-
* </p>
349-
*
350-
* @param m the matcher to use
351-
* @return this instance.
352-
*/
353-
public ObjectStreamClassPredicate reject(final ClassNameMatcher m) {
354-
rejectMatchers.add(m);
355-
return this;
356-
}
357-
358-
/**
359-
* Rejects class names that match the supplied pattern for deserialization, even if they are otherwise accepted.
360-
* <p>
361-
* The reject list takes precedence over the accept list.
362-
* </p>
363-
*
364-
* @param pattern standard Java regexp
365-
* @return this instance.
366-
*/
367-
public ObjectStreamClassPredicate reject(final Pattern pattern) {
368-
rejectMatchers.add(new RegexpClassNameMatcher(pattern));
369-
return this;
370-
}
371-
372-
/**
373-
* Rejects the wildcard specified classes for deserialization, even if they are otherwise accepted.
374-
* <p>
375-
* The reject list takes precedence over the accept list.
376-
* </p>
377-
*
378-
* @param patterns Wildcard file name patterns as defined by {@link org.apache.commons.io.FilenameUtils#wildcardMatch(String, String)
379-
* FilenameUtils.wildcardMatch}
380-
* @return this instance.
381-
*/
382-
public ObjectStreamClassPredicate reject(final String... patterns) {
383-
Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(rejectMatchers::add);
384-
return this;
385-
}
386-
387-
/**
388-
* Tests that the ObjectStreamClass conforms to requirements.
389-
* <p>
390-
* The reject list takes precedence over the accept list.
391-
* </p>
392-
*
393-
* @param objectStreamClass The ObjectStreamClass to test.
394-
* @return true if the input is accepted, false if rejected, false if neither.
395-
*/
396-
@Override
397-
public boolean test(final ObjectStreamClass objectStreamClass) {
398-
return test(objectStreamClass.getName());
399-
}
400-
401-
/**
402-
* Tests that the class name conforms to requirements.
403-
* <p>
404-
* The reject list takes precedence over the accept list.
405-
* </p>
406-
*
407-
* @param name The class name to test.
408-
* @return true if the input is accepted, false if rejected, false if neither.
409-
*/
410-
public boolean test(final String name) {
411-
// The reject list takes precedence over the accept list.
412-
for (final ClassNameMatcher m : rejectMatchers) {
413-
if (m.matches(name)) {
414-
return false;
415-
}
416-
}
417-
for (final ClassNameMatcher m : acceptMatchers) {
418-
if (m.matches(name)) {
419-
return true;
420-
}
421-
}
422-
return false;
423-
}
424-
425-
}
426-
427246
/**
428247
* Constructs a new {@link Builder}.
429248
*

src/test/java/org/apache/commons/io/serialization/ValidatingObjectInputStreamTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.regex.Pattern;
3535

3636
import org.apache.commons.io.serialization.ValidatingObjectInputStream.Builder;
37-
import org.apache.commons.io.serialization.ValidatingObjectInputStream.ObjectStreamClassPredicate;
3837
import org.apache.commons.lang3.SerializationUtils;
3938
import org.junit.jupiter.api.BeforeEach;
4039
import org.junit.jupiter.api.Test;

0 commit comments

Comments
 (0)