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 */
020 package org.apache.directory.shared.ldap.message.control;
021
022
023 import org.apache.directory.shared.asn1.codec.EncoderException;
024 import org.apache.directory.shared.ldap.codec.search.controls.pagedSearch.PagedSearchControlCodec;
025 import org.slf4j.Logger;
026 import org.slf4j.LoggerFactory;
027
028
029 /**
030 * A request/response control used to implement a simple paging of search
031 * results. This is an implementation of RFC 2696 :
032 * <a href="http://www.faqs.org/rfcs/rfc2696.html">LDAP Control Extension for Simple Paged Results Manipulation</a>
033 * <br/>
034 * <pre>
035 * This control is included in the searchRequest and searchResultDone
036 * messages as part of the controls field of the LDAPMessage, as defined
037 * in Section 4.1.12 of [LDAPv3]. The structure of this control is as
038 * follows:
039 *
040 * pagedResultsControl ::= SEQUENCE {
041 * controlType 1.2.840.113556.1.4.319,
042 * criticality BOOLEAN DEFAULT FALSE,
043 * controlValue searchControlValue
044 * }
045 *
046 * The searchControlValue is an OCTET STRING wrapping the BER-encoded
047 * version of the following SEQUENCE:
048 *
049 * realSearchControlValue ::= SEQUENCE {
050 * size INTEGER (0..maxInt),
051 * -- requested page size from client
052 * -- result set size estimate from server
053 * cookie OCTET STRING
054 * }
055 *
056 * </pre>
057 *
058 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
059 * @version $Rev: 678621 $
060 */
061 public class PagedSearchControl extends InternalAbstractControl
062 {
063 private static final long serialVersionUID = -2356861450876343999L;
064
065 /** This class logger */
066 private static final Logger log = LoggerFactory.getLogger( PagedSearchControl.class );
067
068 /** The Paged Search Control OID */
069 public static final String CONTROL_OID = "1.2.840.113556.1.4.319";
070
071 /** The number of entries to return, or returned */
072 private int size;
073
074 /** The exchanged cookie */
075 private byte[] cookie;
076
077 /**
078 *
079 * Creates a new instance of PagedSearchControl.
080 *
081 */
082 public PagedSearchControl()
083 {
084 super();
085 setID( CONTROL_OID );
086 }
087
088
089 /**
090 * @return The requested or returned number of entries
091 */
092 public int getSize()
093 {
094 return size;
095 }
096
097
098 /**
099 * Set the number of entry requested or returned
100 *
101 * @param size The number of entries
102 */
103 public void setSize( int size )
104 {
105 this.size = size;
106 }
107
108
109 /**
110 * @return The stored cookie
111 */
112 public byte[] getCookie()
113 {
114 return cookie;
115 }
116
117
118 /**
119 * @return The integer value for the current cookie
120 */
121 public int getCookieValue()
122 {
123 int value = ((cookie[0]&0x00FF)<<24) + ((cookie[1]&0x00FF)<<16) + ((cookie[2]&0x00FF)<<8) + (cookie[3]&0x00FF);
124
125 return value;
126 }
127
128
129 /**
130 * Set the cookie
131 *
132 * @param cookie The cookie to store in this control
133 */
134 public void setCookie( byte[] cookie )
135 {
136 this.cookie = cookie;
137 }
138
139
140 /**
141 * Encode the control.
142 */
143 public byte[] getEncodedValue()
144 {
145 // should call this codec or something
146 PagedSearchControlCodec pscc = new PagedSearchControlCodec();
147 pscc.setSize( size );
148 pscc.setCookie( cookie );
149
150 try
151 {
152 return pscc.encode( null ).array();
153 }
154 catch ( EncoderException e )
155 {
156 log.error( "Failed to encode paged search control", e );
157 throw new IllegalStateException( "Failed to encode control with encoder.", e );
158 }
159 }
160 }