001/*
002 * Copyright 2007-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2007-2022 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2007-2022 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk.controls;
037
038
039
040import java.util.LinkedHashMap;
041import java.util.List;
042import java.util.Map;
043
044import com.unboundid.asn1.ASN1Element;
045import com.unboundid.asn1.ASN1Exception;
046import com.unboundid.asn1.ASN1Integer;
047import com.unboundid.asn1.ASN1OctetString;
048import com.unboundid.asn1.ASN1Sequence;
049import com.unboundid.ldap.sdk.Control;
050import com.unboundid.ldap.sdk.DecodeableControl;
051import com.unboundid.ldap.sdk.JSONControlDecodeHelper;
052import com.unboundid.ldap.sdk.LDAPException;
053import com.unboundid.ldap.sdk.ResultCode;
054import com.unboundid.ldap.sdk.SearchResult;
055import com.unboundid.util.Base64;
056import com.unboundid.util.Debug;
057import com.unboundid.util.NotMutable;
058import com.unboundid.util.NotNull;
059import com.unboundid.util.Nullable;
060import com.unboundid.util.ThreadSafety;
061import com.unboundid.util.ThreadSafetyLevel;
062import com.unboundid.util.json.JSONField;
063import com.unboundid.util.json.JSONNumber;
064import com.unboundid.util.json.JSONObject;
065import com.unboundid.util.json.JSONString;
066import com.unboundid.util.json.JSONValue;
067
068import static com.unboundid.ldap.sdk.controls.ControlMessages.*;
069
070
071
072/**
073 * This class provides an implementation of the simple paged results control as
074 * defined in <A HREF="http://www.ietf.org/rfc/rfc2696.txt">RFC 2696</A>.  It
075 * allows the client to iterate through a potentially large set of search
076 * results in subsets of a specified number of entries (i.e., "pages").
077 * <BR><BR>
078 * The same control encoding is used for both the request control sent by
079 * clients and the response control returned by the server.  It may contain
080 * two elements:
081 * <UL>
082 *   <LI>Size -- In a request control, this provides the requested page size,
083 *       which is the maximum number of entries that the server should return
084 *       in the next iteration of the search.  In a response control, it is an
085 *       estimate of the total number of entries that match the search
086 *       criteria.</LI>
087 *   <LI>Cookie -- A token which is used by the server to keep track of its
088 *       position in the set of search results.  The first request sent by the
089 *       client should not include a cookie, and the last response sent by the
090 *       server should not include a cookie.  For all other intermediate search
091 *       requests and responses,  the server will include a cookie value in its
092 *       response that the client should include in its next request.</LI>
093 * </UL>
094 * When the client wishes to use the paged results control, the first search
095 * request should include a version of the paged results request control that
096 * was created with a requested page size but no cookie.  The corresponding
097 * response from the server will include a version of the paged results control
098 * that may include an estimate of the total number of matching entries, and
099 * may also include a cookie.  The client should include this cookie in the
100 * next request (with the same set of search criteria) to retrieve the next page
101 * of results.  This process should continue until the response control returned
102 * by the server does not include a cookie, which indicates that the end of the
103 * result set has been reached.
104 * <BR><BR>
105 * Note that the simple paged results control is similar to the
106 * {@link VirtualListViewRequestControl} in that both allow the client to
107 * request that only a portion of the result set be returned at any one time.
108 * However, there are significant differences between them, including:
109 * <UL>
110 *   <LI>In order to use the virtual list view request control, it is also
111 *       necessary to use the {@link ServerSideSortRequestControl} to ensure
112 *       that the entries are sorted.  This is not a requirement for the
113 *       simple paged results control.</LI>
114 *   <LI>The simple paged results control may only be used to iterate
115 *       sequentially through the set of search results.  The virtual list view
116 *       control can retrieve pages out of order, can retrieve overlapping
117 *       pages, and can re-request pages that it had already retrieved.</LI>
118 * </UL>
119 * <H2>Example</H2>
120 * The following example demonstrates the use of the simple paged results
121 * control.  It will iterate through all users, retrieving up to 10 entries at a
122 * time:
123 * <PRE>
124 * // Perform a search to retrieve all users in the server, but only retrieving
125 * // ten at a time.
126 * int numSearches = 0;
127 * int totalEntriesReturned = 0;
128 * SearchRequest searchRequest = new SearchRequest("dc=example,dc=com",
129 *      SearchScope.SUB, Filter.createEqualityFilter("objectClass", "person"));
130 * ASN1OctetString resumeCookie = null;
131 * while (true)
132 * {
133 *   searchRequest.setControls(
134 *        new SimplePagedResultsControl(10, resumeCookie));
135 *   SearchResult searchResult = connection.search(searchRequest);
136 *   numSearches++;
137 *   totalEntriesReturned += searchResult.getEntryCount();
138 *   for (SearchResultEntry e : searchResult.getSearchEntries())
139 *   {
140 *     // Do something with each entry...
141 *   }
142 *
143 *   LDAPTestUtils.assertHasControl(searchResult,
144 *        SimplePagedResultsControl.PAGED_RESULTS_OID);
145 *   SimplePagedResultsControl responseControl =
146 *        SimplePagedResultsControl.get(searchResult);
147 *   if (responseControl.moreResultsToReturn())
148 *   {
149 *     // The resume cookie can be included in the simple paged results
150 *     // control included in the next search to get the next page of results.
151 *     resumeCookie = responseControl.getCookie();
152 *   }
153 *   else
154 *   {
155 *     break;
156 *   }
157 * }
158 * </PRE>
159 */
160@NotMutable()
161@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
162public final class SimplePagedResultsControl
163       extends Control
164       implements DecodeableControl
165{
166  /**
167   * The OID (1.2.840.113556.1.4.319) for the paged results control.
168   */
169  @NotNull public static final String PAGED_RESULTS_OID =
170       "1.2.840.113556.1.4.319";
171
172
173
174  /**
175   * The name of the field used to hold the cookie in the JSON representation of
176   * this control.
177   */
178  @NotNull private static final String JSON_FIELD_COOKIE = "cookie";
179
180
181
182  /**
183   * The name of the field used to hold the size in the JSON representation of
184   * this control.
185   */
186  @NotNull private static final String JSON_FIELD_SIZE = "size";
187
188
189
190  /**
191   * The serial version UID for this serializable class.
192   */
193  private static final long serialVersionUID = 2186787148024999291L;
194
195
196
197  // The encoded cookie returned from the server (for a response control) or
198  // that should be included in the next request to the server (for a request
199  // control).
200  @NotNull private final ASN1OctetString cookie;
201
202  // The maximum requested page size (for a request control), or the estimated
203  // total result set size (for a response control).
204  private final int size;
205
206
207
208  /**
209   * Creates a new empty control instance that is intended to be used only for
210   * decoding controls via the {@code DecodeableControl} interface.
211   */
212  SimplePagedResultsControl()
213  {
214    size   = 0;
215    cookie = new ASN1OctetString();
216  }
217
218
219
220  /**
221   * Creates a new paged results control with the specified page size.  This
222   * version of the constructor should only be used when creating the first
223   * search as part of the set of paged results.  Subsequent searches to
224   * retrieve additional pages should use the response control returned by the
225   * server in their next request, until the response control returned by the
226   * server does not include a cookie.
227   *
228   * @param  pageSize  The maximum number of entries that the server should
229   *                   return in the first page.
230   */
231  public SimplePagedResultsControl(final int pageSize)
232  {
233    super(PAGED_RESULTS_OID, false, encodeValue(pageSize, null));
234
235    size   = pageSize;
236    cookie = new ASN1OctetString();
237  }
238
239
240
241  /**
242   * Creates a new paged results control with the specified page size.  This
243   * version of the constructor should only be used when creating the first
244   * search as part of the set of paged results.  Subsequent searches to
245   * retrieve additional pages should use the response control returned by the
246   * server in their next request, until the response control returned by the
247   * server does not include a cookie.
248   *
249   * @param  pageSize    The maximum number of entries that the server should
250   *                     return in the first page.
251   * @param  isCritical  Indicates whether this control should be marked
252   *                     critical.
253   */
254  public SimplePagedResultsControl(final int pageSize, final boolean isCritical)
255  {
256    super(PAGED_RESULTS_OID, isCritical, encodeValue(pageSize, null));
257
258    size   = pageSize;
259    cookie = new ASN1OctetString();
260  }
261
262
263
264  /**
265   * Creates a new paged results control with the specified page size and the
266   * provided cookie.  This version of the constructor should be used to
267   * continue iterating through an existing set of results, but potentially
268   * using a different page size.
269   *
270   * @param  pageSize  The maximum number of entries that the server should
271   *                   return in the next page of the results.
272   * @param  cookie    The cookie provided by the server after returning the
273   *                   previous page of results, or {@code null} if this request
274   *                   will retrieve the first page of results.
275   */
276  public SimplePagedResultsControl(final int pageSize,
277                                   @Nullable final ASN1OctetString cookie)
278  {
279    super(PAGED_RESULTS_OID, false, encodeValue(pageSize, cookie));
280
281    size = pageSize;
282
283    if (cookie == null)
284    {
285      this.cookie = new ASN1OctetString();
286    }
287    else
288    {
289      this.cookie = cookie;
290    }
291  }
292
293
294
295  /**
296   * Creates a new paged results control with the specified page size and the
297   * provided cookie.  This version of the constructor should be used to
298   * continue iterating through an existing set of results, but potentially
299   * using a different page size.
300   *
301   * @param  pageSize    The maximum number of entries that the server should
302   *                     return in the first page.
303   * @param  cookie      The cookie provided by the server after returning the
304   *                     previous page of results, or {@code null} if this
305   *                     request will retrieve the first page of results.
306   * @param  isCritical  Indicates whether this control should be marked
307   *                     critical.
308   */
309  public SimplePagedResultsControl(final int pageSize,
310                                   @Nullable final ASN1OctetString cookie,
311                                   final boolean isCritical)
312  {
313    super(PAGED_RESULTS_OID, isCritical, encodeValue(pageSize, cookie));
314
315    size = pageSize;
316
317    if (cookie == null)
318    {
319      this.cookie = new ASN1OctetString();
320    }
321    else
322    {
323      this.cookie = cookie;
324    }
325  }
326
327
328
329  /**
330   * Creates a new paged results control from the control with the provided set
331   * of information.  This should be used to decode the paged results response
332   * control returned by the server with a page of results.
333   *
334   * @param  oid         The OID for the control.
335   * @param  isCritical  Indicates whether the control should be marked
336   *                     critical.
337   * @param  value       The encoded value for the control.  This may be
338   *                     {@code null} if no value was provided.
339   *
340   * @throws  LDAPException  If the provided control cannot be decoded as a
341   *                         simple paged results control.
342   */
343  public SimplePagedResultsControl(@NotNull final String oid,
344                                   final boolean isCritical,
345                                   @Nullable final ASN1OctetString value)
346         throws LDAPException
347  {
348    super(oid, isCritical, value);
349
350    if (value == null)
351    {
352      throw new LDAPException(ResultCode.DECODING_ERROR,
353                              ERR_PAGED_RESULTS_NO_VALUE.get());
354    }
355
356    final ASN1Sequence valueSequence;
357    try
358    {
359      final ASN1Element valueElement = ASN1Element.decode(value.getValue());
360      valueSequence = ASN1Sequence.decodeAsSequence(valueElement);
361    }
362    catch (final ASN1Exception ae)
363    {
364      Debug.debugException(ae);
365      throw new LDAPException(ResultCode.DECODING_ERROR,
366                              ERR_PAGED_RESULTS_VALUE_NOT_SEQUENCE.get(ae), ae);
367    }
368
369    final ASN1Element[] valueElements = valueSequence.elements();
370    if (valueElements.length != 2)
371    {
372      throw new LDAPException(ResultCode.DECODING_ERROR,
373                              ERR_PAGED_RESULTS_INVALID_ELEMENT_COUNT.get(
374                                   valueElements.length));
375    }
376
377    try
378    {
379      size = ASN1Integer.decodeAsInteger(valueElements[0]).intValue();
380    }
381    catch (final ASN1Exception ae)
382    {
383      Debug.debugException(ae);
384      throw new LDAPException(ResultCode.DECODING_ERROR,
385                              ERR_PAGED_RESULTS_FIRST_NOT_INTEGER.get(ae), ae);
386    }
387
388    cookie = ASN1OctetString.decodeAsOctetString(valueElements[1]);
389  }
390
391
392
393  /**
394   * {@inheritDoc}
395   */
396  @Override()
397  @NotNull()
398  public SimplePagedResultsControl decodeControl(@NotNull final String oid,
399              final boolean isCritical,
400              @Nullable final ASN1OctetString value)
401         throws LDAPException
402  {
403    return new SimplePagedResultsControl(oid, isCritical, value);
404  }
405
406
407
408  /**
409   * Extracts a simple paged results response control from the provided result.
410   *
411   * @param  result  The result from which to retrieve the simple paged results
412   *                 response control.
413   *
414   * @return  The simple paged results response control contained in the
415   *          provided result, or {@code null} if the result did not contain a
416   *          simple paged results response control.
417   *
418   * @throws  LDAPException  If a problem is encountered while attempting to
419   *                         decode the simple paged results response control
420   *                         contained in the provided result.
421   */
422  @Nullable()
423  public static SimplePagedResultsControl get(
424                     @NotNull final SearchResult result)
425         throws LDAPException
426  {
427    final Control c = result.getResponseControl(PAGED_RESULTS_OID);
428    if (c == null)
429    {
430      return null;
431    }
432
433    if (c instanceof SimplePagedResultsControl)
434    {
435      return (SimplePagedResultsControl) c;
436    }
437    else
438    {
439      return new SimplePagedResultsControl(c.getOID(), c.isCritical(),
440           c.getValue());
441    }
442  }
443
444
445
446  /**
447   * Encodes the provided information into an octet string that can be used as
448   * the value for this control.
449   *
450   * @param  pageSize  The maximum number of entries that the server should
451   *                   return in the next page of the results.
452   * @param  cookie    The cookie provided by the server after returning the
453   *                   previous page of results, or {@code null} if this request
454   *                   will retrieve the first page of results.
455   *
456   * @return  An ASN.1 octet string that can be used as the value for this
457   *          control.
458   */
459  @NotNull()
460  private static ASN1OctetString encodeValue(final int pageSize,
461                      @Nullable final ASN1OctetString cookie)
462  {
463    final ASN1Element[] valueElements;
464    if (cookie == null)
465    {
466      valueElements = new ASN1Element[]
467      {
468        new ASN1Integer(pageSize),
469        new ASN1OctetString()
470      };
471    }
472    else
473    {
474      valueElements = new ASN1Element[]
475      {
476        new ASN1Integer(pageSize),
477        cookie
478      };
479    }
480
481    return new ASN1OctetString(new ASN1Sequence(valueElements).encode());
482  }
483
484
485
486  /**
487   * Retrieves the size for this paged results control.  For a request control,
488   * it may be used to specify the number of entries that should be included in
489   * the next page of results.  For a response control, it may be used to
490   * specify the estimated number of entries in the complete result set.
491   *
492   * @return  The size for this paged results control.
493   */
494  public int getSize()
495  {
496    return size;
497  }
498
499
500
501  /**
502   * Retrieves the cookie for this control, which may be used in a subsequent
503   * request to resume reading entries from the next page of results.  The
504   * value should have a length of zero when used to retrieve the first page of
505   * results for a given search, and also in the response from the server when
506   * there are no more entries to send.  It should be non-empty for all other
507   * conditions.
508   *
509   * @return  The cookie for this control, or an empty cookie (with a value
510   *          length of zero) if there is none.
511   */
512  @NotNull()
513  public ASN1OctetString getCookie()
514  {
515    return cookie;
516  }
517
518
519
520  /**
521   * Indicates whether there are more results to return as part of this search.
522   *
523   * @return  {@code true} if there are more results to return, or
524   *          {@code false} if not.
525   */
526  public boolean moreResultsToReturn()
527  {
528    return (cookie.getValue().length > 0);
529  }
530
531
532
533  /**
534   * {@inheritDoc}
535   */
536  @Override()
537  @NotNull()
538  public String getControlName()
539  {
540    return INFO_CONTROL_NAME_PAGED_RESULTS.get();
541  }
542
543
544
545  /**
546   * Retrieves a representation of this simple paged results control as a JSON
547   * object.  The JSON object uses the following fields:
548   * <UL>
549   *   <LI>
550   *     {@code oid} -- A mandatory string field whose value is the object
551   *     identifier for this control.  For the simple paged results control, the
552   *     OID is "1.2.840.113556.1.4.319".
553   *   </LI>
554   *   <LI>
555   *     {@code control-name} -- An optional string field whose value is a
556   *     human-readable name for this control.  This field is only intended for
557   *     descriptive purposes, and when decoding a control, the {@code oid}
558   *     field should be used to identify the type of control.
559   *   </LI>
560   *   <LI>
561   *     {@code criticality} -- A mandatory Boolean field used to indicate
562   *     whether this control is considered critical.
563   *   </LI>
564   *   <LI>
565   *     {@code value-base64} -- An optional string field whose value is a
566   *     base64-encoded representation of the raw value for this simple paged
567   *     results control.  Exactly one of the {@code value-base64} and
568   *     {@code value-json} fields must be present.
569   *   </LI>
570   *   <LI>
571   *     {@code value-json} -- An optional JSON object field whose value is a
572   *     user-friendly representation of the value for this simple paged results
573   *     control.  Exactly one of the {@code value-base64} and
574   *     {@code value-json} fields must be present, and if the
575   *     {@code value-json} field is used, then it will use the following
576   *     fields:
577   *     <UL>
578   *       <LI>
579   *         {@code size} -- A mandatory integer field.  When used in a request
580   *         control, this represents the maximum number of entries that should
581   *         be returned in the next page of results.  When used in a response
582   *         control, it provides an estimate of the total number of entries
583   *         across all pages of the result set.
584   *       </LI>
585   *       <LI>
586   *         {@code cookie} -- An optional string field.  When used in a request
587   *         control, this should be empty when requesting the first page of
588   *         results, and the value of the {@code cookie} field returned in the
589   *         response control from the previous page of results.  For a response
590   *         control, this will be non-empty for all pages except the last page
591   *         of results.  The cookie value used in the JSON representation of
592   *         the control will be a base64-encoded representation of the raw
593   *         cookie value that would be used in the LDAP representation of the
594   *         control, and it must be treated as an opaque blob by the client.
595   *       </LI>
596   *     </UL>
597   *   </LI>
598   * </UL>
599   *
600   * @return  A JSON object that contains a representation of this control.
601   */
602  @Override()
603  @NotNull()
604  public JSONObject toJSONControl()
605  {
606    final Map<String,JSONValue> valueFields = new LinkedHashMap<>();
607    valueFields.put(JSON_FIELD_SIZE, new JSONNumber(size));
608
609    final byte[] cookieBytes = cookie.getValue();
610    if (cookieBytes.length > 0)
611    {
612      valueFields.put(JSON_FIELD_COOKIE,
613           new JSONString(Base64.encode(cookieBytes)));
614    }
615
616    return new JSONObject(
617         new JSONField(JSONControlDecodeHelper.JSON_FIELD_OID,
618              PAGED_RESULTS_OID),
619         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CONTROL_NAME,
620              INFO_CONTROL_NAME_PAGED_RESULTS.get()),
621         new JSONField(JSONControlDecodeHelper.JSON_FIELD_CRITICALITY,
622              isCritical()),
623         new JSONField(JSONControlDecodeHelper.JSON_FIELD_VALUE_JSON,
624              new JSONObject(valueFields)));
625  }
626
627
628
629  /**
630   * Attempts to decode the provided object as a JSON representation of a
631   * simple paged results control.
632   *
633   * @param  controlObject  The JSON object to be decoded.  It must not be
634   *                        {@code null}.
635   * @param  strict         Indicates whether to use strict mode when decoding
636   *                        the provided JSON object.  If this is {@code true},
637   *                        then this method will throw an exception if the
638   *                        provided JSON object contains any unrecognized
639   *                        fields.  If this is {@code false}, then unrecognized
640   *                        fields will be ignored.
641   *
642   * @return  The simple paged results control that was decoded from
643   *          the provided JSON object.
644   *
645   * @throws  LDAPException  If the provided JSON object cannot be parsed as a
646   *                         valid simple paged results control.
647   */
648  @NotNull()
649  public static SimplePagedResultsControl decodeJSONControl(
650              @NotNull final JSONObject controlObject,
651              final boolean strict)
652         throws LDAPException
653  {
654    final JSONControlDecodeHelper jsonControl = new JSONControlDecodeHelper(
655         controlObject, strict, true, true);
656
657    final ASN1OctetString rawValue = jsonControl.getRawValue();
658    if (rawValue != null)
659    {
660      return new SimplePagedResultsControl(jsonControl.getOID(),
661           jsonControl.getCriticality(), rawValue);
662    }
663
664
665    final JSONObject valueObject = jsonControl.getValueObject();
666
667    final Integer pageSize = valueObject.getFieldAsInteger(JSON_FIELD_SIZE);
668    if (pageSize == null)
669    {
670      throw new LDAPException(ResultCode.DECODING_ERROR,
671           ERR_PAGED_RESULTS_JSON_MISSING_PAGE_SIZE.get(
672                controlObject.toSingleLineString(), JSON_FIELD_SIZE));
673    }
674
675    final ASN1OctetString cookie;
676    final String cookieBase64 = valueObject.getFieldAsString(JSON_FIELD_COOKIE);
677    if ((cookieBase64 == null) || cookieBase64.isEmpty())
678    {
679      cookie = new ASN1OctetString();
680    }
681    else
682    {
683      try
684      {
685        cookie = new ASN1OctetString(Base64.decode(cookieBase64));
686      }
687      catch (final Exception e)
688      {
689        Debug.debugException(e);
690        throw new LDAPException(ResultCode.DECODING_ERROR,
691             ERR_PAGED_RESULTS_JSON_COOKIE_NOT_BASE64.get(
692                  controlObject.toSingleLineString(), JSON_FIELD_COOKIE),
693             e);
694      }
695    }
696
697
698    if (strict)
699    {
700      final List<String> unrecognizedFields =
701           JSONControlDecodeHelper.getControlObjectUnexpectedFields(
702                valueObject, JSON_FIELD_SIZE, JSON_FIELD_COOKIE);
703      if (! unrecognizedFields.isEmpty())
704      {
705        throw new LDAPException(ResultCode.DECODING_ERROR,
706             ERR_PAGED_RESULTS_JSON_UNRECOGNIZED_FIELD.get(
707                  controlObject.toSingleLineString(),
708                  unrecognizedFields.get(0)));
709      }
710    }
711
712
713    return new SimplePagedResultsControl(pageSize, cookie,
714         jsonControl.getCriticality());
715  }
716
717
718
719  /**
720   * {@inheritDoc}
721   */
722  @Override()
723  public void toString(@NotNull final StringBuilder buffer)
724  {
725    buffer.append("SimplePagedResultsControl(pageSize=");
726    buffer.append(size);
727    buffer.append(", isCritical=");
728    buffer.append(isCritical());
729    buffer.append(')');
730  }
731}