org.springframework.ldap.NameNotFoundException Java Examples

The following examples show how to use org.springframework.ldap.NameNotFoundException. 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: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testDoSearch_NameNotFoundException() throws Exception {
	expectGetReadOnlyContext();

	when(searchExecutorMock.executeSearch(dirContextMock)).thenThrow(new javax.naming.NameNotFoundException());

	try {
		tested.search(searchExecutorMock, handlerMock);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
}
 
Example #2
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteReadWrite_NamingException() throws Exception {
	expectGetReadWriteContext();

	javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
	when(contextExecutorMock.executeWithContext(dirContextMock)).thenThrow(ne);

	try {
		tested.executeReadWrite(contextExecutorMock);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
}
 
Example #3
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecuteReadOnly_NamingException() throws Exception {
	expectGetReadOnlyContext();

	javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
	when(contextExecutorMock.executeWithContext(dirContextMock)).thenThrow(ne);

	try {
		tested.executeReadOnly(contextExecutorMock);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
}
 
Example #4
Source File: LdapTemplateNoBaseSuffixITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindAndUnbind_Plain() {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");
	tested.bind("cn=Some Person4, ou=company1, ou=Sweden," + base, adapter, null);

	DirContextAdapter result = (DirContextAdapter) tested
			.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base);

	assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person4");
	assertThat(result.getStringAttribute("sn")).isEqualTo("Person4");
	assertThat(result.getDn()).isEqualTo(LdapUtils.newLdapName("cn=Some Person4,ou=company1,ou=Sweden," + base));

	tested.unbind("cn=Some Person4,ou=company1,ou=Sweden," + base);
	try {
		tested.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example #5
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnbind_NamingException() throws Exception {
	expectGetReadWriteContext();

       javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
       doThrow(ne).when(dirContextMock).unbind(nameMock);

	try {
		tested.unbind(nameMock);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
}
 
Example #6
Source File: ContextSourceTransactionManagerIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnbind() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.unbind(dn, "Some Person");

	try {
		// Verify result - check that the operation was not rolled back
		ldapTemplate.lookup(dn);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

}
 
Example #7
Source File: ContextSourceTransactionManagerNamespaceIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnbind() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	dummyDao.unbind(dn, "Some Person");

	try {
		// Verify result - check that the operation was not rolled back
		ldapTemplate.lookup(dn);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

}
 
Example #8
Source File: ContextSourceAndHibernateTransactionManagerNamespaceITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnbind() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	dummyDao.unbind(person);

	try {
		// Verify result - check that the operation was not rolled back
		ldapTemplate.lookup(dn);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

	person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(1));
	assertThat(person).isNull();
}
 
Example #9
Source File: ContextSourceAndHibernateTransactionManagerIntegrationTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnbind() {
	String dn = "cn=Some Person,ou=company1,ou=Sweden";
	// Perform test
	OrgPerson person = (OrgPerson) this.hibernateTemplate.load(OrgPerson.class, new Integer(1));
	dummyDao.unbind(person);

	try {
		// Verify result - check that the operation was not rolled back
		ldapTemplate.lookup(dn);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

	person = (OrgPerson) this.hibernateTemplate.get(OrgPerson.class, new Integer(1));
	assertThat(person).isNull();
}
 
Example #10
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testBind_NamingException() throws Exception {
	expectGetReadWriteContext();

	Object expectedObject = new Object();
	BasicAttributes expectedAttributes = new BasicAttributes();
       javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
       doThrow(ne).when(dirContextMock).bind(nameMock, expectedObject, expectedAttributes);

	try {
		tested.bind(nameMock, expectedObject, expectedAttributes);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
   }
 
Example #11
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_NameNotFoundException() throws Exception {
	expectGetReadOnlyContext();

	final SearchControls controls = searchControlsRecursive();
	controls.setReturningObjFlag(false);

	javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException("some text");
	when(dirContextMock.search(
               eq(nameMock),
               eq("(ou=somevalue)"),
               argThat(new SearchControlsMatcher(controls)))).thenThrow(ne);

	try {
		tested.search(nameMock, "(ou=somevalue)", handlerMock);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
	verify(dirContextMock).close();
}
 
Example #12
Source File: LdapTemplateLookupTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookup_ContextMapper_NamingException() throws Exception {
    expectGetReadOnlyContext();

    javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
    when(dirContextMock.lookup(nameMock)).thenThrow(ne);

    try {
        tested.lookup(nameMock, contextMapperMock);
        fail("NameNotFoundException expected");
    } catch (NameNotFoundException expected) {
        assertThat(true).isTrue();
    }

    verify(dirContextMock).close();
}
 
Example #13
Source File: LdapTemplateLookupTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookup_AttributesMapper_NamingException() throws Exception {
    expectGetReadOnlyContext();

    javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
    when(dirContextMock.getAttributes(nameMock)).thenThrow(ne);

    try {
        tested.lookup(nameMock, attributesMapperMock);
        fail("NameNotFoundException expected");
    } catch (NameNotFoundException expected) {
        assertThat(true).isTrue();
    }

    verify(dirContextMock).close();
}
 
Example #14
Source File: LdapTemplateLookupTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookup_NamingException() throws Exception {
    expectGetReadOnlyContext();

    javax.naming.NameNotFoundException ne = new javax.naming.NameNotFoundException();
    when(dirContextMock.lookup(nameMock)).thenThrow(ne);

    try {
        tested.lookup(nameMock);
        fail("NameNotFoundException expected");
    } catch (NameNotFoundException expected) {
        assertThat(true).isTrue();
    }

    verify(dirContextMock).close();
}
 
Example #15
Source File: LdapTemplateRecursiveDeleteITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@After
public void cleanup() throws Exception {
	try {
		tested.unbind(DN, true);
	}
	catch (NameNotFoundException ignore) {
		// ignore
	}
}
 
Example #16
Source File: LdapTemplateSearchResultNamespaceConfigITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() {
	try {
		tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
				contextMapper);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example #17
Source File: SimpleLdapTemplateITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void verifyCleanup() {
	try {
		ldapTemplate.lookupContext(DN_STRING);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example #18
Source File: LdapTemplateRecursiveDeleteITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void verifyDeleted(Name dn) {
	try {
		tested.lookup(dn);
		fail("Expected entry '" + dn + "' to be non-existent");
	}
	catch (NameNotFoundException expected) {
		// expected
	}
}
 
Example #19
Source File: LdapIdentityLookup.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public User retrieve(IdentityReference identityReference) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        return ldapTemplate.lookup(identityReference.getReference(), userAttributes, USER_CONTEXT_MAPPER);
    } catch (final NameNotFoundException nnfe) {
        return null;
    } finally {
        Thread.currentThread().setContextClassLoader(classLoader);
    }
}
 
Example #20
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchWithInvalidSearchBaseShouldByDefaultThrowException() {
	try {
		tested.search(BASE_NAME + "ou=unknown", FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS,
				contextMapper);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example #21
Source File: LdapTemplateBindUnbindITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void verifyCleanup() {
	try {
		tested.lookup(DN);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example #22
Source File: ContextSourceTransactionManagerSubtreeIntegrationTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testLdap168DeleteRecursively() {
    dummyDao.deleteRecursively("ou=company1,ou=Sweden");

    try {
        ldapTemplate.lookup("ou=company1,ou=Sweden");
        fail("NameNotFoundException expected");
    } catch (NameNotFoundException expected) {
        assertThat(true).isTrue();
    }
}
 
Example #23
Source File: LdapTemplateRenameITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void verifyDeleted(Name dn) {
	try {
		tested.lookup(dn);
		fail("Expected entry '" + dn + "' to be non-existent");
	}
	catch (NameNotFoundException expected) {
		// expected
	}
}
 
Example #24
Source File: TestLdap.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test(expected = NameNotFoundException.class)
public void readNonExistant() throws Exception {
    odmManager.read(Person.class, LdapUtils.newLdapName("cn=Hili Harvey,ou=Doctors,o=Whoniverse"));
}
 
Example #25
Source File: PasswordComparisonAuthenticator.java    From ranger with Apache License 2.0 4 votes vote down vote up
public DirContextOperations authenticate(final Authentication authentication) {
	Assert.isInstanceOf(UsernamePasswordAuthenticationToken.class,
			authentication,
			"Can only process UsernamePasswordAuthenticationToken objects");
	// locate the user and check the password

	DirContextOperations user = null;
	String username = authentication.getName();
	String password = (String) authentication.getCredentials();

	Iterator dns = getUserDns(username).iterator();

	SpringSecurityLdapTemplate ldapTemplate = new SpringSecurityLdapTemplate(
			getContextSource());

	while (dns.hasNext() && user == null) {
		final String userDn = (String) dns.next();

		try {
			user = ldapTemplate.retrieveEntry(userDn, getUserAttributes());
		} catch (NameNotFoundException ignore) {
		}
	}

	if (user == null && getUserSearch() != null) {
		user = getUserSearch().searchForUser(username);
	}

	if (user == null) {
		throw new UsernameNotFoundException("User not found: " + username);
	}

	if (logger.isDebugEnabled()) {
		logger.debug("Performing LDAP compare of password attribute '"
				+ passwordAttributeName + "' for user '" + user.getDn()
				+ "'");
	}

	String encodedPassword = passwordEncoder.encodePassword(password, null);
	byte[] passwordBytes = encodedPassword.getBytes();

	if (!ldapTemplate.compare(user.getDn().toString(),
			passwordAttributeName, passwordBytes)) {
		throw new BadCredentialsException(messages.getMessage(
				"PasswordComparisonAuthenticator.badCredentials",
				"Bad credentials"));
	}

	return user;
}