directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r795422 - in /directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api: LdapConnection.java listeners/CompareListener.java messages/CompareRequest.java messages/CompareResponse.java
Date Sat, 18 Jul 2009 19:31:04 GMT
Author: kayyagari
Date: Sat Jul 18 19:31:03 2009
New Revision: 795422

URL: http://svn.apache.org/viewvc?rev=795422&view=rev
Log:
added compare operation ( DIRAPI-1 )

Added:
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/CompareListener.java
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareRequest.java
    directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareResponse.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=795422&r1=795421&r2=795422&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 Jul 18 19:31:03 2009
@@ -46,6 +46,7 @@
 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.CompareListener;
 import org.apache.directory.shared.ldap.client.api.listeners.DeleteListener;
 import org.apache.directory.shared.ldap.client.api.listeners.IntermediateResponseListener;
 import org.apache.directory.shared.ldap.client.api.listeners.ModifyDnListener;
@@ -57,6 +58,8 @@
 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.BindResponse;
+import org.apache.directory.shared.ldap.client.api.messages.CompareRequest;
+import org.apache.directory.shared.ldap.client.api.messages.CompareResponse;
 import org.apache.directory.shared.ldap.client.api.messages.DeleteRequest;
 import org.apache.directory.shared.ldap.client.api.messages.DeleteResponse;
 import org.apache.directory.shared.ldap.client.api.messages.IntermediateResponse;
@@ -87,6 +90,8 @@
 import org.apache.directory.shared.ldap.codec.bind.LdapAuthentication;
 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.compare.CompareRequestCodec;
+import org.apache.directory.shared.ldap.codec.compare.CompareResponseCodec;
 import org.apache.directory.shared.ldap.codec.del.DelRequestCodec;
 import org.apache.directory.shared.ldap.codec.del.DelResponseCodec;
 import org.apache.directory.shared.ldap.codec.intermediate.IntermediateResponseCodec;
@@ -127,10 +132,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-// TODO : handle the MessageId for an abandonRequest adding Futures to async methods and
-//        check how to handle the synchronous methods for abandoning
-// TODO : return the created request, instead of an LdapResponse ( partly completed )
-
 /**
  * 
  *  Describe the methods to be implemented by the LdapConnection class.
@@ -181,7 +182,7 @@
     private BlockingQueue<BindResponse> bindResponseQueue;
     
     /** A queue used to store the incoming compare responses */
-    private BlockingQueue<LdapMessageCodec> compareResponseQueue;
+    private BlockingQueue<CompareResponse> compareResponseQueue;
     
     /** A queue used to store the incoming delete responses */
     private BlockingQueue<DeleteResponse> deleteResponseQueue;
@@ -615,7 +616,7 @@
         // Create the responses queues
         addResponseQueue = new LinkedBlockingQueue<AddResponse>();
         bindResponseQueue = new LinkedBlockingQueue<BindResponse>();
-        compareResponseQueue = new LinkedBlockingQueue<LdapMessageCodec>();
+        compareResponseQueue = new LinkedBlockingQueue<CompareResponse>();
         deleteResponseQueue = new LinkedBlockingQueue<DeleteResponse>();
         extendedResponseQueue = new LinkedBlockingQueue<LdapMessageCodec>();
         modifyResponseQueue = new LinkedBlockingQueue<ModifyResponse>();
@@ -1489,7 +1490,23 @@
                 
             case LdapConstants.COMPARE_RESPONSE :
                 // Store the response into the responseQueue
-                compareResponseQueue.add( response ); 
+                CompareResponseCodec compResCodec = response.getCompareResponse();
+                compResCodec.setMessageId( response.getMessageId() );
+                compResCodec.addControl( response.getCurrentControl() );
+                CompareResponse compRes = convert( compResCodec );
+                
+                futureMap.remove( compRes.getMessageId() );
+                
+                CompareListener listener = ( CompareListener ) listenerMap.remove( compRes.getMessageId()
);
+                if( listener != null )
+                {
+                    listener.attributeCompared( this, compRes );
+                }
+                else
+                {
+                    compareResponseQueue.add( compRes ); 
+                }
+                
                 break;
                 
             case LdapConstants.DEL_RESPONSE :
@@ -2174,6 +2191,131 @@
         return response;
     }
 
