0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.naming.ldap;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport com.sun.jndi.ldap.Ber;
0N/Aimport com.sun.jndi.ldap.BerEncoder;
0N/A
0N/A/**
0N/A * Requests that the results of a search operation be returned by the LDAP
0N/A * server in batches of a specified size.
0N/A * The requestor controls the rate at which batches are returned by the rate
0N/A * at which it invokes search operations.
0N/A * <p>
0N/A * The following code sample shows how the class may be used:
0N/A * <pre>
0N/A *
0N/A * // Open an LDAP association
0N/A * LdapContext ctx = new InitialLdapContext();
0N/A *
0N/A * // Activate paged results
0N/A * int pageSize = 20; // 20 entries per page
0N/A * byte[] cookie = null;
0N/A * int total;
0N/A * ctx.setRequestControls(new Control[]{
0N/A * new PagedResultsControl(pageSize, Control.CRITICAL) });
0N/A *
0N/A * do {
0N/A * // Perform the search
0N/A * NamingEnumeration results =
0N/A * ctx.search("", "(objectclass=*)", new SearchControls());
0N/A *
0N/A * // Iterate over a batch of search results
0N/A * while (results != null && results.hasMore()) {
0N/A * // Display an entry
0N/A * SearchResult entry = (SearchResult)results.next();
0N/A * System.out.println(entry.getName());
0N/A * System.out.println(entry.getAttributes());
0N/A *
0N/A * // Handle the entry's response controls (if any)
0N/A * if (entry instanceof HasControls) {
0N/A * // ((HasControls)entry).getControls();
0N/A * }
0N/A * }
0N/A * // Examine the paged results control response
0N/A * Control[] controls = ctx.getResponseControls();
0N/A * if (controls != null) {
0N/A * for (int i = 0; i < controls.length; i++) {
0N/A * if (controls[i] instanceof PagedResultsResponseControl) {
0N/A * PagedResultsResponseControl prrc =
0N/A * (PagedResultsResponseControl)controls[i];
0N/A * total = prrc.getResultSize();
0N/A * cookie = prrc.getCookie();
0N/A * } else {
0N/A * // Handle other response controls (if any)
0N/A * }
0N/A * }
0N/A * }
0N/A *
0N/A * // Re-activate paged results
0N/A * ctx.setRequestControls(new Control[]{
0N/A * new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
0N/A * } while (cookie != null);
0N/A *
0N/A * // Close the LDAP association
0N/A * ctx.close();
0N/A * ...
0N/A *
0N/A * </pre>
0N/A * <p>
0N/A * This class implements the LDAPv3 Control for paged-results as defined in
0N/A * <a href="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</a>.
0N/A *
0N/A * The control's value has the following ASN.1 definition:
0N/A * <pre>
0N/A *
0N/A * realSearchControlValue ::= SEQUENCE {
0N/A * size INTEGER (0..maxInt),
0N/A * -- requested page size from client
0N/A * -- result set size estimate from server
0N/A * cookie OCTET STRING
0N/A * }
0N/A *
0N/A * </pre>
0N/A *
0N/A * @since 1.5
0N/A * @see PagedResultsResponseControl
0N/A * @author Vincent Ryan
0N/A */
0N/Afinal public class PagedResultsControl extends BasicControl {
0N/A
0N/A /**
0N/A * The paged-results control's assigned object identifier
0N/A * is 1.2.840.113556.1.4.319.
0N/A */
0N/A public static final String OID = "1.2.840.113556.1.4.319";
0N/A
0N/A private static final byte[] EMPTY_COOKIE = new byte[0];
0N/A
0N/A private static final long serialVersionUID = 6684806685736844298L;
0N/A
0N/A /**
0N/A * Constructs a control to set the number of entries to be returned per
0N/A * page of results.
0N/A *
0N/A * @param pageSize The number of entries to return in a page.
0N/A * @param criticality If true then the server must honor the control
0N/A * and return search results as indicated by
0N/A * pageSize or refuse to perform the search.
0N/A * If false, then the server need not honor the
0N/A * control.
0N/A * @exception IOException If an error was encountered while encoding the
0N/A * supplied arguments into a control.
0N/A */
0N/A public PagedResultsControl(int pageSize, boolean criticality)
0N/A throws IOException {
0N/A
0N/A super(OID, criticality, null);
0N/A value = setEncodedValue(pageSize, EMPTY_COOKIE);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a control to set the number of entries to be returned per
0N/A * page of results. The cookie is provided by the server and may be
0N/A * obtained from the paged-results response control.
0N/A * <p>
0N/A * A sequence of paged-results can be abandoned by setting the pageSize
0N/A * to zero and setting the cookie to the last cookie received from the
0N/A * server.
0N/A *
0N/A * @param pageSize The number of entries to return in a page.
0N/A * @param cookie A possibly null server-generated cookie.
0N/A * @param criticality If true then the server must honor the control
0N/A * and return search results as indicated by
0N/A * pageSize or refuse to perform the search.
0N/A * If false, then the server need not honor the
0N/A * control.
0N/A * @exception IOException If an error was encountered while encoding the
0N/A * supplied arguments into a control.
0N/A */
0N/A public PagedResultsControl(int pageSize, byte[] cookie,
0N/A boolean criticality) throws IOException {
0N/A
0N/A super(OID, criticality, null);
0N/A if (cookie == null) {
0N/A cookie = EMPTY_COOKIE;
0N/A }
0N/A value = setEncodedValue(pageSize, cookie);
0N/A }
0N/A
0N/A /*
0N/A * Encodes the paged-results control's value using ASN.1 BER.
0N/A * The result includes the BER tag and length for the control's value but
0N/A * does not include the control's object identifier and criticality setting.
0N/A *
0N/A * @param pageSize The number of entries to return in a page.
0N/A * @param cookie A non-null server-generated cookie.
0N/A * @return A possibly null byte array representing the ASN.1 BER encoded
0N/A * value of the LDAP paged-results control.
0N/A * @exception IOException If a BER encoding error occurs.
0N/A */
0N/A private byte[] setEncodedValue(int pageSize, byte[] cookie)
0N/A throws IOException {
0N/A
0N/A // build the ASN.1 encoding
0N/A BerEncoder ber = new BerEncoder(10 + cookie.length);
0N/A
0N/A ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
0N/A ber.encodeInt(pageSize);
0N/A ber.encodeOctetString(cookie, Ber.ASN_OCTET_STR);
0N/A ber.endSeq();
0N/A
0N/A return ber.getTrimmedBuf();
0N/A }
0N/A}