Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding SuppressByMixinFilter to suppress events related to nodes with…
… any of a given set of mixins
- Loading branch information
Showing
3 changed files
with
155 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
fcrepo-kernel-impl/src/main/java/org/fcrepo/kernel/impl/observer/SuppressByMixinFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright 2015 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.kernel.impl.observer; | ||
|
||
import static com.google.common.base.Throwables.propagate; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import javax.jcr.PathNotFoundException; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
import javax.jcr.observation.Event; | ||
|
||
import com.google.common.base.Predicate; | ||
|
||
import org.fcrepo.kernel.observer.EventFilter; | ||
import org.slf4j.Logger; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
/** | ||
* {@link EventFilter} that extends {@link DefaultFilter} to also suppress events | ||
* emitted by nodes with a provided set of mixins. | ||
* | ||
* @author escowles | ||
* @since 2015-04-15 | ||
*/ | ||
public class SuppressByMixinFilter extends DefaultFilter implements EventFilter { | ||
|
||
private static final Logger LOGGER = getLogger(SuppressByMixinFilter.class); | ||
private Set<String> suppressedMixins; | ||
|
||
/** | ||
* Default constructor. | ||
*/ | ||
public SuppressByMixinFilter(final Set<String> suppressedMixins) { | ||
this.suppressedMixins = suppressedMixins; | ||
for (String mixin : suppressedMixins) { | ||
LOGGER.info("Suppressing events for nodes with mixin: {}", mixin); | ||
} | ||
} | ||
|
||
@Override | ||
public Predicate<Event> getFilter(final Session session) { | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean apply(final Event event) { | ||
try { | ||
return super.apply(event) && Collections.disjoint(getMixinTypes(event), suppressedMixins); | ||
} catch (final PathNotFoundException e) { | ||
LOGGER.trace("Dropping event from outside our assigned workspace:\n", e); | ||
return false; | ||
} catch (final RepositoryException e) { | ||
throw propagate(e); | ||
} | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...-kernel-impl/src/test/java/org/fcrepo/kernel/impl/observer/SuppressByMixinFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2015 DuraSpace, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.fcrepo.kernel.impl.observer; | ||
|
||
import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_CONTAINER; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.jcr.Session; | ||
import javax.jcr.nodetype.NodeType; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
/** | ||
* @author escowles | ||
* @since 2015-04-15 | ||
*/ | ||
public class SuppressByMixinFilterTest { | ||
|
||
private DefaultFilter testObj; | ||
|
||
@Mock | ||
private Session mockSession; | ||
|
||
@Mock | ||
private org.modeshape.jcr.api.observation.Event mockEvent; | ||
|
||
@Mock | ||
private NodeType fedoraContainer; | ||
|
||
@Mock | ||
private NodeType internalEvent; | ||
|
||
@Before | ||
public void setUp() { | ||
initMocks(this); | ||
final Set<String> suppressedMixins = new HashSet<>(); | ||
suppressedMixins.add("audit:InternalEvent"); | ||
testObj = new SuppressByMixinFilter(suppressedMixins); | ||
when(fedoraContainer.getName()).thenReturn(FEDORA_CONTAINER); | ||
when(internalEvent.getName()).thenReturn("audit:InternalEvent"); | ||
} | ||
|
||
@Test | ||
public void shouldSuppressMixin() throws Exception { | ||
when(mockEvent.getMixinNodeTypes()).thenReturn(new NodeType[] {fedoraContainer, internalEvent}); | ||
assertFalse(testObj.getFilter(mockSession).apply(mockEvent)); | ||
} | ||
|
||
@Test | ||
public void shouldAllowOthers() throws Exception { | ||
when(mockEvent.getMixinNodeTypes()).thenReturn(new NodeType[] {fedoraContainer}); | ||
assertTrue(testObj.getFilter(mockSession).apply(mockEvent)); | ||
} | ||
} |