OrCondition.java revision 96a86856ac0b95878e74e5ed8d417572493324ce
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna/**
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * Copyright (c) 2009 Sun Microsystems Inc. All Rights Reserved
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
9113290cee1c45359807cabad3c3de47179ef03fMark Craig * The contents of this file are subject to the terms
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * of the Common Development and Distribution License
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * (the License). You may not use this file except in
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * compliance with the License.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * You can obtain a copy of the License at
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * https://opensso.dev.java.net/public/CDDLv1.0.html or
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * opensso/legal/CDDLv1.0.txt
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * See the License for the specific language governing
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * permission and limitations under the License.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * When distributing Covered Code, include this CDDL
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * Header Notice in each file and include the License file
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * at opensso/legal/CDDLv1.0.txt.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * If applicable, add the following below the CDDL Header,
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * with the fields enclosed by brackets [] replaced by
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * your own identifying information:
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * "Portions Copyrighted [year] [name of copyright owner]"
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * $Id: OrCondition.java,v 1.2 2009/09/05 00:24:04 veiming Exp $
f15ff24862dfff2586204f5b8adba4b962128589David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * Portions Copyrighted 2014-2015 ForgeRock AS.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna */
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunapackage com.sun.identity.entitlement;
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunaimport org.forgerock.openam.utils.CollectionUtils;
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunaimport java.util.HashMap;
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunaimport java.util.Map;
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunaimport java.util.Set;
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunaimport javax.security.auth.Subject;
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna/**
f15ff24862dfff2586204f5b8adba4b962128589David Luna * {@link EntitlementCondition} wrapper on a set of
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * {@link EntitlementCondition}s to provide boolean OR logic.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna *
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * Membership of {@link OrCondition} is satisfied if the user is a member of any
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna * of the wrapped {@link EntitlementCondition}.
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna */
1ef266b3b5a471d627b545166acde678b0cc74d2David Lunapublic class OrCondition extends LogicalCondition {
1ef266b3b5a471d627b545166acde678b0cc74d2David Luna
/**
* Constructor.
*/
public OrCondition() {
super();
}
/**
* Constructor for providing {@link EntitlementCondition}s.
*
* @param eConditions Wrapped {@link EntitlementCondition}(s).
*/
public OrCondition(Set<EntitlementCondition> eConditions) {
super(eConditions);
}
/**
* Evaluates this {@link ConditionDecision}'s {@link EntitlementCondition}s to determine the correct
* decision to return - if any of the {@link EntitlementCondition}s are true, the returned decision is
* satisfied and has no advices.
*
* @param realm Realm name.
* @param subject EntitlementCondition under evaluation.
* @param resourceName Resource name.
* @param environment Environment parameters.
* @return the {@link ConditionDecision} having performed the {@link EntitlementCondition}(s) evaluation.
* @throws {@link EntitlementException} in case of any error.
*/
public ConditionDecision evaluate(String realm, Subject subject, String resourceName,
Map<String, Set<String>> environment) throws EntitlementException {
final Set<EntitlementCondition> conditions = getEConditions();
if (CollectionUtils.isEmpty(conditions)) {
return ConditionDecision
.newSuccessBuilder()
.build();
}
Map<String, Set<String>> advices = new HashMap<>();
Map<String, Set<String>> responseAttributes = new HashMap<>();
for (EntitlementCondition condition : conditions) {
ConditionDecision decision = condition.evaluate(realm, subject, resourceName, environment);
advices.putAll(decision.getAdvices());
responseAttributes.putAll(decision.getResponseAttributes());
if (decision.isSatisfied()) {
return ConditionDecision
.newSuccessBuilder()
.setResponseAttributes(responseAttributes)
.build();
}
}
return ConditionDecision
.newFailureBuilder()
.setAdvices(advices)
.build();
}
}