1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Controls.LdapSortControl.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
33 using Novell.Directory.Ldap;
34 using Novell.Directory.Ldap.Asn1;
36 namespace Novell.Directory.Ldap.Controls
39 /// <summary> LdapSortControl is a Server Control to specify how search results are
40 /// to be sorted by the server. If a server does not support
41 /// sorting in general or for a particular query, the results will be
42 /// returned unsorted, along with a control indicating why they were not
43 /// sorted (or that sort controls are not supported). If the control was
44 /// marked "critical", the whole search operation will fail if the sort
45 /// control is not supported.
47 public class LdapSortControl:LdapControl
50 private static int ORDERING_RULE = 0;
51 private static int REVERSE_ORDER = 1;
52 /// <summary> The requestOID of the sort control</summary>
53 private static System.String requestOID = "1.2.840.113556.1.4.473";
55 /// <summary> The responseOID of the sort control</summary>
56 private static System.String responseOID = "1.2.840.113556.1.4.474";
57 /// <summary> Constructs a sort control with a single key.
60 /// <param name="key"> A sort key object, which specifies attribute,
61 /// order, and optional matching rule.
64 /// <param name="critical True">if the search operation is to fail if the
65 /// server does not support this control.
67 public LdapSortControl(LdapSortKey key, bool critical):this(new LdapSortKey[]{key}, critical)
72 /// <summary> Constructs a sort control with multiple sort keys.
75 /// <param name="keys An">array of sort key objects, to be processed in
79 /// <param name="critical True">if the search operation is to fail if the
80 /// server does not support this control.
82 public LdapSortControl(LdapSortKey[] keys, bool critical):base(requestOID, critical, null)
85 Asn1SequenceOf sortKeyList = new Asn1SequenceOf();
87 for (int i = 0; i < keys.Length; i++)
90 Asn1Sequence key = new Asn1Sequence();
92 key.add(new Asn1OctetString(keys[i].Key));
94 if ((System.Object) keys[i].MatchRule != null)
96 key.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, false, ORDERING_RULE), new Asn1OctetString(keys[i].MatchRule), false));
99 if (keys[i].Reverse == true)
102 key.add(new Asn1Tagged(new Asn1Identifier(Asn1Identifier.CONTEXT, false, REVERSE_ORDER), new Asn1Boolean(true), false));
105 sortKeyList.add(key);
108 setValue(sortKeyList.getEncoding(new LBEREncoder()));
111 static LdapSortControl()
114 * This is where we register the control responses
118 * Register the Server Sort Control class which is returned by the
119 * server in response to a Sort Request
123 LdapControl.register(responseOID, System.Type.GetType("Novell.Directory.Ldap.Controls.LdapSortResponse"));
125 catch (System.Exception e)