001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: Rule.java,v 1.8 2009/11/13 23:52:20 asyhuang Exp $
026 *
027 * Portions Copyrighted 2011-2014 ForgeRock AS.
028 */
029package com.sun.identity.policy;
030
031import com.iplanet.sso.SSOException;
032import com.sun.identity.shared.xml.XMLUtils;
033import org.w3c.dom.Node;
034
035import java.util.HashSet;
036import java.util.Map;
037import java.util.Set;
038import java.util.HashMap;
039import java.util.Iterator;
040
041
042/**
043 * The class <code>Rule</code> provides interfaces to manage
044 * a rule that can be added to a policy.
045 * A rule constains the rule name, service type,
046 * a resource and a map containing action names and action values.
047 *
048 * @supported.api
049 * @deprecated since 12.0.0
050 */
051@Deprecated
052public class Rule extends Object implements Cloneable {
053
054    /** Empty resource name. */
055    public static final String EMPTY_RESOURCE_NAME = "";
056
057    // Name of the rule
058    private String ruleName;
059
060    // Service type
061    private String serviceTypeName;
062    private ServiceType serviceType;
063
064    // Resource for which the rule applies.
065    Set<String> resourceNames = new HashSet<String>();
066    private String applicationName;
067
068    // Actions allowed on the resource
069    private Map actions;
070
071    /**
072     * Contruct a <code>Rule</code>.
073     */
074    protected Rule() {
075        // do nothing
076    }
077
078    /**
079     * Constructor to create a rule object with the
080     * service name, resource name and actions. The actions
081     * provided as a <code>Map</code> must have the action
082     * name as key and a <code>Set</code> of <code>String</code>s
083     * as its value. The action names and action values must
084     * conform to the schema specified for the service.
085     * Otherwise, <code>InvalidNameException
086     * </code> is thrown. The parameters <code>ruleName</code>
087     * and <code>resourceName</code> can be <code>null</code>.
088     *
089     * @param serviceName name of the service type as defined by
090     * the service schema
091     * @param resourceName name of the resource for the service type
092     * @param actions map of action and action values for the resource
093     *
094     * @exception NameNotFoundException the service name provided does
095     * not exist
096     * @exception InvalidNameException the resource name, action name,
097     * or values is not valid
098     * @supported.api
099     */
100    public Rule(String serviceName, String resourceName, Map actions) throws
101            NameNotFoundException, InvalidNameException {
102        this(null, serviceName, resourceName, actions);
103    }
104
105    /**
106     * Constructor to create a rule object with the
107     * service name and actions. This is useful for
108     * services (and possibly action names) that do not have
109     * resource names. The actions
110     * provided as a <code>Map</code> must have the action
111     * name as it key and a <code>Set</code> of <code>String</code>s
112     * as its value. The action names and action values must
113     * conform to the schema specified for the service.
114     * Otherwise, <code>InvalidNameException
115     * </code> is thrown. The parameters <code>ruleName</code>
116     * and <code>resourceName</code> can be <code>null</code>.
117     *
118     * @param serviceName name of the service type as defined by
119     * the service schema
120     * @param actions map of action and action values for the resource
121     *
122     * @exception NameNotFoundException the service name provided does
123     * not exist
124     * @exception InvalidNameException the resource name, action name,
125     * or values is not valid
126     * @supported.api
127     */
128    public Rule(String serviceName, Map actions) throws
129            NameNotFoundException, InvalidNameException {
130        this(null, serviceName, null, actions);
131    }
132
133    /**
134     * Constructor to create a rule object with rule name,
135     * service name, resource name and actions. The actions
136     * provided as a <code>Map</code> must have the action
137     * name as it key and a <code>Set</code> of <code>String</code>s
138     * as its value. The action names and action values must
139     * conform to the service schema.
140     * Otherwise, <code>InvalidNameException
141     * </code> is thrown. The parameters <code>ruleName</code>
142     * and <code>resourceName</code> can be <code>null</code>.
143     *
144     * @param ruleName name of the rule
145     * @param serviceName name of the service type as defined by
146     *        the service schema
147     * @param resourceName name of the resource for the service type
148     * @param actions map of action and action values for the resource
149     *
150     * @exception NameNotFoundException the service name provided does
151     * not exist
152     * @exception InvalidNameException the resource name, action name,
153     * or values is not valid
154     * @supported.api
155     */
156    public Rule(String ruleName, String serviceName,
157            String resourceName, Map actions) throws
158            NameNotFoundException, InvalidNameException {
159        // Rule and resource name can be null
160        this.ruleName = (ruleName != null) ? ruleName : ("rule" + ServiceTypeManager.generateRandomName());
161        this.resourceNames = new HashSet<String>();
162        this.serviceTypeName = serviceName;
163
164        if ((resourceName == null) || (resourceName.length() == 0)) {
165            resourceNames.add(EMPTY_RESOURCE_NAME);
166        } else {
167            resourceName = resourceName.trim();
168
169            if (PolicyManager.isMigratedToEntitlementService()) {
170                resourceNames.add(resourceName);
171            } else {
172                // Check the service type name
173                checkAndSetServiceType(serviceName);
174
175                // Verify the action names
176                serviceType.validateActionValues(actions);
177                this.actions = new HashMap(actions);
178
179                try {
180                    resourceNames.add(serviceType.canonicalize(resourceName));
181                } catch (PolicyException pe) {
182                    throw new InvalidNameException(pe, resourceName, 2);
183                }
184            }
185        }
186
187        if (actions != null) {
188            this.actions = new HashMap(actions);
189        } else {
190            this.actions = new HashMap();
191        }
192    }
193
194    /**
195     * Sets application Name.
196     *
197     * @param applicationName Application name.
198     */
199    public void setApplicationName(String applicationName) {
200        this.applicationName = applicationName;
201    }
202
203    /**
204     * Returns application name.
205     *
206     * @return application name.
207     */
208    public String getApplicationName() {
209        return (applicationName == null) ? serviceTypeName : applicationName;
210    }
211
212
213    /**
214     * Constructor to create a <code>Rule</code> object from a XML Node.
215     * @param ruleNode XML node representation of <code>Rule</code>
216     * @throws InvalidFormatException on invalid xml
217     * @throws InvalidNameException thrown by called routines
218     * @throws NameNotFoundException thrown by called routines
219
220     */
221    protected Rule(Node ruleNode) throws InvalidFormatException,
222            InvalidNameException, NameNotFoundException {
223        // Make sure the node name is rule
224        if (!ruleNode.getNodeName().equalsIgnoreCase(
225                PolicyManager.POLICY_RULE_NODE)) {
226            if (PolicyManager.debug.warningEnabled()) {
227                PolicyManager.debug.warning(
228                        "invalid rule xml blob given to constructor");
229            }
230            throw (new InvalidFormatException(ResBundleUtils.rbName,
231                    "invalid_xml_rule_node", null, "", PolicyException.RULE));
232        }
233
234        // Get rule name, can be null
235        if ((ruleName = XMLUtils.getNodeAttributeValue(ruleNode,
236                PolicyManager.NAME_ATTRIBUTE)) == null) {
237            ruleName = "rule" + ServiceTypeManager.generateRandomName();
238        }
239
240        // Get the service type name, cannot be null
241        Node serviceNode = XMLUtils.getChildNode(ruleNode,
242                PolicyManager.POLICY_RULE_SERVICE_NODE);
243        if ((serviceNode == null) || ((serviceTypeName = XMLUtils.getNodeAttributeValue(serviceNode,
244                                                                        PolicyManager.NAME_ATTRIBUTE)) == null)) {
245            if (PolicyManager.debug.warningEnabled()) {
246                PolicyManager.debug.warning(
247                        "invalid service name in rule xml blob in constructor");
248            }
249            String[] objs = {((serviceTypeName == null) ? "null" : serviceTypeName)};
250            throw (new InvalidFormatException(ResBundleUtils.rbName,
251                    "invalid_xml_rule_service_name", objs,
252                    ruleName, PolicyException.RULE));
253        }
254        checkAndSetServiceType(serviceTypeName);
255
256        Node applicationNameNode = XMLUtils.getChildNode(ruleNode,
257            PolicyManager.POLICY_RULE_APPLICATION_NAME_NODE);
258        if (applicationNameNode != null) {
259            applicationName = XMLUtils.getNodeAttributeValue(
260                applicationNameNode, PolicyManager.NAME_ATTRIBUTE);
261        }
262
263        resourceNames = new HashSet<String>();
264        resourceNames.addAll(getResources(ruleNode,
265            PolicyManager.POLICY_RULE_RESOURCE_NODE,
266                PolicyManager.isMigratedToEntitlementService()));
267
268        Set<String> excludeResources = getResources(ruleNode,
269            PolicyManager.POLICY_RULE_EXCLUDED_RESOURCE_NODE,
270                PolicyManager.isMigratedToEntitlementService());
271
272        // Get the actions and action values, cannot be null
273        Set actionNodes = XMLUtils.getChildNodes(ruleNode,
274                PolicyManager.ATTR_VALUE_PAIR_NODE);
275        actions = new HashMap();
276        if (actionNodes != null) {
277            Iterator items = actionNodes.iterator();
278            while (items.hasNext()) {
279                // Get action name & values
280                String actionName = null;
281                Set actionValues = null;
282                Node node = (Node) items.next();
283                Node attrNode = XMLUtils.getChildNode(node, PolicyManager.ATTR_NODE);
284                if ((attrNode == null) || ((actionName = XMLUtils.getNodeAttributeValue(attrNode,
285                        PolicyManager.NAME_ATTRIBUTE)) == null) || ((actionValues =
286                        XMLUtils.getAttributeValuePair(node)) == null)) {
287                    String[] objs = {((actionName == null) ? "null" : actionName)};
288                    throw (new InvalidFormatException(
289                            ResBundleUtils.rbName,
290                            "invalid_xml_rule_action_name", objs,
291                            ruleName, PolicyException.RULE));
292                }
293                actions.put(actionName, actionValues);
294
295            }
296            // Validate the action values
297            //serviceType.validateActionValues(actions);
298        }
299    }
300
301    private Set<String> getResources(
302        Node ruleNode,
303        String childNodeName,
304        boolean isMigratedToEntitlementService
305    ) throws InvalidNameException {
306        Set<String> container = null;
307        Set children = XMLUtils.getChildNodes(ruleNode, childNodeName);
308
309        if ((children != null) && !children.isEmpty()) {
310            container = new HashSet<String>();
311            for (Iterator i = children.iterator(); i.hasNext();) {
312                Node resourceNode = (Node) i.next();
313                String resourceName = XMLUtils.getNodeAttributeValue(
314                    resourceNode, PolicyManager.NAME_ATTRIBUTE);
315                if (resourceName != null) {
316                    resourceName = resourceName.trim();
317                    if (!PolicyManager.isMigratedToEntitlementService()) {
318                        try {
319                            resourceName = serviceType.canonicalize(
320                                resourceName);
321                        } catch (PolicyException pe) {
322                            throw new InvalidNameException(pe, resourceName, 2);
323                        }
324                    }
325                    container.add(resourceName);
326                }
327            }
328        }
329        return container;
330    }
331
332    /**
333     * Sets the service type name of this object.
334     * @param serviceTypeName service type name for this object
335     * @exception NameNotFoundException the service type name provided does
336     * not exist
337     */
338    private void checkAndSetServiceType(String serviceTypeName)
339            throws NameNotFoundException {
340        // Check the service type name
341        ServiceTypeManager stm = null;
342        try {
343            stm = ServiceTypeManager.getServiceTypeManager();
344            serviceType = stm.getServiceType(serviceTypeName);
345        } catch (SSOException ssoe) {
346            PolicyManager.debug.error("Unable to get admin SSO token" + ssoe);
347            throw (new NameNotFoundException(ssoe,
348                    serviceTypeName, PolicyException.SERVICE));
349        } catch (NameNotFoundException e) {
350            if (!PolicyManager.isMigratedToEntitlementService()) {
351                throw e;
352            }
353        }
354    }
355
356    /**
357     * Returns the name assigned to the rule. It could be <code>null</code>
358     * if it was not constructed with a name.
359     *
360     * @return rule name
361     * @supported.api
362     */
363    public String getName() {
364        return (ruleName);
365    }
366
367    /**
368     * Sets the name for the rule. If a name has already been
369     * assigned, it will be replaced with the given name.
370     *
371     * @param ruleName rule name.
372     * @throws InvalidNameException if rule name is invalid.
373     * @supported.api
374     */
375    public void setName(String ruleName) throws InvalidNameException {
376        if (ruleName != null) {
377            this.ruleName = ruleName;
378        } else {
379            this.ruleName = "rule" + ServiceTypeManager.generateRandomName();
380        }
381    }
382
383    /**
384     * Returns the service name for which the rule has been created.
385     * The service name of the rule cannot be changed once the rule is
386     * created.
387     *
388     * @return service name
389     * @supported.api
390     */
391    public String getServiceTypeName() {
392        return (serviceTypeName);
393    }
394
395    /**
396     * Returns the resource name for which the rule has been created.
397     * If the service does not support resource names, the method
398     * will return <code>null</code>. The resource name of
399     * the rule cannot be changed once the rule is created.
400     *
401     * @return resource name
402     * @supported.api
403     */
404    public String getResourceName() {
405        return ((resourceNames == null) || resourceNames.isEmpty())
406                ? EMPTY_RESOURCE_NAME : resourceNames.iterator().next();
407    }
408
409    /**
410     * Returns the resource names for which the rule has been created.
411     * If the service does not support resource names, the method
412     * will return <code>null</code>. The resource name of
413     * the rule cannot be changed once the rule is created.
414     *
415     * @return resource name
416     * @supported.api
417     */
418    public Set<String> getResourceNames() {
419        return resourceNames;
420    }
421
422    /**
423     * Sets the resource names for which the rule has been created.
424     * If the service does not support resource names, the method
425     * will return <code>null</code>. The resource name of
426     * the rule cannot be changed once the rule is created.
427     *
428     * @param resourceNames resource name
429     * @supported.api
430     */
431    public void setResourceNames(Set<String> resourceNames) {
432        this.resourceNames = new HashSet<String>();
433        if (resourceNames != null) {
434            this.resourceNames.addAll(resourceNames);
435        }
436    }
437
438    /**
439     * Returns the action names that have been set for the rule.
440     * The action names returned could be the same as the service's
441     * action names or a subset of it.
442     *
443     * @return action names defined in this rule for the service
444     * @supported.api
445     */
446    public Set getActionNames() {
447        return (new HashSet(actions.keySet()));
448    }
449
450    /**
451     * Returns a set of action values that have been set for the
452     * specified action name.
453     *
454     * @param actionName action name for which to compute values.
455     * @return action names defined in this rule for the service
456     * @throws NameNotFoundException if actions name is not
457     *         found in the rule
458     * @supported.api
459     */
460    public Set getActionValues(String actionName)
461            throws NameNotFoundException {
462        Set<String> answer = (Set<String>) actions.get(actionName);
463        if (answer != null) {
464            Set clone = new HashSet();
465            clone.addAll(answer);
466            return clone;
467        }
468        return (answer);
469    }
470
471    /**
472     * Returns a <code>Map</code> of all action names and their
473     * corresponding action values that have been set in the rule.
474     * The "key" of the <code>Map</code> will be the action name
475     * as a string, and its "value" will be a <code>Set</code>
476     * which contains the action values as strings.
477     *
478     * @return all action names and corresponding action values
479     * @supported.api
480     */
481    public Map getActionValues() {
482        return (new HashMap(actions));
483    }
484
485    /**
486     * Sets the action names and their corresponding actions values
487     * (or permissions) for the resource or the service.
488     *
489     * @param actionValues action names and their corresponding values
490     * @throws InvalidNameException if action name is invalid.
491     * @supported.api
492     */
493    public void setActionValues(Map actionValues)
494            throws InvalidNameException {
495        serviceType.validateActionValues(actionValues);
496        actions = new HashMap(actionValues);
497    }
498
499    /**
500     * Checks if two rule objects are identical. Two rules are
501     * identical only if the service name, resource name,
502     * action name and values match.
503     *
504     * @param obj object against which this rule object
505     * will be checked for equality
506     *
507     * @return <code>true</code> if the service type, resource, actions
508     * and action values match, <code>false</code> otherwise.
509     */
510    @Override
511    public boolean equals(Object obj) {
512        boolean matched = true;
513        if (obj == null || !(obj instanceof Rule)) {
514            return false;
515        }
516        Rule other = (Rule) obj;
517
518        if (applicationName == null) {
519            if (other.applicationName != null) {
520                return false;
521            }
522        } else if (!applicationName.equals(other.applicationName)) {
523            return false;
524        }
525
526        if (resourceNames == null) {
527            if (other.resourceNames != null) {
528                return false;
529            }
530        } else {
531            if (!resourceNames.equals(other.resourceNames)) {
532                return false;
533            }
534        }
535
536        if (!actions.equals(other.actions)) {
537            return false;
538        }
539        return matched;
540    }
541
542    /**
543     * This added by when CheckStyle noticed there was an implementation of
544     * equals without an implementation of hashCode - usually a recipe for
545     * disaster.
546     *
547     * @return the hashCode for this object
548     */
549    @Override
550    public int hashCode() {
551        int result = super.hashCode();
552        if (applicationName != null) {
553            result = 37 * result + applicationName.hashCode();
554        }
555        if (resourceNames != null) {
556            result = 37 * result + resourceNames.hashCode();
557        }
558        if (actions != null) {
559            result = 37 * result + actions.hashCode();
560        }
561        return result;
562    }
563
564    /**
565     * Compares the given service and resource names with the
566     * service and resource name specified in this rule.
567     * The method returns a <code>ResourceMatch</code> object which
568     * specifies if the resources match exactly, do not match, or one
569     * of them is a subordinate resource of the other. If the
570     * service name does not match, the method returns <code>
571     * NO_MATCH</code>.
572     *
573     * @param serviceName name of the service
574     * @param resourceName name of the resource
575     *
576     * @return returns <code>ResourceMatch</code> that
577     * specifies if the service name and resource name are exact match, or
578     * otherwise.
579     */
580    public ResourceMatch isResourceMatch(
581            String serviceName,
582            String resourceName) {
583
584        ResourceMatch rm = null;
585        if (!serviceName.equalsIgnoreCase(serviceTypeName)) {
586            rm = ResourceMatch.NO_MATCH;
587        } else {
588            //rm = serviceType.compare(this.resourceName, resourceName);
589            String res = getResourceNames().iterator().next();
590            rm = serviceType.compare(resourceName, res);
591        }
592
593        return rm;
594    }
595
596    /**
597     * Returns an XML string representing the rule.
598     *
599     * @return an XML string representing the rule.
600     * @supported.api
601     */
602    public String toXML() {
603        StringBuilder answer = new StringBuilder(100);
604        answer.append("\n").append("<Rule");
605        if (ruleName != null) {
606            answer.append(" name=\"");
607            answer.append(XMLUtils.escapeSpecialCharacters(ruleName));
608            answer.append("\">");
609        } else {
610            answer.append(">");
611        }
612
613        answer.append("\n").append("<ServiceName name=\"");
614        answer.append(XMLUtils.escapeSpecialCharacters(serviceTypeName));
615        answer.append("\" />");
616
617        if (applicationName != null) {
618            answer.append("\n").append("<")
619                .append(PolicyManager.POLICY_RULE_APPLICATION_NAME_NODE)
620                .append(" name=\"")
621                .append(XMLUtils.escapeSpecialCharacters(applicationName))
622                .append("\" />");
623        }
624
625        if (resourceNames != null) {
626            for (String resourceName : resourceNames) {
627                answer.append("\n").append("<ResourceName name=\"");
628                answer.append(
629                    XMLUtils.escapeSpecialCharacters(resourceName));
630                answer.append("\" />");
631            }
632        }
633
634        Set actionNames = new HashSet();
635        actionNames.addAll(actions.keySet());
636
637        Iterator actionNamesIter = actionNames.iterator();
638        while (actionNamesIter.hasNext()) {
639            String actionName = (String) actionNamesIter.next();
640            answer.append("\n").append("<AttributeValuePair>");
641            answer.append("\n").append("<Attribute name=\"");
642            answer.append(XMLUtils.escapeSpecialCharacters(actionName));
643            answer.append("\" />");
644            Set values = (Set) actions.get(actionName);
645
646            if (values.size() > 0) {
647                Iterator items = values.iterator();
648                while (items.hasNext()) {
649                    answer.append("\n").append("<Value>");
650                    answer.append(
651                            XMLUtils.escapeSpecialCharacters(
652                            (String) items.next()));
653                    answer.append("</Value>");
654                }
655
656            }
657            answer.append("\n").append("</AttributeValuePair>");
658        }
659
660        answer.append("\n").append("</Rule>");
661        return (answer.toString());
662    }
663
664    /**
665     * Returns service type of this rules.
666     * @return service type of this rule
667     */
668    protected ServiceType getServiceType() {
669        return (serviceType);
670    }
671
672    /**
673     * Returns an XML respresentation of the rule with policy name to
674     * use in resource index tree.
675     * @param policyName policy name to use while creating xml representation
676     * @return an XML respresentation of the rule with policy name to
677     * use in resource index tree
678     */
679    protected String toResourcesXml(String policyName) {
680        StringBuffer beginning = new StringBuffer(100);
681        // "<PolicyCrossReferences name=\"" + serviceTypeName +
682        // "\" type=\"Resources\">"
683        beginning.append("<")
684                .append(PolicyManager.POLICY_INDEX_ROOT_NODE)
685                .append(" ")
686                .append(PolicyManager.POLICY_INDEX_ROOT_NODE_NAME_ATTR)
687                .append("=\"")
688                .append(serviceTypeName)
689                .append("\" ")
690                .append(PolicyManager.POLICY_INDEX_ROOT_NODE_TYPE_ATTR)
691                .append("=\"")
692                .append(PolicyManager.POLICY_INDEX_ROOT_NODE_TYPE_ATTR_RESOURCES_VALUE)
693                .append("\">");
694
695        String normalizedResName = null;
696        if ((resourceNames == null) || resourceNames.isEmpty()) {
697            normalizedResName = ResourceManager.EMPTY_RESOURCE_NAME;
698        } else {
699            normalizedResName = resourceNames.iterator().next();
700        }
701
702        String[] resources = serviceType.split(normalizedResName);
703        int n = resources.length;
704
705        StringBuilder middle = new StringBuilder(100);
706        // "<Reference name=\"" + resources[n-1]) +
707        // "\"><PolicyName name=\"" + policyName +
708        // "\"/></Reference>"
709        middle.append("<")
710                .append(PolicyManager.POLICY_INDEX_REFERENCE_NODE)
711                .append(" ")
712                .append(PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR)
713                .append("=\"")
714                .append(resources[n - 1])
715                .append("\"><")
716                .append(PolicyManager.POLICY_INDEX_POLICYNAME_NODE)
717                .append(" ")
718                .append(PolicyManager.POLICY_INDEX_POLICYNAME_NODE_NAME_ATTR)
719                .append("=\"")
720                .append(policyName)
721                .append("\"/></")
722                .append(PolicyManager.POLICY_INDEX_REFERENCE_NODE)
723                .append(">");
724        String tmp = middle.toString();
725        for (int i = n - 2; i >= 0; i--) {
726            //tmp = "<Reference name=\"" + resources[i] +"\">" +
727            //    tmp + "</Reference>";
728            tmp = "<"
729                    + PolicyManager.POLICY_INDEX_REFERENCE_NODE
730                    + " "
731                    + PolicyManager.POLICY_INDEX_REFERENCE_NODE_NAME_ATTR
732                    + "=\""
733                    + resources[i]
734                    + "\">"
735                    + tmp
736                    + "</"
737                    + PolicyManager.POLICY_INDEX_REFERENCE_NODE
738                    + ">";
739        }
740
741        return (beginning
742                    + tmp
743                    + "</"
744                    + PolicyManager.POLICY_INDEX_ROOT_NODE
745                    + ">");
746    }
747
748    /**
749     * Returns xml string representation of the rule.
750     *
751     * @return xml string representation of the rule
752     */
753    @Override
754    public String toString() {
755        return (toXML());
756    }
757
758    /**
759     * Creates and returns a copy of this object. The returned
760     * <code>Rule</code> object will have the same rule
761     * name, resource, service name, and actions
762     * such that <code>x.clone().equals(x)</code> will be
763     * <code>true</code>. However <code>x.clone()</code>
764     * will not be the same as <code>x</code>, i.e.,
765     * <code>x.clone() != x</code>.
766     *
767     * @return a copy of this object
768     */
769    @Override
770    public Object clone() {
771        Rule answer = null;
772        try {
773            answer = (Rule) super.clone();
774        } catch (CloneNotSupportedException se) {
775            answer = new Rule();
776        }
777
778        answer.ruleName = ruleName;
779        answer.serviceTypeName = serviceTypeName;
780        answer.applicationName = applicationName;
781        answer.serviceType = serviceType;
782        answer.resourceNames = new HashSet();
783        if (resourceNames != null) {
784            answer.resourceNames.addAll(resourceNames);
785        }
786
787        // Copy the actions
788        answer.actions = new HashMap();
789        Iterator items = actions.keySet().iterator();
790        while (items.hasNext()) {
791            Object o = items.next();
792            Set set = (Set) actions.get(o);
793            HashSet aSet = new HashSet();
794            aSet.addAll(set);
795            answer.actions.put(o, aSet);
796        }
797
798        return (answer);
799    }
800
801    /**
802     * Returns action values given resource type, resource name and a set of
803     * action names  by matching the arguments to those of the rule object.
804     *
805     * @param resourceType resource type
806     * @param resourceName resource name
807     * @param actionNames a set of action names for which to compute values.
808     * Each element of the set should be a <code>String</code>
809     * valued action name
810     * @return a map of action values for actions
811     *         Each key of the map is a String valued action name
812     *         Each value of the map is a set of String values
813     * @throws NameNotFoundException if any name in <code>actionNames</code> is
814     *         not found in the rule.
815     */
816    Map getActionValues(String resourceType, String resourceName,
817            Set actionNames) throws NameNotFoundException {
818        Map actionValues = null;
819        if ((serviceTypeName.equalsIgnoreCase(resourceType)) && (actionNames != null)) {
820            ResourceMatch rm = isResourceMatch(resourceType, resourceName);
821            if (ResourceMatch.EXACT_MATCH.equals(rm) || ResourceMatch.WILDCARD_MATCH.equals(rm)) {
822                //if (ResourceMatch.EXACT_MATCH.equals(rm) ) {
823                actionValues = new HashMap();
824                Iterator actionIter = actionNames.iterator();
825                while (actionIter.hasNext()) {
826                    String actionName = (String) actionIter.next();
827                    Set values = getActionValues(actionName);
828                    if (values != null) {
829                        actionValues.put(actionName, values);
830                    }
831
832                }
833            }
834        }
835        return (actionValues);
836    }
837}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.