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 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    protected final EventTopicsMetaData eventTopicsMetaData;
035    private final boolean enabled;
036
037    /**
038     * Create a new AuditEventHandler instance.
039     *
040     * @param name
041     *          The name of this AuditEventHandler.
042     * @param eventTopicsMetaData
043     *          Provides meta-data describing the audit event topics this AuditEventHandler may have to handle.
044     * @param acceptedTopics
045     *          Audit event topics the AuditEventHandler will handle.
046     * @param enabled
047     *          Whether or not the audit event handler is enabled.
048     *
049     */
050    protected AuditEventHandlerBase(
051            final String name,
052            final EventTopicsMetaData eventTopicsMetaData,
053            final Set<String> acceptedTopics,
054            final boolean enabled) {
055        this.name = name;
056        this.eventTopicsMetaData = eventTopicsMetaData.filter(acceptedTopics);
057        this.enabled = enabled;
058    }
059
060    @Override
061    public String getName() {
062        return name;
063    }
064
065    @Override
066    public Set<String> getHandledTopics() {
067        return eventTopicsMetaData.getTopics();
068    }
069
070    @Override
071    public boolean isEnabled() {
072        return enabled;
073    }
074
075    @Override
076    public Promise<ActionResponse, ResourceException> handleAction(Context context, String topic, ActionRequest request) {
077        return new BadRequestException(String.format("Unable to handle action: %s", request.getAction())).asPromise();
078    }
079
080}