001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.kernel.modeshape.observer; 017 018import static com.google.common.collect.Sets.newHashSet; 019import static java.util.Arrays.stream; 020import static org.fcrepo.kernel.api.FedoraJcrTypes.FEDORA_BINARY; 021import static org.fcrepo.kernel.api.FedoraJcrTypes.FEDORA_NON_RDF_SOURCE_DESCRIPTION; 022import static org.fcrepo.kernel.api.FedoraJcrTypes.FEDORA_CONTAINER; 023import static org.fcrepo.kernel.api.FedoraJcrTypes.FEDORA_RESOURCE; 024import static org.slf4j.LoggerFactory.getLogger; 025 026import javax.jcr.PathNotFoundException; 027import javax.jcr.RepositoryException; 028import javax.jcr.nodetype.NodeType; 029import javax.jcr.observation.Event; 030 031import org.fcrepo.kernel.api.exception.RepositoryRuntimeException; 032import org.fcrepo.kernel.api.observer.EventFilter; 033 034import org.slf4j.Logger; 035 036import java.util.HashSet; 037import java.util.stream.Stream; 038 039/** 040 * {@link EventFilter} that passes only events emitted from nodes with a Fedora 041 * JCR type, or properties attached to them, except in the case of a node 042 * removal. In that case, since we cannot test the node for its types, we assume 043 * that any non-JCR namespaced node is fair game. 044 * 045 * @author ajs6f 046 * @author barmintor 047 * @since Dec 2013 048 * @author eddies 049 * @since Feb 7, 2013 050 * @author escowles 051 * @since Oct 3, 2013 052 */ 053public class DefaultFilter implements EventFilter { 054 055 private static final Logger LOGGER = getLogger(DefaultFilter.class); 056 057 private static final HashSet<String> fedoraMixins = 058 newHashSet(FEDORA_BINARY, FEDORA_CONTAINER, FEDORA_NON_RDF_SOURCE_DESCRIPTION, FEDORA_RESOURCE); 059 060 @Override 061 public boolean test(final Event event) { 062 try { 063 return getMixinTypes(event).anyMatch(fedoraMixins::contains); 064 } catch (final PathNotFoundException e) { 065 LOGGER.trace("Dropping event from outside our assigned workspace:\n", e); 066 return false; 067 } catch (final RepositoryException e) { 068 throw new RepositoryRuntimeException(e); 069 } 070 } 071 072 protected static Stream<String> getMixinTypes(final Event event) 073 throws PathNotFoundException, RepositoryException { 074 try { 075 final org.modeshape.jcr.api.observation.Event modeEvent = 076 (org.modeshape.jcr.api.observation.Event) event; 077 return stream(modeEvent.getMixinNodeTypes()).map(NodeType::toString); 078 } catch (final ClassCastException e) { 079 throw new ClassCastException(event + " is not a Modeshape Event"); 080 } 081 } 082 083}