001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.message.controls;
021
022
023import org.apache.directory.api.util.Strings;
024
025import java.util.Arrays;
026
027
028/**
029 * A request/response control used to implement a simple paging of search
030 * results. This is an implementation of RFC 2696 :
031 * <a href="http://www.faqs.org/rfcs/rfc2696.html">LDAP Control Extension for Simple Paged Results Manipulation</a>
032 * <br>
033 * <pre>
034 *    This control is included in the searchRequest and searchResultDone
035 *    messages as part of the controls field of the LDAPMessage, as defined
036 *    in Section 4.1.12 of [LDAPv3]. The structure of this control is as
037 *    follows:
038 *
039 * pagedResultsControl ::= SEQUENCE {
040 *         controlType     1.2.840.113556.1.4.319,
041 *         criticality     BOOLEAN DEFAULT FALSE,
042 *         controlValue    searchControlValue
043 * }
044 * 
045 * The searchControlValue is an OCTET STRING wrapping the BER-encoded
046 * version of the following SEQUENCE:
047 * 
048 * realSearchControlValue ::= SEQUENCE {
049 *         size            INTEGER (0..maxInt),
050 *                                 -- requested page size from client
051 *                                 -- result set size estimate from server
052 *         cookie          OCTET STRING
053 * }
054 * 
055 * </pre>
056 * 
057 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
058 */
059public class PagedResultsImpl extends AbstractControl implements PagedResults
060{
061    /** The number of entries to return, or returned */
062    private int size;
063
064    /** The exchanged cookie */
065    private byte[] cookie = Strings.EMPTY_BYTES;
066
067
068    /**
069     * Creates a new instance of PagedResultsDecorator.
070     */
071    public PagedResultsImpl()
072    {
073        super( OID );
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public int getSize()
082    {
083        return size;
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public void setSize( int size )
092    {
093        this.size = size;
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
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}