Java 类javax.naming.ldap.SortControl 实例源码

项目:teiid    文件:LDAPQueryExecution.java   
/** 
 * Set the standard request controls
 */
private void setRequestControls(byte[] cookie) throws TranslatorException {
    List<Control> ctrl = new ArrayList<Control>();
    SortKey[] keys = searchDetails.getSortKeys();
    try {           
        if (keys != null) {
            ctrl.add(new SortControl(keys, Control.NONCRITICAL));
        }
        if (this.executionFactory.usePagination()) {
            ctrl.add(new PagedResultsControl(this.executionContext.getBatchSize(), cookie, Control.CRITICAL));
        }
        if (!ctrl.isEmpty()) {
            this.ldapCtx.setRequestControls(ctrl.toArray(new Control[ctrl.size()]));
            LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Sort/pagination controls were created successfully."); //$NON-NLS-1$
        }
    } catch (NamingException ne) {
           final String msg = LDAPPlugin.Util.getString("LDAPSyncQueryExecution.setControlsError") +  //$NON-NLS-1$
           " : "+ne.getExplanation(); //$NON-NLS-1$
        throw new TranslatorException(ne, msg);
    } catch(IOException e) {
        throw new TranslatorException(e);
    }
}
项目:cn1    文件:LdapContextServerMockedTest.java   
public void testConnectControls2() throws Exception {
    // set connect controls by property "java.naming.ldap.control.connect"
    env.put("java.naming.ldap.control.connect",
            new Control[] { new SortControl("", Control.NONCRITICAL) });

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    InitialDirContext initialDirContext = new InitialDirContext(env);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_SEARCH_RESULT_DONE,
            new EncodableLdapResult(), null) });
    LdapContext context = (LdapContext) initialDirContext.lookup("");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    Control c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, c.isCritical());
}
项目:cn1    文件:LdapContextServerMockedTest.java   
public void testConnectControls3() throws Exception {
    // set connect controls by InitialLdapContext
    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    InitialLdapContext initialDirContext = new InitialLdapContext(env,
            new Control[] { new SortControl("", Control.NONCRITICAL) });

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_SEARCH_RESULT_DONE,
            new EncodableLdapResult(), null) });
    LdapContext context = (LdapContext) initialDirContext.lookup("");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    Control c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, c.isCritical());

}
项目:freeVM    文件:LdapContextServerMockedTest.java   
public void testConnectControls2() throws Exception {
    // set connect controls by property "java.naming.ldap.control.connect"
    env.put("java.naming.ldap.control.connect",
            new Control[] { new SortControl("", Control.NONCRITICAL) });

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    InitialDirContext initialDirContext = new InitialDirContext(env);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_SEARCH_RESULT_DONE,
            new EncodableLdapResult(), null) });
    LdapContext context = (LdapContext) initialDirContext.lookup("");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    Control c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, c.isCritical());
}
项目:freeVM    文件:LdapContextServerMockedTest.java   
public void testConnectControls3() throws Exception {
    // set connect controls by InitialLdapContext
    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    InitialLdapContext initialDirContext = new InitialLdapContext(env,
            new Control[] { new SortControl("", Control.NONCRITICAL) });

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_SEARCH_RESULT_DONE,
            new EncodableLdapResult(), null) });
    LdapContext context = (LdapContext) initialDirContext.lookup("");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    Control c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, c.isCritical());

}
项目:cn1    文件:SortControlTest.java   
/**
 * @tests isCritical()
 */
