/** * Obtains the part of a DN relative to a supplied base context. * <p> * If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name * is "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if (base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
/** * Remove the supplied path from the beginning of this * <code>DistinguishedName</code> if this instance starts with * <code>path</code>. Useful for stripping base path suffix from a * <code>DistinguishedName</code>. * * @param path the path to remove from the beginning of this instance. */ public void removeFirst(Name path) { if (path != null && this.startsWith(path)) { for (int i = 0; i < path.size(); i++) { this.removeFirst(); } } }
@Test public void testStartsWith_true() { DistinguishedName path1 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName start1 = new DistinguishedName("o=example.com"); DistinguishedName path2 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName start2 = new DistinguishedName("ou=people, ou=EU, o=example.com"); assertThat(path1.startsWith(start1)).isTrue(); assertThat(path2.startsWith(start2)).isTrue(); }
protected void getEntityByInternalId(String internalId, EntitySearchResultHandler handler) throws SecurityException { DistinguishedName principalDN = getRelativeDN(internalId); if (configuration.getSearchDN().size() == 0 || principalDN.startsWith(configuration.getSearchDN())) { SearchControls sc = getSearchControls(SearchControls.OBJECT_SCOPE, true, configuration.getEntityAttributeNames()); PagedSearchExecutor pse = new PagedSearchExecutor(principalDN, defaultSearchFilterStr, sc, handler); handler.setEntityFactory(getEntityFactory()); ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); ldapTemplate.search(pse,pse); } catch (NamingException e) { throw new SecurityException(SecurityException.UNEXPECTED.create(getClass().getName(), "getEntityByInternalId", e.getMessage()), e); } finally { Thread.currentThread().setContextClassLoader(currentClassLoader); handler.setEntityFactory(null); } } }
@Test public void testStartsWith_false() { DistinguishedName path1 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName start1 = new DistinguishedName("ou=people"); DistinguishedName path2 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName start2 = new DistinguishedName("uid=mtah.test, ou=EU, ou=people"); assertThat(path1.startsWith(start1)).isFalse(); assertThat(path2.startsWith(start2)).isFalse(); }
/** * Calculate the relative DN based on the current base. For example, * if the base is "ou=example" and the fullDNString is * "cn=myuser,ou=example", then the returned DN will be "cn=myuser". * * Note: if constructor argument {@code base} was {@code null} then this will throw an exception. * * @param fullDNString the full distinguished name, not {@code null} * @return the corresponding relative distinguished name, not {@code null} */ public DistinguishedName relativeDN(String fullDNString) { DistinguishedName full = new DistinguishedName(fullDNString); DistinguishedName base = new DistinguishedName(this.base); if (this.base.trim().length() == 0) { return full; } else if (base.equals(full)) { return new DistinguishedName(""); } else if (!full.startsWith(base)) { throw new InternalException(String.format( "Full DN (%s) does not start with base DN (%s)", full, base)); } else { full.removeFirst(base); return full; } }
@Test public void testStartsWith_Longer() { DistinguishedName path1 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName path2 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com, o=a.com"); assertThat(path1.startsWith(path2)).isFalse(); }
@Test public void testStartsWith_EmptyPath() { DistinguishedName path1 = new DistinguishedName("uid=mtah.test, ou=people, ou=EU, o=example.com"); DistinguishedName path2 = new DistinguishedName(); assertThat(path1.startsWith(path2)).isFalse(); }
protected DirContextOperations getEntityContextByInternalId(String internalId, boolean withAttributes) throws SecurityException if (configuration.getSearchDN().size() == 0 || principalDN.startsWith(configuration.getSearchDN()))
/** * Obtains the part of a DN relative to a supplied base context. * <p>If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name is * "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if(base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
public void getEntities(Entity parent, Filter filter, EntitySearchResultHandler handler) throws SecurityException { ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader(); DistinguishedName parentDN = getRelativeDN(parent.getInternalId()); if (configuration.getSearchDN().size() == 0 || parentDN.startsWith(configuration.getSearchDN())) { String sf = createSearchFilter(filter); SearchControls sc = getSearchControls(SearchControls.ONELEVEL_SCOPE, true, configuration.getEntityAttributeNames()); handler.setEntityFactory(getEntityFactory()); PagedSearchExecutor pse = new PagedSearchExecutor(parentDN, sf, sc, handler, searchPageSize); try { Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); ldapTemplate.search(pse, pse); } catch (NamingException e) { throw new SecurityException(SecurityException.UNEXPECTED.create(getClass().getName(), "getEntities", e.getMessage()), e); } finally { Thread.currentThread().setContextClassLoader(currentClassLoader); handler.setEntityFactory(null); } } }
/** * Obtains the part of a DN relative to a supplied base context. * <p> * If the DN is "cn=bob,ou=people,dc=springframework,dc=org" and the base context name * is "ou=people,dc=springframework,dc=org" it would return "cn=bob". * </p> * * @param fullDn the DN * @param baseCtx the context to work out the name relative to. * * @return the * * @throws NamingException any exceptions thrown by the context are propagated. */ public static String getRelativeName(String fullDn, Context baseCtx) throws NamingException { String baseDn = baseCtx.getNameInNamespace(); if (baseDn.length() == 0) { return fullDn; } DistinguishedName base = new DistinguishedName(baseDn); DistinguishedName full = new DistinguishedName(fullDn); if (base.equals(full)) { return ""; } Assert.isTrue(full.startsWith(base), "Full DN does not start with base DN"); full.removeFirst(base); return full.toString(); }
protected DistinguishedName getFullDN(DistinguishedName relativeDN) { DistinguishedName fullDN = new DistinguishedName(relativeDN); if (configuration.getBaseDN().size() > 0 && !fullDN.startsWith(configuration.getBaseDN())) { fullDN.prepend(configuration.getBaseDN()); } return fullDN; }