我無法使VLV控件正常工作。我使用ApacheDS 2.0.0並使用Java的JNDI與Sun的ldapbp-1.0.jar進行交談。當查詢服務器說它支持VLV控制時。我沒有收到任何錯誤,但是,我設置了搜索,並找回所有結果而不是子集。我建立了一個新的ApacheDS實例,它有10個用戶。我總是得到全部10個結果。無法使LDAP VirtualListView正常工作
我還得到一個SortResponseControl,並從來沒有VirtualListViewResponseControl。
我見過很多例子,我基本上在做他們自己。這個代碼示例我逐字逐句地改變了連接信息和搜索條件,但仍然遇到同樣的問題。 Post from LDAP Pro forum
一些我已經試過了VirtualListViewControl參數爲:
new VirtualListViewControl(1, 0, 0, 19, Control.CRITICAL); // original - gets all
new VirtualListViewControl(1, 10, 1, 2, Control.CRITICAL); // target offset -gets all
new VirtualListViewControl(20, 3, Control.CRITICAL); // target percentage - gets all
new VirtualListViewControl("Tryit4", 3, Control.CRITICAL); // target value, view size - gets all
new VirtualListViewControl("Tryit4", 2, 1, Control.CRITICAL); // target value, before count, after count
我必須做一些錯誤的,但我看不出它是什麼。任何幫助,將不勝感激。 謝謝
對不起,鏈接不工作。它爲我做了。這裏是我使用的代碼示例,它總是返回一個SortResponseControl和所有的LDAP條目。
/**
*
* VLVJndiClient.java
* Sample code to demostrate how Virtual List View (VLV) Control works.
* Note:
* 1) Note: JNDI Boost package is required for this example to run.
* 2) VLV Control MUST be used in conjunction with Sort Control.
* Otherwise, you will be braced by: [LDAP: error code 60 - VLV Control]
* 3) SunOne Directory Server supports VLV & Microsoft supports VLV since AD2003
*
*/
import java.util.Hashtable;
import java.io.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import com.sun.jndi.ldap.ctl.VirtualListViewControl;
import com.sun.jndi.ldap.ctl.VirtualListViewResponseControl;
import com.sun.jndi.ldap.ctl.SortControl;
public class VLVJndiClientShort
{
static final String VLV_CONTROL_OID = "2.16.840.1.113730.3.4.9";
public static void main(String[] args) throws IOException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://172.16.2.23:10389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS, "test");
try {
/* Create initial context with no connection request controls */
LdapContext ctx = new InitialLdapContext(env, null);
/* Query the server to see if the VLV Control is supported */
if (!isVLVControlSupported(ctx)){
System.out.println(
"The server does not support Virtual List View (VLV) Control.");
System.exit(1);
}
/* Sort Control is required for VLV to work */
SortControl sctl = new SortControl(
new String[]{"cn"}, // sort by cn
Control.CRITICAL
);
/* VLV that returns the first 20 answers 0 to 19 changed 19 to 2 */
VirtualListViewControl vctl =
// new VirtualListViewControl(1, 0, 0, 19, Control.CRITICAL); // original - gets all
// new VirtualListViewControl(1, 10, 1, 2, Control.CRITICAL); // target offset, list size, before count, after count, criticality - gets all
// new VirtualListViewControl(20, 3, Control.CRITICAL); // target percentage, view size, criticality - gets all
// new VirtualListViewControl("Tryit4", 3, Control.CRITICAL); // target value, view size, criticality - gets all
new VirtualListViewControl("Tryit4", 2, 1, Control.CRITICAL); // target value, before count, after count, criticality
/* Set context's request controls */
ctx.setRequestControls(new Control[]{sctl, vctl}); // returns only a sorted control but no VLV control
/* Perform search */
NamingEnumeration answer =
ctx.search("dc=mir3,dc=example,dc=com", "(objectclass=*)", null);
/* Enumerate search results */
while (answer.hasMore()) {
SearchResult si = (SearchResult)answer.next();
System.out.println(si.getName());
}
/* examine the response controls (if any) */
printControls(ctx.getResponseControls());
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
static void printControls(Control[] controls)
{
if(controls == null){
System.out.println("No response controls");
return;
}
for(int j = 0; j < controls.length; j++) {
if(controls[j] instanceof SortResponseControl){
SortResponseControl src = (SortResponseControl)controls[j];
if (src.isSorted()) {
System.out.println("Sorted-Search completed successfully");
} else {
System.out.println(
"Sorted-Search did not complete successfully: error (" +
src.getResultCode() + ") on attribute '" +
src.getAttributeID() + "'");
}
}else if(controls[j] instanceof VirtualListViewResponseControl){
VirtualListViewResponseControl vlv =
(VirtualListViewResponseControl)controls[j];
if (vlv.getResultCode() == 0) {
System.out.println("Sorted-View completed successfully");
System.out.println("TargetOffset: " + vlv.getTargetOffset());
System.out.println("ListSize: " + vlv.getListSize());
} else {
System.out.println("Sorted-View did not complete successfully: "
+ vlv.getResultCode());
}
} else {
System.out.println("Received control: "+ controls[j].getID());
}
}
}
/**
* Is VLV Control supported?
*
* Query the rootDSE object to find out if VLV Control
* is supported.
*/
static boolean isVLVControlSupported(LdapContext ctx)
throws NamingException
{
SearchControls ctl = new SearchControls();
ctl.setReturningAttributes(new String[]{"supportedControl"});
ctl.setSearchScope(SearchControls.OBJECT_SCOPE);
/* search for the rootDSE object */
NamingEnumeration results = ctx.search("", "(objectClass=*)", ctl);
while(results.hasMore()){
SearchResult entry = (SearchResult)results.next();
NamingEnumeration attrs = entry.getAttributes().getAll();
while (attrs.hasMore()){
Attribute attr = (Attribute)attrs.next();
NamingEnumeration vals = attr.getAll();
while (vals.hasMore()){
String value = (String) vals.next();
if (value.equals(VLV_CONTROL_OID))
return true;
}
}
}
return false;
}
}
鏈接不起作用,無論如何代碼必須在這裏發佈。 – EJP
對不起。只需添加代碼。 – eightmd