Java Code Examples for org.springframework.ldap.support.LdapUtils#newLdapName()

The following examples show how to use org.springframework.ldap.support.LdapUtils#newLdapName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModifyAttributesOperationExecutorTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommit() {
	ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
	ModificationItem[] expectedActualItems = new ModificationItem[0];

	Name expectedDn = LdapUtils.newLdapName("cn=john doe");

	ModifyAttributesOperationExecutor tested = new ModifyAttributesOperationExecutor(ldapOperationsMock,
			expectedDn, expectedActualItems, expectedCompensatingItems);

	// No operation here
       verifyNoMoreInteractions(ldapOperationsMock);

	// Perform test
	tested.commit();
}
 
Example 2
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSetDnAttributesValuesOneNewEntry() throws NamingException {
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("uniqueMember", "cn=john doe, ou=company");

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.setAttributeValues("uniqueMember", new Object[]{
            LdapUtils.newLdapName("cn=john doe, ou=company"),
            LdapUtils.newLdapName("cn=jane doe, ou=company")
    });

    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(1);

    ModificationItem modificationItem = modificationItems[0];
    assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE);
    assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember");
    assertThat(modificationItem.getAttribute().get()).isEqualTo("cn=jane doe, ou=company");
}
 
Example 3
Source File: UnbindOperationRecorderTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecordOperation() {
    final LdapName expectedTempName = LdapUtils.newLdapName(
            "cn=john doe_temp");
    final LdapName expectedDn = LdapUtils.newLdapName(
            "cn=john doe");
    UnbindOperationRecorder tested = new UnbindOperationRecorder(
            ldapOperationsMock, renamingStrategyMock);

    when(renamingStrategyMock.getTemporaryName(expectedDn))
            .thenReturn(expectedTempName);

    // Perform test
    CompensatingTransactionOperationExecutor operation = tested
            .recordOperation(new Object[] { expectedDn });

    // Verify result
    assertThat(operation instanceof UnbindOperationExecutor).isTrue();
    UnbindOperationExecutor rollbackOperation = (UnbindOperationExecutor) operation;
    assertThat(rollbackOperation.getLdapOperations()).isSameAs(ldapOperationsMock);
    assertThat(rollbackOperation.getOriginalDn()).isSameAs(expectedDn);
    assertThat(rollbackOperation.getTemporaryDn()).isSameAs(expectedTempName);
}
 
Example 4
Source File: NameAwareAttribute.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Override
public boolean remove(Object attrval) {
    if (attrval instanceof Name) {
        initValuesAsNames();

        Name name = LdapUtils.newLdapName((Name) attrval);
        String removedValue = valuesAsNames.remove(name);
        if(removedValue != null) {
            values.remove(removedValue);

            return true;
        }

        return false;
    }
    return values.remove(attrval);
}
 
Example 5
Source File: LdapTemplateLookupTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookup_String_ReturnAttributes_ContextMapper()
        throws Exception {
    expectGetReadOnlyContext();

    String[] attributeNames = new String[] { "cn" };

    BasicAttributes expectedAttributes = new BasicAttributes();
    expectedAttributes.put("cn", "Some Name");

    when(dirContextMock.getAttributes(DEFAULT_BASE_STRING, attributeNames)).thenReturn(expectedAttributes);

    LdapName name = LdapUtils.newLdapName(DEFAULT_BASE_STRING);
    DirContextAdapter adapter = new DirContextAdapter(expectedAttributes,
            name);

    Object transformed = new Object();
    when(contextMapperMock.mapFromContext(adapter)).thenReturn(transformed);

    Object actual = tested.lookup(DEFAULT_BASE_STRING, attributeNames,
            contextMapperMock);

    verify(dirContextMock).close();

    assertThat(actual).isSameAs(transformed);
}
 
Example 6
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveDnAttributeSyntacticallyEqual() throws NamingException {
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("uniqueMember", "cn=john doe,OU=company");

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(1);

    ModificationItem modificationItem = modificationItems[0];
    assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
    assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember");
}
 
