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 org.forgerock.services.context.Context;
019import org.forgerock.json.JsonValue;
020import org.forgerock.util.Reject;
021
022/**
023 * Stores the state of the details sent to {@link AuditEventHandler#publishEvent(Context, String, JsonValue)}.
024 * The state contains the context, topic, and event content.
025 */
026public final class AuditEventTopicState {
027
028    private final Context context;
029    private final String topic;
030    private final JsonValue event;
031
032    /**
033     * Creates a (topic,event) pair.
034     *
035     * @param context
036     *         The context that triggered the audit event.
037     * @param topic
038     *         The topic.
039     * @param event
040     *         The event content.
041     */
042    public AuditEventTopicState(Context context, String topic, JsonValue event) {
043        Reject.ifNull(topic, event);
044        this.context = context;
045        this.topic = topic;
046        this.event = event;
047    }
048
049    /**
050     * Returns the topic of the event.
051     *
052     * @return the topic
053     */
054    public String getTopic() {
055        return topic;
056    }
057
058    /**
059     * Returns the event content.
060     *
061     * @return the event
062     */
063    public JsonValue getEvent() {
064        return event;
065    }
066
067    /**
068     * Returns the context that triggered the event.
069     *
070     * @return the context
071     */
072    public Context getContext() {
073        return context;
074    }
075
076    @Override
077    public int hashCode() {
078        final int prime = 31;
079        int result = 1;
080        result = prime * result + topic.hashCode();
081        result = prime * result + event.asMap().hashCode();
082        result = prime * result + context.toJsonValue().asMap().hashCode();
083        return result;
084    }
085    @Override
086    public boolean equals(Object obj) {
087        if (this == obj) {
088            return true;
089        }
090        if (obj instanceof AuditEventTopicState) {
091            AuditEventTopicState other = (AuditEventTopicState) obj;
092            if (topic.equals(other.topic)
093                    && event.asMap().equals(other.event.asMap())
094                    && context.toJsonValue().asMap().equals(other.context.toJsonValue().asMap())) {
095                return true;
096            }
097        }
098        return false;
099    }
100
101    @Override
102    public String toString() {
103        return String.format("AuditEventTopicState [topic=%s, event=%s, contextId=%s]", topic, event, context.getId());
104    }
105}