001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.audit;
019
020import static org.fcrepo.audit.AuditProperties.BINARY_TYPE;
021import static org.slf4j.LoggerFactory.getLogger;
022
023import java.util.Set;
024import java.util.stream.Collectors;
025
026import org.fcrepo.kernel.api.observer.EventType;
027
028import org.slf4j.Logger;
029
030/**
031 * Audit utility functions
032 * @author acoburn
033 * @since 2015-04-25
034 */
035public class AuditUtils {
036
037    private static final Logger LOGGER = getLogger(AuditUtils.class);
038
039    /**
040     * Returns the set of event type URIs for the integer event types.
041     *
042     * @param types to be converted to URIs
043     * @return set of type URIs
044     */
045    public static Set<String> getEventURIs(final Set<EventType> types) {
046        final Set<String> uris = types.stream().map(EventType::getType).collect(Collectors.toSet());
047        LOGGER.debug("Constructed event type URIs: {}", uris);
048        return uris;
049    }
050
051    /**
052     * Returns the Audit event type based on fedora event type and properties.
053     *
054     * @param eventTypes from Fedora
055     * @param resourceTypes associated with the object of the Fedora event
056     * @return Audit event
057     */
058    public static String getAuditEventType(final Set<String> eventTypes, final Set<String> resourceTypes) {
059        // mapping event type / resource types to audit event type
060        if (eventTypes.contains(AuditProperties.RESOURCE_CREATION)) {
061            if (resourceTypes != null && resourceTypes.contains(BINARY_TYPE)) {
062                return AuditProperties.CONTENT_ADD;
063            } else {
064                return AuditProperties.OBJECT_ADD;
065            }
066        } else if (eventTypes.contains(AuditProperties.RESOURCE_DELETION)) {
067            if (resourceTypes != null && resourceTypes.contains(BINARY_TYPE)) {
068                return AuditProperties.CONTENT_REM;
069            } else {
070                return AuditProperties.OBJECT_REM;
071            }
072        } else if (eventTypes.contains(AuditProperties.RESOURCE_MODIFICATION)) {
073            if (resourceTypes != null && resourceTypes.contains(BINARY_TYPE)) {
074                return AuditProperties.CONTENT_MOD;
075            } else {
076                return AuditProperties.METADATA_MOD;
077            }
078        }
079        return null;
080    }
081
082    private AuditUtils() {
083        // prevent instantiation
084    }
085}