8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: AndCondition.java,v 1.3 2010/01/12 21:29:58 veiming Exp $
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest *
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest * Portions Copyrighted 2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.entitlement;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrestimport org.forgerock.openam.utils.CollectionUtils;
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrestimport javax.security.auth.Subject;
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrestimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EntitlementCondition</code> wrapper on a set of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EntitlementCondition</code>(s) to provide
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * boolean OR logic Membership is of <code>AndCondition</code> is satisfied
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if the user is a member of any of the wrapped
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EntitlementCondition</code>.
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class AndCondition extends LogicalCondition {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs <code>AndCondition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public AndCondition() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructs AndCondition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param eConditions wrapped <code>EntitlementCondition</code>(s)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public AndCondition(Set<EntitlementCondition> eConditions) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super(eConditions);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns <code>ConditionDecision</code> of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EntitlementCondition</code> evaluation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param realm Realm name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param subject EntitlementCondition who is under evaluation.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param resourceName Resource name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param environment Environment parameters.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>ConditionDecision</code> of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>EntitlementCondition</code> evaluation
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws EntitlementException if error occurs.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest public ConditionDecision evaluate(String realm, Subject subject, String resourceName,
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest Map<String, Set<String>> environment) throws EntitlementException {
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest Set<EntitlementCondition> conditions = getEConditions();
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest if (CollectionUtils.isEmpty(conditions)) {
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest return ConditionDecision
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .newSuccessBuilder()
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .build();
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest }
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest Map<String, Set<String>> advice = new HashMap<>();
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest Map<String, Set<String>> responseAttributes = new HashMap<>();
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest long ttl = Long.MAX_VALUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest for (EntitlementCondition condition : conditions) {
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest ConditionDecision decision = condition.evaluate(realm, subject, resourceName, environment);
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest advice.putAll(decision.getAdvice());
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest responseAttributes.putAll(decision.getResponseAttributes());
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest if (decision.getTimeToLive() < ttl) {
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest ttl = decision.getTimeToLive();
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest }
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest if (!decision.isSatisfied()) {
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest return ConditionDecision
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .newFailureBuilder()
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest .setAdvice(advice)
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest .setResponseAttributes(responseAttributes)
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .build();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest return ConditionDecision
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .newSuccessBuilder()
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .setResponseAttributes(responseAttributes)
f52ca8f9ce43f7765e8c5959e45a667b496526eeAndrew Forrest .setTimeToLive(ttl)
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest .build();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
96a86856ac0b95878e74e5ed8d417572493324ceAndrew Forrest
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}