📄 dsmlcontext.java
字号:
* @param attrIds the identifiers of the attributes to retrieve.
* null indicates that all attributes should be retrieved;
* an empty array indicates that none should be retrieved.
* @return the requested attributes; never null
* @throws javax.naming.NamingException if a naming exception is encountered
*/
public Attributes getAttributes(Name name, String[] attrIds) throws NamingException
{
return getAttributes(name.toString(), attrIds);
}
/**
* Creates and binds a new context, along with associated attributes.
* See {@link #createSubcontext(javax.naming.Name, javax.naming.directory.Attributes)} for details.
*
* @param name the name of the context to create; may not be empty
* @param attrs the attributes to associate with the newly created context
* @return the newly created context
* @throws javax.naming.NameAlreadyBoundException if the name is already bound
* @throws javax.naming.directory.InvalidAttributesException if <code>attrs</code> does not
* contain all the mandatory attributes required for creation
* @throws javax.naming.NamingException if a naming exception is encountered
*/
public DirContext createSubcontext(String name, Attributes attrs) throws NamingException
{
log.finest("createSubcontext (" + name.toString() + ")");
// construct XML
StringBuffer addRequestBuffer = constructAddRequest(name, attrs);
// send XML to server
String response = sendDSMLRequest(addRequestBuffer);
// parse response XML
parseAddResponse(response);
// return new context with new name?
return new DsmlContext(name, environment);
}
void parseAddResponse(String response)
throws NamingException
{
checkForError(response);
// quick check that it really was a modify response...
if (response.indexOf("addResponse>") == -1)
throw new NamingException("Unexpected DSML Response to Add Request:\n " + response);
}
/**
* Creates and binds a new context, along with associated attributes.
* This method creates a new subcontext with the given name, binds it in
* the target context (that named by all but terminal atomic
* component of the name), and associates the supplied attributes
* with the newly created object.
* All intermediate and target contexts must already exist.
* If <tt>attrs</tt> is null, this method is equivalent to
* <tt>Context.createSubcontext()</tt>.
*
* @param name the name of the context to create; may not be empty
* @param attrs the attributes to associate with the newly created context
* @return the newly created context
* @throws javax.naming.NameAlreadyBoundException if the name is already bound
* @throws javax.naming.directory.InvalidAttributesException if <code>attrs</code> does not
* contain all the mandatory attributes required for creation
* @throws javax.naming.NamingException if a naming exception is encountered
* @see javax.naming.Context#createSubcontext(javax.naming.Name)
*/
public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException
{
return createSubcontext(name.toString(), attrs);
}
/**
* Searches in a single context for objects that contain a
* specified set of attributes, and retrieves selected attributes.
* See {@link #search(javax.naming.Name, javax.naming.directory.Attributes, String[])} for details.
*
* @param name the name of the context to search
* @param matchingAttributes the attributes to search for
* @param attributesToReturn the attributes to return
* @return a non-null enumeration of <tt>SearchResult</tt> objects
* @throws javax.naming.NamingException if a naming exception is encountered
*/
public NamingEnumeration search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException
{
// create a search filter based on the matchingAttributes list.
String filter;
filter = getAttributeMatchFilter(matchingAttributes);
// TODO search context crap: alias handling, time limits etc.
return doDsmlSearch(name, WHOLESUBTREE, SEARCHING, 0, 0, false, filter, attributesToReturn);
}
/**
* Searches in a single context for objects that contain a
* specified set of attributes, and retrieves selected attributes.
* The search is performed using the default
* <code>SearchControls</code> settings.
* <p/>
* For an object to be selected, each attribute in
* <code>matchingAttributes</code> must match some attribute of the
* object. If <code>matchingAttributes</code> is empty or
* null, all objects in the target context are returned.
* <p/>
* An attribute <em>A</em><sub>1</sub> in
* <code>matchingAttributes</code> is considered to match an
* attribute <em>A</em><sub>2</sub> of an object if
* <em>A</em><sub>1</sub> and <em>A</em><sub>2</sub> have the same
* identifier, and each value of <em>A</em><sub>1</sub> is equal
* to some value of <em>A</em><sub>2</sub>. This implies that the
* order of values is not significant, and that
* <em>A</em><sub>2</sub> may contain "extra" values not found in
* <em>A</em><sub>1</sub> without affecting the comparison. It
* also implies that if <em>A</em><sub>1</sub> has no values, then
* testing for a match is equivalent to testing for the presence
* of an attribute <em>A</em><sub>2</sub> with the same
* identifier.
* <p/>
* The precise definition of "equality" used in comparing attribute values
* is defined by the underlying directory service. It might use the
* <code>Object.equals</code> method, for example, or might use a schema
* to specify a different equality operation.
* For matching based on operations other than equality (such as
* substring comparison) use the version of the <code>search</code>
* method that takes a filter argument.
* <p/>
* When changes are made to this <tt>DirContext</tt>,
* the effect on enumerations returned by prior calls to this method
* is undefined.
* <p/>
* If the object does not have the attribute
* specified, the directory will ignore the nonexistent attribute
* and return the requested attributes that the object does have.
* <p/>
* A directory might return more attributes than was requested
* (see <strong>Attribute Type Names</strong> in the class description),
* but is not allowed to return arbitrary, unrelated attributes.
* <p/>
* See also <strong>Operational Attributes</strong> in the class
* description.
*
* @param name the name of the context to search
* @param matchingAttributes the attributes to search for. If empty or null,
* all objects in the target context are returned.
* @param attributesToReturn the attributes to return. null indicates that
* all attributes are to be returned;
* an empty array indicates that none are to be returned.
* @return a non-null enumeration of <tt>SearchResult</tt> objects.
* Each <tt>SearchResult</tt> contains the attributes
* identified by <code>attributesToReturn</code>
* and the name of the corresponding object, named relative
* to the context named by <code>name</code>.
* @throws javax.naming.NamingException if a naming exception is encountered
* @see javax.naming.directory.SearchControls
* @see javax.naming.directory.SearchResult
* @see #search(javax.naming.Name, String, Object[], javax.naming.directory.SearchControls)
*/
public NamingEnumeration search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException
{
return search(name.toString(), matchingAttributes, attributesToReturn);
}
/**
* This takes a list of Attributes and produces a 'present' match filter based on them.
*
* @param matchingAttributes a list of attributes required to be present (e.g. 'person', 'favoriteDrink')
* @return the ldap filter for matching the attributes; e.g. (&(person=*)(favoriteDrink=*))
*/
static String getAttributeMatchFilter(Attributes matchingAttributes)
{
String filter;
int numbAtts = (matchingAttributes == null) ? 0 : matchingAttributes.size();
if (numbAtts == 0)
{
filter = "(objectClass=*)";
}
else
{
StringBuffer filterBuffer = new StringBuffer();
if (numbAtts > 1)
filterBuffer.append("(&");
Enumeration atts = matchingAttributes.getIDs(); // ignore NamingException, as these atts come from the client, not the server
while (atts.hasMoreElements())
{
String att = (String) atts.nextElement();
filterBuffer.append("(").append(att).append("=*)");
}
if (numbAtts > 1)
filterBuffer.append(")");
filter = filterBuffer.toString();
}
return filter;
}
/**
* Searches in the named context or object for entries that satisfy the
* given search filter. Performs the search as specified by
* the search controls.
* <p/>
* See {@link #search(javax.naming.Name, String, javax.naming.directory.SearchControls)} for details.
*
* @param name the name of the context or object to search
* @param filter the filter expression to use for the search; may not be null
* @param cons the search controls that control the search. If null,
* the default search controls are used (equivalent
* to <tt>(new SearchControls())</tt>).
* @return an enumeration of <tt>SearchResult</tt>s for
* the objects that satisfy the filter.
* @throws javax.naming.directory.InvalidSearchFilterException if the search filter specified is
* not supported or understood by the underlying directory
* @throws javax.naming.directory.InvalidSearchControlsException if the search controls
* contain invalid settings
* @throws javax.naming.NamingException if a naming exception is encountered
*/
public NamingEnumeration search(String name, String filter, SearchControls cons) throws NamingException
{
return search(name, filter, null, cons); //To change body of implemented methods use File | Settings | File Templates.
}
/**
* Searches in the named context or object for entries that satisfy the
* given search filter. Performs the search as specified by
* the search controls.
* <p/>
* The format and interpretation of <code>filter</code> follows RFC 2254
* with the
* following interpretations for <code>attr</code> and <code>value</code>
* mentioned in the RFC.
* <p/>
* <code>attr</code> is the attribute's identifier.
* <p/>
* <code>value</code> is the string representation the attribute's value.
* The translation of this string representation into the attribute's value
* is directory-specific.
* <p/>
* For the assertion "someCount=127", for example, <code>attr</code>
* is "someCount" and <code>value</code> is "127".
* The provider determines, based on the attribute ID ("someCount")
* (and possibly its schema), that the attribute's value is an integer.
* It then parses the string "127" appropriately.
* <p/>
* Any non-ASCII characters in the filter string should be
* represented by the appropriate Java (Unicode) characters, and
* not encoded as UTF-8 octets. Alternately, the
* "backslash-hexcode" notation described in RFC 2254 may be used.
* <p/>
* If the directory does not support a string representation of
* some or all of its attributes, the form of <code>search</code> that
* accepts filter arguments in the form of Objects can be used instead.
* The service provider for such a directory would then translate
* the filter arguments to its service-specific representation
* for filter evaluation.
* See <code>search(Name, String, Object[], SearchControls)</code>.
* <p/>
* RFC 2254 defines certain operators for the filter, including substring
* matches, equality, approximate match, greater than, less than. These
* operators are mapped to operators with corresponding semantics in the
* underlying directory. For example, for the equals operator, suppose
* the directory has a matching rule defining "equality" of the
* attributes in the filter. This rule would be used for checking
* equality of the attributes specified in the filter with the attributes
* of objects in the directory. Similarly, if the directory has a
* matching rule for ordering, this rule would be used for
* making "greater than" and "less than" comparisons.
* <p/>
* Not all of the operators defined in RFC 2254 are applicable to all
* attributes. When an operator is not applicable, the exception
* <code>InvalidSearchFilterException</code> is thrown.
* <p/>
* The result is returned in an enumeration of <tt>SearchResult</tt>s.
* Each <tt>SearchResult</tt> contains the name of the object
* and other information about the object (see SearchResult).
* The name is either relative to the target context of the search
* (which is named by the <code>name</code> parameter), or
* it is a URL string. If the target context is included in
* the enumeration (as is possible when
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -