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 2014-2015 ForgeRock AS.
015 */
016
017package org.forgerock.openig.audit;
018
019import static java.util.Collections.*;
020import static org.forgerock.util.Reject.*;
021
022import java.util.Collection;
023import java.util.LinkedHashSet;
024import java.util.Map;
025import java.util.Set;
026
027import org.forgerock.openig.el.Bindings;
028
029/**
030 * An AuditEvent represents a point in time during the processing of a Request.
031 * <p>
032 * Instances of this class are <b>not thread-safe</b>: any filter can possibly update the {@code data} while
033 * processing the audit event. Special care must be taken when dealing with {@link org.forgerock.http.protocol.Request},
034 * {@link org.forgerock.http.protocol.Response} and {@link org.forgerock.services.context.Context}.
035 * <p>
036 * The {@literal source} property helps to identify the object that emitted the notification.
037 * The {@literal timestamp} property gives a time marker to keep events organized in a
038 * sequential manner (expressed in milliseconds).
039 * The {@literal data} property gives a pointer to the captured {@code context} (never {@code null}), {@code request}
040 * (never {@code null}) and {@code response} (may be {@code null} when audit happens on the request flow). There is
041 * no way to guarantee, if the notification is processed in an asynchronous way, that the bindings content was not
042 * modified in the meantime.
043 * The {@literal tags} property helps to qualify this notification (no duplicated values).
044 */
045@Deprecated
046public final class AuditEvent {
047    private final AuditSource source;
048    private final long timestamp;
049    private final Map<String, Object> data;
050    private final Set<String> tags = new LinkedHashSet<>();
051
052    /**
053     * Builds a new AuditEvent with provided values.
054     *
055     * @param source
056     *         source of the event (never {@code null})
057     * @param timestamp
058     *         creation date of the notification (expressed in milliseconds)
059     * @param bindings
060     *         Exposed data (never {@code null})
061     * @param tags
062     *         qualifiers (never {@code null})
063     */
064    public AuditEvent(final AuditSource source,
065                      final long timestamp,
066                      final Bindings bindings,
067                      final Collection<String> tags) {
068        this.source = checkNotNull(source);
069        this.timestamp = timestamp;
070        this.data = checkNotNull(bindings).asMap();
071        this.tags.addAll(checkNotNull(tags));
072    }
073
074    /**
075     * Returns the source of the audit event (never {@code null}).
076     *
077     * @return the source of the audit event (never {@code null}).
078     */
079    public AuditSource getSource() {
080        return source;
081    }
082
083    /**
084     * Returns the timestamp of this event (expressed in milliseconds).
085     *
086     * @return the timestamp of this event (expressed in milliseconds).
087     */
088    public long getTimestamp() {
089        return timestamp;
090    }
091
092    /**
093     * Returns the captured {@code data} (never {@code null}).
094     * Notice that this is a pointer to the being processed data (live objects, not copy), so, if this event
095     * is processed asynchronously, the data content may have changed without notice.
096     *
097     * @return the captured {@code data} (never {@code null}).
098     */
099    public Map<String, Object> getData() {
100        return data;
101    }
102
103    /**
104     * Returns an immutable set of event's qualifiers (never {@code null}).
105     *
106     * @return an immutable set of event's qualifiers (never {@code null}).
107     */
108    public Set<String> getTags() {
109        return unmodifiableSet(tags);
110    }
111}