Class PolicyUtils


  • public class PolicyUtils
    extends Object
    The class PolicyUtils provides utility(static) methods that would be used by policy pacakge
    • Constructor Detail

      • PolicyUtils

        public PolicyUtils()
    • Method Detail

      • appendMapToMap

        public static Map appendMapToMap​(Map mapToAdd,
                                         Map toMap)
        Appends a map to another map
        Parameters:
        mapToAdd - map that needs to be added Each key should be a String Each value would be a Set of String values
        toMap - map the map to which the mapToAdd would be added Each key should be a String Each value would be a Set of String values
        Returns:
        the combined map which is also the toMap The combined map is formed by adding the values for each key found in the addToMap to the toMap. If a key was found both in addToMap and toMap, the new value for the key is the combined set of values for the key from the addToMap and original toMap
      • appendElementToMap

        public static Map appendElementToMap​(String key,
                                             Set values,
                                             Map toMap)
        Appends a key/value pair to a map
        Parameters:
        key - a String valued key
        values - a set of String values
        toMap - the map to which to append the key/value pair Each key of the map should be a String Each value of the map should be a Set of String values
        Returns:
        the combined map which is also the toMap The combined map is formed by adding the values for argument key to the toMap with the argument values. If the key is already present in addToMap, the new value for the key is the combined set of values for the key from argument values and original toMap
      • getDisplayName

        public static String getDisplayName​(String dn)
        Returns the display name for a given dn This implementation assumes the display name to be the value of the naming attribute of the entry. So, the value of the naming attribute is the return value.
        Parameters:
        dn - dn of the entry for which to get the display name
        Returns:
        disaplay name for the entry, this is same as the value of the naming attribute of the entry
      • parseEnvParameters

        public static Map parseEnvParameters​(Node pNode)
                                      throws PolicyException
        Parses an XML node which represents a collection of the environment parameters and returns a map which contains these parameters.
        Parameters:
        pNode - the XML DOM node for the environment parameters.
        Returns:
        a map which contains the environment parameters
        Throws:
        PolicyException - if the node can not be parsed into a map
      • parseResponseAttributes

        public static Set parseResponseAttributes​(Node pNode)
                                           throws PolicyException
        Parses an XML node which represents a collection of user response attributes and returns a set which contains the names of these attributes
        Parameters:
        pNode - the XML DOM node for the response attributes
        Returns:
        a set which contains the names of these attributes
        Throws:
        PolicyException - if the node can not be parsed into a set
      • parseAttributeValuePairs

        public static Map parseAttributeValuePairs​(Node pNode)
        Parses an XML node which represents Attribute-Value pairs and returns a map of such values.
        Parameters:
        pNode - the XML DOM node containing Attribute-Value pairs as child nodes.
        Returns:
        a map which contains Attribute-Value pairs
      • getAttributeName

        public static String getAttributeName​(Node pNode)
        Parses an XML node which represents an AttributeValuePair and returns the attribute name.
        Parameters:
        pNode - the XML DOM node for an AttributeValuePair
        Returns:
        the attribute name of the AttributeValuePair
      • getAttributeValues

        public static Set getAttributeValues​(Node pNode)
        Parses an XML node which represents an AttributeValuePair and returns the attribute values.
        Parameters:
        pNode - the XML DOM node for an AttributeValuePair
        Returns:
        the set of attribute values of the AttributeValuePair
      • envParametersToXMLString

        public static String envParametersToXMLString​(Map envMap)
        Converts a map which stores a set of environment parameters into its XML string representation.
        Parameters:
        envMap - a map respresents a collection of the parameters
        Returns:
        its XML string representation
      • responseAttributesToXMLString

        public static String responseAttributesToXMLString​(Set attrs)
        Converts a set which stores a set of response attribute names into its XML string representation.
        Parameters:
        attrs - a set of response attribute names
        Returns:
        XML string representation of set of attributes
      • mapToXMLString

        public static String mapToXMLString​(Map envMap)
        Converts a map to its XML string representation.
        Parameters:
        envMap - a map that has String valued keys. Value corresponding to each key should be a set of String(s).
        Returns:
        its XML string representation of env map
      • attributeValuePairToXMLString

        public static String attributeValuePairToXMLString​(String name,
                                                           Set values)
        Converts an attribute value pair into its XML string representation.
        Parameters:
        name - the attribute name of the attribute value pair
        values - the attribute values of the attribute value pair
        Returns:
        XML string representation of attribue value pair
      • quote

        public static String quote​(String s)
        Return a quoted string Surrounds a string on either side with double quote and returns the quoted string
        Parameters:
        s - string to be quoted
        Returns:
        quoted string
      • quote

        public static String quote​(int i)
        Return a quoted string, quoting an int. Converts an int to string and quotes it on either side with double quote and returns the quoted string
        Parameters:
        i - int to be quoted
        Returns:
        quoted string
      • quote

        public static String quote​(long l)
        Return a quoted string, quoting a long. Converts a long to string and quotes it on either side with double quote and returns the quoted string
        Parameters:
        l - long to be quoted
        Returns:
        quoted string
      • logAccessMessage

        public static void logAccessMessage​(String msgIdName,
                                            String[] data,
                                            SSOToken token)
                                     throws SSOException
        Logs an access message
        Parameters:
        msgIdName - name of message id
        data - array of data to be logged
        token - session token of the user who did the operation that triggered this logging
        Throws:
        SSOException
      • logErrorMessage

        public static void logErrorMessage​(String msgIdName,
                                           String[] data,
                                           SSOToken token)
                                    throws SSOException
        Logs an error message
        Parameters:
        msgIdName - name of message id
        data - array of data to be logged
        token - session token of the user who did the operation that triggered this logging
        Throws:
        SSOException
      • getISDSHostName

        public static String getISDSHostName()
        Returns the LDAP server host used by Access Manager SDK stored in serverconfig.xml file. For multiple hosts, the returned value is a space-delimited list of hosts.
        Returns:
        the LDAP server host used by Access Manager SDK. Returns null if unable to get the host.
      • isLocalDS

        public static boolean isLocalDS​(String hostName)
                                 throws PolicyException
        Checks if the hostName is the same as the one used by the OpenAM SDK.
        Parameters:
        hostName - host name to compare against OpenAM SDK config store host
        Returns:
        true if hostName is the same as the one used by the OpenAM SDK, false otherwise
        Throws:
        PolicyException - if host names comparison does not succeed
      • constructUserFilter

        public static String constructUserFilter​(SSOToken token,
                                                 String userRDNAttrName,
                                                 String userName,
                                                 boolean aliasEnabled)
                                          throws SSOException
        Constructs a search filter used in subject evaluation. If aliasEnabled is true, the user aliases will also be used to construct the search filter.
        Parameters:
        token - SSO token
        userRDNAttrName - naming attribute
        userName - the value of the user name
        aliasEnabled - if true, user alias list will be used to construct the search filter
        Returns:
        search filter
        Throws:
        SSOException - if there is error when trying to retrieve token properties
      • removePolicyRules

        public static void removePolicyRules​(SSOToken token,
                                             String serviceName)
                                      throws SSOException,
                                             AMException
        Removes policy rules defined for a service. All the policy rules defined for a service in the system are removed.
        Parameters:
        token - session token of the user doing the operation
        serviceName - name of the service
        Throws:
        SSOException
        AMException
      • delimStringToSet

        public static Set delimStringToSet​(String str,
                                           String delimiter)
        Parses a string into a set using the specified delimiter
        Parameters:
        str - string to be parsed
        delimiter - delimiter used in the string
        Returns:
        the parsed set
      • getDNDisplayString

        public static String getDNDisplayString​(String strDN)
        Returns a display string for an LDAP distinguished name.
        Parameters:
        strDN - distinguished name.
        Returns:
        display string for the LDAP distinguished name.
      • parseAdvicesXML

        public static Map parseAdvicesXML​(String advicesXML)
                                   throws PolicyException
        Parses an XML string representation of policy advices and returns a Map of advices. The keys of returned map would be advice name keys. Each key is a String object. The values against each key is a Set of String(s) of advice values
        Parameters:
        advicesXML - XML string representation of policy advices conforming to the following DTD. The input string may not be validated against the dtd for performance reasons.
        Returns:
        the map of policy advices parsed from the passed in advicesXML If the passed in advicesXML is null, null would be returned
        Throws:
        PolicyException - if there is any error parsing the passed in advicesXML
      • advicesToXMLString

        public static String advicesToXMLString​(Map advices)
                                         throws PolicyException
        Returns XML string representation of a Map of policy advices
        Parameters:
        advices - Map of policy advices
        Returns:
        XML string representation of policy advices
        Throws:
        PolicyException - if there is any error while converting
      • principalNameEqualsUuid

        public static boolean principalNameEqualsUuid​(SSOToken token)
                                               throws SSOException
        Checks if principal name and uuid are same in the session
        Parameters:
        token - session token
        Returns:
        true if the principal name and uuid are same in the session. Otherwise, false
        Throws:
        SSOException - if the session token is not valid
      • createPolicies

        public static void createPolicies​(PolicyManager pm,
                                          InputStream xmlPolicies)
                                   throws PolicyException,
                                          SSOException
        Creates policy objects given an input stream of policy XML which confines to com/sun/identity/policy/policyAdmin.dtd.
        Parameters:
        pm - Policy manager.
        xmlPolicies - Policy XML input stream.
        Throws:
        PolicyException - if policies cannot be created.
        SSOException - if Single Sign On token used to create policy manager is no longer valid.
      • createOrReplacePolicies

        public static void createOrReplacePolicies​(PolicyManager pm,
                                                   InputStream xmlPolicies,
                                                   boolean replace)
                                            throws PolicyException,
                                                   SSOException
        Creates or replaces policy objects given an input stream of policy XML which confines to com/sun/identity/policy/policyAdmin.dtd.
        Parameters:
        pm - Policy manager.
        xmlPolicies - Policy XML input stream.
        replace - True if the policies should be replaced, otherwise create.
        Throws:
        PolicyException - if policies cannot be updated.
        SSOException - if Single Sign On token used to update policy manager is no longer valid.
      • cloneMap

        public static Map cloneMap​(Map map)
        Returns deep copy of a Map The passed in Map should have String object as keys and Set of String objects as values
        Parameters:
        map - Map that needs to be copied
        Returns:
        a deep copy of passed in Map
      • encrypt

        public static String encrypt​(String plainText)
      • decrypt

        public static String decrypt​(String encryptedText)