directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r784377 - in /directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api: LdapConnection.java listeners/AddListener.java
Date Sat, 13 Jun 2009 11:32:48 GMT
Author: kayyagari
Date: Sat Jun 13 11:32:47 2009
New Revision: 784377

URL: http://svn.apache.org/viewvc?rev=784377&view=rev
Log:
o implemented add operation
o a listener for receiving add responses

Added:
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/AddListener.java
Modified:
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java

Modified: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java?rev=784377&r1=784376&r2=784377&view=diff
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
(original)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
Sat Jun 13 11:32:47 2009
@@ -44,6 +44,7 @@
 import org.apache.directory.shared.asn1.ber.IAsn1Container;
 import org.apache.directory.shared.ldap.client.api.exception.InvalidConnectionException;
 import org.apache.directory.shared.ldap.client.api.exception.LdapException;
+import org.apache.directory.shared.ldap.client.api.listeners.AddListener;
 import org.apache.directory.shared.ldap.client.api.listeners.BindListener;
 import org.apache.directory.shared.ldap.client.api.listeners.DeleteListener;
 import org.apache.directory.shared.ldap.client.api.listeners.IntermediateResponseListener;
@@ -52,6 +53,7 @@
 import org.apache.directory.shared.ldap.client.api.listeners.SearchListener;
 import org.apache.directory.shared.ldap.client.api.messages.AbandonRequest;
 import org.apache.directory.shared.ldap.client.api.messages.AbandonRequestImpl;
+import org.apache.directory.shared.ldap.client.api.messages.AddResponse;
 import org.apache.directory.shared.ldap.client.api.messages.BindRequest;
 import org.apache.directory.shared.ldap.client.api.messages.BindRequestImpl;
 import org.apache.directory.shared.ldap.client.api.messages.BindResponse;
@@ -86,6 +88,8 @@
 import org.apache.directory.shared.ldap.codec.LdapResultCodec;
 import org.apache.directory.shared.ldap.codec.TwixTransformer;
 import org.apache.directory.shared.ldap.codec.abandon.AbandonRequestCodec;
+import org.apache.directory.shared.ldap.codec.add.AddRequestCodec;
+import org.apache.directory.shared.ldap.codec.add.AddResponseCodec;
 import org.apache.directory.shared.ldap.codec.bind.BindRequestCodec;
 import org.apache.directory.shared.ldap.codec.bind.BindResponseCodec;
 import org.apache.directory.shared.ldap.codec.bind.LdapAuthentication;
@@ -674,6 +678,7 @@
         return true;
     }
     
+    
     //------------------------ The LDAP operations ------------------------//
     // Add operations                                                      //
     //---------------------------------------------------------------------//
@@ -682,75 +687,95 @@
      * to wait for the response until the AddResponse is returned.
      * 
      * @param entry The entry to add
-     * @result AddResponse The resulting response 
-     *
-    public AddResponse add( Entry entry )
+     * @result the add operation's response 
+     */
+    public AddResponse add( Entry entry ) throws LdapException
     {
         if ( entry == null ) 
         {
-            LOG.debug( "Cannot add empty entry" );
-            return null;
+            String msg = "Cannot add empty entry";
+            LOG.debug( msg );
+            throw new NullPointerException( msg );
         }
         
-        
+        return add( new AddRequest( entry ), null );
     }
     
     
