下面列出了javax.naming.directory.SearchControls#SUBTREE_SCOPE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Method that calls the actual search on the jndi context.
*
* @param searchbase the domain name (relative to initial context in ldap) to search from.
* @param filter the non-null filter to use for the search
* @param scope the scope level of the search, one off "SearchControls.ONELEVEL_SCOPE
* @param limit the maximum number of results to return
* @param timeout the maximum time to wait before abandoning the search
* @param returnAttributes an array of strings containing the names of attributes to search. (null = all, empty array = none)
* @return
* @throws NamingException
*/
public NamingEnumeration search(String searchbase, String filter,
String[] returnAttributes, int scope, int limit, int timeout)
throws NamingException {
SearchControls constraints = new SearchControls();
if (SearchControls.ONELEVEL_SCOPE == scope) {
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else if (SearchControls.SUBTREE_SCOPE == scope) {
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
} else if (SearchControls.OBJECT_SCOPE == scope) {
constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
} else {
throw new NamingException("Unknown search scope: " + scope);
}
if (returnAttributes != null && returnAttributes.length == 0)
returnAttributes = new String[] { "objectClass" };
constraints.setCountLimit(limit);
constraints.setTimeLimit(timeout);
constraints.setReturningAttributes(returnAttributes);
Name n = new CompositeName().add(searchbase);
NamingEnumeration results = ctx.search(n, filter, constraints);
return results;
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
private void prepNextChild() throws NamingException {
if(hasMoreChildren()) {
try {
currentChild = getNextChild();
currentReturned = false;
} catch (NamingException e){
if (debug) System.out.println(e);
if (debug) e.printStackTrace();
}
} else {
currentChild = null;
return;
}
if(scope == SearchControls.SUBTREE_SCOPE &&
currentChild.getObject() instanceof Context) {
currentChildEnum = newEnumerator(
(Context)(currentChild.getObject()),
scope, currentChild.getName(),
false);
currentChildExpanded = true;
if(debug) {System.out.println("prepNextChild: expanded");}
} else {
currentChildExpanded = false;
currentChildEnum = null;
if(debug) {System.out.println("prepNextChild: normal");}
}
}
@Override
protected AuthenticationInfo queryForAuthenticationInfo(AuthenticationToken token, LdapContextFactory ldapContextFactory) throws NamingException {
final GreenStepBaseUsernamePasswordToken usernamePasswordToken = (GreenStepBaseUsernamePasswordToken) token;
LdapContext ctx = null;
/*
try {
ctx = ldapContextFactory.getSystemLdapContext();
final String attribName = "userPrincipalName";
final SearchControls searchControls = new SearchControls(SearchControls.SUBTREE_SCOPE, 1, 0, new String[] { attribName }, false, false);
final NamingEnumeration<SearchResult> search = ctx.search(searchBase, this.getCustomQueryAttributeValue(), new Object[] { usernamePasswordToken.getPrincipal() }, searchControls);
if (search.hasMore()) {
final SearchResult next = search.next();
String loginUser= next.getAttributes().get(attribName).get().toString();
if (search.hasMore()) {
throw new RuntimeException("More than one user matching: "+usernamePasswordToken.getPrincipal());
} else {
try {
ldapContextFactory.getLdapContext(loginUser, usernamePasswordToken.getPassword());
} catch (Exception ex) {
throw ex;
}
}
}
else {
throw new RuntimeException("No user matching: " + usernamePasswordToken.getPrincipal());
}
} catch (NamingException ne) {
throw ne;
} finally {
LdapUtils.closeContext(ctx);
}
*/
String searchBaseArr[] = StringUtils.defaultString(searchBase).split( Constants.ID_DELIMITER );
boolean searchUser = false;
for (int i = 0; searchBaseArr != null && !searchUser && i<searchBaseArr.length; i++) {
try {
ctx = ldapContextFactory.getSystemLdapContext();
final String attribName = "userPrincipalName";
final SearchControls searchControls = new SearchControls(SearchControls.SUBTREE_SCOPE, 1, 0, new String[] { attribName }, false, false);
final NamingEnumeration<SearchResult> search = ctx.search(searchBaseArr[i], this.getCustomQueryAttributeValue(), new Object[] { usernamePasswordToken.getPrincipal() }, searchControls);
if (search.hasMore()) {
searchUser = true;
final SearchResult next = search.next();
String loginUser= next.getAttributes().get(attribName).get().toString();
if (search.hasMore()) {
throw new RuntimeException("More than one user matching: "+usernamePasswordToken.getPrincipal());
} else {
try {
ldapContextFactory.getLdapContext(loginUser, usernamePasswordToken.getPassword());
} catch (Exception ex) {
throw ex;
}
}
}
/*
else {
throw new RuntimeException("No user matching: " + usernamePasswordToken.getPrincipal());
}
*/
} catch (NamingException ne) {
throw ne;
} finally {
LdapUtils.closeContext(ctx);
}
}
if (!searchUser) {
throw new RuntimeException("No user matching: " + usernamePasswordToken.getPrincipal());
}
return buildAuthenticationInfo(usernamePasswordToken.getUsername(), usernamePasswordToken.getPassword());
}
public void setSearchSubTreeScope() {
this.searchScope = SearchControls.SUBTREE_SCOPE;
}
@Override
public void configure(String id, Properties properties) throws PIPException {
/*
* Handle the standard properties
*/
super.configure(id, properties);
String propertyPrefix = id + ".";
/*
* Configure the LDAP environment: I think the only required property is the provider_url
*/
if (!this.configureStringProperty(propertyPrefix, Context.PROVIDER_URL, properties, null)) {
throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No " + propertyPrefix + Context.PROVIDER_URL);
}
this.configureStringProperty(propertyPrefix, Context.AUTHORITATIVE, properties, null);
this.configureIntegerProperty(propertyPrefix, Context.BATCHSIZE, properties, null);
this.configureStringProperty(propertyPrefix, Context.DNS_URL, properties, null);
this.configureStringProperty(propertyPrefix, Context.INITIAL_CONTEXT_FACTORY, properties, DEFAULT_CONTEXT_FACTORY);
this.configureStringProperty(propertyPrefix, Context.LANGUAGE, properties, null);
this.configureStringProperty(propertyPrefix, Context.OBJECT_FACTORIES, properties, null);
this.configureStringProperty(propertyPrefix, Context.REFERRAL, properties, null);
this.configureStringProperty(propertyPrefix, Context.SECURITY_AUTHENTICATION, properties, null);
this.configureStringProperty(propertyPrefix, Context.SECURITY_CREDENTIALS, properties, null);
this.configureStringProperty(propertyPrefix, Context.SECURITY_PRINCIPAL, properties, null);
this.configureStringProperty(propertyPrefix, Context.SECURITY_PROTOCOL, properties, null);
this.configureStringProperty(propertyPrefix, Context.STATE_FACTORIES, properties, null);
this.configureStringProperty(propertyPrefix, Context.URL_PKG_PREFIXES, properties, null);
String ldapScopeValue = properties.getProperty(propertyPrefix + PROP_LDAP_SCOPE, DEFAULT_SCOPE);
if (LDAP_SCOPE_SUBTREE.equals(ldapScopeValue)) {
this.ldapScope = SearchControls.SUBTREE_SCOPE;
} else if (LDAP_SCOPE_OBJECT.equals(ldapScopeValue)) {
this.ldapScope = SearchControls.OBJECT_SCOPE;
} else if (LDAP_SCOPE_ONELEVEL.equals(ldapScopeValue)) {
this.ldapScope = SearchControls.ONELEVEL_SCOPE;
} else {
this.logger.warn("Invalid LDAP Scope value {}: using {}", ldapScopeValue, DEFAULT_SCOPE);
this.ldapScope = SearchControls.SUBTREE_SCOPE;
}
/*
* Get list of resolvers defined for this LDAP Engine
*/
String resolversList = properties.getProperty(propertyPrefix + PROP_RESOLVERS);
if (resolversList == null || resolversList.isEmpty()) {
throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No " + propertyPrefix + PROP_RESOLVERS);
}
/*
* Iterate the resolvers
*/
for (String resolver : Splitter.on(',').trimResults().omitEmptyStrings().split(resolversList)) {
/*
* Get the LDAPResolver for this LDAPEngine
*/
String resolverClassName = properties.getProperty(propertyPrefix + PROP_RESOLVER + "." + resolver + ".classname");
if (resolverClassName == null) {
throw new PIPException("Invalid configuration for " + this.getClass().getName() + ": No " + propertyPrefix + PROP_RESOLVER + "." + resolver + ".classname");
}
LDAPResolver ldapResolverNew = null;
try {
Class<?> classResolver = Class.forName(resolverClassName);
if (!LDAPResolver.class.isAssignableFrom(classResolver)) {
throw new PIPException("LDAPResolver class " + resolverClassName + " does not implement " + LDAPResolver.class.getCanonicalName());
}
ldapResolverNew = LDAPResolver.class.cast(classResolver.newInstance());
} catch (Exception ex) {
throw new PIPException("Exception instantiating LDAPResolver for class '" + resolverClassName + "'", ex);
}
assert(ldapResolverNew != null);
ldapResolverNew.configure(propertyPrefix + PROP_RESOLVER + "." + resolver, properties, this.getIssuer());
this.ldapResolvers.add(ldapResolverNew);
}
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
/**
*
*/
public LdapUtils() {
super();
this.searchScope = SearchControls.SUBTREE_SCOPE;
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}
public static Node fromXML(TransformationGraph graph, Element nodeXML) throws XMLConfigurationException, AttributeNotFoundException {
ComponentXMLAttributes xattribs = new ComponentXMLAttributes(nodeXML, graph);
LdapReader aLdapReader = null;
int i_scope = SearchControls.OBJECT_SCOPE;
String scope = xattribs.getString(XML_SCOPE_ATTRIBUTE, null);
String sMsg = null;
if (scope == null) {
sMsg = "Missing scope specification";
} else if(scope.equalsIgnoreCase("OBJECT")) {
// i_scope = SearchControls.OBJECT_SCOPE; // default value
} else if(scope.equalsIgnoreCase("ONELEVEL")) {
i_scope = SearchControls.ONELEVEL_SCOPE;
} else if(scope.equalsIgnoreCase("SUBTREE")) {
i_scope = SearchControls.SUBTREE_SCOPE;
} else {
sMsg = "Invalid scope specification \"" + scope + "\"";
}
if (sMsg != null) {
StringBuffer msg = new StringBuffer();
msg.append(sMsg);
msg.append(" in component ").append(xattribs.getString(Node.XML_ID_ATTRIBUTE, "unknown ID"));
msg.append("; defaulting to scope \"OBJECT\"");
logger.warn(msg.toString());
}
if(xattribs.exists(XML_USER_ATTRIBUTE) && xattribs.exists(XML_PASSWORD_ATTRIBUTE) ) {
aLdapReader = new LdapReader(
xattribs.getString(Node.XML_ID_ATTRIBUTE),
xattribs.getStringEx(XML_LDAPURL_ATTRIBUTE, null, RefResFlag.URL),
xattribs.getString(XML_BASE_ATTRIBUTE, null),
xattribs.getString(XML_FILTER_ATTRIBUTE, null),
i_scope,
xattribs.getString(XML_USER_ATTRIBUTE),
xattribs.getStringEx(XML_PASSWORD_ATTRIBUTE, RefResFlag.PASSWORD));
} else {
aLdapReader = new LdapReader(
xattribs.getString(Node.XML_ID_ATTRIBUTE),
xattribs.getStringEx(XML_LDAPURL_ATTRIBUTE, null, RefResFlag.URL),
xattribs.getString(XML_BASE_ATTRIBUTE, null),
xattribs.getString(XML_FILTER_ATTRIBUTE, null),
i_scope);
}
if (xattribs.exists(XML_MULTI_VALUE_SEPARATOR_ATTRIBUTE)) {
aLdapReader.setMultiValueSeparator(xattribs.getString(XML_MULTI_VALUE_SEPARATOR_ATTRIBUTE));
}
if (xattribs.exists(XML_ALIAS_HANDLING_ATTRIBUTE)) {
aLdapReader.setAliasHandling(Enum.valueOf(AliasHandling.class, xattribs.getString(XML_ALIAS_HANDLING_ATTRIBUTE)));
}
if (xattribs.exists(XML_REFERRAL_HANDLING_ATTRIBUTE)) {
aLdapReader.setReferralHandling(Enum.valueOf(ReferralHandling.class, xattribs.getString(XML_REFERRAL_HANDLING_ATTRIBUTE)));
}
if (xattribs.exists(XML_DEFAULT_MAPPING_FIELD)){
aLdapReader.setDefaultMappingField(xattribs.getString(XML_DEFAULT_MAPPING_FIELD));
}
if (xattribs.exists(XML_PAGE_SIZE)){
aLdapReader.setPageSize(xattribs.getInteger(XML_PAGE_SIZE));
}
if (xattribs.exists(XML_ALL_LDAP_ATTRIBUTES)){
aLdapReader.setAllAttributes(xattribs.getBoolean(XML_ALL_LDAP_ATTRIBUTES));
}
if (xattribs.exists(XML_ADDITIONAL_BINARY_ATTRIBUTES)){
aLdapReader.setAdditionalBinaryAttributes(xattribs.getString(XML_ADDITIONAL_BINARY_ATTRIBUTES));
}
if (xattribs.exists(XML_ADDITIONAL_LDAP_ENV)){
aLdapReader.setLdapExtraPropertiesDef(xattribs.getString(XML_ADDITIONAL_LDAP_ENV));
}
return aLdapReader;
}
@Test
public void testSearchScopeSubTreeScopeSetInConstructorIsUsed() throws Exception {
DefaultDirContextValidator tested = new DefaultDirContextValidator(SearchControls.SUBTREE_SCOPE);
assertThat(tested.getSearchControls().getSearchScope()).as("SUBTREE_SCOPE, ").isEqualTo(SearchControls.SUBTREE_SCOPE);
}
public ContextEnumerator(Context context) throws NamingException {
this(context, SearchControls.SUBTREE_SCOPE);
}