Class files implementing Various LDAP Controls
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Controls / LdapPersistSearchControl.cs
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.LdapPersistSearchControl.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31 \r
32 using System;\r
33 using Novell.Directory.Ldap;\r
34 using Novell.Directory.Ldap.Asn1;\r
35 \r
36 namespace Novell.Directory.Ldap.Controls\r
37 {\r
38         \r
39         \r
40         /// <summary>  LdapPersistSearchControl is a Server Control that allows a client\r
41         /// to receive notifications from the server of changes to entries within\r
42         /// the searches result set. The client can be notified when an entry is\r
43         /// added to the result set, when an entry is deleted from the result set,\r
44         /// when a DN has been changed or when and attribute value has been changed.\r
45         /// \r
46         /// <p>Sample Code: <a href="http://developer.novell.com/ndk/doc/samplecode/\r
47         /// jldap_sample/controls/SearchPersist.java.html">SearchPersist.java</p>\r
48         /// </summary>\r
49         public class LdapPersistSearchControl:LdapControl\r
50         {\r
51                 /// <summary>  Returns the change types to be monitored as a logical OR of any or\r
52                 /// all of these values: ADD, DELETE, MODIFY, and/or MODDN.\r
53                 /// \r
54                 /// </summary>\r
55                 /// <returns>  the change types to be monitored. The logical or of any of\r
56                 /// the following values: ADD, DELETE, MODIFY, and/or MODDN.\r
57                 /// </returns>\r
58                 /// <summary>  Sets the change types to be monitored.\r
59                 /// \r
60                 /// types  The change types to be monitored as a logical OR of any or all\r
61                 /// of these types: ADD, DELETE, MODIFY, and/or MODDN. Can also be set\r
62                 /// to the value ANY which is defined as the logical OR of all of the\r
63                 /// preceding values.\r
64                 /// </summary>\r
65                 virtual public int ChangeTypes\r
66                 {\r
67                         get\r
68                         {\r
69                                 return m_changeTypes;\r
70                         }\r
71                         \r
72                         set\r
73                         {\r
74                                 m_changeTypes = value;\r
75                                 m_sequence.set_Renamed(CHANGETYPES_INDEX, new Asn1Integer(m_changeTypes));\r
76                                 setValue();\r
77                                 return ;\r
78                         }\r
79                         \r
80                 }\r
81                 /// <summary>  Returns true if entry change controls are to be returned with the\r
82                 /// search results.\r
83                 /// \r
84                 /// </summary>\r
85                 /// <returns>  true if entry change controls are to be returned with the\r
86                 /// search results. Otherwise, false is returned\r
87                 /// </returns>\r
88                 /// <summary>  When set to true, requests that entry change controls be returned with\r
89                 /// the search results.\r
90                 /// \r
91                 /// </summary>\r
92                 /// <param name="returnControls">  true to return entry change controls.\r
93                 /// </param>\r
94                 virtual public bool ReturnControls\r
95                 {\r
96                         get\r
97                         {\r
98                                 return m_returnControls;\r
99                         }\r
100                         \r
101                         set\r
102                         {\r
103                                 m_returnControls = value;\r
104                                 m_sequence.set_Renamed(RETURNCONTROLS_INDEX, new Asn1Boolean(m_returnControls));\r
105                                 setValue();\r
106                                 return ;\r
107                         }\r
108                         \r
109                 }\r
110                 /// <summary>  getChangesOnly returns true if only changes are to be returned.\r
111                 /// Results from the initial search are not returned.\r
112                 /// \r
113                 /// </summary>\r
114                 /// <returns>  true of only changes are to be returned\r
115                 /// </returns>\r
116                 /// <summary>  When set to true, requests that only changes be returned, results from\r
117                 /// the initial search are not returned.\r
118                 /// </summary>\r
119                 /// <param name="changesOnly"> true to skip results for the initial search\r
120                 /// </param>\r
121                 virtual public bool ChangesOnly\r
122                 {\r
123                         get\r
124                         {\r
125                                 return m_changesOnly;\r
126                         }\r
127                         \r
128                         set\r
129                         {\r
130                                 m_changesOnly = value;\r
131                                 m_sequence.set_Renamed(CHANGESONLY_INDEX, new Asn1Boolean(m_changesOnly));\r
132                                 setValue();\r
133                                 return ;\r
134                         }\r
135                         \r
136                 }\r
137                 /* private data members */\r
138                 private static int SEQUENCE_SIZE = 3;\r
139                 \r
140                 private static int CHANGETYPES_INDEX = 0;\r
141                 private static int CHANGESONLY_INDEX = 1;\r
142                 private static int RETURNCONTROLS_INDEX = 2;\r
143                 \r
144                 private static LBEREncoder s_encoder;\r
145                 \r
146                 private int m_changeTypes;\r
147                 private bool m_changesOnly;\r
148                 private bool m_returnControls;\r
149                 private Asn1Sequence m_sequence;\r
150                 \r
151                 /// <summary> The requestOID of the persistent search control</summary>\r
152                 private static System.String requestOID = "2.16.840.1.113730.3.4.3";\r
153                 \r
154                 /// <summary> The responseOID of the psersistent search - entry change control</summary>\r
155                 private static System.String responseOID = "2.16.840.1.113730.3.4.7";\r
156                 \r
157                 /// <summary>  Change type specifying that you want to track additions of new entries\r
158                 /// to the directory.\r
159                 /// </summary>\r
160                 public const int ADD = 1;\r
161                 \r
162                 /// <summary>  Change type specifying that you want to track removals of entries from\r
163                 /// the directory.\r
164                 /// </summary>\r
165                 public const int DELETE = 2;\r
166                 \r
167                 /// <summary>  Change type specifying that you want to track modifications of entries\r
168                 /// in the directory.\r
169                 /// </summary>\r
170                 public const int MODIFY = 4;\r
171                 \r
172                 /// <summary>  Change type specifying that you want to track modifications of the DNs\r
173                 /// of entries in the directory.\r
174                 /// </summary>\r
175                 public const int MODDN = 8;\r
176                 \r
177                 /// <summary>  Change type specifying that you want to track any of the above\r
178                 /// modifications.\r
179                 /// </summary>\r
180                 public static readonly int ANY = ADD | DELETE | MODIFY | MODDN;\r
181                 \r
182                 /* public constructors */\r
183                 \r
184                 /// <summary>  The default constructor. A control with changes equal to ANY,\r
185                 /// isCritical equal to true, changesOnly equal to true, and\r
186                 /// returnControls equal to true\r
187                 /// </summary>\r
188                 public LdapPersistSearchControl():this(ANY, true, true, true)\r
189                 {\r
190                         return ;\r
191                 }\r
192                 \r
193                 /// <summary>  <p>Constructs an LdapPersistSearchControl object according to the\r
194                 /// supplied parameters. The resulting control is used to specify a\r
195                 /// persistent search.</p>\r
196                 /// \r
197                 /// </summary>\r
198                 /// <param name="changeTypes"> the change types to monitor. The bitwise OR of any\r
199                 /// of the following values:\r
200                 /// <li>                           LdapPersistSearchControl.ADD</li>\r
201                 /// <li>                           LdapPersistSearchControl.DELETE</li>\r
202                 /// <li>                           LdapPersistSearchControl.MODIFY</li>\r
203                 /// <li>                           LdapPersistSearchControl.MODDN</li>\r
204                 /// To track all changes the value can be set to:\r
205                 /// <li>                           LdapPersistSearchControl.ANY</li>\r
206                 /// <br>\r
207                 /// </param>\r
208                 /// <param name="changesOnly"> true if you do not want the server to return\r
209                 /// all existing entries in the directory that match the search\r
210                 /// criteria. (Use this if you just want the changed entries to be\r
211                 /// returned.)\r
212                 /// <br><br>\r
213                 /// </param>\r
214                 /// <param name="returnControls"> true if you want the server to return entry\r
215                 /// change controls with each entry in the search results. You need to\r
216                 /// return entry change controls to discover what type of change\r
217                 /// and other additional information about the change.\r
218                 /// <br><br>\r
219                 /// </param>\r
220                 /// <param name="isCritical"> true if this control is critical to the search\r
221                 /// operation. If true and the server does not support this control,\r
222                 /// the server will not perform the search at all.\r
223                 /// </param>\r
224                 public LdapPersistSearchControl(int changeTypes, bool changesOnly, bool returnControls, bool isCritical):base(requestOID, isCritical, null)\r
225                 {\r
226                         \r
227                         m_changeTypes = changeTypes;\r
228                         m_changesOnly = changesOnly;\r
229                         m_returnControls = returnControls;\r
230                         \r
231                         m_sequence = new Asn1Sequence(SEQUENCE_SIZE);\r
232                         \r
233                         m_sequence.add(new Asn1Integer(m_changeTypes));\r
234                         m_sequence.add(new Asn1Boolean(m_changesOnly));\r
235                         m_sequence.add(new Asn1Boolean(m_returnControls));\r
236                         \r
237                         setValue();\r
238                         return ;\r
239                 }\r
240                 \r
241                 public override System.String ToString()\r
242                 {\r
243                         sbyte[] data = m_sequence.getEncoding(s_encoder);\r
244                         \r
245                         System.Text.StringBuilder buf = new System.Text.StringBuilder(data.Length);\r
246                         \r
247                         for (int i = 0; i < data.Length; i++)\r
248                         {\r
249                                 buf.Append(data[i].ToString());\r
250                                 if (i < data.Length - 1)\r
251                                         buf.Append(",");\r
252                         }\r
253                         \r
254                         return buf.ToString();\r
255                 }\r
256                 \r
257                 /// <summary>  Sets the encoded value of the LdapControlClass</summary>\r
258                 private void  setValue()\r
259                 {\r
260                         base.setValue(m_sequence.getEncoding(s_encoder));\r
261                         return ;\r
262                 }\r
263                 static LdapPersistSearchControl()\r
264                 {\r
265                         s_encoder = new LBEREncoder();\r
266                         /*\r
267                         * This is where we register the control response\r
268                         */\r
269                         {\r
270                                 /* Register the Entry Change control class which is returned by the\r
271                                 * server in response to a persistent search request\r
272                                 */\r
273                                 try\r
274                                 {\r
275                                         // Register LdapEntryChangeControl\r
276                                         LdapControl.register(responseOID, System.Type.GetType("Novell.Directory.Ldap.Controls.LdapEntryChangeControl"));\r
277                                 }\r
278                                 catch (System.Exception e)\r
279                                 {\r
280                                 }\r
281                         }\r
282                 }\r
283         } // end class LdapPersistentSearchControl\r
284 }\r