-    public void add( AddRequest addRequest ) throws InvalidConnectionException
+    /**
+     * Add an entry present in the AddRequest to the server.
+     * @param addRequest the request object containing an entry and controls(if any)
+     * @return the add operation's response
+     * @throws LdapException
+     */
+    public AddResponse add( AddRequest addRequest, AddListener listener ) throws LdapException
     {
-        // If the session has not been establish, or is closed, we get out immediately
         checkSession();
 
-        // Guarantee that for this session, we don't have more than one operation
-        // running at the same time
         lockSession();
         
-        // Create the AddRequest
-        LdapDN dn = new LdapDN( name );
+        AddRequestCodec addReqCodec = new AddRequestCodec();
         
-        InternalAddRequest addRequest = new InternalBindRequest();
-        bindRequest.setName( dn );
-        bindRequest.setVersion( LDAP_V3 );
-        
-        // Create the Simple authentication
-        SimpleAuthentication simpleAuth = new SimpleAuthentication();
-        simpleAuth.setSimple( credentials );
+        int newId = messageId.incrementAndGet();
+        LdapMessageCodec message = new LdapMessageCodec();
+        message.setMessageId( newId );
+        addReqCodec.setMessageId( newId );
 
-        bindRequest.setAuthentication( simpleAuth );
+        addReqCodec.setEntry( addRequest.getEntry() );
+        addReqCodec.setEntryDn( addRequest.getEntry().getDn() );
+        setControls( addRequest.getControls(), addReqCodec );
         
-        // Encode the request
-        LdapMessage message = new LdapMessage();
-        message.setMessageId( messageId++ );
-        message.setProtocolOP( bindRequest );
+        message.setProtocolOP( addReqCodec );
         
-        LOG.debug( "-----------------------------------------------------------------" );
-        LOG.debug( "Sending request \n{}", message );
-
         // Send the request to the server
         ldapSession.write( message );
-
-        // Read the response, waiting for it if not available immediately
-        LdapMessage response = bindResponseQueue.poll( timeOut, TimeUnit.MILLISECONDS );
-    
-        // Check that we didn't get out because of a timeout
-        if ( response == null )
+        if( listener == null )
         {
-            // We didn't received anything : this is an error
-            LOG.error( "Bind failed : timeout occured" );
+            LdapMessageCodec response = null;
+            try
+            {
+                long timeout = getTimeout( addRequest.getTimeout() );
+                response = addResponseQueue.poll( timeout, TimeUnit.MILLISECONDS );
+                
+                if ( response == null )
+                {
+                    LOG.error( "Add failed : timeout occured" );
+                    unlockSession();
+                    throw new LdapException( TIME_OUT_ERROR );
+                }
+            }
+            catch( Exception e )
+            {
+                LOG.error( NO_RESPONSE_ERROR );
+                unlockSession();
+                throw new LdapException( e );
+            }
+            
             unlockSession();
-            throw new Exception( "TimeOut occured" );
+            
+            return convert( response.getAddResponse() );
         }
+        else
+        {
+            listenerMap.put( newId, listener );
+            return null;
+        }
+    }
+
+    
+    /**
+     * converts the AddResponseCodec to AddResponse.
+     */
+    private AddResponse convert( AddResponseCodec addRespCodec )
+    {
+        AddResponse addResponse = new AddResponse();
         
-        operationMutex.release();
-        
-        // Everything is fine, return the response
-        LdapResponse resp = response.getBindResponse();
-        
-        LOG.debug( "Bind successful : {}", resp );
+        addResponse.setMessageId( addRespCodec.getMessageId() );
+        addResponse.setLdapResult( convert( addRespCodec.getLdapResult() ) );
         
-        return resp;
+        return addResponse;
     }
-    */
+
     
     //------------------------ The LDAP operations ------------------------//
     // Abandon operations                                                  //
@@ -1374,7 +1399,17 @@
         {
             case LdapConstants.ADD_RESPONSE :
                 // Store the response into the responseQueue
-                addResponseQueue.add( response ); 
+                AddResponseCodec addRespCodec = response.getAddResponse();
+                addRespCodec.addControl( response.getCurrentControl() );
+                AddListener addListener = ( AddListener ) listenerMap.get( addRespCodec.getMessageId()
);
+                if( addListener != null )
+                {
+                    addListener.entryAdded( this, convert( addRespCodec ) );
+                }
+                else
+                {
+                    addResponseQueue.add( response ); 
+                }
                 break;
                 
             case LdapConstants.BIND_RESPONSE: 
@@ -1889,7 +1924,7 @@
             cursor = cursorMap.get( dn ); 
             if( cursor == null )
             {
-                cursor = search( dn.getUpName(), "(objectClass=*)", SearchScope.ONELEVEL,
SchemaConstants.ENTRY_UUID_AT );
+                cursor = search( dn.getUpName(), "(objectClass=*)", SearchScope.ONELEVEL,
null ); 
                 LOG.debug( "putting curosr for {}", dn.getUpName() );
                 cursorMap.put( dn, cursor );
             }

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/AddListener.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/AddListener.java?rev=784377&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/AddListener.java
(added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/AddListener.java
Sat Jun 13 11:32:47 2009
@@ -0,0 +1,43 @@
+/*
+ *   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.shared.ldap.client.api.listeners;
+
+import org.apache.directory.shared.ldap.client.api.LdapConnection;
+import org.apache.directory.shared.ldap.client.api.exception.LdapException;
+import org.apache.directory.shared.ldap.client.api.messages.AddResponse;
+
+/**
+ * interface used for invoking the callback after completing the add operation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface AddListener extends OperationResponseListener
+{
+    /**
+     * callback method called after completing the add operation.
+     *
+     * @param connection the LdapConnection
+     * @param response the add operation's response
+     * @throws LdapException
+     */
+    public void entryAdded( LdapConnection connection, AddResponse response ) throws LdapException;
+}



Mime
View raw message