-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding SuppressByMixinFilter to suppress events related to nodes with any of a given set of mixins #769
Merged
Merged
Adding SuppressByMixinFilter to suppress events related to nodes with any of a given set of mixins #769
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7852d61
Adding SuppressByMixinFilter to suppress events related to nodes with…
escowles 89b4821
Simplifying DefaultFilter implementation
escowles 3b3df9e
Adding SuppressByMixinFilter IT
escowles c8c6bfd
Updating IT to use awaitility instead of just counting events processed
escowles 1e4974d
Using c: syntax for constructor arguments
escowles fa59462
Moving awaitility version to top-level pom.xml, clarifying spring bea…
escowles 0547b6c
Moving awaitility to top-level depenencyManagement section
escowles File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we can use awaitility here as we do for other event-driven tests in fcrepo-jms? |
||
|
||
/** | ||
* 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fcrepo-jms
also uses awaitility, so maybe we should move the versioning info up intofcrepo4
's pom?