Novell.Directory.Ldap.LdapConnection.chaseReferral C# (CSharp) Method

chaseReferral() private method

Follow referrals if necessary referral following enabled. This function is called only by synchronous requests. Search responses come here only if referral following is enabled and if we are processing a SearchResultReference or a Response with a status of REFERRAL, i.e. we are going to follow a referral. This functions recursively follows a referral until a result is returned or until the hop limit is reached.
LdapException A general exception which includes an error /// message and an Ldap error code. ///
private chaseReferral ( LdapMessageQueue queue, LdapConstraints cons, LdapMessage msg, System initialReferrals, int hopCount, bool searchReference, System connectionList ) : System.Collections.ArrayList
queue LdapMessageQueue The LdapResponseQueue for this request /// ///
cons LdapConstraints The constraints that apply to the request /// ///
msg LdapMessage The referral or search reference response message /// ///
initialReferrals System The referral array returned from the /// initial request. /// ///
hopCount int the number of hops already used while /// following this referral /// ///
searchReference bool true if the message is a search reference /// ///
connectionList System An optional array list used to store /// the LdapConnection objects used in following the referral. /// ///
return System.Collections.ArrayList
        internal virtual System.Collections.ArrayList chaseReferral(LdapMessageQueue queue, LdapConstraints cons, LdapMessage msg, System.String[] initialReferrals, int hopCount, bool searchReference, System.Collections.ArrayList connectionList)
        {
            System.Collections.ArrayList connList = connectionList;
            LdapConnection rconn = null; // new conn for following referral
            ReferralInfo rinfo = null; // referral info
            LdapMessage origMsg;

            // Get a place to store new connections
            if (connList == null)
            {
                connList = new System.Collections.ArrayList(cons.HopLimit);
            }
            // Following referrals or search reference
            System.String[] refs; // referral list
            if (initialReferrals != null)
            {
                // Search continuation reference from a search request
                refs = initialReferrals;
                origMsg = msg.RequestingMessage;
            }
            else
            {
                // Not a search request
                LdapResponse resp = (LdapResponse) queue.getResponse();
                if (resp.ResultCode != LdapException.REFERRAL)
                {
                    // Not referral result,throw Exception if nonzero result
                    resp.chkResultCode();
                    return connList;
                }
                // We have a referral response
                refs = resp.Referrals;
                origMsg = resp.RequestingMessage;
            }
            LdapUrl refUrl; // referral represented as URL
            try
            {
                // increment hop count, check max hops
                if (hopCount++ > cons.HopLimit)
                {
                    throw new LdapLocalException("Max hops exceeded", LdapException.REFERRAL_LIMIT_EXCEEDED);
                }
                // Get a connection to follow the referral
                rinfo = getReferralConnection(refs);
                rconn = rinfo.ReferralConnection;
                refUrl = rinfo.ReferralUrl;
                connList.Add(rconn);

                // rebuild msg into new msg changing msgID,dn,scope,filter
                LdapMessage newMsg = rebuildRequest(origMsg, refUrl, searchReference);

                // Send new message on new connection
                try
                {
                    MessageAgent agent;
                    if (queue is LdapResponseQueue)
                    {
                        agent = queue.MessageAgent;
                    }
                    else
                    {
                        agent = queue.MessageAgent;
                    }
                    agent.sendMessage(rconn.Connection, newMsg, defSearchCons.TimeLimit, queue, null);
                }
                catch (InterThreadException ex)
                {
                    // Error ending request to referred server
                    LdapReferralException rex = new LdapReferralException(ExceptionMessages.REFERRAL_SEND, LdapException.CONNECT_ERROR, null, ex);
                    rex.setReferrals(initialReferrals);
                    ReferralInfo ref_Renamed = rconn.Connection.ActiveReferral;
                    rex.FailedReferral = ref_Renamed.ReferralUrl.ToString();
                    throw rex;
                }

                if (initialReferrals == null)
                {
                    // For operation results, when all responses are complete,
                    // the stack unwinds back to the original and returns
                    // to the application.
                    // An exception is thrown for an error
                    connList = chaseReferral(queue, cons, null, null, hopCount, false, connList);
                }
                else
                {
                    // For search, just return to LdapSearchResults object
                    return connList;
                }
            }
            catch (System.Exception ex)
            {

                if (ex is LdapReferralException)
                {
                    throw (LdapReferralException) ex;
                }
                else
                {

                    // Set referral list and failed referral
                    LdapReferralException rex = new LdapReferralException(ExceptionMessages.REFERRAL_ERROR, ex);
                    rex.setReferrals(refs);
                    if (rinfo != null)
                    {
                        rex.FailedReferral = rinfo.ReferralUrl.ToString();
                    }
                    else
                    {
                        rex.FailedReferral = refs[refs.Length - 1];
                    }
                    throw rex;
                }
            }
            return connList;
        }