View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.message.controls;
21  
22  
23  import org.apache.directory.api.util.StringConstants;
24  import org.apache.directory.api.util.Strings;
25  
26  import java.util.Arrays;
27  
28  
29  /**
30   * A request/response control used to implement a simple paging of search
31   * results. This is an implementation of RFC 2696 :
32   * <a href="http://www.faqs.org/rfcs/rfc2696.html">LDAP Control Extension for Simple Paged Results Manipulation</a>
33   * <br/>
34   * <pre>
35   *    This control is included in the searchRequest and searchResultDone
36   *    messages as part of the controls field of the LDAPMessage, as defined
37   *    in Section 4.1.12 of [LDAPv3]. The structure of this control is as
38   *    follows:
39   *
40   * pagedResultsControl ::= SEQUENCE {
41   *         controlType     1.2.840.113556.1.4.319,
42   *         criticality     BOOLEAN DEFAULT FALSE,
43   *         controlValue    searchControlValue
44   * }
45   * 
46   * The searchControlValue is an OCTET STRING wrapping the BER-encoded
47   * version of the following SEQUENCE:
48   * 
49   * realSearchControlValue ::= SEQUENCE {
50   *         size            INTEGER (0..maxInt),
51   *                                 -- requested page size from client
52   *                                 -- result set size estimate from server
53   *         cookie          OCTET STRING
54   * }
55   * 
56   * </pre>
57   * 
58   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
59   */
60  public class PagedResultsImpl extends AbstractControl implements PagedResults
61  {
62      /** The number of entries to return, or returned */
63      private int size;
64  
65      /** The exchanged cookie */
66      private byte[] cookie = StringConstants.EMPTY_BYTES;
67  
68  
69      /**
70       * Creates a new instance of PagedResultsDecorator.
71       */
72      public PagedResultsImpl()
73      {
74          super( OID );
75      }
76  
77  
78      public int getSize()
79      {
80          return size;
81      }
82  
83  
84      public void setSize( int size )
85      {
86          this.size = size;
87      }
88  
89  
90      public byte[] getCookie()
91      {
92          return cookie;
93      }
94  
95  
96      public void setCookie( byte[] cookie )
97      {
98          this.cookie = cookie;
99      }
100 
101 
102     public int getCookieValue()
103     {
104         int value = 0;
105 
106         switch ( cookie.length )
107         {
108             case 1:
109                 value = cookie[0] & 0x00FF;
110                 break;
111 
112             case 2:
113                 value = ( ( cookie[0] & 0x00FF ) << 8 ) + ( cookie[1] & 0x00FF );
114                 break;
115 
116             case 3:
117                 value = ( ( cookie[0] & 0x00FF ) << 16 ) + ( ( cookie[1] & 0x00FF ) << 8 ) + ( cookie[2] & 0x00FF );
118                 break;
119 
120             case 4:
121                 value = ( ( cookie[0] & 0x00FF ) << 24 ) + ( ( cookie[1] & 0x00FF ) << 16 )
122                     + ( ( cookie[2] & 0x00FF ) << 8 ) + ( cookie[3] & 0x00FF );
123                 break;
124 
125         }
126 
127         return value;
128     }
129 
130 
131     /**
132      * @see Object#hashCode()
133      */
134     @Override
135     public int hashCode()
136     {
137         int h = super.hashCode();
138 
139         h = h * 37 + size;
140 
141         if ( cookie != null )
142         {
143             for ( byte b : cookie )
144             {
145                 h = h * 17 + b;
146             }
147         }
148 
149         return h;
150     }
151 
152 
153     /**
154      * @see Object#equals(Object)
155      */
156     @Override
157     public boolean equals( Object o )
158     {
159         if ( !super.equals( o ) )
160         {
161             return false;
162         }
163 
164         PagedResults otherControl = ( PagedResults ) o;
165 
166         return ( size == otherControl.getSize() ) && Arrays.equals( cookie, otherControl.getCookie() );
167     }
168 
169 
170     /**
171      * Return a String representing this PagedSearchControl.
172      */
173     public String toString()
174     {
175         StringBuffer sb = new StringBuffer();
176 
177         sb.append( "    Paged Search Control\n" );
178         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
179         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
180         sb.append( "        size   : '" ).append( size ).append( "'\n" );
181         sb.append( "        cookie   : '" ).append( Strings.dumpBytes( cookie ) ).append( "'\n" );
182 
183         return sb.toString();
184     }
185 }