public void testSortControlStringArrayBoolean002() throws Exception {
    assertTrue(new SortControl("", true).isCritical());
    assertFalse(new SortControl("", false).isCritical());

    assertTrue(new SortControl(new String[0], true).isCritical());
    assertFalse(new SortControl(new String[0], false).isCritical());

    assertTrue(new SortControl(new SortKey[0], true).isCritical());
    assertFalse(new SortControl(new SortKey[0], false).isCritical());

}
项目:cn1    文件:LdapContextImplTest.java   
public void test_reconnect() throws Exception {
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env
    .put("java.naming.ldap.control.connect",
            new Control[] { new PagedResultsControl(10,
                    Control.NONCRITICAL) });

    MockLdapClient client = new MockLdapClient();
    context = new LdapContextImpl(client, env, "cn=test");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    Control c = controls[0];
    assertTrue(c instanceof PagedResultsControl);
    assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c).isCritical());

    context.reconnect(new Control[] { new SortControl("",
            Control.NONCRITICAL) });

    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());

    context.reconnect(null);

    assertNull(context.getConnectControls());
}
项目:cn1    文件:LdapContextServerMockedTest.java   
public void testnewInstance() throws Exception {
    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    InitialLdapContext initialDirContext = new InitialLdapContext(env, null);
    Control[] reqCtls = initialDirContext.getRequestControls();
    assertEquals(1, reqCtls.length);
    assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
    assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    LdapContext context = initialDirContext
            .newInstance(new Control[] { new SortControl("",
                    Control.NONCRITICAL) });

    assertNotSame(initialDirContext, context);
    reqCtls = context.getRequestControls();
    assertEquals(2, reqCtls.length);
    Control control = reqCtls[0];
    if (control instanceof SortControl) {
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
    } else {
        assertEquals("2.16.840.1.113730.3.4.2", control.getID());
        assertEquals(Control.NONCRITICAL, control.isCritical());
        assertTrue(reqCtls[1] instanceof SortControl);
        assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
    }

}
项目:freeVM    文件:SortControlTest.java   
/**
 * @tests isCritical()
 */
public void testSortControlStringArrayBoolean002() throws Exception {
    assertTrue(new SortControl("", true).isCritical());
    assertFalse(new SortControl("", false).isCritical());

    assertTrue(new SortControl(new String[0], true).isCritical());
    assertFalse(new SortControl(new String[0], false).isCritical());

    assertTrue(new SortControl(new SortKey[0], true).isCritical());
    assertFalse(new SortControl(new SortKey[0], false).isCritical());

}
项目:freeVM    文件:SortControlTest.java   
/**
 * @tests isCritical()
 */
public void testSortControlStringArrayBoolean002() throws Exception {
    assertTrue(new SortControl("", true).isCritical());
    assertFalse(new SortControl("", false).isCritical());

    assertTrue(new SortControl(new String[0], true).isCritical());
    assertFalse(new SortControl(new String[0], false).isCritical());

    assertTrue(new SortControl(new SortKey[0], true).isCritical());
    assertFalse(new SortControl(new SortKey[0], false).isCritical());

}
项目:freeVM    文件:LdapContextImplTest.java   
public void test_reconnect() throws Exception {
    Hashtable<Object, Object> env = new Hashtable<Object, Object>();
    env
    .put("java.naming.ldap.control.connect",
            new Control[] { new PagedResultsControl(10,
                    Control.NONCRITICAL) });

    MockLdapClient client = new MockLdapClient();
    context = new LdapContextImpl(client, env, "cn=test");

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    Control c = controls[0];
    assertTrue(c instanceof PagedResultsControl);
    assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c).isCritical());

    context.reconnect(new Control[] { new SortControl("",
            Control.NONCRITICAL) });

    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());

    context.reconnect(null);

    assertNull(context.getConnectControls());
}
项目:freeVM    文件:LdapContextServerMockedTest.java   
public void testnewInstance() throws Exception {
    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    InitialLdapContext initialDirContext = new InitialLdapContext(env, null);
    Control[] reqCtls = initialDirContext.getRequestControls();
    assertEquals(1, reqCtls.length);
    assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
    assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    LdapContext context = initialDirContext
            .newInstance(new Control[] { new SortControl("",
                    Control.NONCRITICAL) });

    assertNotSame(initialDirContext, context);
    reqCtls = context.getRequestControls();
    assertEquals(2, reqCtls.length);
    Control control = reqCtls[0];
    if (control instanceof SortControl) {
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
    } else {
        assertEquals("2.16.840.1.113730.3.4.2", control.getID());
        assertEquals(Control.NONCRITICAL, control.isCritical());
        assertTrue(reqCtls[1] instanceof SortControl);
        assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
    }

}
项目:spring-ldap    文件:SortControlDirContextProcessorTest.java   
@Test
public void testCreateRequestControl() throws Exception {
    SortControl result = (SortControl) tested.createRequestControl();
    assertThat(result).isNotNull();
    assertThat(result.getID()).isEqualTo("1.2.840.113556.1.4.473");
    assertThat(result.getEncodedValue().length).isEqualTo(9);
}
项目:camunda-bpm-platform    文件:LdapIdentityProviderSession.java   
protected void applyRequestControls(AbstractQuery<?, ?> query) {

    try {
      List<Control> controls = new ArrayList<Control>();

      List<QueryOrderingProperty> orderBy = query.getOrderingProperties();
      if(orderBy != null) {
        for (QueryOrderingProperty orderingProperty : orderBy) {
          String propertyName = orderingProperty.getQueryProperty().getName();
          if(UserQueryProperty.USER_ID.getName().equals(propertyName)) {
            controls.add(new SortControl(ldapConfiguration.getUserIdAttribute(), Control.CRITICAL));

          } else if(UserQueryProperty.EMAIL.getName().equals(propertyName)) {
            controls.add(new SortControl(ldapConfiguration.getUserEmailAttribute(), Control.CRITICAL));

          } else if(UserQueryProperty.FIRST_NAME.getName().equals(propertyName)) {
            controls.add(new SortControl(ldapConfiguration.getUserFirstnameAttribute(), Control.CRITICAL));

          } else if(UserQueryProperty.LAST_NAME.getName().equals(propertyName)) {
            controls.add(new SortControl(ldapConfiguration.getUserLastnameAttribute(), Control.CRITICAL));
          }
        }
      }

      initialContext.setRequestControls(controls.toArray(new Control[0]));

    } catch (Exception e) {
      throw new IdentityProviderException("Exception while setting paging settings", e);
    }
  }
