Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Licensed to the Apache Software Foundation (ASF) under one
   *  or more contributor license agreements.  See the NOTICE file
   *  distributed with this work for additional information
   *  regarding copyright ownership.  The ASF licenses this file
   *  to you under the Apache License, Version 2.0 (the
   *  "License"); you may not use this file except in compliance
   *  with the License.  You may obtain a copy of the License at
   *
  *    http://www.apache.org/licenses/LICENSE-2.0
  *
  *  Unless required by applicable law or agreed to in writing,
  *  software distributed under the License is distributed on an
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  *  KIND, either express or implied.  See the License for the
  *  specific language governing permissions and limitations
  *  under the License.
  *
  */
 package org.apache.directory.studio.connection.core.io.api;
 
 
 import java.util.List;
 import java.util.Map;
 
 
 import  org.apache.directory.api.ldap.codec.api.CodecControl;
 import  org.apache.directory.api.ldap.codec.api.LdapApiService;
 import  org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
 import  org.apache.directory.api.ldap.model.cursor.SearchCursor;
 import  org.apache.directory.api.ldap.model.entry.AttributeUtils;
 import  org.apache.directory.api.ldap.model.message.Referral;
 import  org.apache.directory.api.ldap.model.message.Response;
 import  org.apache.directory.api.ldap.model.message.SearchResultDone;
 import  org.apache.directory.api.ldap.model.message.SearchResultEntry;
 import  org.apache.directory.api.ldap.model.message.SearchResultReference;
 import  org.apache.directory.api.ldap.model.url.LdapUrl;
 import  org.apache.directory.studio.common.core.jobs.StudioProgressMonitor;
A naming enumeration that handles referrals itself.

Author(s):
Apache Directory Project
 
 {
     private SearchCursor cursor;
     private SearchResultEntry currentSearchResultEntry;
     private List<Referral> referralsList = new ArrayList<Referral>();
     private List<StringcurrentReferralUrlsList;
     private SearchResultDone searchResultDone;
 
     // @TODO: By Alex: temporary fix until things are in order (needs to be fixed)
     private LdapApiService codec = LdapApiServiceFactory.getSingleton();


    
Creates a new instance of ReferralNamingEnumeration.

Parameters:
connection the connection
cursor the search cursor
searchBase the search base
filter the filter
searchControls the search controls
aliasesDereferencingMethod the aliases dereferencing method
referralsHandlingMethod the referrals handling method
controls the LDAP controls
monitor the progress monitor
referralsInfo the referrals info
 
     public CursorStudioNamingEnumerationConnection connection, SearchCursor cursor,
         String searchBaseString filterSearchControls searchControls,
         AliasDereferencingMethod aliasesDereferencingMethodReferralHandlingMethod referralsHandlingMethod,
         Control[] controlslong requestNum, StudioProgressMonitor monitorReferralsInfo referralsInfo )
     {
         superconnectionsearchBasefiltersearchControlsaliasesDereferencingMethod,
             referralsHandlingMethodcontrolsrequestNummonitorreferralsInfo );
        this. = connection;
        this. = cursor;
        this. = searchBase;
        this. = filter;
        this. = searchControls;
        this. = aliasesDereferencingMethod;
        this. = referralsHandlingMethod;
        this. = controls;
        this. = monitor;
        this. = referralsInfo;
    }


    

See also:
javax.naming.NamingEnumeration.close()
    public void close() throws NamingException
    {
        try
        {
            .close();
        }
        catch ( Exception e )
        {
            throw new NamingExceptione.getMessage() );
        }
    }


    

See also:
javax.naming.NamingEnumeration.hasMore()
    public boolean hasMore() throws NamingException
    {
        try
        {
            // Nulling the current search result entry
             = null;
            // Do we have another response in the cursor?
            while ( .next() )
            {
                Response currentResponse = .get();
                // Is it a search result entry?
                if ( currentResponse instanceof SearchResultEntry )
                {
                     = ( SearchResultEntry ) currentResponse;
                    // return true if the current response is a search result entry
                    return true;
                }
                // Is it a search result reference (ie. a referral)?
                else if ( currentResponse instanceof SearchResultReference )
                {
                    // Are we ignoring referrals?
                    if (  != . )
                    {
                        // Storing the referral for later use
                        .add( ( ( SearchResultReference ) currentResponse ).getReferral() );
                    }
                }
            }
            // Storing the search result done (if needed)
            if (  == null )
            {
                 = ( ( SearchCursor )  ).getSearchResultDone();
            }
            // Are we following referrals manually?
            {
                // Checking the current referral's URLs list
                if ( (  != null ) && ( .size() > 0 ) )
                {
                    // return true if there's at least one referral LDAP URL to handle
                    return true;
                }
                // Checking the referrals list
                if ( (  != null ) && ( .size() > 0 ) )
                {
                    // Getting the list of the next referral
                     = new ArrayList<String>( .remove( 0 ).getLdapUrls() );
                    // return true if there's at least one referral LDAP URL to handle
                    return .size() > 0;
                }
            }
            // Are we following referrals automatically?
            else if (  == . )
            {
                if ( (  != null ) && ( .hasMore() ) )
                {
                    // return true if there's at least one more entry in the current cursor naming enumeration
                    return true;
                }
                if ( (  != null ) && ( .size() > 0 ) )
                {
                    Referral referral = .remove( 0 );
                    List<StringreferralUrls = new ArrayList<String>( referral.getLdapUrls() );
                    LdapUrl url = new LdapUrl( referralUrls.get( 0 ) );
                    Connection referralConnection = ConnectionWrapperUtils.getReferralConnectionreferral,
                        this );
                    if ( referralConnection != null )
                    {
                        String referralSearchBase = url.getDn() != null && !url.getDn().isEmpty() ? url.getDn()
                            .getName()
                            : ;
                        String referralFilter = url.getFilter() != null && url.getFilter().length() == 0 ? url
                            .getFilter()
                            : ;
                        SearchControls referralSearchControls = new SearchControls();
                        referralSearchControls.setSearchScopeurl.getScope().getScope() > -1 ? url.getScope()
                            .getScope()
                            : .getSearchScope() );
                        referralSearchControls.setReturningAttributesurl.getAttributes() != null
                            && url.getAttributes().size() > 0 ? url.getAttributes().toArray(
                            new String[url.getAttributes().size()] ) : .getReturningAttributes() );
                        referralSearchControls.setCountLimit.getCountLimit() );
                        referralSearchControls.setTimeLimit.getTimeLimit() );
                        referralSearchControls.setDerefLinkFlag.getDerefLinkFlag() );
                        referralSearchControls.setReturningObjFlag.getReturningObjFlag() );
                         = referralConnection.getConnectionWrapper()
                            .search(
                                referralSearchBasereferralFilterreferralSearchControls,
                                 );
                        return .hasMore();
                    }
                }
            }
            for ( IJndiLogger logger : ConnectionCorePlugin.getDefault().getJndiLoggers() )
            {
                logger.logSearchResultDonenull );
            }
            return false;
        }
        catch ( Exception e )
        {
            throw new NamingExceptione.getMessage() );
        }
    }


    

