You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2009/06/03 17:38:33 UTC

svn commit: r781438 - in /directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api: LdapConnection.java messages/ModifyRequest.java messages/ModifyResponse.java

Author: kayyagari
Date: Wed Jun  3 15:38:33 2009
New Revision: 781438

URL: http://svn.apache.org/viewvc?rev=781438&view=rev
Log:
implementation of modify operation

Added:
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyRequest.java
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyResponse.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=781438&r1=781437&r2=781438&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 Wed Jun  3 15:38:33 2009
@@ -24,6 +24,7 @@
 import java.net.SocketAddress;
 import java.text.ParseException;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -54,6 +55,8 @@
 import org.apache.directory.shared.ldap.client.api.messages.IntermediateResponseImpl;
 import org.apache.directory.shared.ldap.client.api.messages.LdapResult;
 import org.apache.directory.shared.ldap.client.api.messages.LdapResultImpl;
+import org.apache.directory.shared.ldap.client.api.messages.ModifyRequest;
+import org.apache.directory.shared.ldap.client.api.messages.ModifyResponse;
 import org.apache.directory.shared.ldap.client.api.messages.Referral;
 import org.apache.directory.shared.ldap.client.api.messages.ReferralImpl;
 import org.apache.directory.shared.ldap.client.api.messages.SearchRequest;
@@ -80,6 +83,8 @@
 import org.apache.directory.shared.ldap.codec.bind.SaslCredentials;
 import org.apache.directory.shared.ldap.codec.bind.SimpleAuthentication;
 import org.apache.directory.shared.ldap.codec.intermediate.IntermediateResponseCodec;
+import org.apache.directory.shared.ldap.codec.modify.ModifyRequestCodec;
+import org.apache.directory.shared.ldap.codec.modify.ModifyResponseCodec;
 import org.apache.directory.shared.ldap.codec.search.Filter;
 import org.apache.directory.shared.ldap.codec.search.SearchRequestCodec;
 import org.apache.directory.shared.ldap.codec.search.SearchResultDoneCodec;
@@ -88,6 +93,11 @@
 import org.apache.directory.shared.ldap.codec.unbind.UnBindRequestCodec;
 import org.apache.directory.shared.ldap.cursor.Cursor;
 import org.apache.directory.shared.ldap.cursor.ListCursor;
+import org.apache.directory.shared.ldap.entry.Entry;
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.Modification;
+import org.apache.directory.shared.ldap.entry.ModificationOperation;
+import org.apache.directory.shared.ldap.entry.Value;
 import org.apache.directory.shared.ldap.filter.ExprNode;
 import org.apache.directory.shared.ldap.filter.FilterParser;
 import org.apache.directory.shared.ldap.filter.SearchScope;
@@ -120,7 +130,7 @@
 public class LdapConnection  extends IoHandlerAdapter
 {
     /** logger for reporting errors that might not be handled properly upstream */
-    private static final Logger LOG = LoggerFactory.getLogger( LdapConnectionImpl.class );
+    private static final Logger LOG = LoggerFactory.getLogger( LdapConnection.class );
 
     /** Define the default ports for LDAP and LDAPS */
     private static final int DEFAULT_LDAP_PORT = 389; 
@@ -1338,7 +1348,7 @@
         // Feed the response and store it into the session
         LdapMessageCodec response = (LdapMessageCodec)message;
 
-        LOG.debug( "-------> {} Message received <-------", response.getMessageTypeName() );
+        System.out.println( "-------> {} Message received <-------"+ response.getMessageTypeName() );
         
         switch ( response.getMessageType() )
         {
@@ -1463,4 +1473,112 @@
              default: LOG.error( "~~~~~~~~~~~~~~~~~~~~~ Unknown message type {} ~~~~~~~~~~~~~~~~~~~~~", response.getMessageTypeName() );
         }
     }
