1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ldap.handlers.controls;
21
22
23 import java.util.HashSet;
24 import java.util.Set;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
29 import org.apache.directory.api.ldap.model.cursor.Cursor;
30 import org.apache.directory.api.ldap.model.entry.Entry;
31 import org.apache.directory.api.ldap.model.exception.LdapException;
32 import org.apache.directory.api.ldap.model.message.SearchRequest;
33 import org.apache.directory.api.ldap.model.name.Dn;
34 import org.apache.directory.api.ldap.model.schema.AttributeType;
35 import org.apache.directory.api.ldap.model.schema.SchemaManager;
36 import org.apache.directory.api.util.Strings;
37 import org.apache.directory.server.ldap.LdapSession;
38
39
40
41
42
43
44
45
46
47 public class PagedSearchContext
48 {
49
50 private SearchRequest previousSearchRequest;
51
52
53 private int currentPosition;
54
55
56 private byte[] cookie;
57
58
59 private AtomicInteger cookieValue;
60
61
62 private Cursor<Entry> cursor;
63
64
65
66
67
68
69
70 public PagedSearchContext( SearchRequest searchRequest )
71 {
72 previousSearchRequest = searchRequest;
73 currentPosition = 0;
74
75
76
77
78 cookieValue = new AtomicInteger( searchRequest.getMessageId() << 16 );
79
80 cookie = BerValue.getBytes( cookieValue.get() );
81 }
82
83
84
85
86
87
88
89
90
91 public byte[] getCookie()
92 {
93 return cookie;
94 }
95
96
97 public int getCookieValue()
98 {
99 return cookieValue.get();
100 }
101
102
103
104
105
106
107
108
109 public byte[] getNewCookie()
110 {
111 cookie = BerValue.getBytes( cookieValue.incrementAndGet() );
112
113 return cookie;
114 }
115
116
117
118
119
120 private Set<String> buildAttributeSet( SearchRequest request,
121 SchemaManager schemaManager )
122 {
123 Set<String> requestSet = new HashSet<>();
124
125
126 for ( String attribute : request.getAttributes() )
127 {
128 try
129 {
130 AttributeType at = schemaManager.lookupAttributeTypeRegistry( attribute );
131 requestSet.add( at.getOid() );
132 }
133 catch ( LdapException le )
134 {
135
136 if ( attribute.equals( SchemaConstants.ALL_OPERATIONAL_ATTRIBUTES )
137 || attribute.equals( SchemaConstants.ALL_USER_ATTRIBUTES )
138 || attribute.equals( SchemaConstants.NO_ATTRIBUTE ) )
139 {
140 requestSet.add( attribute );
141 }
142
143
144 }
145 }
146
147 return requestSet;
148 }
149
150
151
152
153
154
155
156
157
158
159 public boolean hasSameRequest( SearchRequest request, LdapSession session )
160 {
161
162 if ( request.getScope() != previousSearchRequest.getScope() )
163 {
164 return false;
165 }
166
167
168 if ( request.getSizeLimit() != previousSearchRequest.getSizeLimit() )
169 {
170 return false;
171 }
172
173
174 if ( request.getTimeLimit() != previousSearchRequest.getTimeLimit() )
175 {
176 return false;
177 }
178
179
180 if ( request.getTypesOnly() != previousSearchRequest.getTypesOnly() )
181 {
182 return false;
183 }
184
185
186 if ( request.getDerefAliases() != previousSearchRequest.getDerefAliases() )
187 {
188 return false;
189 }
190
191 SchemaManager schemaManager =
192 session.getLdapServer().getDirectoryService().getSchemaManager();
193
194
195 if ( request.getAttributes() == null )
196 {
197 if ( previousSearchRequest.getAttributes() != null )
198 {
199 return false;
200 }
201 }
202 else
203 {
204 if ( previousSearchRequest.getAttributes() == null )
205 {
206 return false;
207 }
208 else
209 {
210
211 if ( request.getAttributes().size() != previousSearchRequest.getAttributes().size() )
212 {
213 return false;
214 }
215
216
217 Set<String> requestSet = buildAttributeSet( request, schemaManager );
218 Set<String> previousRequestSet = buildAttributeSet( previousSearchRequest, schemaManager );
219
220
221
222 if ( requestSet.size() != previousRequestSet.size() )
223 {
224 return false;
225 }
226
227 for ( String attribute : requestSet )
228 {
229 previousRequestSet.remove( attribute );
230 }
231
232
233 if ( !previousRequestSet.isEmpty() )
234 {
235 return false;
236 }
237 }
238 }
239
240
241 try
242 {
243 if ( !request.getBase().isSchemaAware() )
244 {
245 request.setBase( new Dn( schemaManager, request.getBase() ) );
246 }
247
248 if ( !previousSearchRequest.getBase().isSchemaAware() )
249 {
250 previousSearchRequest.setBase( new Dn( schemaManager, previousSearchRequest.getBase() ) );
251 }
252
253 if ( !request.getBase().equals( previousSearchRequest.getBase() ) )
254 {
255 return false;
256 }
257 }
258 catch ( LdapException le )
259 {
260 return false;
261 }
262
263
264
265
266
267
268 return true;
269 }
270
271
272
273
274
275
276 public int getCurrentPosition()
277 {
278 return currentPosition;
279 }
280
281
282
283
284
285
286
287
288 public void incrementCurrentPosition( int returnedEntries )
289 {
290 this.currentPosition += returnedEntries;
291 }
292
293
294
295
296
297 public SearchRequest getPreviousSearchRequest()
298 {
299 return previousSearchRequest;
300 }
301
302
303
304
305
306 public Cursor<Entry> getCursor()
307 {
308 return cursor;
309 }
310
311
312
313
314
315
316 public void setCursor( Cursor<Entry> cursor )
317 {
318 this.cursor = cursor;
319 }
320
321
322
323
324
325 public String toString()
326 {
327 StringBuilder sb = new StringBuilder();
328
329 sb.append( "PagedSearch context : <" );
330 sb.append( Strings.dumpBytes( cookie ) );
331 sb.append( ", " );
332 sb.append( currentPosition );
333 sb.append( ">" );
334
335 return sb.toString();
336 }
337 }