001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015-2016 ForgeRock AS.
015 */
016package org.forgerock.audit.events.handlers;
017
018import java.util.Set;
019
020import org.forgerock.audit.events.EventTopicsMetaData;
021import org.forgerock.json.resource.ActionRequest;
022import org.forgerock.json.resource.ActionResponse;
023import org.forgerock.json.resource.BadRequestException;
024import org.forgerock.json.resource.ResourceException;
025import org.forgerock.services.context.Context;
026import org.forgerock.util.promise.Promise;
027
028/**
029 * Abstract AuditEventHandler class.
030 */
031public abstract class AuditEventHandlerBase implements AuditEventHandler {
032
033    private final String name;
034    /** The event topic meta data for the handler. */
035    protected final EventTopicsMetaData eventTopicsMetaData;
036    private final boolean enabled;
037
038    /**
039     * Create a new AuditEventHandler instance.
040     *
041     * @param name
042     *          The name of this AuditEventHandler.
043     * @param eventTopicsMetaData
044     *          Provides meta-data describing the audit event topics this AuditEventHandler may have to handle.
045     * @param acceptedTopics
046     *          Audit event topics the AuditEventHandler will handle.
047     * @param enabled
048     *          Whether or not the audit event handler is enabled.
049     *
050     */
051    protected AuditEventHandlerBase(
052            final String name,
053            final EventTopicsMetaData eventTopicsMetaData,
054            final Set<String> acceptedTopics,
055            final boolean enabled) {
056        this.name = name;
057        this.eventTopicsMetaData = eventTopicsMetaData.filter(acceptedTopics);
058        this.enabled = enabled;
059    }
060
061    @Override
062    public String getName() {
063        return name;
064    }
065
066    @Override
067    public Set<String> getHandledTopics() {
068        return eventTopicsMetaData.getTopics();
069    }
070
071    @Override
072    public boolean isEnabled() {
073        return enabled;
074    }
075
076    @Override
077    public Promise<ActionResponse, ResourceException> handleAction(Context context, String topic,
078            ActionRequest request) {
079        return new BadRequestException(String.format("Unable to handle action: %s", request.getAction())).asPromise();
080    }
081
082}