[ldap-sdk-commits] SF.net SVN: ldap-sdk:[1582] trunk
A Java-based LDAP API
Brought to you by:
dirmgr,
kennethleo
From: <di...@us...> - 2023-01-06 18:08:32
|
Revision: 1582 http://sourceforge.net/p/ldap-sdk/code/1582 Author: dirmgr Date: 2023-01-06 18:08:29 +0000 (Fri, 06 Jan 2023) Log Message: ----------- Add DN.getDNRelativeToBaseDN Added a DN.getDNRelativeToBaseDN method that may be used to retrieve the portion of a DN that is relative to a given base DN (that is, the portion of a DN with the given base DN stripped off). Modified Paths: -------------- trunk/docs/release-notes.html trunk/messages/unboundid-ldapsdk-ldap.properties trunk/src/com/unboundid/ldap/sdk/DN.java trunk/tests/unit/src/com/unboundid/ldap/sdk/DNTestCase.java Modified: trunk/docs/release-notes.html =================================================================== --- trunk/docs/release-notes.html 2023-01-02 17:08:44 UTC (rev 1581) +++ trunk/docs/release-notes.html 2023-01-06 18:08:29 UTC (rev 1582) @@ -14,6 +14,13 @@ <ul> <li> + Added a DN.getDNRelativeToBaseDN method that may be used to retrieve the portion + of a DN that is relative to a given base DN (that is, the portion of a DN with + the given base DN stripped off). + <br><br> + </li> + + <li> Added support for encrypted PKCS #8 private keys. Private keys can now be formatted in encrypted PEM when provided with an encryption password and a set of encryption properties, and the PKCS #8 PEM file reader can read encrypted Modified: trunk/messages/unboundid-ldapsdk-ldap.properties =================================================================== --- trunk/messages/unboundid-ldapsdk-ldap.properties 2023-01-02 17:08:44 UTC (rev 1581) +++ trunk/messages/unboundid-ldapsdk-ldap.properties 2023-01-06 18:08:29 UTC (rev 1582) @@ -92,7 +92,6 @@ ERR_CONTROLS_DECODE_ELEMENT_NOT_SEQUENCE=Unable to decode the provided \ sequence as a set of controls because one of the elements could not be \ decoded as a control sequence: {0} - ERR_DELETE_INTERRUPTED=Delete processing was interrupted while waiting for a \ response from server {0}. ERR_DN_ENDS_WITH_COMMA=Unable to parse string ''{0}'' as a DN because it ends \ @@ -738,6 +737,9 @@ ERR_DN_MATCHES_UNSUPPORTED_SCOPE=Unable to determine whether DN ''{0}'' \ matches the specified base and scope because the provided scope ''{1}'' is \ not supported for this determination. +ERR_DN_FULL_DN_NOT_DESCENDANT_OF_BASE=Unable to obtain the portion of full DN \ + ''{0}'' that is relative to base DN ''{1}'' because the full DN is neither \ + a descendant of nor equal to the base DN. ERR_CONNECT_THREAD_INTERRUPTED=A thread was interrupted while waiting for the \ connect thread to establish a connection to {0}:{1,number,0}: {2} ERR_CONNECT_THREAD_TIMEOUT=Unable to establish a connection to server \ Modified: trunk/src/com/unboundid/ldap/sdk/DN.java =================================================================== --- trunk/src/com/unboundid/ldap/sdk/DN.java 2023-01-02 17:08:44 UTC (rev 1581) +++ trunk/src/com/unboundid/ldap/sdk/DN.java 2023-01-06 18:08:29 UTC (rev 1582) @@ -1892,4 +1892,92 @@ { return new DN(s1, schema).compareTo(new DN(s2, schema)); } + + + + /** + * Retrieves a string that represents the portion of the provided full DN that + * is relative to the given base DN (that is, the full DN with the base DN + * stripped off). For example, if the provided full DN is + * "uid=jdoe,ou=People,dc=example,dc=com" and the base DN is + * "dc=example,dc=com", then the returned DN will be "uid=jdoe,ou=People". + * + * @param fullDN The full DN for which to obtain the portion relative to the + * base DN. It must not be {@code null}, and it must + * represent a valid DN that is a descendant of or equal to + * the base DN. + * @param baseDN The base DN to strip off of the provided full DN. It must + * not be {@code null}, and it must be an ancestor of or equal + * to the full DN. + * + * @return A string representation of the DN that represents the portion of + * the full DN that is relative to the base DN, an empty string if + * the full DN is equal to the base DN, or the provided full DN if + * the base DN represents the null DN. + * + * @throws LDAPException If either of the provided strings is not a valid + * DN, or if the provided full DN is not an ancestor + * of or equal to the given base DN. + */ + @NotNull() + public static String getDNRelativeToBaseDN(@NotNull final String fullDN, + @NotNull final String baseDN) + throws LDAPException + { + final DN parsedFullDN = new DN(fullDN); + final DN parsedBaseDN = new DN(baseDN); + return getDNRelativeToBaseDN(parsedFullDN, parsedBaseDN).toString(); + } + + + + /** + * Retrieves a portion of the provided full DN that is relative to the given + * base DN (that is, the full DN with the base DN stripped off). For example, + * if the provided full DN is "uid=jdoe,ou=People,dc=example,dc=com" and the + * base DN is "dc=example,dc=com", then the returned DN will be + * "uid=jdoe,ou=People". + * + * @param fullDN The full DN for which to obtain the portion relative to the + * base DN. It must not be {@code null}, and it must be a + * descendant of or equal to the base DN. + * @param baseDN The base DN to strip off of the provided full DN. It must + * not be {@code null}, and it must be an ancestor of or equal + * to the full DN. + * + * @return A DN that represents the portion of the full DN that is relative + * to the base DN, {@link #NULL_DN} if the full DN is equal to the + * base DN, or the provided full DN if the base DN is the null DN. + * + * @throws LDAPException If the provided full DN is not an ancestor of or + * equal to the given base DN. + */ + @NotNull() + public static DN getDNRelativeToBaseDN(@NotNull final DN fullDN, + @NotNull final DN baseDN) + throws LDAPException + { + if (baseDN.isNullDN()) + { + return fullDN; + } + + if (! fullDN.isDescendantOf(baseDN, true)) + { + throw new LDAPException(ResultCode.PARAM_ERROR, + ERR_DN_FULL_DN_NOT_DESCENDANT_OF_BASE.get(String.valueOf(fullDN), + String.valueOf(baseDN))); + } + + final RDN[] fullRDNs = fullDN.getRDNs(); + final RDN[] baseRDNs = baseDN.getRDNs(); + if (fullRDNs.length == baseRDNs.length) + { + return NULL_DN; + } + + final RDN[] relativeRDNs = new RDN[fullRDNs.length - baseRDNs.length]; + System.arraycopy(fullRDNs, 0, relativeRDNs, 0, relativeRDNs.length); + return new DN(relativeRDNs); + } } Modified: trunk/tests/unit/src/com/unboundid/ldap/sdk/DNTestCase.java =================================================================== --- trunk/tests/unit/src/com/unboundid/ldap/sdk/DNTestCase.java 2023-01-02 17:08:44 UTC (rev 1581) +++ trunk/tests/unit/src/com/unboundid/ldap/sdk/DNTestCase.java 2023-01-06 18:08:29 UTC (rev 1582) @@ -1977,4 +1977,79 @@ assertEquals(dn.toNormalizedString(), "case-exact-attr=This Is A Test,dc=example,dc=com"); } + + + + /** + * Provides test coverage for the getDNRelativeToBaseDN method. + * + * @throws Exception If an unexpected problem occurs. + */ + @Test() + public void testGetDNRelativeToBaseDN() + throws Exception + { + assertDNsEqual( + DN.getDNRelativeToBaseDN("uid=jdoe,ou=People,dc=example,dc=com", + "dc=example,dc=com"), + "uid=jdoe,ou=People"); + + assertDNsEqual( + DN.getDNRelativeToBaseDN("ou=People,dc=example,dc=com", + "dc=example,dc=com"), + "ou=People"); + + assertDNsEqual( + DN.getDNRelativeToBaseDN("dc=example,dc=com", "dc=example,dc=com"), + ""); + + assertDNsEqual( + DN.getDNRelativeToBaseDN("dc=example,dc=com", ""), + "dc=example,dc=com"); + + try + { + DN.getDNRelativeToBaseDN("o=example1", "o=example2"); + fail("Expected an exception when the full DN and base DN have no " + + "components in common."); + } + catch (final LDAPException e) + { + // This was expected + } + + try + { + DN.getDNRelativeToBaseDN("ou=test1,dc=example,dc=com", + "ou=test2,dc=example,dc=com"); + fail("Expected an exception when the full DN and base DN are peers."); + } + catch (final LDAPException e) + { + // This was expected + } + + try + { + DN.getDNRelativeToBaseDN("dc=example,dc=com", + "ou=test,dc=example,dc=com"); + fail("Expected an exception when the full DN is an ancestor of the " + + "base DN."); + } + catch (final LDAPException e) + { + // This was expected + } + + try + { + DN.getDNRelativeToBaseDN("", "dc=example,dc=com"); + fail("Expected an exception when the full DN is empty and the base DN " + + "is not."); + } + catch (final LDAPException e) + { + // This was expected + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |