2016-10-18 74 views
0

我無法使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; 
    } 

} 
+0

鏈接不起作用,無論如何代碼必須在這裏發佈。 – EJP

+0

對不起。只需添加代碼。 – eightmd

回答

-1

似乎沒有人對此有任何意見,但我會告訴你我發現了什麼。如上所述,我嘗試讓VLV在OpenLDAP和apacheDS上工作。當查詢服務器說他們支持控制,但是當你使用它時,你會取回所有的條目,而不是被請求的子集。

由於我的客戶使用的是SunOne LDAP服務器,我想我會試試。 SunOne在21世紀初只有幾年的時間,但直到2011年才被Oracle Directory Service支持。您可以從他們的網站Oracle site for download下載它。查找Oracle Directory Server企業版(11.1.1.7.0)。我相信你需要一個Oracle開發人員登錄。安裝說明在這裏Installation Documentation

Oracle目錄服務是我在有限的搜索中找到的唯一一個LDAP服務器,它正確地支持VLV控件,因爲它有文件記錄。

請注意,服務器會說他們支持VLV,但是當他們也支持分頁搜索時,VLV將無法正確實現。

這就是我發現的,如果我錯誤,我很想聽聽其他LDAP服務器如何支持VLV進行分頁。對於任何人查看這篇文章謝謝!

+0

VLV在我的OpenLDAP上正常工作。當他們支持分頁搜索時,我不知道你的意思。 sssvlv覆蓋層代替OpenLDAP中的分頁結果覆蓋層。 – EJP

+0

那麼我在查詢時使用的OpenLDAP版本說它支持VLV,但是當我給它一個VLV控件時,它將返回所有條目而不是我要求的條目。我不知道sssvlv覆蓋。我沒有安裝這樣的東西,但OpenLDAP也表示它支持分頁結果,並且它能正常工作。 – eightmd