/** Return true or false based on a Regular Expression match * @param {String} s1 the string to test * @param {String} s2 the regex string to test it with * @type Boolean * @return a boolean true or false */ function matches(s1,s2) { // Build search and replace options. var options = ""; var re = new RegExp(s2, options); if (s1.match(re)) { return true; } else { return false; } } importPackage(Packages.com.novell.ldap); /** * ldapSearch * * @param (String} host LDAP Server, either DNS or IP-Address * @param (Number} port LDAP listening port * @param (String} user user account, full distinguished name, LDAP syntax * @param (String} password the cleartext LDAP userpassword * @param (String} base search base * @param (String} scope (base | one | sub) * @param (String} filter LDAP search filter according to RFC2254 (see {@link #ldapCount(DirContext, String, String)} * @param (String} attrList comma separated list of attributes to return * @param (Number} maxResultSet maximum result set size (0=unlimited) * @type Nodeset * @return NodeSet containing instances from search result, or status element with error message */ function ldapSearch(host, port, user, password, base, scope, filter, attrList, maxResultSet) { var nodeSet = new Packages.com.novell.xml.xpath.NodeSet(); var document = Packages.com.novell.xml.dom.DocumentFactory.newDocument(); var ndsElement = document.createElement("nds"); document.appendChild(ndsElement); ndsElement.setAttributeNS(null, "dtdversion", "3.5"); var outputElement = document.createElement("output"); ndsElement.appendChild(outputElement); var searchScope = LDAPConnection.SCOPE_ONE; if (scope == "base") { searchScope = LDAPConnection.SCOPE_BASE; } else if (scope == "sub") { searchScope = LDAPConnection.SCOPE_SUB; } var attrSplit = attrList.split(','); var attrArray = java.lang.reflect.Array.newInstance(java.lang.String, attrSplit.length); for (var attrIndex in attrSplit) { attrArray[attrIndex] = attrSplit[attrIndex]; } var lc = new LDAPConnection(); // set search result set size limit var searchConstraints = new LDAPSearchConstraints(lc.getSearchConstraints()); searchConstraints.setMaxResults(maxResultSet); lc.setConstraints(searchConstraints); try { // connect and bind to the server lc.connect( host, port ); lc.bind( LDAPConnection.LDAP_V3, user, new java.lang.String(password).getBytes("UTF8") ); var searchResults = lc.search(base, searchScope, filter, attrArray, // return all attributes false); // return attrs and values /* process the results * -- The first while loop goes through all the entries * -- The second while loop goes through all the attributes * -- The third while loop goes through all the attribute values */ while (searchResults.hasMore()) { var nextEntry = searchResults.next(); // create the instance node var instanceElement = document.createElement("instance"); instanceElement.setAttributeNS(null,"src-dn", nextEntry.getDN()); var attributeSet = nextEntry.getAttributeSet(); var allAttributes = attributeSet.iterator(); // create the attr nodes while(allAttributes.hasNext()) { var attribute = allAttributes.next(); var attrElement = document.createElement("attr"); attrElement.setAttributeNS(null, "attr-name", attribute.getName()); var allValues = attribute.getStringValues(); // create the value nodes if( allValues != null) { while(allValues.hasMoreElements()) { var valueElement = document.createElement("value"); valueElement.appendChild(document.createTextNode(allValues.nextElement())); attrElement.appendChild(valueElement); } instanceElement.appendChild(attrElement); } } outputElement.appendChild(instanceElement); nodeSet.add(instanceElement); } } catch(e) { var statusElement = document.createElement("status"); statusElement.setAttributeNS(null, "level", "error"); statusElement.appendChild(document.createTextNode(e.toString())); outputElement.appendChild(statusElement); nodeSet.add(statusElement); } finally { // disconnect with the server lc.disconnect(); } return nodeSet; } function serialize(nodeSet) { var stringWriter = new java.io.StringWriter(); for (var node = nodeSet.first(); node; node=nodeSet.next()) { var domWriter = new Packages.com.novell.xml.dom.DOMWriter(node, stringWriter); domWriter.setIndent(true); domWriter.write(); domWriter.flush(); stringWriter.write('\n'); } return stringWriter.toString(); } function appendToFile(fileName, text) { var fileWriter = new java.io.FileWriter(fileName, true); try { fileWriter.write(text); fileWriter.write("\n"); } finally { fileWriter.close(); } } importPackage(Packages.com.novell.xml.xpath); importPackage(Packages.com.novell.xsl.util); importClass(java.util.HashSet); /** Create a NodeSet that eliminates nodes from the input nodeset with duplicate string values * @param {String} input the input NodeSet * @type NodeSet * @return a NodeSet that eliminates nodes from the input nodeset with duplicate string values */ function unique(input) { // create the output node set var nodeSet = new InsertionOrderNodeSet(); // create HashSet to keep track of duplicates var set = new HashSet(); // loop through the Nodes in the NodeSet for (var node = input.first(); node != null; node = input.next()) { var value = Util.getXSLStringValue(node).toUpperCase(); if (!set.contains(value)) { set.add(value); nodeSet.add(node); } } return nodeSet; } /** Create a NodeSet that contains nodes from the input nodeset with duplicate string values * @param {String} input the input NodeSet * @type NodeSet * @return a NodeSet that contains only nodes from the input nodeset with duplicate string values */ function duplicate(input) { // create the output node set var nodeSet = new InsertionOrderNodeSet(); // create HashSet to keep track of duplicates var set = new HashSet(); // loop through the Nodes in the NodeSet for (var node = input.first(); node != null; node = input.next()) { var value = Util.getXSLStringValue(node).toUpperCase(); if (set.contains(value)) { nodeSet.add(node); } else { set.add(value); } } return nodeSet; } /** Create a NodeSet that contains nodes from the input nodeset with duplicate string values * @param {String} input the input NodeSet * @type NodeSet * @return a NodeSet that contains only nodes from the input nodeset with duplicate string values */ function blanks(input) { // create the output node set var nodeSet = new InsertionOrderNodeSet(); // create HashSet to keep track of duplicates var set = new HashSet(); // loop through the Nodes in the NodeSet for (var node = input.first(); node != null; node = input.next()) { var value = Util.getXSLStringValue(node); if (value = null) { nodeSet.add(node); } } return nodeSet; } /** Perform a binary compare for a bitmask * @param {int} value binary value, as a decimal representation * @param {int} bits binary value, as a decimal representation * @type int * @return boolean true or false if the tested VALUE contains the BIT specified. */ function bittest(value, bits) { return ((value & bits) == bits); } importClass(Packages.com.novell.xml.util.Base64Codec); /** * Convert a Base64 encoded GUID attribute value to an ASCII string * in the format the NDS2NDS driver uses as its association. * * @param {String} s Base64 encoded GUID attribute value * * @type String * @return ASCII string * * @throws IOException - */ function base64conversion(s) { var bytes = Base64Codec.decode(s); var s1 = encodeAsciiHex(bytes); return s1; } var digits = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" ]; function encodeAsciiHex(abyte0) { var buffer = ""; for(var i = 0; i < abyte0.length; i++) { var byte0 = abyte0[i]; buffer += digits[byte0 >> 4 & 0xf]; buffer += digits[byte0 & 0xf]; } return buffer; }