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 org.slf4j.LoggerFactory.getLogger;
019
020import javax.jcr.PathNotFoundException;
021import javax.jcr.RepositoryException;
022import javax.jcr.observation.Event;
023
024import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
025
026import org.slf4j.Logger;
027
028import java.util.Set;
029
030/**
031 * Suppresses events emitted by nodes with a provided set of mixins.
032 *
033 * @author escowles
034 * @author ajs6f
035 * @since 2015-04-15
036 */
037public class SuppressByMixinFilter extends DefaultFilter {
038
039    private static final Logger LOGGER = getLogger(SuppressByMixinFilter.class);
040    private final Set<String> suppressedMixins;
041
042    /**
043     * @param suppressedMixins Resources with these mixins will be filtered out
044     */
045    public SuppressByMixinFilter(final Set<String> suppressedMixins) {
046        this.suppressedMixins = suppressedMixins;
047        LOGGER.info("Suppressing events for nodes with mixins: {}", suppressedMixins);
048    }
049
050    @Override
051    public boolean test(final Event event) {
052        try {
053            return super.test(event) && !getMixinTypes(event).anyMatch(suppressedMixins::contains);
054        } catch (final PathNotFoundException e) {
055            LOGGER.trace("Dropping event from outside our assigned workspace:\n", e);
056            return false;
057        } catch (final RepositoryException e) {
058            throw new RepositoryRuntimeException(e);
059        }
060    }
061
062}