Example 7
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateWithIdSpecified() throws NamingException {
    expectGetReadWriteContext();

    Object expectedObject = new Object();
    LdapName expectedName = LdapUtils.newLdapName("ou=someOu");
    when(odmMock.getId(expectedObject)).thenReturn(expectedName);

    ArgumentCaptor<DirContextAdapter> ctxCaptor = ArgumentCaptor.forClass(DirContextAdapter.class);
    doNothing().when(odmMock).mapToLdapDataEntry(eq(expectedObject), ctxCaptor.capture());

    tested.create(expectedObject);

    verify(odmMock, never()).setId(expectedObject, expectedName);
    verify(dirContextMock).bind(expectedName, ctxCaptor.getValue(), null);
    verify(dirContextMock).close();
}
 
Example 8
Source File: DefaultTempEntryRenamingStrategy.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public Name getTemporaryName(Name originalName) {
    LdapName temporaryName = LdapUtils.newLdapName(originalName);

    // Add tempSuffix to the leaf node name.
    try {
        String leafNode = (String) temporaryName.remove(temporaryName.size() - 1);
        temporaryName.add(new Rdn(leafNode  + tempSuffix));
    } catch (InvalidNameException e) {
        throw new org.springframework.ldap.InvalidNameException(e);
    }

    return temporaryName;
}
 
Example 9
Source File: PersonDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Override
public Person doMapFromContext(DirContextOperations context) {
	Person person = new Person();

          LdapName dn = LdapUtils.newLdapName(context.getDn());
	person.setCountry(LdapUtils.getStringValue(dn, 0));
	person.setCompany(LdapUtils.getStringValue(dn, 1));
	person.setFullName(context.getStringAttribute("cn"));
	person.setLastName(context.getStringAttribute("sn"));
	person.setDescription(context.getStringAttribute("description"));
	person.setPhone(context.getStringAttribute("telephoneNumber"));

	return person;
}
 
Example 10
Source File: ModifyAttributesOperationExecutorTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformOperation() {
	ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
	ModificationItem[] expectedActualItems = new ModificationItem[0];

	Name expectedDn = LdapUtils.newLdapName("cn=john doe");

	ModifyAttributesOperationExecutor tested = new ModifyAttributesOperationExecutor(ldapOperationsMock,
			expectedDn, expectedActualItems, expectedCompensatingItems);

	// Perform test
	tested.performOperation();

	verify(ldapOperationsMock).modifyAttributes(expectedDn, expectedActualItems);
}
 
Example 11
Source File: ModifyAttributesOperationExecutorTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollback() {
	ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
	ModificationItem[] expectedActualItems = new ModificationItem[0];

	Name expectedDn = LdapUtils.newLdapName("cn=john doe");

	ModifyAttributesOperationExecutor tested = new ModifyAttributesOperationExecutor(ldapOperationsMock,
			expectedDn, expectedActualItems, expectedCompensatingItems);

	// Perform test
	tested.rollback();

	verify(ldapOperationsMock).modifyAttributes(expectedDn, expectedCompensatingItems);
}
 
Example 12
Source File: DefaultTempEntryRenamingStrategyTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTemporaryDN_MultivalueDN() {
    LdapName expectedOriginalName = LdapUtils.newLdapName(
            "cn=john doe+sn=doe, ou=somecompany, c=SE");
    DefaultTempEntryRenamingStrategy tested = new DefaultTempEntryRenamingStrategy();

    Name result = tested.getTemporaryName(expectedOriginalName);
    assertThat(result.toString()).isEqualTo("cn=john doe+sn=doe_temp,ou=somecompany,c=SE");
}
 
Example 13
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetDnAttributeValueIdentical() {
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("uniqueMember", "cn=john doe, ou=company");

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.setAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(0);
}
 
