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.LdapResponse.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
33 using Novell
.Directory
.Ldap
.Asn1
;
34 using Novell
.Directory
.Ldap
.Rfc2251
;
35 using Novell
.Directory
.Ldap
.Utilclass
;
37 namespace Novell
.Directory
.Ldap
40 /// <summary> A message received from an LdapServer
41 /// in response to an asynchronous request.
44 /// <seealso cref="LdapConnection.Search">
48 * Note: Exceptions generated by the reader thread are returned
49 * to the application as an exception in an LdapResponse. Thus
50 * if <code>exception</code> has a value, it is not a server response,
51 * but instad an exception returned to the application from the API.
53 public class LdapResponse
:LdapMessage
55 /// <summary> Returns any error message in the response.
58 /// <returns> Any error message in the response.
60 virtual public System
.String ErrorMessage
64 if (exception
!= null)
66 return exception
.LdapErrorMessage
;
69 /* RfcResponse resp=(RfcResponse)( message.Response);
71 Console.WriteLine(" Response is null");
73 Console.WriteLine(" Response is non null");
74 string str=resp.getErrorMessage().stringValue();
76 Console.WriteLine("str is null..");
77 Console.WriteLine(" Response is non null" + str);
80 return ((RfcResponse
) message
.Response
).getErrorMessage().stringValue();
84 /// <summary> Returns the partially matched DN field from the server response,
85 /// if the response contains one.
88 /// <returns> The partially matched DN field, if the response contains one.
91 virtual public System
.String MatchedDN
95 if (exception
!= null)
97 return exception
.MatchedDN
;
99 return ((RfcResponse
) message
.Response
).getMatchedDN().stringValue();
103 /// <summary> Returns all referrals in a server response, if the response contains any.
106 /// <returns> All the referrals in the server response.
108 virtual public System
.String
[] Referrals
112 System
.String
[] referrals
= null;
113 RfcReferral ref_Renamed
= ((RfcResponse
) message
.Response
).getReferral();
115 if (ref_Renamed
== null)
117 referrals
= new System
.String
[0];
121 // convert RFC 2251 Referral to String[]
122 int size
= ref_Renamed
.size();
123 referrals
= new System
.String
[size
];
124 for (int i
= 0; i
< size
; i
++)
126 System
.String aRef
= ((Asn1OctetString
) ref_Renamed
.get_Renamed(i
)).stringValue();
129 // get the referral URL
130 LdapUrl urlRef
= new LdapUrl(aRef
);
131 if ((System
.Object
) urlRef
.getDN() == null)
133 RfcLdapMessage origMsg
= base.Asn1Object
.RequestingMessage
.Asn1Object
;
135 if ((System
.Object
) (dn
= origMsg
.RequestDN
) != null)
138 aRef
= urlRef
.ToString();
142 catch (System
.UriFormatException mex
)
156 /// <summary> Returns the result code in a server response.
158 /// For a list of result codes, see the LdapException class.
161 /// <returns> The result code.
163 virtual public int ResultCode
167 if (exception
!= null)
169 return exception
.ResultCode
;
171 return ((RfcResponse
) message
.Response
).getResultCode().intValue();
175 /// <summary> Checks the resultCode and generates the appropriate exception or
178 virtual internal LdapException ResultException
184 LdapException ex
= null;
188 case LdapException
.SUCCESS
:
189 case LdapException
.COMPARE_TRUE
:
190 case LdapException
.COMPARE_FALSE
:
193 case LdapException
.REFERRAL
:
194 System
.String
[] refs
= Referrals
;
195 ex
= new LdapReferralException("Automatic referral following not enabled", LdapException
.REFERRAL
, ErrorMessage
);
196 ((LdapReferralException
) ex
).setReferrals(refs
);
200 ex
= new LdapException(LdapException
.resultCodeToString(ResultCode
), ResultCode
, ErrorMessage
, MatchedDN
);
201 // ex = new LdapException("49", 49, "hello error", "hi error..");
209 /// <summary> Returns any controls in the message.
212 /// <seealso cref="Novell.Directory.Ldap.LdapMessage.Controls">
214 override public LdapControl
[] Controls
218 if (exception
!= null)
222 return base.Controls
;
226 /// <summary> Returns the message ID.
229 /// <seealso cref="Novell.Directory.Ldap.LdapMessage.MessageID">
231 override public int MessageID
235 if (exception
!= null)
237 return exception
.MessageID
;
239 return base.MessageID
;
243 /// <summary> Returns the Ldap operation type of the message.
246 /// <returns> The operation type of the message.
249 /// <seealso cref="Novell.Directory.Ldap.LdapMessage.Type">
251 override public int Type
255 if (exception
!= null)
257 return exception
.ReplyType
;
263 /// <summary> Returns an embedded exception response
266 /// <returns> an embedded exception if any
268 virtual internal LdapException Exception
278 /// <summary> Indicates the referral instance being followed if the
279 /// connection created to follow referrals.
282 /// <returns> the referral being followed
284 virtual internal ReferralInfo ActiveReferral
290 return activeReferral
;
294 private InterThreadException exception
= null;
295 private ReferralInfo activeReferral
;
297 /// <summary> Creates an LdapResponse using an LdapException.
298 /// Used to wake up the user following an abandon.
299 /// Note: The abandon doesn't have to be user initiated
300 /// but may be the result of error conditions.
302 /// Referral information is available if this connection created solely
303 /// to follow a referral.
306 /// <param name="ex"> The exception
309 /// <param name="activeReferral"> The referral actually used to create the
312 public LdapResponse(InterThreadException ex
, ReferralInfo activeReferral
)
315 this.activeReferral
= activeReferral
;
320 /// <summary> Creates a response LdapMessage when receiving an asynchronous
321 /// response from a server.
324 /// <param name="message"> The RfcLdapMessage from a server.
327 internal LdapResponse(RfcLdapMessage message
):base(message
)
332 /// <summary> Creates a SUCCESS response LdapMessage. Typically the response
333 /// comes from a source other than a BER encoded Ldap message,
334 /// such as from DSML. Other values which are allowed in a response
335 /// are set to their empty values.
338 /// <param name="type"> The message type as defined in LdapMessage.
341 /// <seealso cref="LdapMessage">
343 public LdapResponse(int type
):this(type
, LdapException
.SUCCESS
, null, null, null, null)
348 /// <summary> Creates a response LdapMessage from parameters. Typically the data
349 /// comes from a source other than a BER encoded Ldap message,
350 /// such as from DSML.
353 /// <param name="type"> The message type as defined in LdapMessage.
356 /// <param name="resultCode"> The result code as defined in LdapException.
359 /// <param name="matchedDN"> The name of the lowest entry that was matched
360 /// for some error result codes, an empty string
361 /// or <code>null</code> if none.
364 /// <param name="serverMessage"> A diagnostic message returned by the server,
365 /// an empty string or <code>null</code> if none.
368 /// <param name="referrals"> The referral URLs returned for a REFERRAL result
369 /// code or <code>null</code> if none.
372 /// <param name="controls"> Any controls returned by the server or
373 /// <code>null</code> if none.
376 /// <seealso cref="LdapMessage">
378 /// <seealso cref="LdapException">
380 public LdapResponse(int type
, int resultCode
, System
.String matchedDN
, System
.String serverMessage
, System
.String
[] referrals
, LdapControl
[] controls
):base(new RfcLdapMessage(RfcResultFactory(type
, resultCode
, matchedDN
, serverMessage
, referrals
)))
386 private static Asn1Sequence
RfcResultFactory(int type
, int resultCode
, System
.String matchedDN
, System
.String serverMessage
, System
.String
[] referrals
)
390 if ((System
.Object
) matchedDN
== null)
392 if ((System
.Object
) serverMessage
== null)
399 ret
= new RfcSearchResultDone(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
403 ret
= null; // Not yet implemented
406 case SEARCH_RESPONSE
:
407 ret
= null; // Not yet implemented
410 case MODIFY_RESPONSE
:
411 ret
= new RfcModifyResponse(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
415 ret
= new RfcAddResponse(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
419 ret
= new RfcDelResponse(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
422 case MODIFY_RDN_RESPONSE
:
423 ret
= new RfcModifyDNResponse(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
426 case COMPARE_RESPONSE
:
427 ret
= new RfcCompareResponse(new Asn1Enumerated(resultCode
), new RfcLdapDN(matchedDN
), new RfcLdapString(serverMessage
), null);
430 case SEARCH_RESULT_REFERENCE
:
431 ret
= null; // Not yet implemented
434 case EXTENDED_RESPONSE
:
435 ret
= null; // Not yet implemented
439 throw new System
.SystemException("Type " + type
+ " Not Supported");
445 /// <summary> Checks the resultCode and throws the appropriate exception.
448 /// <exception> LdapException A general exception which includes an error
449 /// message and an Ldap error code.
452 internal virtual void chkResultCode()
454 if (exception
!= null)
460 LdapException ex
= ResultException
;
469 /* Methods from LdapMessage */
471 /// <summary> Indicates if this response is an embedded exception response
474 /// <returns> true if contains an embedded Ldapexception
477 internal virtual bool hasException()
479 return (exception
!= null);