directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmach...@apache.org
Subject svn commit: rev 6236 - incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded
Date Tue, 20 Jan 2004 19:41:19 GMT
Author: jmachols
Date: Tue Jan 20 11:41:17 2004
New Revision: 6236

Added:
   incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapSearchParameters.java
Modified:
   incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClient.java
   incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClientImpl.java
Log:
Added the search operation to the embedded clients


Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClient.java
==============================================================================
--- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClient.java
(original)
+++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClient.java
Tue Jan 20 11:41:17 2004
@@ -58,6 +58,8 @@
 import java.io.IOException;
 import java.net.UnknownHostException;
 
+import javax.naming.NamingEnumeration;
+
 /**
  * Interface into the LDAPd session
  *
@@ -249,9 +251,33 @@
          * results
          * @throws IOException  Error with the IO communication to the server
          */
-        public LdapClientResponse ldapSearch ( String a_filterString )
+        public LdapClientResponse ldapSearch ( LdapSearchParameters a_parms )
                 throws IOException ;   	
 			
+        
+        /**
+         * Print each attribute name only.  This is used when the Types Only
+         * mode is selected from the command line options
+         * 
+         * @param a_attributeList NamingEnumeration of Attributes 
+         */
+        public void printAttributeNames ( NamingEnumeration a_attributeList ) ;
 		 
-
+        /**
+         * Print each attribute and the values for each attribute from the
+         * list passed into the method.  Each attribute has an ID, which is 
+         * the name of the attribute and a list of values.  This is the default
+         * method for the search client output.
+         * 
+         * Each value of the same attribute will also print the ID, for example an 
+         * entry with two value for objectclass will look like this:
+         * 
+         * ou=unix,ou=People,dc=fl,dc=wellspringres,dc=com
+         * objectClass=top
+         * objectClass=organizationalunit
+         * ou=unix
+         *  
+         * @param a_attributeList NamingEnumeration of Attributes 
+         */
+        public void printAttributeList( NamingEnumeration a_attributeList ) ;
 }

Modified: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClientImpl.java
==============================================================================
--- incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClientImpl.java
(original)
+++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapClientImpl.java
Tue Jan 20 11:41:17 2004
@@ -63,12 +63,17 @@
 import java.text.ParseException;
 import java.util.LinkedList;
 
+import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.ModificationItem;
 
+import org.apache.ldap.clients.standalone.SearchOpts;
+import org.apache.ldap.common.filter.ExprNode;
+import org.apache.ldap.common.filter.FilterParser;
+import org.apache.ldap.common.filter.FilterParserImpl;
 import org.apache.ldap.common.ldif.LdifEntry;
 import org.apache.ldap.common.ldif.LdifParser;
 import org.apache.ldap.common.ldif.LdifParserImpl;
@@ -80,12 +85,20 @@
 import org.apache.ldap.common.message.DeleteRequest;
 import org.apache.ldap.common.message.DeleteRequestImpl;
 import org.apache.ldap.common.message.DeleteResponse;
+import org.apache.ldap.common.message.DerefAliasesEnum;
 import org.apache.ldap.common.message.LdapResult;
 import org.apache.ldap.common.message.MessageDecoder;
 import org.apache.ldap.common.message.MessageEncoder;
 import org.apache.ldap.common.message.ModifyRequest;
 import org.apache.ldap.common.message.ModifyRequestImpl;
 import org.apache.ldap.common.message.ModifyResponse;
+import org.apache.ldap.common.message.ResultCodeEnum;
+import org.apache.ldap.common.message.ScopeEnum;
+import org.apache.ldap.common.message.SearchRequest;
+import org.apache.ldap.common.message.SearchRequestImpl;
+import org.apache.ldap.common.message.SearchResponseDoneImpl;
+import org.apache.ldap.common.message.SearchResponseEntryImpl;
+import org.apache.ldap.common.message.SearchResponseReferenceImpl;
 import org.apache.ldap.common.message.UnbindRequest;
 import org.apache.ldap.common.message.UnbindRequestImpl;
 