Example 14
Source File: LdapTemplateLookupITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testLookup_GetNameInNamespace_Plain() {
       String expectedDn = "cn=Some Person2, ou=company1,ou=Sweden";
       DirContextAdapter result = (DirContextAdapter) tested.lookup(expectedDn);

       LdapName expectedName = LdapUtils.newLdapName(expectedDn);
       assertThat(result.getDn()).isEqualTo(expectedName);
	assertThat(result.getNameInNamespace()).isEqualTo("cn=Some Person2,ou=company1,ou=Sweden," + base);
}
 
Example 15
Source File: DefaultDirObjectFactory.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a DirContextAdapter given the supplied paramters. The
 * <code>name</code> is normally a JNDI <code>CompositeName</code>, which
 * needs to be handled with particuclar care. Specifically the escaping of a
 * <code>CompositeName</code> destroys proper escaping of Distinguished
 * Names. Also, the name might contain referral information, in which case
 * we need to separate the server information from the actual Distinguished
 * Name so that we can create a representing DirContextAdapter.
 *
 * @param attrs the attributes
 * @param name the Name, typically a <code>CompositeName</code>, possibly
 * including referral information.
 * @param nameInNamespace the Name in namespace.
 * @return a {@link DirContextAdapter} representing the specified
 * information.
 */
DirContextAdapter constructAdapterFromName(Attributes attrs, Name name, String nameInNamespace) {
	String nameString;
	String referralUrl = "";

	if (name instanceof CompositeName) {
		// Which it most certainly will be, and therein lies the
		// problem. CompositeName.toString() completely screws up the
		// formatting
		// in some cases, particularly when backslashes are involved.
		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("/")) {
				// We don't want any slash in the beginning of the
				// Distinguished Name.
				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;
   }
 
Example 16
Source File: LdapEntryIdentificationContextMapper.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
public LdapEntryIdentification mapFromContext(Object ctx) {
    DirContextOperations adapter = (DirContextOperations) ctx;
    return new LdapEntryIdentification(
            LdapUtils.newLdapName(adapter.getNameInNamespace()),
            LdapUtils.newLdapName(adapter.getDn()));
}
 
Example 17
Source File: DepartmentRepoImpl.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Override
public String mapFromNameClassPair(NameClassPair nameClassPair) throws NamingException {
    LdapName name = LdapUtils.newLdapName(nameClassPair.getName());
    return LdapUtils.getStringValue(name, "ou");
}
 
Example 18
Source File: LdapAttributes.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
public void setName(Name name) {
    this.dn = LdapUtils.newLdapName(name);
}
 
Example 19
Source File: LdapEntryIdentification.java    From spring-ldap with Apache License 2.0 3 votes vote down vote up
/**
 * Construct an LdapEntryIdentification instance.
 * @param absoluteDn the absolute DN of the identified entry, e.g. as
 * returned by {@link DirContext#getNameInNamespace()}.
 * @param relativeDn the DN of the identified entry relative to the base
 * LDAP path, e.g. as returned by {@link DirContextOperations#getDn()}.
    * @deprecated {@link DistinguishedName} and associated classes and methods are deprecated as of 2.0.
    * use {@link #LdapEntryIdentification(javax.naming.ldap.LdapName, javax.naming.ldap.LdapName)} instead.
 */
public LdapEntryIdentification(DistinguishedName absoluteDn, DistinguishedName relativeDn) {
	Assert.notNull(absoluteDn, "Absolute DN must not be null");
	Assert.notNull(relativeDn, "Relative DN must not be null");
	this.absoluteDn = LdapUtils.newLdapName(absoluteDn);
	this.relativeDn = LdapUtils.newLdapName(relativeDn);
}
 
Example 20
Source File: DirContextAdapter.java    From spring-ldap with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new DirContextAdapter from the supplied DN String.
 * @param dnString the DN string. Must be syntactically correct, or an
 * exception will be thrown.
 */
public DirContextAdapter(String dnString) {
	this(LdapUtils.newLdapName(dnString));
}