See also:
java.util.Enumeration.hasMoreElements()
    public boolean hasMoreElements()
    {
        throw new UnsupportedOperationException"Call hasMore() instead of hasMoreElements() !" ); //$NON-NLS-1$
    }


    

See also:
javax.naming.NamingEnumeration.next()
    public StudioSearchResult next() throws NamingException
    {
        try
        {
            if (  != null )
            {
                ++;
                SearchResult sr = new SearchResult.getObjectName().toString(), null,
                    AttributeUtils.toAttributes( .getEntry() ) );
                sr.setNameInNamespace.getObjectName().toString() );
                // Converting the SearchResult to a StudioSearchResult
                StudioSearchResult ssr = new StudioSearchResultsrfalsenull );
                return ssr;
            }
            // Are we following referrals manually?
            {
                // Checking the current referral's URLs list
                if ( (  != null ) && ( .size() > 0 ) )
                {
                    ++;
                    // Building an LDAP URL from the the url
                    LdapUrl url = new LdapUrl( .remove( 0 ) );
                    // Building the search result
                    SearchResult searchResult = new SearchResulturl.getDn().getName(), null,
                        new BasicAttributes(),
                        false );
                    searchResult.setNameInNamespaceurl.getDn().getName() );
                    return new StudioSearchResultsearchResultnullfalseurl );
                }
            }
            // Are we following referrals automatically?
            else if (  == . )
            {
                ++;
                return new StudioSearchResult.next(), truenull );
            }
            return null;
        }
        catch ( Exception e )
        {
            throw new NamingExceptione.getMessage() );
        }
    }


    

See also:
java.util.Enumeration.nextElement()
    {
        throw new UnsupportedOperationException"Call next() instead of nextElement() !" ); //$NON-NLS-1$
    }


    
Gets the connection.

Returns:
the connection
    public Connection getConnection()
    {
        return ;
    }


    
Gets the response controls.

Returns:
the response controls, may be null
Throws:
NamingException the naming exception
    public Control[] getResponseControls() throws NamingException
    {
        if (  != null )
        {
            Map<String, org.apache.directory.api.ldap.model.message.Control> controlsMap = 
                .getControls();
            if ( ( controlsMap != null ) && ( controlsMap.size() > 0 ) )
            {
                return convertControlscontrolsMap.values() );
            }
        }
        return new Control[0];
    }


    
Converts the controls.

Parameters:
controls an array of controls
Returns:
an array of converted controls
    private Control[] convertControlsCollection<org.apache.directory.api.ldap.model.message.Control> controls )
    {
        if ( controls != null )
        {
            List<ControlconvertedControls = new ArrayList<Control>();
            for ( org.apache.directory.api.ldap.model.message.Control control : controls )
            {
                Control convertedControl = null;
                CodecControl<? extends org.apache.directory.api.ldap.model.message.Control> wrapped = null;
                if ( control instanceof CodecControl )
                {
                    wrapped = ( org.apache.directory.api.ldap.codec.api.CodecControl<?> ) control;
                }
                else
                {
                    wrapped = .newControl( control );
                }
                if ( ..equalscontrol.getOid() ) )
                {
                    // Special case for the PagedResultsResponseControl
                    try
                    {
                        convertedControl = new PagedResultsResponseControlwrapped.getOid(), wrapped.isCritical(),
                            wrapped.getValue() );
                    }
                    catch ( IOException e )
                    {
                        convertedControl = new BasicControlwrapped.getOid(), wrapped.isCritical(),
                            wrapped.getValue() );
                    }
                }
                else
                {
                    // Default case
                    convertedControl = new BasicControlwrapped.getOid(), wrapped.isCritical(),
                        wrapped.getValue() );
                }
                convertedControls.addconvertedControl );
            }
            return convertedControls.toArraynew Control[0] );
        }
        else
        {
            return new Control[0];
        }
    }
New to GrepCode? Check out our FAQ X