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 */
017
018package org.forgerock.services.context;
019
020import java.util.HashMap;
021import java.util.Map;
022
023import org.forgerock.http.session.SessionContext;
024import org.forgerock.json.JsonValue;
025
026/**
027 * An {@code AttributesContext} is a mechanism for transferring transient state between components when processing a
028 * single request. For example, a filter may store information about the end-user in the {@code AttributeContext} which
029 * can then be accessed in subsequent filters and handlers in order to perform access control decisions, routing
030 * decisions, etc.
031 * <p>
032 * The {@code AttributesContext} has the same life-cycle as the request with which it is associated. Specifically, any
033 * attributes stored when processing one request will not be accessible when processing a subsequent request, even if it
034 * is from the same logical client.
035 * <p>
036 * Use a {@link SessionContext SessionContext} for maintaining state between successive requests
037 * from the same logical client.
038 */
039public final class AttributesContext extends AbstractContext {
040
041    /**
042     * Attributes associated with the current request. This field is not serialized in the {@link JsonValue}
043     * representation of this context.
044     */
045    private final Map<String, Object> attributes = new HashMap<>();
046
047    /**
048     * Constructs a new {@code AttributesContext}.
049     *
050     * @param parent
051     *         The parent {@code Context}.
052     */
053    public AttributesContext(Context parent) {
054        super(parent, "attributes");
055    }
056
057    /**
058     * Restore from JSON representation.
059     *
060     * @param savedContext
061     *         The JSON representation from which this context's attributes should be parsed.
062     * @param classLoader
063     *         The ClassLoader which can properly resolve the persisted class-name.
064     */
065    public AttributesContext(final JsonValue savedContext, final ClassLoader classLoader) {
066        super(savedContext, classLoader);
067    }
068
069    /**
070     * Returns the attributes associated with the current request.
071     *
072     * @return The attributes associated with the current request.
073     */
074    public Map<String, Object> getAttributes() {
075        return attributes;
076    }
077}