下面列出了javax.naming.directory.SearchControls#ONELEVEL_SCOPE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ldapConnection(String _server){
// defaults
scope = SearchControls.ONELEVEL_SCOPE;
maxEntries = 0; // translated as no limit when sent to server
modifyType = MODIFY_REPLACE;
separator = ',';
searchbase = null;
filter = null;
server = _server;
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
if ( _server.indexOf(':') == -1 ){
env.put(Context.PROVIDER_URL, "ldap://" + _server + ":389");
}else{
env.put(Context.PROVIDER_URL, "ldap://" + _server);
}
env.put("java.naming.ldap.referral.limit", "0");
env.put(Context.REFERRAL, "follow");
}
/**
* 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;
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public void setSearchOneLevelScope() {
this.searchScope = SearchControls.ONELEVEL_SCOPE;
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_SCOPE);
}
public ContextEnumerator(Context context, int scope)
throws NamingException {
// return this object except when searching single-level
this(context, scope, "", scope != SearchControls.ONELEVEL_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;
}
@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);
}
}
@Test
public void testSearchScopeOneLevelScopeSetInConstructorIsUsed() throws Exception {
DefaultDirContextValidator tested = new DefaultDirContextValidator(SearchControls.ONELEVEL_SCOPE);
assertThat(tested.getSearchControls().getSearchScope()).as("ONELEVEL_SCOPE, ").isEqualTo(SearchControls.ONELEVEL_SCOPE);
}