File: LdapVirtualListControl.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (525 lines) | stat: -rw-r--r-- 18,048 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/******************************************************************************
* The MIT License
* Copyright (c) 2003 Novell Inc.  www.novell.com
* 
* Permission is hereby granted, free of charge, to any person obtaining  a copy
* of this software and associated documentation files (the Software), to deal
* in the Software without restriction, including  without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
* copies of the Software, and to  permit persons to whom the Software is 
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in 
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
//
// Novell.Directory.Ldap.Controls.LdapVirtualListControl.cs
//
// Author:
//   Sunil Kumar (Sunilk@novell.com)
//
// (C) 2003 Novell, Inc (http://www.novell.com)
//

using System;
using Novell.Directory.Ldap;
using Novell.Directory.Ldap.Asn1;

namespace Novell.Directory.Ldap.Controls
{
	
	/* The following is the ASN.1 of the VLV Request packet:
	*
	* VirtualListViewRequest ::= SEQUENCE {
	*      beforeCount    INTEGER (0..maxInt),
	*         afterCount     INTEGER (0..maxInt),
	*      CHOICE {
	*          byoffset [0] SEQUENCE {
	*              offset          INTEGER (0 .. maxInt),
	*              contentCount    INTEGER (0 .. maxInt) },
	*          greaterThanOrEqual [1] AssertionValue },
	*      contextID     OCTET STRING OPTIONAL }
	*
	*/
	
	/// <summary> LdapVirtualListControl is a Server Control used to specify
	/// that results from a search are to be returned in pages - which are
	/// subsets of the entire virtual result set.
	/// 
	/// On success, an updated LdapVirtualListResponse object is
	/// returned as a response Control, containing information on the virtual
	/// list size and the actual first index. This object can then be used
	/// by the client with a new requested position or length and sent to the
	/// server to obtain a different segment of the virtual list.
	/// 
	/// </summary>
	public class LdapVirtualListControl:LdapControl
	{
		/// <summary>    Returns the number of entries after the top/center one to return per
		/// page of results.
		/// </summary>
		virtual public int AfterCount
		{
			get
			{
				return m_afterCount;
			}
			
		}
		/// <summary>    Returns the number of entries before the top/center one to return per
		/// page of results.
		/// </summary>
		virtual public int BeforeCount
		{
			get
			{
				return m_beforeCount;
			}
			
		}
		/// <summary>    Returns the size of the virtual search results list. For a newly
		/// constructed control - one which is not the result of parseResponse on
		/// a control returned by a server - the method returns -1.
		/// </summary>
		/// <summary>    Sets the assumed size of the virtual search results list. This will
		/// typically be a number returned on a previous virtual list request in
		/// an LdapVirtualListResponse.
		/// </summary>
		virtual public int ListSize
		{
			get
			{
				return m_contentCount;
			}
			
			set
			{
				m_contentCount = value;
				
				/* since we just changed a field we need to rebuild the ber
				* encoded control
				*/
				BuildIndexedVLVRequest();
				
				/* Set the request data field in the in the parent LdapControl to
				* the ASN.1 encoded value of this control.  This encoding will be
				* appended to the search request when the control is sent.
				*/
				setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
			}
			
		}
		/// <summary>   Returns the cookie used by some servers to optimize the processing of
		/// virtual list requests.
		/// </summary>
		/// <summary>    Sets the cookie used by some servers to optimize the processing of
		/// virtual list requests. It should be the context field returned in a
		/// virtual list response control for the same search.
		/// </summary>
		virtual public System.String Context
		{
			get
			{
				return m_context;
			}
			
			set
			{
				/* Index of the context field if one exists in the ber
				*/
				int CONTEXTIDINDEX = 3;
				
				/* Save off the new value in private variable
				*/
				m_context = value;
				
				/* Is there a context field that is already in the ber
				*/
				if (m_vlvRequest.size() == 4)
				{
					/* If YES then replace it */
					m_vlvRequest.set_Renamed(CONTEXTIDINDEX, new Asn1OctetString(m_context));
				}
				else if (m_vlvRequest.size() == 3)
				{
					/* If no then add a new one */
					m_vlvRequest.add(new Asn1OctetString(m_context));
				}
				
				/* Set the request data field in the in the parent LdapControl to
				* the ASN.1 encoded value of this control.  This encoding will be
				* appended to the search request when the control is sent.
				*/
				setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
			}
			
		}
		
		/* The ASN.1 for the VLV Request has CHOICE field. These private
		* variables represent differnt ids for these different options
		*/
		private static int BYOFFSET = 0;
		private static int GREATERTHANOREQUAL = 1;
		
		
		/// <summary> The Request OID for a VLV Request</summary>
		private static System.String requestOID = "2.16.840.1.113730.3.4.9";
		
		/*
		* The Response stOID for a VLV Response
		*/
		private static System.String responseOID = "2.16.840.1.113730.3.4.10";
		
		/*
		* The encoded ASN.1 VLV Control is stored in this variable
		*/
		private Asn1Sequence m_vlvRequest;
		
		
		/* Private instance variables go here.
		* These variables are used to store copies of various fields
		* that can be set in a VLV control. One could have managed
		* without really defining these private variables by reverse
		* engineering each field from the ASN.1 encoded control.
		* However that would have complicated and slowed down the code.
		*/
		private int m_beforeCount;
		private int m_afterCount;
		private System.String m_jumpTo;
		private System.String m_context = null;
		private int m_startIndex = 0;
		private int m_contentCount = - 1;
		
		/// <summary> Constructs a virtual list control using the specified filter
		/// expression.
		/// 
		/// The expression specifies the first entry to be used for the
		/// virtual search results. The other two paramers are the number of
		/// entries before and after a located index to be returned.
		/// 
		/// </summary>
		/// <param name="jumpTo">           A search expression that defines the first
		/// element to be returned in the virtual search results. The filter
		/// expression in the search operation itself may be, for example,
		/// "objectclass=person" and the jumpTo expression in the virtual
		/// list control may be "cn=m*", to retrieve a subset of entries
		/// starting at or centered around those with a common name beginning
		/// with the letter "M". 
		/// 
		/// </param>
		/// <param name="beforeCount">   The number of entries before startIndex (the
		/// reference entry) to be returned. 
		/// 
		/// </param>
		/// <param name="afterCount">       The number of entries after startIndex to be
		/// returned. 
		/// </param>
		public LdapVirtualListControl(System.String jumpTo, int beforeCount, int afterCount):this(jumpTo, beforeCount, afterCount, null)
		{
			return ;
		}
		
		
		
		/// <summary> Constructs a virtual list control using the specified filter
		/// expression along with an optional server context.
		/// 
		/// The expression specifies the first entry to be used for the
		/// virtual search results. The other two paramers are the number of
		/// entries before and after a located index to be returned.
		/// 
		/// </summary>
		/// <param name="jumpTo">   A search expression that defines the first
		/// element to be returned in the virtual search results. The filter
		/// expression in the search operation itself may be, for example,
		/// "objectclass=person" and the jumpTo expression in the virtual
		/// list control may be "cn=m*", to retrieve a subset of entries
		/// starting at or centered around those with a common name beginning
		/// with the letter "M".
		/// 
		/// </param>
		/// <param name="beforeCount">The number of entries before startIndex (the
		/// reference entry) to be returned. 
		/// 
		/// </param>
		/// <param name="afterCount">The number of entries after startIndex to be
		/// returned. 
		/// 
		/// </param>
		/// <param name="context">Used by some implementations to process requests
		/// more efficiently. The context should be null on the first search,
		/// and thereafter it should be whatever was returned by the server in the
		/// virtual list response control.
		/// </param>
		public LdapVirtualListControl(System.String jumpTo, int beforeCount, int afterCount, System.String context):base(requestOID, true, null)
		{
			
			/* Save off the fields in local variables
			*/
			m_beforeCount = beforeCount;
			m_afterCount = afterCount;
			m_jumpTo = jumpTo;
			m_context = context;
			
			/* Call private method to build the ASN.1 encoded request packet.
			*/
			BuildTypedVLVRequest();
			
			/* Set the request data field in the in the parent LdapControl to
			* the ASN.1 encoded value of this control.  This encoding will be
			* appended to the search request when the control is sent.
			*/
			setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
			return ;
		}
		
		/// <summary>Private method used to construct the ber encoded control
		/// Used only when using the typed mode of VLV Control.
		/// </summary>
		private void  BuildTypedVLVRequest()
		{
			/* Create a new Asn1Sequence object */
			m_vlvRequest = new Asn1Sequence(4);
			
			/* Add the beforeCount and afterCount fields to the sequence */
			m_vlvRequest.add(new Asn1Integer(m_beforeCount));
			m_vlvRequest.add(new Asn1Integer(m_afterCount));
			
			/* The next field is dependent on the type of indexing being used.
			* A "typed" VLV request uses a ASN.1 OCTET STRING to index to the
			* correct object in the list.  Encode the ASN.1 CHOICE corresponding
			* to this option (as indicated by the greaterthanOrEqual field)
			* in the ASN.1.
			*/
			m_vlvRequest.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, false, GREATERTHANOREQUAL), new Asn1OctetString(m_jumpTo), false));
			
			/* Add the optional context string if one is available.
			*/
			if ((System.Object) m_context != null)
				m_vlvRequest.add(new Asn1OctetString(m_context));
			
			return ;
		}
		
		/// <summary> Use this constructor to fetch a subset when the size of the
		/// virtual list is known,
		/// 
		/// 
		/// </summary>
		/// <param name="beforeCount">The number of entries before startIndex (the
		/// reference entry) to be returned. 
		/// 
		/// </param>
		/// <param name="afterCount">   The number of entries after startIndex to be
		/// returned.
		/// 
		/// </param>
		/// <param name="startIndex">The index of the reference entry to be returned.
		/// 
		/// </param>
		/// <param name="contentCount">The total number of entries assumed to be in the
		/// list. This is a number returned on a previous search, in the
		/// LdapVirtualListResponse. The server may use this number to adjust
		/// the returned subset offset.
		/// </param>
		public LdapVirtualListControl(int startIndex, int beforeCount, int afterCount, int contentCount):this(startIndex, beforeCount, afterCount, contentCount, null)
		{
			return ;
		}
		
		
		
		/// <summary> Use this constructor to fetch a subset when the size of the
		/// virtual list is known,
		/// 
		/// 
		/// </summary>
		/// <param name="beforeCount">   The number of entries before startIndex (the
		/// reference entry) to be returned.
		/// 
		/// </param>
		/// <param name="afterCount">       The number of entries after startIndex to be
		/// returned.
		/// 
		/// </param>
		/// <param name="startIndex">    The index of the reference entry to be
		/// returned.
		/// 
		/// </param>
		/// <param name="contentCount">   The total number of entries assumed to be in the
		/// list. This is a number returned on a previous search, in the
		/// LdapVirtualListResponse. The server may use this number to adjust
		/// the returned subset offset.
		/// 
		/// </param>
		/// <param name="context">       Used by some implementations to process requests
		/// more efficiently. The context should be null on the first search,
		/// and thereafter it should be whatever was returned by the server in the
		/// virtual list response control.
		/// </param>
		public LdapVirtualListControl(int startIndex, int beforeCount, int afterCount, int contentCount, System.String context):base(requestOID, true, null)
		{
			
			
			/* Save off the fields in local variables
			*/
			m_beforeCount = beforeCount;
			m_afterCount = afterCount;
			m_startIndex = startIndex;
			m_contentCount = contentCount;
			m_context = context;
			
			/* Call private method to build the ASN.1 encoded request packet.
			*/
			BuildIndexedVLVRequest();
			
			/* Set the request data field in the in the parent LdapControl to
			* the ASN.1 encoded value of this control.  This encoding will be
			* appended to the search request when the control is sent.
			*/
			setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
			return ;
		}
		
		/// <summary>Private method used to construct the ber encoded control
		/// Used only when using the Indexed mode of VLV Control
		/// </summary>
		private void  BuildIndexedVLVRequest()
		{
			/* Create a new Asn1Sequence object */
			m_vlvRequest = new Asn1Sequence(4);
			
			/* Add the beforeCount and afterCount fields to the sequence */
			m_vlvRequest.add(new Asn1Integer(m_beforeCount));
			m_vlvRequest.add(new Asn1Integer(m_afterCount));
			
			/* The next field is dependent on the type of indexing being used.
			* An "indexed" VLV request uses a ASN.1 SEQUENCE to index to the
			* correct object in the list.  Encode the ASN.1 CHOICE corresponding
			* to this option (as indicated by the byoffset fieldin the ASN.1.
			*/
			Asn1Sequence byoffset = new Asn1Sequence(2);
			byoffset.add(new Asn1Integer(m_startIndex));
			byoffset.add(new Asn1Integer(m_contentCount)); ;
			
			/* Add the ASN.1 sequence to the encoded data
			*/
			m_vlvRequest.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, true, BYOFFSET), byoffset, false));
			
			/* Add the optional context string if one is available.
			*/
			if ((System.Object) m_context != null)
				m_vlvRequest.add(new Asn1OctetString(m_context));
			
			return ;
		}
		
		
		
		/// <summary> Sets the center or starting list index to return, and the number of
		/// results before and after.
		/// 
		/// 
		/// </summary>
		/// <param name="listIndex">       The center or starting list index to be
		/// returned. 
		/// 
		/// </param>
		/// <param name="beforeCount">       The number of entries before "listIndex" to be
		/// returned. 
		/// 
		/// </param>
		/// <param name="afterCount">       The number of entries after "listIndex" to be
		/// returned. 
		/// </param>
		public virtual void  setRange(int listIndex, int beforeCount, int afterCount)
		{
			
			/* Save off the fields in local variables
			*/
			m_beforeCount = beforeCount;
			m_afterCount = afterCount;
			m_startIndex = listIndex;
			
			/* since we just changed a field we need to rebuild the ber
			* encoded control
			*/
			BuildIndexedVLVRequest();
			
			/* Set the request data field in the in the parent LdapControl to
			* the ASN.1 encoded value of this control.  This encoding will be
			* appended to the search request when the control is sent.
			*/
			setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
		}
		
		// PROPOSED ADDITION TO NEXT VERSION OF DRAFT (v7)
		/// <summary> Sets the center or starting list index to return, and the number of
		/// results before and after.
		/// 
		/// 
		/// </summary>
		/// <param name="jumpTo">A search expression that defines the first
		/// element to be returned in the virtual search results. The filter
		/// expression in the search operation itself may be, for example,
		/// "objectclass=person" and the jumpTo expression in the virtual
		/// list control may be "cn=m*", to retrieve a subset of entries
		/// starting at or centered around those with a common name
		/// beginning with the letter "M".
		/// 
		/// </param>
		/// <param name="beforeCount">   The number of entries before "listIndex" to be
		/// returned.
		/// 
		/// </param>
		/// <param name="afterCount">The number of entries after "listIndex" to be
		/// returned.
		/// </param>
		
		public virtual void  setRange(System.String jumpTo, int beforeCount, int afterCount)
		{
			/* Save off the fields in local variables
			*/
			m_beforeCount = beforeCount;
			m_afterCount = afterCount;
			m_jumpTo = jumpTo;
			
			/* since we just changed a field we need to rebuild the ber
			* encoded control
			*/
			BuildTypedVLVRequest();
			
			/* Set the request data field in the in the parent LdapControl to
			* the ASN.1 encoded value of this control.  This encoding will be
			* appended to the search request when the control is sent.
			*/
			setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
		}
		static LdapVirtualListControl()
		{
			/*
			* This is where we register the control responses
			*/
			{
				/* Register the VLV Sort Control class which is returned by the server
				* in response to a VLV Sort Request
				*/
				try
				{
					LdapControl.register(responseOID, System.Type.GetType("Novell.Directory.Ldap.Controls.LdapVirtualListResponse"));
				}
				catch (System.Exception e)
				{
				}
			}
		}
	}
}