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.LdapVirtualListResponse.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31 
32 using System;
33 using Novell.Directory.Ldap;
34 using Novell.Directory.Ldap.Asn1;
35 
36 namespace Novell.Directory.Ldap.Controls
37 {
38 
39 	/// <summary>
40 	/// LdapVirtualListResponse is a Server Control returned by the server in
41 	/// response to a virtual list search request.
42 	/// </summary>
43 	/// <summary>
44 	/// In response to a VLV Search request the server returns an error code
45 	/// and if the search was successful returns the following information:
46 	/// <li> an index into the search results from where the returned list begins</li>
47 	/// <li> an estimate of the total number of elements in the search result</li>
48 	/// <li> an optional context field to be returned to the server with
49 	/// subsequent VLV request.</li>
50 	///
51 	/// </summary>
52 	public class LdapVirtualListResponse:LdapControl
53 	{
54 		/// <summary> Returns the size of the virtual search results list.  This integer as
55 		/// the servers current estimate of what the search result size.
56 		/// </summary>
57 		virtual public int ContentCount
58 		{
59 			get
60 			{
61 				return m_ContentCount;
62 			}
63 
64 		}
65 		/// <summary> Returns the index of the first entry in the returned list.  The server uses
66 		/// the clients request information in conjunction with its current search result
67 		/// list to estimate what list of entries the client is requesting.  This integer
68 		/// is the index into the search results that is returned to the client.
69 		/// </summary>
70 		virtual public int FirstPosition
71 		{
72 			get
73 			{
74 				return m_firstPosition;
75 			}
76 
77 		}
78 		/// <summary> Returns the result code for the virtual list search request.</summary>
79 		virtual public int ResultCode
80 		{
81 			get
82 			{
83 				return m_resultCode;
84 			}
85 
86 		}
87 		/// <summary> Returns the cookie used by some servers to optimize the processing of
88 		/// virtual list requests. Subsequent VLV requests to the same server
89 		/// should return this String to the server.
90 		/// </summary>
91 		virtual public System.String Context
92 		{
93 			get
94 			{
95 				return m_context;
96 			}
97 
98 		}
99 		/* The parsed fields are stored in these private variables */
100 		private int m_firstPosition;
101 		private int m_ContentCount;
102 		private int m_resultCode;
103 
104 		/* The context field if one was returned by the server */
105 		private System.String m_context = null;
106 
107 		/// <summary> This constructor is usually called by the SDK to instantiate an
108 		/// a LdapControl corresponding to the Server response to a Ldap
109 		/// VLV Control request.  Application programmers should not have
110 		/// any reason to call the constructor.  This constructor besides
111 		/// constructing a LdapVirtualListResponse control object also
112 		/// parses the contents of the response into local variables.
113 		///
114 		/// RFC 2891 defines this response control as follows:
115 		///
116 		/// The controlValue is an OCTET STRING, whose value is the BER
117 		/// encoding of a value of the following ASN.1:
118 		///
119 		/// VirtualListViewResponse ::= SEQUENCE {
120 		/// targetPosition    INTEGER (0 .. maxInt),
121 		/// contentCount     INTEGER (0 .. maxInt),
122 		/// virtualListViewResult ENUMERATED {
123 		/// success (0),
124 		/// operationsError (1),
125 		/// unwillingToPerform (53),
126 		/// insufficientAccessRights (50),
127 		/// busy (51),
128 		/// timeLimitExceeded (3),
129 		/// adminLimitExceeded (11),
130 		/// sortControlMissing (60),
131 		/// offsetRangeError (61),
132 		/// other (80) },
133 		/// contextID     OCTET STRING OPTIONAL }
134 		///
135 		///
136 		/// </summary>
137 		/// <param name="oid">    The OID of the control, as a dotted string.
138 		///
139 		/// </param>
140 		/// <param name="critical">  True if the Ldap operation should be discarded if
141 		/// the control is not supported. False if
142 		/// the operation can be processed without the control.
143 		///
144 		/// </param>
145 		/// <param name="values">    The control-specific data.
146 		/// </param>
147 		[CLSCompliantAttribute(false)]
LdapVirtualListResponse(System.String oid, bool critical, sbyte[] values)148 		public LdapVirtualListResponse(System.String oid, bool critical, sbyte[] values):base(oid, critical, values)
149 		{
150 
151 			/* Create a decoder object */
152 			LBERDecoder decoder = new LBERDecoder();
153 			if (decoder == null)
154 				throw new System.IO.IOException("Decoding error");
155 
156 			/* We should get back an ASN.1 Sequence object */
157 			Asn1Object asnObj = decoder.decode(values);
158 			if ((asnObj == null) || (!(asnObj is Asn1Sequence)))
159 				throw new System.IO.IOException("Decoding error");
160 
161 			/* Else we got back a ASN.1 sequence - print it if running debug code */
162 
163 			/* Get the 1st element which should be an integer containing the
164 			* targetPosition (firstPosition)
165 			*/
166 			Asn1Object asn1firstPosition = ((Asn1Sequence) asnObj).get_Renamed(0);
167 			if ((asn1firstPosition != null) && (asn1firstPosition is Asn1Integer))
168 				m_firstPosition = ((Asn1Integer) asn1firstPosition).intValue();
169 			else
170 				throw new System.IO.IOException("Decoding error");
171 
172 			/* Get the 2nd element which should be an integer containing the
173 			* current estimate of the contentCount
174 			*/
175 			Asn1Object asn1ContentCount = ((Asn1Sequence) asnObj).get_Renamed(1);
176 			if ((asn1ContentCount != null) && (asn1ContentCount is Asn1Integer))
177 				m_ContentCount = ((Asn1Integer) asn1ContentCount).intValue();
178 			else
179 				throw new System.IO.IOException("Decoding error");
180 
181 			/* The 3rd element is an enum containing the errorcode */
182 			Asn1Object asn1Enum = ((Asn1Sequence) asnObj).get_Renamed(2);
183 			if ((asn1Enum != null) && (asn1Enum is Asn1Enumerated))
184 				m_resultCode = ((Asn1Enumerated) asn1Enum).intValue();
185 			else
186 				throw new System.IO.IOException("Decoding error");
187 
188 			/* Optional 4th element could be the context string that the server
189 			* wants the client to send back with each subsequent VLV request
190 			*/
191 			if (((Asn1Sequence) asnObj).size() > 3)
192 			{
193 				Asn1Object asn1String = ((Asn1Sequence) asnObj).get_Renamed(3);
194 				if ((asn1String != null) && (asn1String is Asn1OctetString))
195 					m_context = ((Asn1OctetString) asn1String).stringValue();
196 			}
197 			return ;
198 		}
199 	}
200 }
201