Java Code Examples for javax.naming.directory.SearchControls#setReturningObjFlag()

The following examples show how to use javax.naming.directory.SearchControls#setReturningObjFlag() . 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 testSearch_NamingException() throws Exception {
	expectGetReadOnlyContext();

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

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

	try {
		tested.search(nameMock, "(ou=somevalue)", handlerMock);
		fail("LimitExceededException expected");
	}
	catch (LimitExceededException expected) {
		// expected
	}

       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 testSearch_String_AttributesMapper() throws Exception {
	expectGetReadOnlyContext();

	SearchControls controls = searchControlsOneLevel();
	controls.setReturningObjFlag(false);

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResultWithStringBase(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", 1, attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 3
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_String_AttributesMapper_Default() throws Exception {
	expectGetReadOnlyContext();

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

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResultWithStringBase(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 4
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_AttributesMapper_Default() throws Exception {
	expectGetReadOnlyContext();

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

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResult(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(nameMock, "(ou=somevalue)", attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 5
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_String_CallbackHandler_DirContextProcessor() throws Exception {
	expectGetReadOnlyContext();

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

	SearchResult searchResult = new SearchResult("", new Object(), new BasicAttributes());

	singleSearchResultWithStringBase(controls, searchResult);

	tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", controls, handlerMock, dirContextProcessorMock);

       verify(dirContextProcessorMock).preProcess(dirContextMock);
       verify(dirContextProcessorMock).postProcess(dirContextMock);
       verify(namingEnumerationMock).close();
       verify(handlerMock).handleNameClassPair(searchResult);
       verify(dirContextMock).close();
}
 
Example 6
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_CallbackHandler_DirContextProcessor() throws Exception {
	expectGetReadOnlyContext();

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

	SearchResult searchResult = new SearchResult("", new Object(), new BasicAttributes());

	singleSearchResult(controls, searchResult);

	tested.search(nameMock, "(ou=somevalue)", controls, handlerMock, dirContextProcessorMock);

       verify(dirContextProcessorMock).preProcess(dirContextMock);
       verify(dirContextProcessorMock).postProcess(dirContextMock);
       verify(namingEnumerationMock).close();
       verify(handlerMock).handleNameClassPair(searchResult);
       verify(dirContextMock).close();
}
 
Example 7
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 8
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_Name_SearchControls_AttributesMapper() throws Exception {
	expectGetReadOnlyContext();

	SearchControls controls = searchControlsOneLevel();
	controls.setReturningObjFlag(false);

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResult(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(nameMock, "(ou=somevalue)", controls, attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 9
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSearch_String_SearchControls_AttributesMapper() throws Exception {
	expectGetReadOnlyContext();

	SearchControls controls = searchControlsOneLevel();
	controls.setReturningObjFlag(false);

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResultWithStringBase(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", controls, attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 10
Source File: LdapTemplate.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private SearchControls getDefaultSearchControls(int searchScope, boolean returningObjFlag, String[] attrs) {
	SearchControls controls = new SearchControls();
	controls.setSearchScope(searchScope);
       controls.setTimeLimit(defaultTimeLimit);
       controls.setCountLimit(defaultCountLimit);
	controls.setReturningObjFlag(returningObjFlag);
	controls.setReturningAttributes(attrs);
	return controls;
}
 
Example 11
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_String_SearchControls_ContextMapper_ReturningObjFlagNotSet() throws Exception {
	expectGetReadOnlyContext();

	SearchControls controls = new SearchControls();
	controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

	SearchControls expectedControls = new SearchControls();
	expectedControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
	expectedControls.setReturningObjFlag(true);

	Object expectedObject = new Object();
	SearchResult searchResult = new SearchResult("", expectedObject, new BasicAttributes());

	singleSearchResultWithStringBase(expectedControls, searchResult);

	Object expectedResult = expectedObject;
	when(contextMapperMock.mapFromContext(expectedObject)).thenReturn(expectedResult);

	List list = tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", controls, contextMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 12
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
private SearchControls makeSearchControls()
{
    final SearchControls searchControls = new SearchControls();
    searchControls.setReturningObjFlag( false );
    searchControls.setReturningAttributes( new String[0] );
    searchControls.setSearchScope( searchHelper.getSearchScope().getJndiScopeInt() );
    final String[] returnAttributes = searchHelper.getAttributes() == null
            ? null
            : searchHelper.getAttributes().toArray( new String[searchHelper.getAttributes().size()] );

    searchControls.setReturningAttributes( returnAttributes );
    searchControls.setTimeLimit( searchHelper.getTimeLimit() );
    searchControls.setCountLimit( searchHelper.getMaxResults() );
    return searchControls;
}
 
Example 13
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_String_AttributesMapper_ReturningAttrs() throws Exception {
	expectGetReadOnlyContext();

	String[] attrs = new String[0];
	SearchControls controls = new SearchControls();
	controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
	controls.setReturningObjFlag(false);
	controls.setReturningAttributes(attrs);

	BasicAttributes expectedAttributes = new BasicAttributes();
	SearchResult searchResult = new SearchResult("", null, expectedAttributes);

	singleSearchResultWithStringBase(controls, searchResult);

	Object expectedResult = new Object();
	when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

	List list = tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", 1, attrs, attributesMapperMock);

       verify(namingEnumerationMock).close();
       verify(dirContextMock).close();

       assertThat(list).isNotNull();
	assertThat(list).hasSize(1);
	assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 14
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyThatDefaultSearchControlParametersAreAutomaticallyAppliedInSearch() throws Exception {
    tested.setDefaultSearchScope(SearchControls.ONELEVEL_SCOPE);
    tested.setDefaultCountLimit(5000);
    tested.setDefaultTimeLimit(500);

    expectGetReadOnlyContext();

    SearchControls controls = new SearchControls();
    controls.setReturningObjFlag(false);
    controls.setCountLimit(5000);
    controls.setTimeLimit(500);
    controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);

    BasicAttributes expectedAttributes = new BasicAttributes();
    SearchResult searchResult = new SearchResult("", null, expectedAttributes);

    singleSearchResult(controls, searchResult);

    Object expectedResult = new Object();
    when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expectedResult);

    List list = tested.search(nameMock, "(ou=somevalue)", attributesMapperMock);

    verify(namingEnumerationMock).close();
    verify(dirContextMock).close();

    assertThat(list).isNotNull();
    assertThat(list).hasSize(1);
    assertThat(list.get(0)).isSameAs(expectedResult);
}
 
Example 15
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearch_CallbackHandler_Defaults() throws Exception {
	expectGetReadOnlyContext();

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

	SearchResult searchResult = new SearchResult("", new Object(), new BasicAttributes());

	singleSearchResult(controls, searchResult);

	tested.search(nameMock, "(ou=somevalue)", handlerMock);

       verify(handlerMock).handleNameClassPair(searchResult);
       verify(dirContextMock).close();
}
 
Example 16
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearch_String_CallbackHandler_Defaults() throws Exception {
	expectGetReadOnlyContext();

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

	SearchResult searchResult = new SearchResult("", new Object(), new BasicAttributes());

	singleSearchResultWithStringBase(controls, searchResult);

	tested.search(DEFAULT_BASE_STRING, "(ou=somevalue)", handlerMock);

       verify(handlerMock).handleNameClassPair(searchResult);
       verify(dirContextMock).close();
}
 
Example 17
Source File: LdapAuthenticator.java    From onedev with MIT License 4 votes vote down vote up
private Collection<String> retrieveGroupsByFilter(DirContext ctx, DirContext referralCtx, String userDN) {
	Collection<String> groupNames = new HashSet<>();
	try {
    	SearchGroupsUsingFilter groupRetrieval = (SearchGroupsUsingFilter) getGroupRetrieval();
    	String groupNameAttribute = groupRetrieval.getGroupNameAttribute();
        Name groupSearchBase = new CompositeName().add(groupRetrieval.getGroupSearchBase());
        String groupSearchFilter = StringUtils.replace(groupRetrieval.getGroupSearchFilter(), "{0}", userDN);
        groupSearchFilter = StringUtils.replace(groupSearchFilter, "\\", "\\\\");

        logger.debug("Evaluated group search filter: " + groupSearchFilter);
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        searchControls.setReturningAttributes(new String[]{groupNameAttribute});
        searchControls.setReturningObjFlag(true);

    	NamingEnumeration<SearchResult> results;
        if (referralCtx != null)
            results = referralCtx.search(groupSearchBase, groupSearchFilter, searchControls);
        else
            results = ctx.search(groupSearchBase, groupSearchFilter, searchControls);
        if (results != null) {
            while (results.hasMore()) {
            	SearchResult searchResult = (SearchResult) results.next();
                Attributes searchResultAttributes = searchResult.getAttributes();
                if (searchResultAttributes == null 
                		|| searchResultAttributes.get(groupNameAttribute) == null
                        || searchResultAttributes.get(groupNameAttribute).get() == null) {
                    throw new RuntimeException("Can not find attribute '" 
                    		+ groupNameAttribute + "' in the returned group object.");
                }
                groupNames.add((String) searchResultAttributes.get(groupNameAttribute).get());
            }
        }
       } catch (PartialResultException pre) {
           logger.warn("Partial exception detected. You may try to set property " +
           		"'follow referrals' to true to avoid this exception.", pre);
	} catch (NamingException e) {
		logger.error("Error retrieving groups by filter", e);
	}
	return groupNames;
}
 
Example 18
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
private SearchControls searchControlsRecursive() {
	SearchControls controls = new SearchControls();
	controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
	controls.setReturningObjFlag(true);
	return controls;
}
 
Example 19
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
private SearchControls searchControlsOneLevel() {
	SearchControls controls = new SearchControls();
	controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
	controls.setReturningObjFlag(true);
	return controls;
}
 
Example 20
Source File: LdapEntryPoisoning.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void unsafe2() {
    SearchControls ctrl = new SearchControls();
    ctrl.setReturningObjFlag(true); //!!
}