项目:Openfire    文件:LdapUserTester.java   
/**
 * Returns a list of usernames with a sample of the users found in LDAP.
 *
 * @param maxSample the max size of the sample to return.
 * @return a list of usernames with a sample of the users found in LDAP.
 * @throws NamingException if something goes wrong....
 */
public List<String> getSample(int maxSample) throws NamingException {
    List<String> usernames = new ArrayList<>();
    LdapContext ctx = null;

    try {
        ctx = manager.getContext();

        // Sort on username field.
        Control[] searchControl;
        try {
            searchControl = new Control[]{
                    new SortControl(new String[]{manager.getUsernameField()}, Control.NONCRITICAL)
            };
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
            return Collections.emptyList();
        }
        ctx.setRequestControls(searchControl);

        // Search for the dn based on the username.
        SearchControls searchControls = new SearchControls();
        // See if recursive searching is enabled. Otherwise, only search one level.
        if (manager.isSubTreeSearch()) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        searchControls.setReturningAttributes(new String[]{manager.getUsernameField()});
        // Limit results to those we'll need to process
        searchControls.setCountLimit(maxSample);
        String filter = MessageFormat.format(manager.getSearchFilter(), "*");
        NamingEnumeration answer = ctx.search("", filter, searchControls);

        while (answer.hasMoreElements()) {
            // Get the next userID.
            String username = (String) ((SearchResult) answer.next()).getAttributes().get(
                    manager.getUsernameField()).get();
            // Escape username and add to results.
            usernames.add(JID.escapeNode(username));
        }
        // Close the enumeration.
        answer.close();
    } finally {
        try {
            if (ctx != null) {
                ctx.setRequestControls(null);
                ctx.close();
            }
        }
        catch (Exception ignored) {
            // Ignore.
        }
    }
    return usernames;
}
项目:cn1    文件:ReferralExceptionTest.java   
public void testGetReferralContext3() throws Exception {
    Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();

    initialEnv.put(Context.REFERRAL, "throw");
    initialEnv.put("test.getReferralContext", "GetReferralContext");

    String[] referrals = new String[] { server.getURL() };
    ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
            referrals, initialEnv);

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();

    env.put(Context.REFERRAL, "follow");
    env.put("test.getReferralContext", "changed");

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    assertEquals(referrals[0], ex.getReferralInfo());
    Context refContext = ex.getReferralContext(env, new Control[] {
            new PagedResultsControl(1, true),
            new SortControl("hello", true) });

    Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
            .getEnvironment();

    assertEquals(env.get(Context.REFERRAL), refEnv.get(Context.REFERRAL));
    assertEquals(env.get("test.getReferralContext"), refEnv
            .get("test.getReferralContext"));
    Control[] cs = (Control[]) refEnv
            .get("java.naming.ldap.control.connect");

    assertNotNull(cs);
    assertEquals(2, cs.length);
    assertTrue(cs[0] instanceof PagedResultsControl);
    assertTrue(cs[1] instanceof SortControl);

    assertFalse(ex.skipReferral());

    assertNull(ex.getReferralInfo());

    // do nothing
    ex.retryReferral();
}
项目:cn1    文件:LdapContextServerMockedTest.java   
public void testRequestControls() throws Exception {

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

        InitialLdapContext initialContext = new InitialLdapContext(env, null);

        Control[] reqCtls = initialContext.getRequestControls();
        assertEquals(1, reqCtls.length);
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

        initialContext.setRequestControls(new Control[] { new SortControl("",
                Control.NONCRITICAL) });

        reqCtls = initialContext.getRequestControls();
        assertEquals(2, reqCtls.length);
        Control control = reqCtls[0];
        if (control instanceof SortControl) {
            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        } else {
            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
            assertEquals(Control.NONCRITICAL, control.isCritical());
            assertTrue(reqCtls[1] instanceof SortControl);
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        }

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
                new EncodableLdapResult(), null) });

        LdapContext context = (LdapContext) initialContext.lookup("");
        // request controls are not inherited
        reqCtls = context.getRequestControls();
        assertEquals(1, reqCtls.length);
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
        context = context.newInstance(new Control[] { new SortControl("",
                Control.NONCRITICAL) });
        reqCtls = context.getRequestControls();

        assertEquals(2, reqCtls.length);
        control = reqCtls[0];
        if (control instanceof SortControl) {
            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        } else {
            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
            assertEquals(Control.NONCRITICAL, control.isCritical());
            assertTrue(reqCtls[1] instanceof SortControl);
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        }
    }
