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/07/26 16:24:40 UTC
svn commit: r797939 - in /directory:
apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/
shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/
shared/trunk/client-api/src/main/java/...
Author: kayyagari
Date: Sun Jul 26 14:24:39 2009
New Revision: 797939
URL: http://svn.apache.org/viewvc?rev=797939&view=rev
Log:
added support for extended operation
Added:
directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientExtendedRequestTest.java
directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/ExtendedListener.java
directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedRequest.java
directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedResponse.java
Modified:
directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/LdapConnection.java
Added: directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientExtendedRequestTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientExtendedRequestTest.java?rev=797939&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientExtendedRequestTest.java (added)
+++ directory/apacheds/trunk/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/operations/ClientExtendedRequestTest.java Sun Jul 26 14:24:39 2009
@@ -0,0 +1,106 @@
+/*
+ * 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.client.api.operations;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import javax.naming.ldap.StartTlsRequest;
+
+import org.apache.directory.server.core.integ.Level;
+import org.apache.directory.server.core.integ.annotations.CleanupLevel;
+import org.apache.directory.server.integ.SiRunner;
+import org.apache.directory.server.ldap.LdapServer;
+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.listeners.ExtendedListener;
+import org.apache.directory.shared.ldap.client.api.messages.ExtendedRequest;
+import org.apache.directory.shared.ldap.client.api.messages.ExtendedResponse;
+import org.apache.directory.shared.ldap.message.ResultCodeEnum;
+import org.apache.directory.shared.ldap.name.LdapDN;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests the extended operation
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+@RunWith(SiRunner.class)
+@CleanupLevel(Level.CLASS)
+public class ClientExtendedRequestTest
+{
+ /** The server instance */
+ public static LdapServer ldapServer;
+
+ private LdapConnection connection;
+
+ @Before
+ public void setup() throws Exception
+ {
+ connection = new LdapConnection( "localhost", ldapServer.getPort() );
+ LdapDN bindDn = new LdapDN( "uid=admin,ou=system" );
+ connection.bind( bindDn.getUpName(), "secret" );
+ }
+
+
+ @Test
+ public void testExtended() throws Exception
+ {
+ ExtendedRequest extendedRequest = new ExtendedRequest( StartTlsRequest.OID );
+
+ ExtendedResponse response = connection.extended( extendedRequest, null );
+ assertNotNull( response );
+ assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
+ }
+
+
+ @Test
+ public void testExtendedAsync() throws Exception
+ {
+ ExtendedRequest extendedRequest = new ExtendedRequest( StartTlsRequest.OID );
+
+ final AtomicBoolean done = new AtomicBoolean( false );
+
+ ExtendedListener listener = new ExtendedListener()
+ {
+ public void extendedOperationCompleted( LdapConnection connection, ExtendedResponse response ) throws LdapException
+ {
+ assertNotNull( response );
+ assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
+ done.set( true );
+ }
+ };
+
+ ExtendedResponse response = connection.extended( extendedRequest, listener );
+ assertNull( response );
+
+ while( !done.get() )
+ {
+ Thread.sleep( 1000 );
+ }
+ }
+}
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=797939&r1=797938&r2=797939&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 Sun Jul 26 14:24:39 2009
@@ -49,6 +49,7 @@
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.ExtendedListener;
import org.apache.directory.shared.ldap.client.api.listeners.IntermediateResponseListener;
import org.apache.directory.shared.ldap.client.api.listeners.ModifyDnListener;
import org.apache.directory.shared.ldap.client.api.listeners.ModifyListener;
@@ -63,6 +64,8 @@
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.ExtendedRequest;
+import org.apache.directory.shared.ldap.client.api.messages.ExtendedResponse;
import org.apache.directory.shared.ldap.client.api.messages.IntermediateResponse;
import org.apache.directory.shared.ldap.client.api.messages.LdapResult;
import org.apache.directory.shared.ldap.client.api.messages.ModifyDnRequest;
@@ -95,6 +98,8 @@
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.extended.ExtendedRequestCodec;
+import org.apache.directory.shared.ldap.codec.extended.ExtendedResponseCodec;
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;
@@ -192,7 +197,7 @@
private BlockingQueue<DeleteResponse> deleteResponseQueue;
/** A queue used to store the incoming extended responses */
- private BlockingQueue<LdapMessageCodec> extendedResponseQueue;
+ private BlockingQueue<ExtendedResponse> extendedResponseQueue;
/** A queue used to store the incoming modify responses */
private BlockingQueue<ModifyResponse> modifyResponseQueue;
@@ -603,7 +608,7 @@
bindResponseQueue = new LinkedBlockingQueue<BindResponse>();
compareResponseQueue = new LinkedBlockingQueue<CompareResponse>();
deleteResponseQueue = new LinkedBlockingQueue<DeleteResponse>();
- extendedResponseQueue = new LinkedBlockingQueue<LdapMessageCodec>();
+ extendedResponseQueue = new LinkedBlockingQueue<ExtendedResponse>();
modifyResponseQueue = new LinkedBlockingQueue<ModifyResponse>();
modifyDNResponseQueue = new LinkedBlockingQueue<ModifyDnResponse>();
searchResponseQueue = new LinkedBlockingQueue<SearchResponse>();
@@ -1489,8 +1494,22 @@
break;
case LdapConstants.EXTENDED_RESPONSE :
- // Store the response into the responseQueue
- extendedResponseQueue.add( response );
+ ExtendedResponseCodec extResCodec = response.getExtendedResponse();
+ extResCodec.setMessageId( response.getMessageId() );
+ extResCodec.addControl( response.getCurrentControl() );
+
+ ExtendedResponse extResponse = convert( extResCodec );
+ ExtendedListener extListener = ( ExtendedListener ) listenerMap.remove( extResCodec.getMessageId() );
+ if( extListener != null )
+ {
+ extListener.extendedOperationCompleted( this, extResponse );
+ }
+ else
+ {
+ // Store the response into the responseQueue
+ extendedResponseQueue.add( extResponse );
+ }
+
break;
case LdapConstants.INTERMEDIATE_RESPONSE:
@@ -2321,6 +2340,89 @@
/**
+ * requests the server to perform an extended operation based on the given request.
+ *
+ * @param extendedRequest the object containing the details of the extended operation to be performed
+ * @param listener an ExtendedListener instance, if a non-null instance is provided the result will be sent to this listener
+ * @return extended operation's reponse, or null if a non-null listener instance is provided
+ * @throws LdapException
+ */
+ public ExtendedResponse extended( ExtendedRequest extendedRequest, ExtendedListener listener ) throws LdapException
+ {
+ checkSession();
+
+ ExtendedRequestCodec extReqCodec = new ExtendedRequestCodec();
+
+ int newId = messageId.incrementAndGet();
+ LdapMessageCodec message = new LdapMessageCodec();
+ message.setMessageId( newId );
+ extReqCodec.setMessageId( newId );
+
+ extReqCodec.setRequestName( extendedRequest.getOid() );
+ extReqCodec.setRequestValue( extendedRequest.getValue() );
+ setControls( extendedRequest.getControls(), extReqCodec );
+
+ message.setProtocolOP( extReqCodec );
+
+ ResponseFuture extendedFuture = new ResponseFuture( extendedResponseQueue );
+ futureMap.put( newId, extendedFuture );
+
+ // Send the request to the server
+ ldapSession.write( message );
+
+ ExtendedResponse response = null;
+ if( listener == null )
+ {
+ try
+ {
+ long timeout = getTimeout( extendedRequest.getTimeout() );
+ response = ( ExtendedResponse ) extendedFuture.get( timeout, TimeUnit.MILLISECONDS );
+
+ if ( response == null )
+ {
+ LOG.error( "Extended operation failed : timeout occured" );
+
+ 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 );
+
+ throw new LdapException( e );
+ }
+ }
+ else
+ {
+ listenerMap.put( newId, listener );
+ }
+
+ return response;
+ }
+
+
+ /**
+ * converts the ExtendedResponseCodec to ExtendedResponse.
+ */
+ private ExtendedResponse convert( ExtendedResponseCodec extRespCodec )
+ {
+ ExtendedResponse extResponse = new ExtendedResponse();
+
+ extResponse.setValue( extRespCodec.getResponse() );
+ extResponse.setMessageId( extRespCodec.getMessageId() );
+ extResponse.setLdapResult( convert( extRespCodec.getLdapResult() ) );
+
+ return extResponse;
+ }
+
+
+ /**
* checks if a control with the given OID is supported
*
* @param controlOID the OID of the control
Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/ExtendedListener.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/ExtendedListener.java?rev=797939&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/ExtendedListener.java (added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/listeners/ExtendedListener.java Sun Jul 26 14:24:39 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.ExtendedResponse;
+
+/**
+ * interface used for invoking the callback after completing the extended operation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface ExtendedListener extends OperationResponseListener
+{
+ /**
+ * callback method called after completing the extended operation.
+ *
+ * @param connection the LdapConnection
+ * @param response the extended operation's response
+ * @throws LdapException
+ */
+ public void extendedOperationCompleted( LdapConnection connection, ExtendedResponse response ) throws LdapException;
+}
Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedRequest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedRequest.java?rev=797939&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedRequest.java (added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedRequest.java Sun Jul 26 14:24:39 2009
@@ -0,0 +1,85 @@
+/*
+ * 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.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.primitives.OID;
+import org.apache.directory.shared.ldap.util.StringTools;
+
+
+/**
+ * Class for representing client's extended operation request.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ExtendedRequest extends AbstractRequest implements RequestWithResponse, AbandonableRequest
+{
+ /** requested extended operation's OID (a.k.a request name) */
+ private OID oid;
+
+ /** requested extended operation's value (a.k.a request value) */
+ private byte[] value;
+
+
+ public ExtendedRequest( String oid )
+ {
+ try
+ {
+ this.oid = new OID( oid );
+ }
+ catch ( DecoderException e )
+ {
+ throw new IllegalArgumentException( e );
+ }
+ }
+
+
+ public ExtendedRequest( OID oid )
+ {
+ this.oid = oid;
+ }
+
+
+ public byte[] getValue()
+ {
+ return value;
+ }
+
+
+ public void setValue( String value )
+ {
+ this.value = StringTools.getBytesUtf8( value );
+ }
+
+
+ public void setValue( byte[] value )
+ {
+ this.value = value;
+ }
+
+
+ public OID getOid()
+ {
+ return oid;
+ }
+}
Added: directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedResponse.java?rev=797939&view=auto
==============================================================================
--- directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedResponse.java (added)
+++ directory/shared/trunk/client-api/src/main/java/org/apache/directory/shared/ldap/client/api/messages/ExtendedResponse.java Sun Jul 26 14:24:39 2009
@@ -0,0 +1,87 @@
+/*
+ * 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.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.primitives.OID;
+
+
+/**
+ * Response object for extended operation
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class ExtendedResponse extends AbstractResponseWithResult
+{
+ /** extended operation response OID */
+ private OID oid;
+
+ /** extended operation response value */
+ private Object value;
+
+ public ExtendedResponse()
+ {
+ }
+
+ public ExtendedResponse( String oid )
+ {
+ super();
+ try
+ {
+ this.oid = new OID( oid );
+ }
+ catch ( DecoderException e )
+ {
+ throw new IllegalArgumentException( e );
+ }
+ }
+
+
+ public ExtendedResponse( OID oid )
+ {
+ super();
+ this.oid = oid;
+ }
+
+
+ public Object getValue()
+ {
+ return value;
+ }
+
+
+ public void setValue( Object value )
+ {
+ this.value = value;
+ }
+
+
+ public void setOid( OID oid )
+ {
+ this.oid = oid;
+ }
+
+ public OID getOid()
+ {
+ return oid;
+ }
+}