@@ -94,7 +107,7 @@
  *
  * @author <a href="mailto:jmachols@comcast.net">Jeff Machols</a>
  * @author $Author: jmachols $
- * @version $Revision: 1.8 $
+ * @version $Revision:  $
  */
 public class LdapClientImpl 
     implements LdapClient
@@ -529,13 +542,166 @@
     }
 
     
-    public LdapClientResponse ldapSearch ( String a_filterString )
+    public LdapClientResponse ldapSearch ( LdapSearchParameters a_parms )
        throws IOException
     {    
-        LdapClientResponse l_response = new LdapClientResponse () ;
+        LdapClientResponse l_clientReponse = new LdapClientResponse () ;
+        Object l_response = new Object () ;
+        SearchResponseDoneImpl l_searchResponseDone = 
+            new SearchResponseDoneImpl ( BigInteger.ONE ); 
+        SearchResponseEntryImpl l_searchResponseEntry ;
+        SearchResponseReferenceImpl l_searchResponseReference ;
+        FilterParser l_filter = null ;
+        ExprNode l_expr = null ;
+        ScopeEnum l_scope = null;
         
-        return l_response ;    
-    }
+        SearchRequest l_request = new SearchRequestImpl( m_messageId ) ;
+        m_messageId.add( BigInteger.ONE ) ;
+        
+        // Setup the ASN1 Enoder and Decoder
+        MessageEncoder l_encoder = new MessageEncoder();
+        MessageDecoder l_decoder = new MessageDecoder();
+        
+        try 
+        {
+            l_filter = new FilterParserImpl () ;
+        }
+        catch ( IOException e )
+        {
+            System.out.println( "Failed to create Filter Parser" + e.getMessage() ) ;
+            System.exit( 1 ) ;
+        }
+        
+        try 
+        {
+            l_expr = l_filter.parse(  a_parms.getFilter() ) ;
+        }
+        catch ( IOException ioe )
+        {
+            System.out.println( "IO Error while parsing the filter " 
+                        + ioe.getMessage() ) ;
+            System.exit( 1 ) ;
+        }
+        catch ( ParseException pe )
+        {
+            System.out.println( "Parsing Exception " + pe.getMessage() ) ;
+            System.exit( 1 ) ;
+        }
+             
+        l_request.setFilter( l_expr ) ;
+        l_request.setBase( a_parms.getBase() );
+        l_request.setScope( a_parms.getScope() ) ;
+        l_request.setDerefAliases( DerefAliasesEnum.NEVERDEREFALIASES ) ;
+        l_request.setTimeLimit( 
+            BigInteger.valueOf( ( long ) a_parms.getMaxEntries() ) ) ;
+        l_request.setSizeLimit( 
+                BigInteger.valueOf( ( long ) a_parms.getMaxEntries() ) ) ;
+        l_request.setTypesOnly( a_parms.isTypesMode() ) ;
             
+//        System.out.println( "Sending the following request to the ldap" 
+//                + " server " + a_parms.getHost() + ":" + a_parms.getPort() + "\n" 
+//                + requestToString( l_request )  ) ;
+        
+        // Send encoded request to server
+        l_encoder.encode( null, m_socket.getOutputStream(), l_request ) ;
 