项目:cn1    文件:LdapContextServerMockedTest.java   
public void testReconnect() throws Exception {
    Control[] expected = new Control[] { new PagedResultsControl(10,
            Control.NONCRITICAL) };
    env.put("java.naming.ldap.control.connect", expected);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    LdapContext context = new InitialLdapContext(env, null);

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertNotSame(expected, controls);

    Control c = controls[0];
    assertTrue(c instanceof PagedResultsControl);
    assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c)
            .isCritical());
    assertEquals(expected[0], c);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    expected = new Control[] { new SortControl("", Control.NONCRITICAL) };
    context.reconnect(expected);

    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());
    assertNotSame(expected, controls);
    assertEquals(expected[0], c);

    expected[0] = new PagedResultsControl(10, Control.NONCRITICAL);
    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    context.reconnect(null);

    assertNull(context.getConnectControls());
}
项目:g3server    文件:LdapUserTester.java   
/**
 * Returns a list of usernames with a sample of the users found in LDAP.
 *
 * @param maxSample the max size of the sample to return.
 * @return a list of usernames with a sample of the users found in LDAP.
 * @throws NamingException if something goes wrong....
 */
public List<String> getSample(int maxSample) throws NamingException {
    List<String> usernames = new ArrayList<String>();
    LdapContext ctx = null;

    try {
        ctx = manager.getContext();

        // Sort on username field.
        Control[] searchControl;
        try {
            searchControl = new Control[]{
                    new SortControl(new String[]{manager.getUsernameField()}, Control.NONCRITICAL)
            };
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
            return Collections.emptyList();
        }
        ctx.setRequestControls(searchControl);

        // Search for the dn based on the username.
        SearchControls searchControls = new SearchControls();
        // See if recursive searching is enabled. Otherwise, only search one level.
        if (manager.isSubTreeSearch()) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        searchControls.setReturningAttributes(new String[]{manager.getUsernameField()});
        // Limit results to those we'll need to process
        searchControls.setCountLimit(maxSample);
        String filter = MessageFormat.format(manager.getSearchFilter(), "*");
        NamingEnumeration answer = ctx.search("", filter, searchControls);

        while (answer.hasMoreElements()) {
            // Get the next userID.
            String username = (String) ((SearchResult) answer.next()).getAttributes().get(
                    manager.getUsernameField()).get();
            // Escape username and add to results.
            usernames.add(JID.escapeNode(username));
        }
        // Close the enumeration.
        answer.close();
    } finally {
        try {
            if (ctx != null) {
                ctx.setRequestControls(null);
                ctx.close();
            }
        }
        catch (Exception ignored) {
            // Ignore.
        }
    }
    return usernames;
}
项目:openfire    文件:LdapUserTester.java   
/**
 * Returns a list of usernames with a sample of the users found in LDAP.
 *
 * @param maxSample the max size of the sample to return.
 * @return a list of usernames with a sample of the users found in LDAP.
 * @throws NamingException if something goes wrong....
 */
