DefaultDirObjectFactory implements DirObjectFactory { DirContextAdapter constructAdapterFromName(Attributes attrs, Name name, String nameInNamespace) { String nameString; String referralUrl = ""; if (name instanceof CompositeName) { nameString = LdapUtils .convertCompositeNameToString((CompositeName) name); } else { LOG .warn("Expecting a CompositeName as input to getObjectInstance but received a '" + name.getClass().toString() + "' - using toString and proceeding with undefined results"); nameString = name.toString(); } if (nameString.startsWith(LDAP_PROTOCOL_PREFIX) || nameString.startsWith(LDAPS_PROTOCOL_PREFIX)) { if (LOG.isDebugEnabled()) { LOG.debug("Received name '" + nameString + "' contains protocol delimiter; indicating a referral." + "Stripping protocol and address info to enable construction of a proper LdapName"); } try { URI url = new URI(nameString); String pathString = url.getPath(); referralUrl = nameString.substring(0, nameString.length() - pathString.length()); if (StringUtils.hasLength(pathString) && pathString.startsWith("/")) { pathString = pathString.substring(1); } nameString = pathString; } catch (URISyntaxException e) { throw new IllegalArgumentException( "Supplied name starts with protocol prefix indicating a referral," + " but is not possible to parse to an URI", e); } if (LOG.isDebugEnabled()) { LOG.debug("Resulting name after removal of referral information: '" + nameString + "'"); } } DirContextAdapter dirContextAdapter = new DirContextAdapter(attrs, LdapUtils.newLdapName(nameString), LdapUtils.newLdapName(nameInNamespace), referralUrl); dirContextAdapter.setUpdateMode(true); return dirContextAdapter; }  @Override final Object getObjectInstance(
            Object obj,
            Name name,
            Context nameCtx,
            Hashtable<?, ?> environment,
            Attributes attrs); @Override Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment); static final String JNDI_ENV_BASE_PATH_KEY; }
@Test public void testConstructAdapterFromName() throws InvalidNameException { CompositeName name = new CompositeName(); name.add("ldap: DefaultDirObjectFactory tested = new DefaultDirObjectFactory(); DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, ""); assertThat(result.getDn().toString()).isEqualTo("ou=People,o=JNDITutorial"); assertThat(result.getReferralUrl().toString()).isEqualTo("ldap: }
@Test public void testConstructAdapterFromName_Ldaps() throws InvalidNameException { CompositeName name = new CompositeName(); name.add("ldaps: DefaultDirObjectFactory tested = new DefaultDirObjectFactory(); DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, ""); assertThat(result.getDn().toString()).isEqualTo("ou=People,o=JNDITutorial"); assertThat(result.getReferralUrl().toString()).isEqualTo("ldaps: }
@Test public void testConstructAdapterFromName_EmptyName() throws InvalidNameException { CompositeName name = new CompositeName(); name.add("ldap: DefaultDirObjectFactory tested = new DefaultDirObjectFactory(); DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, ""); assertThat(result.getDn().toString()).isEqualTo(""); assertThat(result.getReferralUrl().toString()).isEqualTo("ldap: }
@Test public void testConstructAdapterFromName_OnlySlash() throws InvalidNameException { CompositeName name = new CompositeName(); name.add("ldap: DefaultDirObjectFactory tested = new DefaultDirObjectFactory(); DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, ""); assertThat(result.getDn().toString()).isEqualTo(""); assertThat(result.getReferralUrl().toString()).isEqualTo("ldap: }