directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 56543 - incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol
Date Thu, 04 Nov 2004 00:53:17 GMT
Author: akarasulu
Date: Wed Nov  3 16:53:17 2004
New Revision: 56543

Modified:
   incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/AddHandler.java
   incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/BindHandler.java
   incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/SearchHandler.java
Log:
implemented add, bind and search

Modified: incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/AddHandler.java
==============================================================================
--- incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/AddHandler.java
(original)
+++ incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/AddHandler.java
Wed Nov  3 16:53:17 2004
@@ -19,12 +19,13 @@
 
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
 
 import org.apache.seda.listener.ClientKey;
 import org.apache.seda.protocol.AbstractSingleReplyHandler;
 
-import org.apache.ldap.common.name.LdapName;
 import org.apache.ldap.common.message.*;
+import org.apache.ldap.common.util.ExceptionUtils;
 
 
 /**
@@ -42,17 +43,28 @@
         resp.setLdapResult( new LdapResultImpl( resp ) );
         InitialContext ictx = SessionRegistry.getSingleton( null ).get( key );
 
-        LdapName dn;
-
         try
         {
-            dn = new LdapName( req.getName() );
+            DirContext ctx = ( DirContext ) ictx.lookup( "" );
+            ctx.createSubcontext( req.getName(), req.getEntry() );
         }
         catch ( NamingException e )
         {
-            //resp.getLdapResult().setResultCode( ResultCodeEnum);
+            String msg = "failed to add entry " + req.getName() + ":\n";
+            msg += ExceptionUtils.getStackTrace( e );
+            ResultCodeEnum code;
+            code = ResultCodeEnum.getBestEstimate( e, req.getType() );
+            resp.getLdapResult().setResultCode( code );
+            resp.getLdapResult().setErrorMessage( msg );
+
+            if ( e.getResolvedName() != null )
+            {
+                resp.getLdapResult().setMatchedDn( e.getResolvedName().toString() );
+            }
         }
 
+        resp.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
+        resp.getLdapResult().setMatchedDn( req.getName() );
         return resp;
     }
 }

Modified: incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/BindHandler.java
==============================================================================
--- incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/BindHandler.java
(original)
+++ incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/BindHandler.java
Wed Nov  3 16:53:17 2004
@@ -73,6 +73,8 @@
         }
 
         SessionRegistry.getSingleton( null ).put( key, ictx );
+        resp.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
+        resp.getLdapResult().setMatchedDn( req.getName() );
         return resp;
     }
 }

Modified: incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/SearchHandler.java
==============================================================================
--- incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/SearchHandler.java
(original)
+++ incubator/directory/eve/trunk/protocol/src/java/org/apache/eve/protocol/SearchHandler.java
Wed Nov  3 16:53:17 2004
@@ -17,12 +17,22 @@
 package org.apache.eve.protocol;
 
 
-import java.util.Iterator;
+import java.util.*;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.ldap.LdapContext;
+import javax.naming.directory.SearchResult;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.Attribute;
 
 import org.apache.seda.protocol.AbstractManyReplyHandler;
 import org.apache.seda.listener.ClientKey;
 
-import org.apache.ldap.common.NotImplementedException;
+import org.apache.ldap.common.util.ExceptionUtils;
+import org.apache.ldap.common.util.ArrayUtils;
+import org.apache.ldap.common.message.*;
 
 
 /**
@@ -33,6 +43,9 @@
  */
 public class SearchHandler extends AbstractManyReplyHandler
 {
+    private static final String DEREFALIASES_KEY = "java.naming.ldap.derefAliases";
+
+
     public SearchHandler()
     {
         super( true );
@@ -41,6 +54,257 @@
 
     public Iterator handle( ClientKey key, Object request )
     {
-        throw new NotImplementedException();
+        LdapContext ctx;
+        SearchRequest req = ( SearchRequest ) request;
+        InitialContext ictx = SessionRegistry.getSingleton( null ).get( key );
+        NamingEnumeration list = null;
+
+        // check the attributes to see if a referral's ref attribute is included
+        String[] ids = null;
+        Collection retAttrs = new HashSet();
+        retAttrs.addAll( req.getAttributes() );
+        if ( retAttrs.size() > 0 && ! retAttrs.contains( "ref" ) )
+        {
+            retAttrs.add( "ref" );
+            ids = ( String [] ) retAttrs.toArray( ArrayUtils.EMPTY_STRING_ARRAY );
+        }
+        else if ( retAttrs.size() > 0 )
+        {
+            ids = ( String [] ) retAttrs.toArray( ArrayUtils.EMPTY_STRING_ARRAY );
+        }
+
+        // prepare all the search controls
+        SearchControls controls = new SearchControls();
+        controls.setCountLimit( req.getSizeLimit() );
+        controls.setTimeLimit( req.getTimeLimit() );
+        controls.setSearchScope( req.getScope().getValue() );
+        controls.setReturningObjFlag( req.getTypesOnly() );
+        controls.setReturningAttributes( ids );
+        controls.setDerefLinkFlag( true );
+
+        try
+        {
+            ctx = ( LdapContext ) ictx.lookup( "" );
+            ctx.addToEnvironment( DEREFALIASES_KEY, req.getDerefAliases().getName() );
+            list = ctx.search( req.getBase(), req.getFilter().toString(), controls );
+        }
+        catch ( NamingException e )
+        {
+            String msg = "failed on search operation:\n" + req + "\n";
+            msg += ExceptionUtils.getStackTrace( e );
+            SearchResponseDone resp = new SearchResponseDoneImpl( req.getMessageId() );
+            ResultCodeEnum rc = ResultCodeEnum.getBestEstimate( e, req.getType() );
+            resp.setLdapResult( new LdapResultImpl( resp ) );
+            resp.getLdapResult().setResultCode( rc );
+            resp.getLdapResult().setErrorMessage( msg );
+            if ( e.getResolvedName() != null )
+            {
+                resp.getLdapResult().setMatchedDn( e.getResolvedName().toString() );
+            }
+
+            return Collections.singleton( resp ).iterator();
+        }
+
+        return new SearchResponseIterator( req, list );
+    }
+
+
+    SearchResponseDone getResponse( SearchRequest req, NamingException e )
+    {
+        String msg = "failed on search operation:\n" + req + "\n";
+        msg += ExceptionUtils.getStackTrace( e );
+        SearchResponseDone resp = new SearchResponseDoneImpl( req.getMessageId() );
+        ResultCodeEnum rc = ResultCodeEnum.getBestEstimate( e, req.getType() );
+        resp.setLdapResult( new LdapResultImpl( resp ) );
+        resp.getLdapResult().setResultCode( rc );
+        resp.getLdapResult().setErrorMessage( msg );
+        if ( e.getResolvedName() != null )
+        {
+            resp.getLdapResult().setMatchedDn( e.getResolvedName().toString() );
+        }
+
+        return resp;
+    }
+
+
+    class SearchResponseIterator implements Iterator
+    {
+        private final SearchRequest req;
+        private final NamingEnumeration underlying;
+        private SearchResponseDone respDone;
+        private boolean done = false;
+        private Object prefetched;
+
+
+        /**
+         * Creates a search response iterator for the resulting enumeration
+         * over a search request.
+         *
+         * @param req the search request to generate responses to
+         * @param underlying the underlying JNDI enumeration containing SearchResults
+         */
+        public SearchResponseIterator( SearchRequest req, NamingEnumeration underlying )
+        {
+            this.req = req;
+            this.underlying = underlying;
+
+            try
+            {
+                if ( underlying.hasMore() )
+                {
+                    SearchResult result = ( SearchResult ) underlying.next();
+
+                    /*
+                     * Now we have to build the prefetched object from the 'result'
+                     * local variable for the following call to next()
+                     */
+                    Attribute ref = result.getAttributes().get( "ref" );
+                    if ( ref == null && ref.size() > 0 )
+                    {
+                        SearchResponseEntry respEntry;
+                        respEntry = new SearchResponseEntryImpl( req.getMessageId() );
+                        respEntry.setAttributes( result.getAttributes() );
+                        respEntry.setObjectName( result.getName() );
+                        prefetched = respEntry;
+                    }
+                    else
+                    {
+                        SearchResponseReference respRef;
+                        respRef = new SearchResponseReferenceImpl( req.getMessageId() );
+                        respRef.setReferral( new ReferralImpl( respRef ) );
+                        for ( int ii = 0; ii < ref.size(); ii++ )
+                        {
+                            String url;
+
+                            try
+                            {
+                                url = ( String ) ref.get( ii );
+                                respRef.getReferral().addLdapUrl( url );
+                            }
+                            catch ( NamingException e )
+                            {
+                                try { underlying.close(); } catch( Throwable t ) {}
+                                prefetched = null;
+                                respDone = getResponse( req, e );
+                            }
+                        }
+                        prefetched = respRef;
+                    }
+                }
+            }
+            catch ( NamingException e )
+            {
+                try { this.underlying.close(); } catch( Exception e2 ) {}
+                respDone = getResponse( req, e );
+            }
+        }
+
+
+        public boolean hasNext()
+        {
+            return !done;
+        }
+
+
+        public Object next()
+        {
+            Object next = prefetched;
+            SearchResult result = null;
+
+            // if we're done we got nothing to give back
+            if ( done )
+            {
+                throw new NoSuchElementException();
+            }
+
+            // if respDone has been assembled this is our last object to return
+            if ( respDone != null )
+            {
+                done = true;
+                return respDone;
+            }
+
+            /*
+             * If we have gotten this far then we have a valid next entry
+             * or referral to return from this call in the 'next' variable.
+             */
+            try
+            {
+                /*
+                 * If we have more results from the underlying cursorr then
+                 * we just set the result and build the response object below.
+                 */
+                if ( underlying.hasMore() )
+                {
+                    result = ( SearchResult ) underlying.next();
+                }
+                else
+                {
+                    try { underlying.close(); } catch( Throwable t ) {}
+                    respDone = new SearchResponseDoneImpl( req.getMessageId() );
+                    respDone.setLdapResult( new LdapResultImpl( respDone ) );
+                    respDone.getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
+                    respDone.getLdapResult().setMatchedDn( req.getBase() );
+                    prefetched = null;
+                    return next;
+                }
+            }
+            catch ( NamingException e )
+            {
+                try { underlying.close(); } catch( Throwable t ) {}
+                prefetched = null;
+                respDone = getResponse( req, e );
+                return next;
+            }
+
+            /*
+             * Now we have to build the prefetched object from the 'result'
+             * local variable for the following call to next()
+             */
+            Attribute ref = result.getAttributes().get( "ref" );
+            if ( ref == null && ref.size() > 0 )
+            {
+                SearchResponseEntry respEntry = new SearchResponseEntryImpl( req.getMessageId()
);
+                respEntry.setAttributes( result.getAttributes() );
+                respEntry.setObjectName( result.getName() );
+                prefetched = respEntry;
+            }
+            else
+            {
+                SearchResponseReference respRef = new SearchResponseReferenceImpl( req.getMessageId()
);
+                respRef.setReferral( new ReferralImpl( respRef ) );
+                for ( int ii = 0; ii < ref.size(); ii++ )
+                {
+                    String url;
+
+                    try
+                    {
+                        url = ( String ) ref.get( ii );
+                        respRef.getReferral().addLdapUrl( url );
+                    }
+                    catch ( NamingException e )
+                    {
+                        try { underlying.close(); } catch( Throwable t ) {}
+                        prefetched = null;
+                        respDone = getResponse( req, e );
+                        return next;
+                    }
+                }
+                prefetched = respRef;
+            }
+
+            return next;
+        }
+
+
+        /**
+         * Unsupported so it throws an exception.
+         *
+         * @throws UnsupportedOperationException
+         */
+        public void remove()
+        {
+            throw new UnsupportedOperationException();
+        }
     }
 }

Mime
View raw message