public List<String> getSample(int maxSample) throws NamingException {
    List<String> usernames = new ArrayList<String>();
    LdapContext ctx = null;

    try {
        ctx = manager.getContext();

        // Sort on username field.
        Control[] searchControl;
        try {
            searchControl = new Control[]{
                    new SortControl(new String[]{manager.getUsernameField()}, Control.NONCRITICAL)
            };
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
            return Collections.emptyList();
        }
        ctx.setRequestControls(searchControl);

        // Search for the dn based on the username.
        SearchControls searchControls = new SearchControls();
        // See if recursive searching is enabled. Otherwise, only search one level.
        if (manager.isSubTreeSearch()) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        searchControls.setReturningAttributes(new String[]{manager.getUsernameField()});
        // Limit results to those we'll need to process
        searchControls.setCountLimit(maxSample);
        String filter = MessageFormat.format(manager.getSearchFilter(), "*");
        NamingEnumeration answer = ctx.search("", filter, searchControls);

        while (answer.hasMoreElements()) {
            // Get the next userID.
            String username = (String) ((SearchResult) answer.next()).getAttributes().get(
                    manager.getUsernameField()).get();
            // Escape username and add to results.
            usernames.add(JID.escapeNode(username));
        }
        // Close the enumeration.
        answer.close();
    } finally {
        try {
            if (ctx != null) {
                ctx.setRequestControls(null);
                ctx.close();
            }
        }
        catch (Exception ignored) {
            // Ignore.
        }
    }
    return usernames;
}
项目:freeVM    文件:ReferralExceptionTest.java   
public void testGetReferralContext3() throws Exception {
    Hashtable<Object, Object> initialEnv = new Hashtable<Object, Object>();

    initialEnv.put(Context.REFERRAL, "throw");
    initialEnv.put("test.getReferralContext", "GetReferralContext");

    String[] referrals = new String[] { server.getURL() };
    ReferralExceptionImpl ex = new ReferralExceptionImpl("cn=dn",
            referrals, initialEnv);

    Hashtable<Object, Object> env = new Hashtable<Object, Object>();

    env.put(Context.REFERRAL, "follow");
    env.put("test.getReferralContext", "changed");

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    assertEquals(referrals[0], ex.getReferralInfo());
    Context refContext = ex.getReferralContext(env, new Control[] {
            new PagedResultsControl(1, true),
            new SortControl("hello", true) });

    Hashtable<Object, Object> refEnv = (Hashtable<Object, Object>) refContext
            .getEnvironment();

    assertEquals(env.get(Context.REFERRAL), refEnv.get(Context.REFERRAL));
    assertEquals(env.get("test.getReferralContext"), refEnv
            .get("test.getReferralContext"));
    Control[] cs = (Control[]) refEnv
            .get("java.naming.ldap.control.connect");

    assertNotNull(cs);
    assertEquals(2, cs.length);
    assertTrue(cs[0] instanceof PagedResultsControl);
    assertTrue(cs[1] instanceof SortControl);

    assertFalse(ex.skipReferral());

    assertNull(ex.getReferralInfo());

    // do nothing
    ex.retryReferral();
}
项目:freeVM    文件:LdapContextServerMockedTest.java   
public void testRequestControls() throws Exception {

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

        InitialLdapContext initialContext = new InitialLdapContext(env, null);

        Control[] reqCtls = initialContext.getRequestControls();
        assertEquals(1, reqCtls.length);
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

        initialContext.setRequestControls(new Control[] { new SortControl("",
                Control.NONCRITICAL) });

        reqCtls = initialContext.getRequestControls();
        assertEquals(2, reqCtls.length);
        Control control = reqCtls[0];
        if (control instanceof SortControl) {
            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        } else {
            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
            assertEquals(Control.NONCRITICAL, control.isCritical());
            assertTrue(reqCtls[1] instanceof SortControl);
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        }

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_SEARCH_RESULT_DONE,
                new EncodableLdapResult(), null) });

        LdapContext context = (LdapContext) initialContext.lookup("");
        // request controls are not inherited
        reqCtls = context.getRequestControls();
        assertEquals(1, reqCtls.length);
        assertEquals("2.16.840.1.113730.3.4.2", reqCtls[0].getID());
        assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());

        server.setResponseSeq(new LdapMessage[] { new LdapMessage(
                LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
        context = context.newInstance(new Control[] { new SortControl("",
                Control.NONCRITICAL) });
        reqCtls = context.getRequestControls();

        assertEquals(2, reqCtls.length);
        control = reqCtls[0];
        if (control instanceof SortControl) {
            assertEquals(Control.NONCRITICAL, reqCtls[0].isCritical());
            assertEquals("2.16.840.1.113730.3.4.2", reqCtls[1].getID());
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        } else {
            assertEquals("2.16.840.1.113730.3.4.2", control.getID());
            assertEquals(Control.NONCRITICAL, control.isCritical());
            assertTrue(reqCtls[1] instanceof SortControl);
            assertEquals(Control.NONCRITICAL, reqCtls[1].isCritical());
        }
    }
项目:freeVM    文件:LdapContextServerMockedTest.java   
public void testReconnect() throws Exception {
    Control[] expected = new Control[] { new PagedResultsControl(10,
            Control.NONCRITICAL) };
    env.put("java.naming.ldap.control.connect", expected);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    LdapContext context = new InitialLdapContext(env, null);

    Control[] controls = context.getConnectControls();
    assertNotNull(controls);
    assertNotSame(expected, controls);

    Control c = controls[0];
    assertTrue(c instanceof PagedResultsControl);
    assertEquals(Control.NONCRITICAL, ((PagedResultsControl) c)
            .isCritical());
    assertEquals(expected[0], c);

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });

    expected = new Control[] { new SortControl("", Control.NONCRITICAL) };
    context.reconnect(expected);

    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());
    assertNotSame(expected, controls);
    assertEquals(expected[0], c);

    expected[0] = new PagedResultsControl(10, Control.NONCRITICAL);
    controls = context.getConnectControls();
    assertNotNull(controls);
    assertEquals(1, controls.length);
    c = controls[0];
    assertTrue(c instanceof SortControl);
    assertEquals(Control.NONCRITICAL, ((SortControl) c).isCritical());

    server.setResponseSeq(new LdapMessage[] { new LdapMessage(
            LdapASN1Constant.OP_BIND_RESPONSE, new BindResponse(), null) });
    context.reconnect(null);

    assertNull(context.getConnectControls());
}
项目:openfire-bespoke    文件:LdapUserTester.java   
/**
 * Returns a list of usernames with a sample of the users found in LDAP.
 *
 * @param maxSample the max size of the sample to return.
 * @return a list of usernames with a sample of the users found in LDAP.
 * @throws NamingException if something goes wrong....
 */