+
+    /**
+     * @see #compare(LdapDN, String, Object) 
+     */
+    public CompareResponse compare( String dn, String attributeName, Object value ) throws
LdapException
+    {
+        try
+        {
+            return compare( new LdapDN( dn ), attributeName, value );
+        }
+        catch( Exception e )
+        {
+            LOG.error( "Failed to perform compare operation", e );
+            throw new LdapException( e );
+        }
+    }
+
+    
+    /**
+     * compares a whether a given attribute's value matches that of the 
+     * existing value of the attribute present in the entry with the given DN
+     *
+     * @param dn the target entry's DN
+     * @param attributeName the attribute's name
+     * @param value a value with which the target entry's attribute value to be compared
with
+     * @return compare operation's response
+     * @throws LdapException
+     */
+    public CompareResponse compare( LdapDN dn, String attributeName, Object value ) throws
LdapException
+    {
+        CompareRequest compareRequest = new CompareRequest();
+        compareRequest.setEntryDn( dn );
+        compareRequest.setAttrName( attributeName );
+        compareRequest.setValue( value );
+        
+        return compare( compareRequest, null );
+    }
+
+    
+    /**
+     * compares an entry's attribute's value with that of the given value
+     *   
+     * @param compareRequest the CompareRequest which contains the target DN, attribute name
and value
+     * @param listener a non-null listener if provided will be called to receive this operation's
response asynchronously
+     * @return compare operation's reponse
+     * @throws LdapException
+     */
+    public CompareResponse compare( CompareRequest compareRequest, CompareListener listener
) throws LdapException
+    {
+        checkSession();
+
+        lockSession();
+        
+        CompareRequestCodec compareReqCodec = new CompareRequestCodec();
+        
+        int newId = messageId.incrementAndGet();
+        LdapMessageCodec message = new LdapMessageCodec();
+        message.setMessageId( newId );
+        compareReqCodec.setMessageId( newId );
+
+        compareReqCodec.setEntry( compareRequest.getEntryDn() );
+        compareReqCodec.setAttributeDesc( compareRequest.getAttrName() );
+        compareReqCodec.setAssertionValue( compareRequest.getValue() );
+        setControls( compareRequest.getControls(), compareReqCodec );
+        
+        message.setProtocolOP( compareReqCodec );
+        
+        ResponseFuture compareFuture = new ResponseFuture( compareResponseQueue );
+        futureMap.put( newId, compareFuture );
+
+        // Send the request to the server
+        ldapSession.write( message );
+        
+        CompareResponse response = null;
+        if( listener == null )
+        {
+            try
+            {
+                long timeout = getTimeout( compareRequest.getTimeout() );
+                response = ( CompareResponse ) compareFuture.get( timeout, TimeUnit.MILLISECONDS
);
+                
+                if ( response == null )
+                {
+                    LOG.error( "Compare failed : timeout occured" );
+                    unlockSession();
+                    throw new LdapException( TIME_OUT_ERROR );
+                }
+            }
+            catch( InterruptedException ie )
+            {
+                LOG.error( "Operation would have been cancelled", ie );
+                throw new LdapException( ie );
+            }
+            catch( Exception e )
+            {
+                LOG.error( NO_RESPONSE_ERROR );
+                futureMap.remove( newId );
+                unlockSession();
+                throw new LdapException( e );
+            }
+        }
+        else
+        {
+            listenerMap.put( newId, listener );            
+        }
+
+        unlockSession();
+        
+        return response;
+    }
+    
+    
+    /**
+     * converts the CompareResponseCodec to CompareResponse.
+     */
+    private CompareResponse convert( CompareResponseCodec compareRespCodec )
+    {
+        CompareResponse compareResponse = new CompareResponse();
+        
+        compareResponse.setMessageId( compareRespCodec.getMessageId() );
+        compareResponse.setLdapResult( convert( compareRespCodec.getLdapResult() ) );
+        
+        return compareResponse;
+    }
+
     
     /**
      * converts the DeleteResponseCodec to DeleteResponse object.

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/CompareListener.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/CompareListener.java?rev=795422&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/CompareListener.java
(added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/CompareListener.java
Sat Jul 18 19:31:03 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.CompareResponse;
+
+/**
+ * interface used for invoking the callback after completing the compare operation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface CompareListener extends OperationResponseListener
+{
+    /**
+     * callback method called after completing the compare operation.
+     *
+     * @param connection the LdapConnection
+     * @param response the compare operation's response
+     * @throws LdapException
+     */
+    public void attributeCompared( LdapConnection connection, CompareResponse response )
throws LdapException;
+}

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareRequest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareRequest.java?rev=795422&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareRequest.java
(added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareRequest.java
Sat Jul 18 19:31:03 2009
@@ -0,0 +1,72 @@
+/*
+ *   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 org.apache.directory.shared.ldap.name.LdapDN;
+
+
+/**
+ * Class for representing client's compare operation request.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class CompareRequest extends AbstractRequest implements RequestWithResponse, AbandonableRequest
+{
+    /** the target entry's DN */
+    private LdapDN entryDn;
+    
+    private String attrName;
+    
+    private Object value;
+
+    public LdapDN getEntryDn()
+    {
+        return entryDn;
+    }
+
+    public void setEntryDn( LdapDN dn )
+    {
+        this.entryDn = dn;
+    }
+
+    public String getAttrName()
+    {
+        return attrName;
+    }
+
+    public void setAttrName( String attrName )
+    {
+        this.attrName = attrName;
+    }
+
+    public Object getValue()
+    {
+        return value;
+    }
+
+    public void setValue( Object attrValue )
+    {
+        this.value = attrValue;
+    }
+    
+}

Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareResponse.java?rev=795422&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareResponse.java
(added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/CompareResponse.java
Sat Jul 18 19:31:03 2009
@@ -0,0 +1,45 @@
+/*
+ *   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 org.apache.directory.shared.ldap.message.ResultCodeEnum;
+
+
+/**
+ * Response object for compare operation
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class CompareResponse extends AbstractResponseWithResult
+{
+    public CompareResponse()
+    {
+        super();
+    }
+
+
+    public boolean isTrue()
+    {
+        return super.getLdapResult().getResultCode() == ResultCodeEnum.COMPARE_TRUE;
+    }
+}



Mime
View raw message