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.Strings;
24  
25  import java.util.Arrays;
26  
27  
28  /**
29   * A request/response control used to implement a simple paging of search
30   * results. This is an implementation of RFC 2696 :
31   * <a href="http://www.faqs.org/rfcs/rfc2696.html">LDAP Control Extension for Simple Paged Results Manipulation</a>
32   * <br>
33   * <pre>
34   *    This control is included in the searchRequest and searchResultDone
35   *    messages as part of the controls field of the LDAPMessage, as defined
36   *    in Section 4.1.12 of [LDAPv3]. The structure of this control is as
37   *    follows:
38   *
39   * pagedResultsControl ::= SEQUENCE {
40   *         controlType     1.2.840.113556.1.4.319,
41   *         criticality     BOOLEAN DEFAULT FALSE,
42   *         controlValue    searchControlValue
43   * }
44   * 
45   * The searchControlValue is an OCTET STRING wrapping the BER-encoded
46   * version of the following SEQUENCE:
47   * 
48   * realSearchControlValue ::= SEQUENCE {
49   *         size            INTEGER (0..maxInt),
50   *                                 -- requested page size from client
51   *                                 -- result set size estimate from server
52   *         cookie          OCTET STRING
53   * }
54   * 
55   * </pre>
56   * 
57   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
58   */
59  public class PagedResultsImpl extends AbstractControl implements PagedResults
60  {
61      /** The number of entries to return, or returned */
62      private int size;
63  
64      /** The exchanged cookie */
65      private byte[] cookie = Strings.EMPTY_BYTES;
66  
67  
68      /**
69       * Creates a new instance of PagedResultsDecorator.
70       */
71      public PagedResultsImpl()
72      {
73          super( OID );
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public int getSize()
82      {
83          return size;
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public void setSize( int size )
92      {
93          this.size = size;
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public byte[] getCookie()
102     {
103         return cookie;
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public void setCookie( byte[] cookie )
112     {
113         this.cookie = cookie;
114     }
115 
116 
117     /**
118      * {@inheritDoc}
119      */
120     @Override
121     public int getCookieValue()
122     {
123         int value = 0;
124 
125         switch ( cookie.length )
126         {
127             case 1:
128                 value = cookie[0] & 0x00FF;
129                 break;
130 
131             case 2:
132                 value = ( ( cookie[0] & 0x00FF ) << 8 ) + ( cookie[1] & 0x00FF );
133                 break;
134 
135             case 3:
136                 value = ( ( cookie[0] & 0x00FF ) << 16 ) + ( ( cookie[1] & 0x00FF ) << 8 ) + ( cookie[2] & 0x00FF );
137                 break;
138 
139             case 4:
140                 value = ( ( cookie[0] & 0x00FF ) << 24 ) + ( ( cookie[1] & 0x00FF ) << 16 )
141                     + ( ( cookie[2] & 0x00FF ) << 8 ) + ( cookie[3] & 0x00FF );
142                 break;
143 
144             default:
145                 break;
146         }
147 
148         return value;
149     }
150 
151 
152     /**
153      * @see Object#hashCode()
154      */
155     @Override
156     public int hashCode()
157     {
158         int h = super.hashCode();
159 
160         h = h * 37 + size;
161 
162         if ( cookie != null )
163         {
164             for ( byte b : cookie )
165             {
166                 h = h * 17 + b;
167             }
168         }
169 
170         return h;
171     }
172 
173 
174     /**
175      * @see Object#equals(Object)
176      */
177     @Override
178     public boolean equals( Object o )
179     {
180         if ( !super.equals( o ) )
181         {
182             return false;
183         }
184 
185         PagedResults otherControl = ( PagedResults ) o;
186 
187         return ( size == otherControl.getSize() ) && Arrays.equals( cookie, otherControl.getCookie() );
188     }
189 
190 
191     /**
192      * Return a String representing this PagedSearchControl.
193      */
194     @Override
195     public String toString()
196     {
197         StringBuilder sb = new StringBuilder();
198 
199         sb.append( "    Paged Search Control\n" );
200         sb.append( "        oid : " ).append( getOid() ).append( '\n' );
201         sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
202         sb.append( "        size   : '" ).append( size ).append( "'\n" );
203         sb.append( "        cookie   : '" ).append( Strings.dumpBytes( cookie ) ).append( "'\n" );
204 
205         return sb.toString();
206     }
207 }