public List<String> getSample(int maxSample) throws NamingException {
    List<String> usernames = new ArrayList<String>();
    LdapContext ctx = null;

    try {
        ctx = manager.getContext();

        // Sort on username field.
        Control[] searchControl;
        try {
            searchControl = new Control[]{
                    new SortControl(new String[]{manager.getUsernameField()}, Control.NONCRITICAL)
            };
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
            return Collections.emptyList();
        }
        ctx.setRequestControls(searchControl);

        // Search for the dn based on the username.
        SearchControls searchControls = new SearchControls();
        // See if recursive searching is enabled. Otherwise, only search one level.
        if (manager.isSubTreeSearch()) {
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        } else {
            searchControls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        }
        searchControls.setReturningAttributes(new String[]{manager.getUsernameField()});
        // Limit results to those we'll need to process
        searchControls.setCountLimit(maxSample);
        String filter = MessageFormat.format(manager.getSearchFilter(), "*");
        NamingEnumeration answer = ctx.search("", filter, searchControls);

        while (answer.hasMoreElements()) {
            // Get the next userID.
            String username = (String) ((SearchResult) answer.next()).getAttributes().get(
                    manager.getUsernameField()).get();
            // Escape username and add to results.
            usernames.add(JID.escapeNode(username));
        }
        // Close the enumeration.
        answer.close();
    } finally {
        try {
            if (ctx != null) {
                ctx.setRequestControls(null);
                ctx.close();
            }
        }
        catch (Exception ignored) {
            // Ignore.
        }
    }
    return usernames;
}
项目:cn1    文件:SortControlTest.java   
/**
 * <p>
 * Test method for 'javax.naming.ldap.SortControl.getEncodedValue()'
 * </p>
 * <p>
 * Here we are testing if this method retrieves the control's ASN.1 BER
 * encoded value. In this case we create a sort control using an array of
 * sortkey.
 * </p>
 * <p>
 * The expecting result is the control's ASN.1 BER encoded value.
 * </p>
 */
