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.impl.observer;
017
018import static org.slf4j.LoggerFactory.getLogger;
019
020import javax.jcr.PathNotFoundException;
021import javax.jcr.RepositoryException;
022import javax.jcr.Session;
023import javax.jcr.observation.Event;
024
025import com.google.common.base.Predicate;
026
027import org.fcrepo.kernel.exception.RepositoryRuntimeException;
028import org.fcrepo.kernel.observer.EventFilter;
029import org.slf4j.Logger;
030
031import java.util.Collections;
032import java.util.Set;
033
034/**
035 * {@link EventFilter} that extends {@link DefaultFilter} to also suppress events
036 * emitted by nodes with a provided set of mixins.
037 *
038 * @author escowles
039 * @since 2015-04-15
040 */
041public class SuppressByMixinFilter extends DefaultFilter implements EventFilter {
042
043    private static final Logger LOGGER = getLogger(SuppressByMixinFilter.class);
044    private Set<String> suppressedMixins;
045
046    /**
047     * @param suppressedMixins Resources with these mixins will be filtered out
048     */
049    public SuppressByMixinFilter(final Set<String> suppressedMixins) {
050        this.suppressedMixins = suppressedMixins;
051        for (String mixin : suppressedMixins) {
052            LOGGER.info("Suppressing events for nodes with mixin: {}", mixin);
053        }
054    }
055
056    @Override
057    public Predicate<Event> getFilter(final Session session) {
058        return this;
059    }
060
061    @Override
062    public boolean apply(final Event event) {
063        try {
064            return super.apply(event) && Collections.disjoint(getMixinTypes(event), suppressedMixins);
065        } catch (final PathNotFoundException e) {
066            LOGGER.trace("Dropping event from outside our assigned workspace:\n", e);
067            return false;
068        } catch (final RepositoryException e) {
069            throw new RepositoryRuntimeException(e);
070        }
071    }
072
073}