Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #769 from fcrepo4/event-suppression
Adding SuppressByMixinFilter to suppress events related to nodes with any of a given set of mixins Resolves: https://jira.duraspace.org/browse/FCREPO-1456
- Loading branch information
Showing
9 changed files
with
350 additions
and
28 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
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
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 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.exception.RepositoryRuntimeException; | ||
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 new RepositoryRuntimeException(e); | ||
} | ||
} | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
...pl/src/test/java/org/fcrepo/integration/kernel/impl/observer/SuppressByMixinFilterIT.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,138 @@ | ||
/** | ||
* 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.integration.kernel.impl.observer; | ||
|
||
import static com.jayway.awaitility.Awaitility.await; | ||
import static com.jayway.awaitility.Duration.ONE_SECOND; | ||
import static org.fcrepo.kernel.FedoraJcrTypes.FEDORA_CONTAINER; | ||
import static org.fcrepo.kernel.FedoraJcrTypes.LDP_DIRECT_CONTAINER; | ||
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.Callable; | ||
|
||
import javax.inject.Inject; | ||
import javax.jcr.Node; | ||
import javax.jcr.Repository; | ||
import javax.jcr.RepositoryException; | ||
import javax.jcr.Session; | ||
|
||
import org.fcrepo.integration.kernel.impl.AbstractIT; | ||
import org.fcrepo.kernel.observer.FedoraEvent; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import com.google.common.eventbus.Subscribe; | ||
|
||
|
||
/** | ||
* <p>SuppressByMixinFilterIT class.</p> | ||
* | ||
* @author escowles | ||
* @since 2015-04-15 | ||
*/ | ||
@ContextConfiguration({"/spring-test/eventing-suppress.xml", "/spring-test/repo.xml"}) | ||
public class SuppressByMixinFilterIT extends AbstractIT { | ||
|
||
/** | ||
* Time to wait for a set of test messages, in milliseconds. | ||
*/ | ||
private static final long TIMEOUT = 20000; | ||
|
||
@Inject | ||
private Repository repository; | ||
|
||
@Inject | ||
private EventBus eventBus; | ||
|
||
private Set<String> eventsReceived = new HashSet<>(); | ||
|
||
@Test(timeout = TIMEOUT) | ||
public void shouldSuppressWithMixin() throws RepositoryException { | ||
|
||
final Session se = repository.login(); | ||
try { | ||
// add node with suppressed mixin ldp:DirectContainer | ||
final Node node = se.getRootNode().addNode("/object1"); | ||
node.addMixin(FEDORA_CONTAINER); | ||
node.addMixin(LDP_DIRECT_CONTAINER); | ||
|
||
// add second node without suppressed mixin | ||
se.getRootNode().addNode("/object2").addMixin(FEDORA_CONTAINER); | ||
se.save(); | ||
|
||
// should only see second node | ||
waitForEvent("/object2"); | ||
assertFalse("Event not suppressed!", eventsReceived.contains("/object1")); | ||
} catch (final RepositoryException e) { | ||
se.logout(); | ||
} | ||
} | ||
|
||
@Test(timeout = TIMEOUT) | ||
public void shouldAllowWithoutMixin() throws RepositoryException { | ||
|
||
final Session se = repository.login(); | ||
try { | ||
se.getRootNode().addNode("/object3").addMixin(FEDORA_CONTAINER); | ||
se.save(); | ||
|
||
// should see one message | ||
waitForEvent("/object3"); | ||
} catch (final RepositoryException e) { | ||
se.logout(); | ||
} | ||
} | ||
|
||
@Before | ||
public void acquireConnections() { | ||
eventBus.register(this); | ||
} | ||
|
||
@After | ||
public void releaseConnections() { | ||
eventBus.unregister(this); | ||
} | ||
|
||
@Subscribe | ||
public void receiveEvents(final FedoraEvent e) throws RepositoryException { | ||
final Set<String> properties = e.getProperties(); | ||
assertNotNull(properties); | ||
|
||
final String expected = REPOSITORY_NAMESPACE + "mixinTypes"; | ||
assertTrue("Should contain: " + expected + properties, properties.contains(expected)); | ||
|
||
eventsReceived.add(e.getPath()); | ||
} | ||
|
||
private void waitForEvent(final String id) { | ||
await().pollInterval(ONE_SECOND).until(new Callable<Boolean>() { | ||
|
||
@Override | ||
public Boolean call() { | ||
return eventsReceived.contains(id); | ||
} | ||
}); | ||
} | ||
|
||
} |
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
Oops, something went wrong.