+        // Retrieve the response back from server to my last request.
+        l_response = l_decoder.decode( null, m_socket.getInputStream() ) ;
+        while ( !l_response.getClass().isInstance( l_searchResponseDone ) )
+        {
+            l_searchResponseEntry = ( SearchResponseEntryImpl ) l_response ;
+            System.out.println ( l_searchResponseEntry.getObjectName() ) ;
+            if ( a_parms.isTypesMode() )
+            {
+                printAttributeNames (
+                    l_searchResponseEntry.getAttributes().getAll() ) ;
+            }
+            else if ( !a_parms.isQuietMode() )
+            {
+                printAttributeList( 
+                    l_searchResponseEntry.getAttributes().getAll() );
+            }           
+            System.out.println () ;
+            l_response = l_decoder.decode( null, m_socket.getInputStream() ) ;
+        }
+        
+        // We are done with the responses, the last one contains the status
+        l_searchResponseDone = ( SearchResponseDoneImpl ) l_response ;
+        if ( l_searchResponseDone.getLdapResult().getResultCode()
+                != ResultCodeEnum.SUCCESS )
+        {
+            System.out.println(
+                    "Search failed for the following reasons :\n" + new String( 
+                            l_searchResponseDone.getLdapResult().getErrorMessage() ) ) ;
+            System.exit( -1 ) ;
+        }
+        else
+        {
+            System.out.println( "Search with filter " + a_parms.getFilter()
+                    + " was successful" ) ;
+        }
+        return l_clientReponse ;    
+    }
+    
+    /**
+     * Print each attribute name only.  This is used when the Types Only
+     * mode is selected from the command line options
+     * 
+     * @param a_attributeList NamingEnumeration of Attributes 
+     */
+    public void printAttributeNames ( NamingEnumeration a_attributeList )
+    {
+        Attribute l_attr;
+        NamingEnumeration l_attrValues;
+
+        while ( a_attributeList.hasMoreElements() )
+        {
+            l_attr = ( Attribute ) a_attributeList.nextElement() ;  
+            System.out.println( l_attr.getID() ) ;
+        }
+    }
+     
+    /**
+     * Print each attribute and the values for each attribute from the
+     * list passed into the method.  Each attribute has an ID, which is 
+     * the name of the attribute and a list of values.  This is the default
+     * method for the search client output.
+     * 
+     * Each value of the same attribute will also print the ID, for example an 
+     * entry with two value for objectclass will look like this:
+     * 
+     * ou=unix,ou=People,dc=fl,dc=wellspringres,dc=com
+     * objectClass=top
+     * objectClass=organizationalunit
+     * ou=unix
+     *  
+     * @param a_attributeList NamingEnumeration of Attributes 
+     */
+    public void printAttributeList( NamingEnumeration a_attributeList )
+    {
+        
+        Attribute l_attr;
+        NamingEnumeration l_attrValues;
+
+        while ( a_attributeList.hasMoreElements() )
+        {
+            l_attr = ( Attribute ) a_attributeList.nextElement() ;  
+            try
+            {
+                l_attrValues = l_attr.getAll() ;
+                while ( l_attrValues.hasMoreElements() )
+                {
+                    System.out.print( l_attr.getID() + "=" ) ;
+                    System.out.println ( 
+                            l_attrValues.nextElement().toString() ) ;
+                }
+            }
+            catch ( NamingException e )
+            {
+                System.out.println( "Naming Exception thrown retreiving" 
+                        + " values for attribute " + l_attr.getID() ) ;
+            }
+        }
+    }
+       
 }

