Skip to content

Commit

Permalink
Adding SuppressByMixinFilter to suppress events related to nodes with…
Browse files Browse the repository at this point in the history
… any of a given set of mixins
  • Loading branch information
escowles committed Apr 15, 2015
1 parent ceacc98 commit 7852d61
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 7 deletions.
Expand Up @@ -38,7 +38,6 @@
import org.slf4j.Logger;

import java.util.Collection;
import java.util.List;

/**
* {@link EventFilter} that passes only events emitted from nodes with a Fedora
Expand Down Expand Up @@ -79,10 +78,7 @@ public Predicate<Event> getFilter(final Session session) {
@Override
public boolean apply(final Event event) {
try {
final org.modeshape.jcr.api.observation.Event modeEvent = getJcr21Event(event);

final List<NodeType> nodeTypes = ImmutableList.copyOf(modeEvent.getMixinNodeTypes());
final Collection<String> mixinTypes = ImmutableSet.copyOf(transform(nodeTypes, nodetype2string));
final Collection<String> mixinTypes = getMixinTypes(event);
return mixinTypes.contains(FEDORA_RESOURCE)
|| mixinTypes.contains(FEDORA_BINARY)
|| mixinTypes.contains(FEDORA_NON_RDF_SOURCE_DESCRIPTION)
Expand All @@ -95,9 +91,13 @@ public boolean apply(final Event event) {
}
}

private static org.modeshape.jcr.api.observation.Event getJcr21Event(final Event event) {
protected static Collection<String> getMixinTypes(final Event event)
throws PathNotFoundException, RepositoryException {
try {
return (org.modeshape.jcr.api.observation.Event) event;
final org.modeshape.jcr.api.observation.Event modeEvent =
(org.modeshape.jcr.api.observation.Event) event;
return ImmutableSet.copyOf(transform(ImmutableList.copyOf(modeEvent.getMixinNodeTypes()),
nodetype2string));
} catch (final ClassCastException e) {
throw new ClassCastException(event + " is not a Modeshape Event");
}
Expand Down
@@ -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);
}
}

}
@@ -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));
}
}

0 comments on commit 7852d61

Please sign in to comment.