Path: blob/master/src/java.naming/share/classes/javax/naming/ldap/PagedResultsControl.java
41159 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.naming.ldap;2627import java.io.IOException;28import com.sun.jndi.ldap.Ber;29import com.sun.jndi.ldap.BerEncoder;3031/**32* Requests that the results of a search operation be returned by the LDAP33* server in batches of a specified size.34* The requestor controls the rate at which batches are returned by the rate35* at which it invokes search operations.36* <p>37* The following code sample shows how the class may be used:38* <pre>{@code39*40* // Open an LDAP association41* LdapContext ctx = new InitialLdapContext();42*43* // Activate paged results44* int pageSize = 20; // 20 entries per page45* byte[] cookie = null;46* int total;47* ctx.setRequestControls(new Control[]{48* new PagedResultsControl(pageSize, Control.CRITICAL) });49*50* do {51* // Perform the search52* NamingEnumeration results =53* ctx.search("", "(objectclass=*)", new SearchControls());54*55* // Iterate over a batch of search results56* while (results != null && results.hasMore()) {57* // Display an entry58* SearchResult entry = (SearchResult)results.next();59* System.out.println(entry.getName());60* System.out.println(entry.getAttributes());61*62* // Handle the entry's response controls (if any)63* if (entry instanceof HasControls) {64* // ((HasControls)entry).getControls();65* }66* }67* // Examine the paged results control response68* Control[] controls = ctx.getResponseControls();69* if (controls != null) {70* for (int i = 0; i < controls.length; i++) {71* if (controls[i] instanceof PagedResultsResponseControl) {72* PagedResultsResponseControl prrc =73* (PagedResultsResponseControl)controls[i];74* total = prrc.getResultSize();75* cookie = prrc.getCookie();76* } else {77* // Handle other response controls (if any)78* }79* }80* }81*82* // Re-activate paged results83* ctx.setRequestControls(new Control[]{84* new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });85* } while (cookie != null);86*87* // Close the LDAP association88* ctx.close();89* ...90*91* } </pre>92* <p>93* This class implements the LDAPv3 Control for paged-results as defined in94* <a href="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</a>.95*96* The control's value has the following ASN.1 definition:97* <pre>{@code98*99* realSearchControlValue ::= SEQUENCE {100* size INTEGER (0..maxInt),101* -- requested page size from client102* -- result set size estimate from server103* cookie OCTET STRING104* }105*106* }</pre>107*108* @since 1.5109* @see PagedResultsResponseControl110* @author Vincent Ryan111*/112public final class PagedResultsControl extends BasicControl {113114/**115* The paged-results control's assigned object identifier116* is 1.2.840.113556.1.4.319.117*/118public static final String OID = "1.2.840.113556.1.4.319";119120private static final byte[] EMPTY_COOKIE = new byte[0];121122private static final long serialVersionUID = 6684806685736844298L;123124/**125* Constructs a control to set the number of entries to be returned per126* page of results.127*128* @param pageSize The number of entries to return in a page.129* @param criticality If true then the server must honor the control130* and return search results as indicated by131* pageSize or refuse to perform the search.132* If false, then the server need not honor the133* control.134* @throws IOException If an error was encountered while encoding the135* supplied arguments into a control.136*/137public PagedResultsControl(int pageSize, boolean criticality)138throws IOException {139140super(OID, criticality, null);141value = setEncodedValue(pageSize, EMPTY_COOKIE);142}143144/**145* Constructs a control to set the number of entries to be returned per146* page of results. The cookie is provided by the server and may be147* obtained from the paged-results response control.148* <p>149* A sequence of paged-results can be abandoned by setting the pageSize150* to zero and setting the cookie to the last cookie received from the151* server.152*153* @param pageSize The number of entries to return in a page.154* @param cookie A possibly null server-generated cookie.155* @param criticality If true then the server must honor the control156* and return search results as indicated by157* pageSize or refuse to perform the search.158* If false, then the server need not honor the159* control.160* @throws IOException If an error was encountered while encoding the161* supplied arguments into a control.162*/163public PagedResultsControl(int pageSize, byte[] cookie,164boolean criticality) throws IOException {165166super(OID, criticality, null);167if (cookie == null) {168cookie = EMPTY_COOKIE;169}170value = setEncodedValue(pageSize, cookie);171}172173/**174* Encodes the paged-results control's value using ASN.1 BER.175* The result includes the BER tag and length for the control's value but176* does not include the control's object identifier and criticality setting.177*178* @param pageSize The number of entries to return in a page.179* @param cookie A non-null server-generated cookie.180* @return A possibly null byte array representing the ASN.1 BER encoded181* value of the LDAP paged-results control.182* @throws IOException If a BER encoding error occurs.183*/184private byte[] setEncodedValue(int pageSize, byte[] cookie)185throws IOException {186187// build the ASN.1 encoding188BerEncoder ber = new BerEncoder(10 + cookie.length);189190ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);191ber.encodeInt(pageSize);192ber.encodeOctetString(cookie, Ber.ASN_OCTET_STR);193ber.endSeq();194195return ber.getTrimmedBuf();196}197}198199200