+    
+    
+    /**
+     * 
+     * modifies all the attributes present in the entry by applying the same operation.
+     *
+     * @param entry the entry whise attributes to be modified
+     * @param modOp the operation to be applied on all the attributes of the above entry
+     * @return the modify operation's response
+     * @throws LdapException in case of modify operation failure or timeout happens
+     */
+    public ModifyResponse modify( Entry entry, ModificationOperation modOp ) throws LdapException
+    {
+        if( entry == null )
+        {
+            LOG.debug( "received a null entry for modification" );
+            throw new NullPointerException( "Entry to be modified cannot be null" );
+        }
+        
+        ModifyRequest modReq = new ModifyRequest( entry.getDn() );
+        
+        Iterator<EntryAttribute> itr = entry.iterator();
+        while( itr.hasNext() )
+        {
+            modReq.addModification( itr.next(), modOp );
+        }
+        
+        return modify( modReq );
+    }
+    
+    
+    /**
+     * 
+     * performs modify operation based on the modifications present in the ModifyRequest.
+     *
+     * @param modRequest the request for modify operation
+     * @return the modify operation's response
+     * @throws LdapException in case of modify operation failure or timeout happens
+     */
+    public ModifyResponse modify( ModifyRequest modRequest )  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 new message and update the messageId
+        LdapMessageCodec modifyMessage = new LdapMessageCodec();
+        
+        // Creates the messageID and stores it into the 
+        // initial message and the transmitted message.
+        int newId = messageId.incrementAndGet();
+        modRequest.setMessageId( newId );
+        modifyMessage.setMessageId( newId );
+        
+        ModifyRequestCodec modReqCodec = new ModifyRequestCodec();
+        modReqCodec.setModifications( modRequest.getMods() );
+        modReqCodec.setObject( modRequest.getDn() );
+
+        modifyMessage.setProtocolOP( modReqCodec );
+        
+        ldapSession.write( modifyMessage );
+
+        LdapMessageCodec response = null;
+        try
+        {
+            response = modifyResponseQueue.poll( modRequest.getTimeout(), TimeUnit.MILLISECONDS );
+            
+            // Check that we didn't get out because of a timeout
+            if ( response == null )
+            {
+                // We didn't received anything : this is an error
+                LOG.error( "Modify failed : timeout occured" );
+                unlockSession();
+                throw new LdapException( "TimeOut occured" );
+            }
+        }
+        catch( Exception e )
+        {
+            LOG.error( "The response queue has been emptied, no response was found." );
+            unlockSession();
+            LdapException ldapException = new LdapException();
+            ldapException.initCause( e );
+            
+            throw ldapException;
+        }
+
+        unlockSession();
+        
+        return convert( response.getModifyResponse() );
+    }
+    
+    
+    /**
+     * converts the ModifyResponseCodec to ModifyResponse.
+     */
+    private ModifyResponse convert( ModifyResponseCodec modRespCodec )
+    {
+        ModifyResponse modResponse = new ModifyResponse();
+        
+        modResponse.setMessageId( modRespCodec.getMessageId() );
+        modResponse.setLdapResult( convert( modRespCodec.getLdapResult() ) );
+
+        return modResponse;
+    }
+
 }

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyRequest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyRequest.java?rev=781438&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyRequest.java (added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyRequest.java Wed Jun  3 15:38:33 2009
@@ -0,0 +1,207 @@
+/*
+ *   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.messages;
+
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.directory.shared.ldap.entry.EntryAttribute;
+import org.apache.directory.shared.ldap.entry.Modification;
+import org.apache.directory.shared.ldap.entry.ModificationOperation;
+import org.apache.directory.shared.ldap.entry.client.ClientModification;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
+import org.apache.directory.shared.ldap.name.LdapDN;
+
+
+/**
+ * ModificationRequest for performing modify operation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ModifyRequest extends AbstractRequest
+{
+    /** DN of the target Entry to be modified */
+    private LdapDN dn;
+
+    /** modifications list */
+    private List<Modification> mods = new ArrayList<Modification>();
+
+
+    /**
+     * 
+     * Creates a new instance of ModifyRequest.
+     *
+     * @param dn DN of the Entry to be modified 
+     */
+    public ModifyRequest( LdapDN dn )
+    {
+        super();
+        setTimeout( 1000 * 1000 ); //TODO should this value be set in AbstractRequest as default?
+        this.dn = dn;
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for addition in the target entry with the 
+     * given values.
+     *
+     * @param attributeName name of the attribute to be added
+     * @param attributeValue values of the attribute
+     */
+    public void add( String attributeName, String... attributeValue )
+    {
+        addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * @see #add(String, String...)
+     */
+    public void add( String attributeName, byte[]... attributeValue )
+    {
+        addModification( ModificationOperation.ADD_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for addition in the target entry.
+     *
+     * @param attr the attribute to be added
+     */
+    public void add( EntryAttribute attr )
+    {
+        addModification( attr, ModificationOperation.ADD_ATTRIBUTE );
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for replacement with the given 
+     * values in the target entry. 
+     *
+     * @param attributeName name of the attribute to be added
+     * @param attributeValue values of the attribute
+     */
+    public void replace( String attributeName, String... attributeValue )
+    {
+        addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * @see #remove(String, String...)
+     */
+    public void replace( String attributeName, byte[]... attributeValue )
+    {
+        addModification( ModificationOperation.REPLACE_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for replacement in the target entry.
+     *
+     * @param attr the attribute to be added
+     */
+    public void replace( EntryAttribute attr )
+    {
+        addModification( attr, ModificationOperation.REPLACE_ATTRIBUTE );
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for removal with the given 
+     * values from the target entry. 
+     *
+     * @param attributeName name of the attribute to be added
+     * @param attributeValue values of the attribute
+     */
+    public void remove( String attributeName, String... attributeValue )
+    {
+        addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * @see #remove(String, String...)
+     */
+    public void remove( String attributeName, byte[]... attributeValue )
+    {
+        addModification( ModificationOperation.REMOVE_ATTRIBUTE, attributeName, attributeValue );
+    }
+
+
+    /**
+     * 
+     * marks a given attribute for removal from the target entry.
+     *
+     * @param attr the attribute to be added
+     */
+    public void remove( EntryAttribute attr )
+    {
+        addModification( attr, ModificationOperation.REMOVE_ATTRIBUTE );
+    }
+
+
+    private void addModification( ModificationOperation modOp, String attributeName, String... attributeValue )
+    {
+        EntryAttribute attr = new DefaultClientAttribute( attributeName, attributeValue );
+        addModification( attr, modOp );
+    }
+
+
+    private void addModification( ModificationOperation modOp, String attributeName, byte[]... attributeValue )
+    {
+        EntryAttribute attr = new DefaultClientAttribute( attributeName, attributeValue );
+        addModification( attr, modOp );
+    }
+
+
+    public void addModification( EntryAttribute attr, ModificationOperation modOp )
+    {
+        mods.add( new ClientModification( modOp, attr ) );
+    }
+
+
+    /**
+     * @return the target entry's DN
+     */
+    public LdapDN getDn()
+    {
+        return dn;
+    }
+
+
+    /**
+     * @return the list of modifications
+     */
+    public List<Modification> getMods()
+    {
+        return mods;
+    }
+
+}

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyResponse.java?rev=781438&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyResponse.java (added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ModifyResponse.java Wed Jun  3 15:38:33 2009
@@ -0,0 +1,36 @@
+/*
+ *   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.messages;
+
+
+/**
+ * Modify operation's response.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ModifyResponse extends AbstractResponseWithResult
+{
+    public ModifyResponse()
+    {
+        super();
+    }
+}