public void testEncodedValueOfSortControl001() throws Exception {
    SortKey[] sk = { new SortKey("pepe", false, "leo") };

    SortControl sc = new SortControl(sk, true);
    assertEquals("30 10 30 0e 04 04 70 65 70 65 80 03 6c 65 6f 81 01 ff",
            toHexString(sc.getEncodedValue()));
}
项目:freeVM    文件:SortControlTest.java   
/**
 * <p>
 * Test method for 'javax.naming.ldap.SortControl.getEncodedValue()'
 * </p>
 * <p>
 * Here we are testing if this method retrieves the control's ASN.1 BER
 * encoded value. In this case we create a sort control using an array of
 * sortkey.
 * </p>
 * <p>
 * The expecting result is the control's ASN.1 BER encoded value.
 * </p>
 */
public void testEncodedValueOfSortControl001() throws Exception {
    SortKey[] sk = { new SortKey("pepe", false, "leo") };

    SortControl sc = new SortControl(sk, true);
    assertEquals("30 10 30 0e 04 04 70 65 70 65 80 03 6c 65 6f 81 01 ff",
            toHexString(sc.getEncodedValue()));
}
项目:freeVM    文件:SortControlTest.java   
/**
 * <p>
 * Test method for 'javax.naming.ldap.SortControl.getEncodedValue()'
 * </p>
 * <p>
 * Here we are testing if this method retrieves the control's ASN.1 BER
 * encoded value. In this case we create a sort control using an array of
 * sortkey.
 * </p>
 * <p>
 * The expecting result is the control's ASN.1 BER encoded value.
 * </p>
 */
public void testEncodedValueOfSortControl001() throws Exception {
    SortKey[] sk = { new SortKey("pepe", false, "leo") };

    SortControl sc = new SortControl(sk, true);
    assertEquals("30 10 30 0e 04 04 70 65 70 65 80 03 6c 65 6f 81 01 ff",
            toHexString(sc.getEncodedValue()));
}