src/java.naming/share/classes/javax/naming/ldap/PagedResultsControl.java
author stefank
Mon, 25 Nov 2019 15:00:32 +0100
changeset 59255 a74627659f96
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234602: ZGC: Windows compile error in ZHeuristic Reviewed-by: pliden, eosterlund
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.naming.ldap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jndi.ldap.Ber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.jndi.ldap.BerEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Requests that the results of a search operation be returned by the LDAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * server in batches of a specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The requestor controls the rate at which batches are returned by the rate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * at which it invokes search operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The following code sample shows how the class may be used:
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
    39
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *     // Open an LDAP association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *     LdapContext ctx = new InitialLdapContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     // Activate paged results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *     int pageSize = 20; // 20 entries per page
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *     byte[] cookie = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     int total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     ctx.setRequestControls(new Control[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         new PagedResultsControl(pageSize, Control.CRITICAL) });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *         // Perform the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         NamingEnumeration results =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *             ctx.search("", "(objectclass=*)", new SearchControls());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *         // Iterate over a batch of search results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *         while (results != null && results.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *             // Display an entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *             SearchResult entry = (SearchResult)results.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *             System.out.println(entry.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *             System.out.println(entry.getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *             // Handle the entry's response controls (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *             if (entry instanceof HasControls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *                 // ((HasControls)entry).getControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         // Examine the paged results control response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *         Control[] controls = ctx.getResponseControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         if (controls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *             for (int i = 0; i < controls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *                 if (controls[i] instanceof PagedResultsResponseControl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *                     PagedResultsResponseControl prrc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *                         (PagedResultsResponseControl)controls[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *                     total = prrc.getResultSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *                     cookie = prrc.getCookie();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *                     // Handle other response controls (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         // Re-activate paged results
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *         ctx.setRequestControls(new Control[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *             new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     } while (cookie != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     // Close the LDAP association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
    92
 * } </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * This class implements the LDAPv3 Control for paged-results as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <a href="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * The control's value has the following ASN.1 definition:
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
    98
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     realSearchControlValue ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *         size      INTEGER (0..maxInt),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *                           -- requested page size from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *                           -- result set size estimate from server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *         cookie    OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   107
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @see PagedResultsResponseControl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
final public class PagedResultsControl extends BasicControl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The paged-results control's assigned object identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * is 1.2.840.113556.1.4.319.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final String OID = "1.2.840.113556.1.4.319";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final byte[] EMPTY_COOKIE = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final long serialVersionUID = 6684806685736844298L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Constructs a control to set the number of entries to be returned per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * page of results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param   pageSize        The number of entries to return in a page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param   criticality     If true then the server must honor the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *                          and return search results as indicated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *                          pageSize or refuse to perform the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *                          If false, then the server need not honor the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *                          control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception IOException   If an error was encountered while encoding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *                          supplied arguments into a control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public PagedResultsControl(int pageSize, boolean criticality)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super(OID, criticality, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        value = setEncodedValue(pageSize, EMPTY_COOKIE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Constructs a control to set the number of entries to be returned per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * page of results. The cookie is provided by the server and may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * obtained from the paged-results response control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * A sequence of paged-results can be abandoned by setting the pageSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * to zero and setting the cookie to the last cookie received from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param   pageSize        The number of entries to return in a page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param   cookie          A possibly null server-generated cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param   criticality     If true then the server must honor the control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *                          and return search results as indicated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *                          pageSize or refuse to perform the search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                          If false, then the server need not honor the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *                          control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception IOException   If an error was encountered while encoding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                          supplied arguments into a control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public PagedResultsControl(int pageSize, byte[] cookie,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        boolean criticality) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        super(OID, criticality, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (cookie == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            cookie = EMPTY_COOKIE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        value = setEncodedValue(pageSize, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Encodes the paged-results control's value using ASN.1 BER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * The result includes the BER tag and length for the control's value but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * does not include the control's object identifier and criticality setting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param   pageSize        The number of entries to return in a page.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param   cookie          A non-null server-generated cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return A possibly null byte array representing the ASN.1 BER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         value of the LDAP paged-results control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception IOException If a BER encoding error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private byte[] setEncodedValue(int pageSize, byte[] cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // build the ASN.1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        BerEncoder ber = new BerEncoder(10 + cookie.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        ber.beginSeq(Ber.ASN_SEQUENCE | Ber.ASN_CONSTRUCTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            ber.encodeInt(pageSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            ber.encodeOctetString(cookie, Ber.ASN_OCTET_STR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        ber.endSeq();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return ber.getTrimmedBuf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}