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 */
016
017package org.forgerock.services.context;
018
019import org.forgerock.json.JsonValue;
020import org.forgerock.util.time.TimeService;
021
022/**
023 * A context for audit information for an incoming request.
024 */
025public class RequestAuditContext extends AbstractContext {
026    private static final String NAME = "requestAudit";
027    /** The time of the request. */
028    private static final String RECEIVED_TIME = "receivedTime";
029
030    /**
031     * Constructs a new context using the specified parent and the current time as the request received time.
032     * @param parent The parent context.
033     */
034    public RequestAuditContext(Context parent) {
035        this(parent, TimeService.SYSTEM);
036    }
037
038    /**
039     * Constructs a new context using the specified parent and the current time as the request received time.
040     * @param parent The parent context.
041     * @param time The instance of {@code TimeService} to use.
042     */
043    public RequestAuditContext(Context parent, TimeService time) {
044        super(parent, NAME);
045        data.put(RECEIVED_TIME, time.now());
046    }
047
048    /**
049     * Restores a saved context.
050     * @param savedContext The saved state.
051     * @param classLoader The {@code ClassLoader} to use.
052     */
053    public RequestAuditContext(JsonValue savedContext, ClassLoader classLoader) {
054        super(savedContext, classLoader);
055    }
056
057    /**
058     * Get the time in milliseconds since the epoch that the request was received.
059     * @return The request received time.
060     */
061    public long getRequestReceivedTime() {
062        return data.get(RECEIVED_TIME).asLong();
063    }
064}