8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2006 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: Conditions.java,v 1.4 2008/06/25 05:43:43 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Portions Copyrighted [2011] [ForgeRock AS]
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.sun.identity.policy;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.w3c.dom.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.policy.interfaces.Condition;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.iplanet.sso.*;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.xml.XMLUtils;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport com.sun.identity.shared.debug.Debug;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The class <code>Conditions</code> provides methods to maintain
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * a collection of <code>Condition</code> objects that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * apply to a <code>Policy</code>. This class provides methods to add, replace
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * and remove <code>Condition</code> objects from this condition collection.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The <code>Policy</code> object provides methods to set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>Conditions</code>, which identifies conditions that apply
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to the policy
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class Conditions {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final Debug DEBUG
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = Debug.getInstance(PolicyManager.POLICY_DEBUG_NAME);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String description;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map conditions = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * No argument constructor
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Conditions() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructor used by <code>Policy</code> to obtain
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * an instance of <code>Conditions</code> from W3C DOM
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * representation of the object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param ctm <code>ConditionTypeManager</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * providing methods to handle the <code>Conditions</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param conditionsNode node that represents the Conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected Conditions(ConditionTypeManager ctm, Node conditionsNode)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws InvalidFormatException, InvalidNameException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NameNotFoundException, PolicyException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if the node name is PolicyManager.POLICY_CONDITIONS_NODE
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!conditionsNode.getNodeName().equalsIgnoreCase(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.POLICY_CONDITIONS_NODE)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.warning(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid conditions xml blob given to construct conditions");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new InvalidFormatException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_xml_conditions_root_node", null, "",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException.CONDITION_COLLECTION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the conditions name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((name = XMLUtils.getNodeAttributeValue(conditionsNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.NAME_ATTRIBUTE)) == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster name = "Conditions:" + ServiceTypeManager.generateRandomName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the description
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((description = XMLUtils.getNodeAttributeValue(conditionsNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.DESCRIPTION_ATTRIBUTE)) == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster description = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get individual conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator conditionNodes = XMLUtils.getChildNodes(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionsNode, PolicyManager.CONDITION_POLICY).iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (conditionNodes.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node conditionNode = (Node) conditionNodes.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionType = XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionNode, PolicyManager.TYPE_ATTRIBUTE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (conditionType == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (PolicyManager.debug.warningEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.debug.warning("condition type is null");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new InvalidFormatException(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "invalid_xml_conditions_root_node", null, "",
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyException.CONDITION_COLLECTION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Construct the condition object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Condition condition = ctm.getCondition(conditionType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get and set the properties
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map properties = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster NodeList attrValuePairNodes = conditionNode.getChildNodes();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int numAttrValuePairNodes = attrValuePairNodes.getLength();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int j = 0; j < numAttrValuePairNodes; j++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node attrValuePairNode = attrValuePairNodes.item(j);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Node attributeNode
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = XMLUtils.getChildNode(attrValuePairNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.ATTR_NODE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( attributeNode != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String name = XMLUtils.getNodeAttributeValue(attributeNode,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyManager.NAME_ATTRIBUTE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set values = XMLUtils.getAttributeValuePair(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attrValuePairNode);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( ( name != null ) && ( values != null ) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster properties.put(name, values);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster condition.setProperties(properties);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the friendly name given to condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionName = XMLUtils.getNodeAttributeValue(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionNode, PolicyManager.NAME_ATTRIBUTE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Add the condition to conditions collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addCondition(conditionName, condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Constructor to obtain an instance of <code>Conditions</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to hold collection of conditions represented as
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>Condition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name name for the collection of <code>Condition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param description user friendly description for
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the collection of <code>Condition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Conditions(String name, String description) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.name = (name == null) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ("Conditions:" + ServiceTypeManager.generateRandomName()) : name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.description = (description == null) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "" : description;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the name for the collection of conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * represented as <code>Condition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return name of the collection of conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getName() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (name);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the description for the collection of conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * represented as <code>Condition</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return description for the collection of conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getDescription() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (description);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sets the name for this instance of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>Conditions<code> which contains a collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of conditions respresented as <code>Condition</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param name for the collection of conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setName(String name) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.name = (name == null) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ("Conditions:" + ServiceTypeManager.generateRandomName()) : name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Sets the description for this instance of the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>Conditions</code> which contains a collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of conditions respresented as <code>Condition</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param description description for the collection conditions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void setDescription(String description) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.description = (description == null) ?
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "" : description;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the names of <code>Condition</code> objects
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * contained in this object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return names of <code>Condition</code> contained in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Set getConditionNames() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (conditions.keySet());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the <code>Condition</code> object associated
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the given condition name.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param conditionName name of the condition object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return condition object corresponding to condition name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameNotFoundException if a condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the given name is not present
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Condition getCondition(String conditionName)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws NameNotFoundException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Condition answer = (Condition) conditions.get(conditionName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (answer == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] objs = { conditionName };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new NameNotFoundException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "name_not_present", objs,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionName, PolicyException.CONDITION_COLLECTION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds a <code>Condition</code> object to the this instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of condition collection. Since the name is not provided it
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be dynamically assigned such that it is unique within
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * this instance of the condition collection. However if a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * condition entry with the same name already exists in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * condition collection <code>NameAlreadyExistsException</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be thrown.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param condition instance of the condition object added to this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameAlreadyExistsException throw if a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * condition object is present with the same name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void addCondition(Condition condition)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws NameAlreadyExistsException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster addCondition(null, condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Adds a <code>Condition</code> object to the this instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of conditions collection. If another condition with the same
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name already exists in the conditions collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>NameAlreadyExistsException</code> will be thrown.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param conditionName name for the condition instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param condition instance of the condition object added to this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameAlreadyExistsException if a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * condition object is present with the same name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void addCondition(String conditionName, Condition condition)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws NameAlreadyExistsException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (conditionName == null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionName = "Condition:" +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceTypeManager.generateRandomName();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (conditions.containsKey(conditionName)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] objs = { conditionName };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new NameAlreadyExistsException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "name_already_present", objs,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionName, PolicyException.CONDITION_COLLECTION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditions.put(conditionName, condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Replaces an existing condition object having the same name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the new one. If a <code>Condition</code> with the given
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name does not exist, <code>NameNotFoundException</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be thrown.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param conditionName name for the condition instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param condition instance of the condition object that will
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * replace another condition object having the given name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @exception NameNotFoundException if a condition instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the given name is not present
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void replaceCondition(String conditionName, Condition condition)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws NameNotFoundException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!conditions.containsKey(conditionName)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] objs = { conditionName };
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new NameNotFoundException(ResBundleUtils.rbName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster "name_not_present", objs,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditionName, PolicyException.CONDITION_COLLECTION));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster conditions.put(conditionName, condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes the <code>Condition</code> object identified by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the condition name. If a condition instance with the given
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * name does not exist, the method will return silently.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param conditionName name of the condition instance that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be removed from the conditions collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the condition that was just removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Condition removeCondition(String conditionName) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (Condition)conditions.remove(conditionName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Removes the <code>Condition</code> object identified by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * object's <code>equals</code> method. If a condition instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * does not exist, the method will return silently.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param condition condition object that
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be removed from the conditions collection
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the condition that was just removed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Condition removeCondition(Condition condition) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionName = getConditionName(condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (conditionName != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (Condition) removeCondition(conditionName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the name associated with the given condition object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * It uses the <code>equals</code> method on the condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * to determine equality. If a condition instance that matches
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the given condition object is not present, the method
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * returns <code>null</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param condition condition object for which this method will
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * return its associated name
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return user friendly name given to the condition object;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>null</code> if not present
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String getConditionName(Condition condition) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String answer = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = conditions.keySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionName = (String) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (condition.equals(conditions.get(conditionName))) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = conditionName;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster break;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Checks if two <code>Conditions</code> are identical.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Two conditions (or conditions collections) are identical only
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * if both have the same set of <code>Condition</code> objects.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param o object againt which this conditions object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * will be checked for equality
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true</code> if all the conditions match;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * <code>false</code> otherwise
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean equals(Object o) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (o instanceof Conditions) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Conditions s = (Conditions) o;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator iter = conditions.entrySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (iter.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object ss = ((Map.Entry) iter.next()).getValue();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!s.conditions.containsValue(ss)) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (true);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (false);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns a new copy of this object with the identical
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * set of conditions collections (conditions).
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return a copy of this object with identical values
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public Object clone() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Conditions answer = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster try {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = (Conditions) super.clone();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } catch (CloneNotSupportedException se) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = new Conditions();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer.name = name;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer.description = description;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer.conditions = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = conditions.keySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object item = items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Condition condition = (Condition) conditions.get(item);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer.conditions.put(item, condition.clone());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (answer);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Checks whether the effective result of conditions is an allow or deny.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The effective result is an allow only if each condition type of this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * contraint collection evaluates to allow, for the environment parameters
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * passed in env. When there are multiple condition elements in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * conditions collection, the condition evaluation logic does a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * logical or for the condition elements of the same type and does a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * logical and between sets of condition elements of different condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * types
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token single sign on token of the user
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param env a map of key/value pairs containing any information
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * that could be used by each contraint to evaluate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the allow/deny result
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>true</code> if the effective result is an allow.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Otherwise <code>false</code>.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PolicyException if an error occured
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SSOException if the token is invalid
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public boolean isAllowed(SSOToken token, Map env)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return getConditionDecision(token, env).isAllowed();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Gets result of evalutating the conditions.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The effective result is an allow only if each condition type of this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * contraint collection evaluates to allow, for the environment parameters
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * passed in env. When there are multiple condition elements in the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * conditions collection, the condition evaluation logic does a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * logical or for the condition elements of the same type and does a
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * logical and between sets of condition elements of different condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * types
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param token single sign on token of the user
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param env a map of key/value pairs containing any information
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * that could be used by each contraint to evaluate
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the allow/deny result
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return <code>result of evaluating the conditions</code>
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws PolicyException if an error occured
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @throws SSOException if the token is invalid
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ConditionDecision getConditionDecision(SSOToken token, Map env)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throws PolicyException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean allowed = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HashMap allowMap = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster HashMap advicesMap = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster long timeToLive = Long.MAX_VALUE;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = conditions.entrySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Condition condition = (Condition)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ((Map.Entry) items.next()).getValue();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionType
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster = ConditionTypeManager.conditionTypeName(condition);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean previousAllowed = false;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Boolean previousValue = (Boolean) allowMap.get(conditionType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( previousValue != null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster previousAllowed = previousValue.booleanValue();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ConditionDecision cd = condition.getConditionDecision(token, env);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster boolean currentAllowed = cd.isAllowed();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster currentAllowed = currentAllowed || previousAllowed;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster allowMap.put(conditionType, Boolean.valueOf(currentAllowed));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map cdAdvices = cd.getAdvices();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (cdAdvices != null) && (!cdAdvices.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map advices = (Map) advicesMap.get(conditionType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( advices == null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster advices = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster advicesMap.put(conditionType, advices);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyUtils.appendMapToMap(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster cdAdvices, (Map)advicesMap.get(conditionType));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster long ttl = cd.getTimeToLive();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( ttl < timeToLive) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster timeToLive = ttl;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map effectiveAdvices = new HashMap();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( !allowMap.containsValue(Boolean.FALSE) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster allowed = true;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator conditionTypes = advicesMap.keySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( conditionTypes.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionType = (String) conditionTypes.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Boolean result = (Boolean) allowMap.get(conditionType);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( result.equals(Boolean.FALSE) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster PolicyUtils.appendMapToMap(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster (Map)advicesMap.get(conditionType),
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster effectiveAdvices);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( DEBUG.messageEnabled()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster DEBUG.message("At Conditions.getConditionDecision():allowed,"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "timeToLive, " + " advices=" + allowed +"," + timeToLive
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + "," + effectiveAdvices);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return new ConditionDecision(allowed, timeToLive, effectiveAdvices);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns XML string representation of the condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (conditions collection) object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return xml string representation of this object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public String toString() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (toXML());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns XML string representation of the condition
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (conditions collection) object.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return xml string representation of this object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster protected String toXML() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster StringBuilder sb = new StringBuilder(100);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(CONDITIONS_ELEMENT_BEGIN)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(XMLUtils.escapeSpecialCharacters(name))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(CONDITIONS_DESCRIPTION)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(XMLUtils.escapeSpecialCharacters(description))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("\">");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator items = conditions.keySet().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (items.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String conditionName = (String) items.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Condition condition = (Condition) conditions.get(conditionName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(CONDITION_ELEMENT)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(XMLUtils.escapeSpecialCharacters(conditionName))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(CONDITION_TYPE)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(XMLUtils.escapeSpecialCharacters(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ConditionTypeManager.conditionTypeName(condition)))
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("\">");
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Add attribute values pairs
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Map properties = condition.getProperties();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (properties != null) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set keySet = properties.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator keys = keySet.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( keys.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(ATTR_VALUE_PAIR_BEGIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String key = (String) keys.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(ATTR_NAME_BEGIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(quote(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster XMLUtils.escapeSpecialCharacters(key)));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(ATTR_NAME_END);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set valueSet = (Set) properties.get(key);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( (valueSet != null ) && (!valueSet.isEmpty()) ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator values = valueSet.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while ( values.hasNext() ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String value = (String) values.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(VALUE_BEGIN);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(XMLUtils.escapeSpecialCharacters(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster value));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append(VALUE_END);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(ATTR_VALUE_PAIR_END);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(CONDITION_ELEMENT_END);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\n").append(CONDITIONS_ELEMENT_END);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return (sb.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Places quotes around a string
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private String quote(String s) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String str = null;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ( s == null ) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster str = "\"\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster } else {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster str = "\"" + s + "\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return str;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Returns the number of <code>Condition</code> elements in this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * </code>Conditions</code> object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @return the number of <code>Condition</code> elements in this
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * </code>Conditions</code> object
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster int size() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster return conditions.size();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Private variables to construct the XML document
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITIONS_ELEMENT_BEGIN = "<Conditions name=\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITIONS_DESCRIPTION = "\" description=\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITIONS_ELEMENT_END = "</Conditions>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITION_ELEMENT = "<Condition name=\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITION_TYPE = "\" type=\"";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String CONDITION_ELEMENT_END = "</Condition>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String VALUE_BEGIN = "<Value>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String VALUE_END = "</Value>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String ATTR_VALUE_PAIR_BEGIN = "<AttributeValuePair>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String ATTR_NAME_BEGIN = "<Attribute name=";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String ATTR_NAME_END = "/>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static String ATTR_VALUE_PAIR_END = "</AttributeValuePair>";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}