Added: incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapSearchParameters.java
==============================================================================
--- (empty file)
+++ incubator/directory/ldap/trunk/clients/src/java/org/apache/ldap/clients/embedded/LdapSearchParameters.java
Tue Jan 20 11:41:17 2004
@@ -0,0 +1,213 @@
+/* ====================================================================
+ * The Apache Software License, Version 1.1
+ *
+ * Copyright (c) 2000 The Apache Software Foundation.  All rights
+ * reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. The end-user documentation included with the redistribution,
+ *    if any, must include the following acknowledgment:
+ *       "This product includes software developed by the
+ *        Apache Software Foundation (http://www.apache.org/)."
+ *    Alternately, this acknowledgment may appear in the software itself,
+ *    if and wherever such third-party acknowledgments normally appear.
+ *
+ * 4. The names "Apache" and "Apache Software Foundation" must
+ *    not be used to endorse or promote products derived from this
+ *    software without prior written permission. For written
+ *    permission, please contact apache@apache.org.
+ *
+ * 5. Products derived from this software may not be called "Apache",
+ *    nor may "Apache" appear in their name, without prior written
+ *    permission of the Apache Software Foundation.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.ldap.clients.embedded ;
+
+import org.apache.ldap.common.message.ScopeEnum;
+
+/**
+ * Class to hold the parameters for a search operation.  
+ *
+ * @author <a href="mailto:jmachols@apache.org">Jeff Machols</a>
+ * @author $Author: jmachols $
+ * @version $Revision:  $
+ */
+public class LdapSearchParameters 
+{
+
+		private String m_filter ;
+		
+		/**
+		 * Base DN to start the search from
+		 */
+		private String m_base ;
+
+		/**
+		 * Search Scope:
+		 *  base - only the base DN
+		 *  one  - only the current subtree level
+		 *  sub  - the entire tree
+		 */
+		private ScopeEnum m_scope = ScopeEnum.SINGLELEVEL ;
+
+		private boolean m_quietMode = false ;
+		
+		private boolean m_typesMode = false ;
+		
+		private int m_maxEntries = 0 ;
+		
+		private int m_maxTime = 0 ;
+		
+
+		/**
+		 * Get the base DN to start the search from 
+		 * 
+		 * @return DN to start the search from 
+		 */
+		public String getBase()
+		{
+			return m_base;
+		}
+
+		/**
+		 * Get the search filter in a string format
+		 * 
+		 * @return Search filter
+		 */
+		public String getFilter()
+		{
+			return m_filter;
+		}
+
+		/**
+		 * Get the base DN to start the search from 
+		 * 
+		 * @param a_string DN to start the search from 
+		 */
+		public void setBase( String a_string )
+		{
+			m_base = a_string;
+		}
+
+		/**
+		 * Set the search filter
+		 * 
+		 * @param a_string Search Filter
+		 */
+		public void setFilter( String a_string )
+		{
+			m_filter = a_string;
+		}
+		
+		/**
+		 * Get the Scope for the search
+		 * 
+		 * @return Search Scope:
+		 *  BASEOBJECT   - only the base DN
+		 *  SINGLELEVEL  - only the current subtree level
+		 *  WHOLESUBTREE - the entire tree
+		 */
+		public ScopeEnum getScope()
+		{
+			return m_scope;
+		}
+
+		/**
+		 * Set the Scope for the search
+		 * 
+		 * @param a_enum Search Scope:
+		 *  BASEOBJECT   - only the base DN
+		 *  SINGLELEVEL  - only the current subtree level
+		 *  WHOLESUBTREE - the entire tree
+		 */
+		public void setScope( ScopeEnum a_enum )
+		{
+			m_scope = a_enum;
+		}
+		
+		/**
+		 * @return
+		 */
+		public boolean isQuietMode()
+		{
+			return m_quietMode;
+		}
+
+		/**
+		 * @param a_b
+		 */
+		public void setQuietMode(boolean a_b)
+		{
+			m_quietMode = a_b;
+		}
+
+		/**
+		 * @return
+		 */
+		public boolean isTypesMode()
+		{
+			return m_typesMode;
+		}
+
+		/**
+		 * @param a_b
+		 */
+		public void setTypesMode(boolean a_b)
+		{
+			m_typesMode = a_b;
+		}
+
+		public void setMaxEntries( int a_maxEntries )
+		{
+			m_maxEntries = a_maxEntries ;
+		}
+		
+		public int getMaxEntries() 
+		{
+			return m_maxEntries ;
+		}
+		
+		public void setMaxTime( int a_maxTime )
+		{
+			m_maxTime = a_maxTime ;
+		}
+		
+		public int getMaxTime()
+		{
+			return m_maxTime ;
+		}
+        
+        
+}

Mime
View raw message