PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Novell.Directory.Ldap/Novell.Directory.Ldap.Controls/LdapVirtualListControl.cs

https://bitbucket.org/danipen/mono
C# | 525 lines | 148 code | 58 blank | 319 comment | 9 complexity | 5a50366740539a2b07f038ac97f5f383 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. /******************************************************************************
  2. * The MIT License
  3. * Copyright (c) 2003 Novell Inc. www.novell.com
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the Software), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *******************************************************************************/
  23. //
  24. // Novell.Directory.Ldap.Controls.LdapVirtualListControl.cs
  25. //
  26. // Author:
  27. // Sunil Kumar (Sunilk@novell.com)
  28. //
  29. // (C) 2003 Novell, Inc (http://www.novell.com)
  30. //
  31. using System;
  32. using Novell.Directory.Ldap;
  33. using Novell.Directory.Ldap.Asn1;
  34. namespace Novell.Directory.Ldap.Controls
  35. {
  36. /* The following is the ASN.1 of the VLV Request packet:
  37. *
  38. * VirtualListViewRequest ::= SEQUENCE {
  39. * beforeCount INTEGER (0..maxInt),
  40. * afterCount INTEGER (0..maxInt),
  41. * CHOICE {
  42. * byoffset [0] SEQUENCE {
  43. * offset INTEGER (0 .. maxInt),
  44. * contentCount INTEGER (0 .. maxInt) },
  45. * greaterThanOrEqual [1] AssertionValue },
  46. * contextID OCTET STRING OPTIONAL }
  47. *
  48. */
  49. /// <summary> LdapVirtualListControl is a Server Control used to specify
  50. /// that results from a search are to be returned in pages - which are
  51. /// subsets of the entire virtual result set.
  52. ///
  53. /// On success, an updated LdapVirtualListResponse object is
  54. /// returned as a response Control, containing information on the virtual
  55. /// list size and the actual first index. This object can then be used
  56. /// by the client with a new requested position or length and sent to the
  57. /// server to obtain a different segment of the virtual list.
  58. ///
  59. /// </summary>
  60. public class LdapVirtualListControl:LdapControl
  61. {
  62. /// <summary> Returns the number of entries after the top/center one to return per
  63. /// page of results.
  64. /// </summary>
  65. virtual public int AfterCount
  66. {
  67. get
  68. {
  69. return m_afterCount;
  70. }
  71. }
  72. /// <summary> Returns the number of entries before the top/center one to return per
  73. /// page of results.
  74. /// </summary>
  75. virtual public int BeforeCount
  76. {
  77. get
  78. {
  79. return m_beforeCount;
  80. }
  81. }
  82. /// <summary> Returns the size of the virtual search results list. For a newly
  83. /// constructed control - one which is not the result of parseResponse on
  84. /// a control returned by a server - the method returns -1.
  85. /// </summary>
  86. /// <summary> Sets the assumed size of the virtual search results list. This will
  87. /// typically be a number returned on a previous virtual list request in
  88. /// an LdapVirtualListResponse.
  89. /// </summary>
  90. virtual public int ListSize
  91. {
  92. get
  93. {
  94. return m_contentCount;
  95. }
  96. set
  97. {
  98. m_contentCount = value;
  99. /* since we just changed a field we need to rebuild the ber
  100. * encoded control
  101. */
  102. BuildIndexedVLVRequest();
  103. /* Set the request data field in the in the parent LdapControl to
  104. * the ASN.1 encoded value of this control. This encoding will be
  105. * appended to the search request when the control is sent.
  106. */
  107. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  108. }
  109. }
  110. /// <summary> Returns the cookie used by some servers to optimize the processing of
  111. /// virtual list requests.
  112. /// </summary>
  113. /// <summary> Sets the cookie used by some servers to optimize the processing of
  114. /// virtual list requests. It should be the context field returned in a
  115. /// virtual list response control for the same search.
  116. /// </summary>
  117. virtual public System.String Context
  118. {
  119. get
  120. {
  121. return m_context;
  122. }
  123. set
  124. {
  125. /* Index of the context field if one exists in the ber
  126. */
  127. int CONTEXTIDINDEX = 3;
  128. /* Save off the new value in private variable
  129. */
  130. m_context = value;
  131. /* Is there a context field that is already in the ber
  132. */
  133. if (m_vlvRequest.size() == 4)
  134. {
  135. /* If YES then replace it */
  136. m_vlvRequest.set_Renamed(CONTEXTIDINDEX, new Asn1OctetString(m_context));
  137. }
  138. else if (m_vlvRequest.size() == 3)
  139. {
  140. /* If no then add a new one */
  141. m_vlvRequest.add(new Asn1OctetString(m_context));
  142. }
  143. /* Set the request data field in the in the parent LdapControl to
  144. * the ASN.1 encoded value of this control. This encoding will be
  145. * appended to the search request when the control is sent.
  146. */
  147. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  148. }
  149. }
  150. /* The ASN.1 for the VLV Request has CHOICE field. These private
  151. * variables represent differnt ids for these different options
  152. */
  153. private static int BYOFFSET = 0;
  154. private static int GREATERTHANOREQUAL = 1;
  155. /// <summary> The Request OID for a VLV Request</summary>
  156. private static System.String requestOID = "2.16.840.1.113730.3.4.9";
  157. /*
  158. * The Response stOID for a VLV Response
  159. */
  160. private static System.String responseOID = "2.16.840.1.113730.3.4.10";
  161. /*
  162. * The encoded ASN.1 VLV Control is stored in this variable
  163. */
  164. private Asn1Sequence m_vlvRequest;
  165. /* Private instance variables go here.
  166. * These variables are used to store copies of various fields
  167. * that can be set in a VLV control. One could have managed
  168. * without really defining these private variables by reverse
  169. * engineering each field from the ASN.1 encoded control.
  170. * However that would have complicated and slowed down the code.
  171. */
  172. private int m_beforeCount;
  173. private int m_afterCount;
  174. private System.String m_jumpTo;
  175. private System.String m_context = null;
  176. private int m_startIndex = 0;
  177. private int m_contentCount = - 1;
  178. /// <summary> Constructs a virtual list control using the specified filter
  179. /// expression.
  180. ///
  181. /// The expression specifies the first entry to be used for the
  182. /// virtual search results. The other two paramers are the number of
  183. /// entries before and after a located index to be returned.
  184. ///
  185. /// </summary>
  186. /// <param name="jumpTo"> A search expression that defines the first
  187. /// element to be returned in the virtual search results. The filter
  188. /// expression in the search operation itself may be, for example,
  189. /// "objectclass=person" and the jumpTo expression in the virtual
  190. /// list control may be "cn=m*", to retrieve a subset of entries
  191. /// starting at or centered around those with a common name beginning
  192. /// with the letter "M".
  193. ///
  194. /// </param>
  195. /// <param name="beforeCount"> The number of entries before startIndex (the
  196. /// reference entry) to be returned.
  197. ///
  198. /// </param>
  199. /// <param name="afterCount"> The number of entries after startIndex to be
  200. /// returned.
  201. /// </param>
  202. public LdapVirtualListControl(System.String jumpTo, int beforeCount, int afterCount):this(jumpTo, beforeCount, afterCount, null)
  203. {
  204. return ;
  205. }
  206. /// <summary> Constructs a virtual list control using the specified filter
  207. /// expression along with an optional server context.
  208. ///
  209. /// The expression specifies the first entry to be used for the
  210. /// virtual search results. The other two paramers are the number of
  211. /// entries before and after a located index to be returned.
  212. ///
  213. /// </summary>
  214. /// <param name="jumpTo"> A search expression that defines the first
  215. /// element to be returned in the virtual search results. The filter
  216. /// expression in the search operation itself may be, for example,
  217. /// "objectclass=person" and the jumpTo expression in the virtual
  218. /// list control may be "cn=m*", to retrieve a subset of entries
  219. /// starting at or centered around those with a common name beginning
  220. /// with the letter "M".
  221. ///
  222. /// </param>
  223. /// <param name="beforeCount">The number of entries before startIndex (the
  224. /// reference entry) to be returned.
  225. ///
  226. /// </param>
  227. /// <param name="afterCount">The number of entries after startIndex to be
  228. /// returned.
  229. ///
  230. /// </param>
  231. /// <param name="context">Used by some implementations to process requests
  232. /// more efficiently. The context should be null on the first search,
  233. /// and thereafter it should be whatever was returned by the server in the
  234. /// virtual list response control.
  235. /// </param>
  236. public LdapVirtualListControl(System.String jumpTo, int beforeCount, int afterCount, System.String context):base(requestOID, true, null)
  237. {
  238. /* Save off the fields in local variables
  239. */
  240. m_beforeCount = beforeCount;
  241. m_afterCount = afterCount;
  242. m_jumpTo = jumpTo;
  243. m_context = context;
  244. /* Call private method to build the ASN.1 encoded request packet.
  245. */
  246. BuildTypedVLVRequest();
  247. /* Set the request data field in the in the parent LdapControl to
  248. * the ASN.1 encoded value of this control. This encoding will be
  249. * appended to the search request when the control is sent.
  250. */
  251. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  252. return ;
  253. }
  254. /// <summary>Private method used to construct the ber encoded control
  255. /// Used only when using the typed mode of VLV Control.
  256. /// </summary>
  257. private void BuildTypedVLVRequest()
  258. {
  259. /* Create a new Asn1Sequence object */
  260. m_vlvRequest = new Asn1Sequence(4);
  261. /* Add the beforeCount and afterCount fields to the sequence */
  262. m_vlvRequest.add(new Asn1Integer(m_beforeCount));
  263. m_vlvRequest.add(new Asn1Integer(m_afterCount));
  264. /* The next field is dependent on the type of indexing being used.
  265. * A "typed" VLV request uses a ASN.1 OCTET STRING to index to the
  266. * correct object in the list. Encode the ASN.1 CHOICE corresponding
  267. * to this option (as indicated by the greaterthanOrEqual field)
  268. * in the ASN.1.
  269. */
  270. m_vlvRequest.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, false, GREATERTHANOREQUAL), new Asn1OctetString(m_jumpTo), false));
  271. /* Add the optional context string if one is available.
  272. */
  273. if ((System.Object) m_context != null)
  274. m_vlvRequest.add(new Asn1OctetString(m_context));
  275. return ;
  276. }
  277. /// <summary> Use this constructor to fetch a subset when the size of the
  278. /// virtual list is known,
  279. ///
  280. ///
  281. /// </summary>
  282. /// <param name="beforeCount">The number of entries before startIndex (the
  283. /// reference entry) to be returned.
  284. ///
  285. /// </param>
  286. /// <param name="afterCount"> The number of entries after startIndex to be
  287. /// returned.
  288. ///
  289. /// </param>
  290. /// <param name="startIndex">The index of the reference entry to be returned.
  291. ///
  292. /// </param>
  293. /// <param name="contentCount">The total number of entries assumed to be in the
  294. /// list. This is a number returned on a previous search, in the
  295. /// LdapVirtualListResponse. The server may use this number to adjust
  296. /// the returned subset offset.
  297. /// </param>
  298. public LdapVirtualListControl(int startIndex, int beforeCount, int afterCount, int contentCount):this(startIndex, beforeCount, afterCount, contentCount, null)
  299. {
  300. return ;
  301. }
  302. /// <summary> Use this constructor to fetch a subset when the size of the
  303. /// virtual list is known,
  304. ///
  305. ///
  306. /// </summary>
  307. /// <param name="beforeCount"> The number of entries before startIndex (the
  308. /// reference entry) to be returned.
  309. ///
  310. /// </param>
  311. /// <param name="afterCount"> The number of entries after startIndex to be
  312. /// returned.
  313. ///
  314. /// </param>
  315. /// <param name="startIndex"> The index of the reference entry to be
  316. /// returned.
  317. ///
  318. /// </param>
  319. /// <param name="contentCount"> The total number of entries assumed to be in the
  320. /// list. This is a number returned on a previous search, in the
  321. /// LdapVirtualListResponse. The server may use this number to adjust
  322. /// the returned subset offset.
  323. ///
  324. /// </param>
  325. /// <param name="context"> Used by some implementations to process requests
  326. /// more efficiently. The context should be null on the first search,
  327. /// and thereafter it should be whatever was returned by the server in the
  328. /// virtual list response control.
  329. /// </param>
  330. public LdapVirtualListControl(int startIndex, int beforeCount, int afterCount, int contentCount, System.String context):base(requestOID, true, null)
  331. {
  332. /* Save off the fields in local variables
  333. */
  334. m_beforeCount = beforeCount;
  335. m_afterCount = afterCount;
  336. m_startIndex = startIndex;
  337. m_contentCount = contentCount;
  338. m_context = context;
  339. /* Call private method to build the ASN.1 encoded request packet.
  340. */
  341. BuildIndexedVLVRequest();
  342. /* Set the request data field in the in the parent LdapControl to
  343. * the ASN.1 encoded value of this control. This encoding will be
  344. * appended to the search request when the control is sent.
  345. */
  346. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  347. return ;
  348. }
  349. /// <summary>Private method used to construct the ber encoded control
  350. /// Used only when using the Indexed mode of VLV Control
  351. /// </summary>
  352. private void BuildIndexedVLVRequest()
  353. {
  354. /* Create a new Asn1Sequence object */
  355. m_vlvRequest = new Asn1Sequence(4);
  356. /* Add the beforeCount and afterCount fields to the sequence */
  357. m_vlvRequest.add(new Asn1Integer(m_beforeCount));
  358. m_vlvRequest.add(new Asn1Integer(m_afterCount));
  359. /* The next field is dependent on the type of indexing being used.
  360. * An "indexed" VLV request uses a ASN.1 SEQUENCE to index to the
  361. * correct object in the list. Encode the ASN.1 CHOICE corresponding
  362. * to this option (as indicated by the byoffset fieldin the ASN.1.
  363. */
  364. Asn1Sequence byoffset = new Asn1Sequence(2);
  365. byoffset.add(new Asn1Integer(m_startIndex));
  366. byoffset.add(new Asn1Integer(m_contentCount)); ;
  367. /* Add the ASN.1 sequence to the encoded data
  368. */
  369. m_vlvRequest.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, true, BYOFFSET), byoffset, false));
  370. /* Add the optional context string if one is available.
  371. */
  372. if ((System.Object) m_context != null)
  373. m_vlvRequest.add(new Asn1OctetString(m_context));
  374. return ;
  375. }
  376. /// <summary> Sets the center or starting list index to return, and the number of
  377. /// results before and after.
  378. ///
  379. ///
  380. /// </summary>
  381. /// <param name="listIndex"> The center or starting list index to be
  382. /// returned.
  383. ///
  384. /// </param>
  385. /// <param name="beforeCount"> The number of entries before "listIndex" to be
  386. /// returned.
  387. ///
  388. /// </param>
  389. /// <param name="afterCount"> The number of entries after "listIndex" to be
  390. /// returned.
  391. /// </param>
  392. public virtual void setRange(int listIndex, int beforeCount, int afterCount)
  393. {
  394. /* Save off the fields in local variables
  395. */
  396. m_beforeCount = beforeCount;
  397. m_afterCount = afterCount;
  398. m_startIndex = listIndex;
  399. /* since we just changed a field we need to rebuild the ber
  400. * encoded control
  401. */
  402. BuildIndexedVLVRequest();
  403. /* Set the request data field in the in the parent LdapControl to
  404. * the ASN.1 encoded value of this control. This encoding will be
  405. * appended to the search request when the control is sent.
  406. */
  407. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  408. }
  409. // PROPOSED ADDITION TO NEXT VERSION OF DRAFT (v7)
  410. /// <summary> Sets the center or starting list index to return, and the number of
  411. /// results before and after.
  412. ///
  413. ///
  414. /// </summary>
  415. /// <param name="jumpTo">A search expression that defines the first
  416. /// element to be returned in the virtual search results. The filter
  417. /// expression in the search operation itself may be, for example,
  418. /// "objectclass=person" and the jumpTo expression in the virtual
  419. /// list control may be "cn=m*", to retrieve a subset of entries
  420. /// starting at or centered around those with a common name
  421. /// beginning with the letter "M".
  422. ///
  423. /// </param>
  424. /// <param name="beforeCount"> The number of entries before "listIndex" to be
  425. /// returned.
  426. ///
  427. /// </param>
  428. /// <param name="afterCount">The number of entries after "listIndex" to be
  429. /// returned.
  430. /// </param>
  431. public virtual void setRange(System.String jumpTo, int beforeCount, int afterCount)
  432. {
  433. /* Save off the fields in local variables
  434. */
  435. m_beforeCount = beforeCount;
  436. m_afterCount = afterCount;
  437. m_jumpTo = jumpTo;
  438. /* since we just changed a field we need to rebuild the ber
  439. * encoded control
  440. */
  441. BuildTypedVLVRequest();
  442. /* Set the request data field in the in the parent LdapControl to
  443. * the ASN.1 encoded value of this control. This encoding will be
  444. * appended to the search request when the control is sent.
  445. */
  446. setValue(m_vlvRequest.getEncoding(new LBEREncoder()));
  447. }
  448. static LdapVirtualListControl()
  449. {
  450. /*
  451. * This is where we register the control responses
  452. */
  453. {
  454. /* Register the VLV Sort Control class which is returned by the server
  455. * in response to a VLV Sort Request
  456. */
  457. try
  458. {
  459. LdapControl.register(responseOID, System.Type.GetType("Novell.Directory.Ldap.Controls.LdapVirtualListResponse"));
  460. }
  461. catch (System.Exception e)
  462. {
  463. }
  464. }
  465. }
  466. }
  467. }