directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r1072852 - in /directory/shared/branches/akarasulu: ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ ldap-extras/codec/src/main/java/org/apache/dir...
Date Mon, 21 Feb 2011 05:26:45 GMT
Author: akarasulu
Date: Mon Feb 21 05:26:44 2011
New Revision: 1072852

URL: http://svn.apache.org/viewvc?rev=1072852&view=rev
Log:
DIRSHARED-86: properly implemented the graceful disconnect plumbing

Added:
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponse.java
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponseImpl.java
      - copied, changed from r1072813, directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnect.java
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectResponseDecorator.java
Removed:
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnect.java
Modified:
    directory/shared/branches/akarasulu/ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ExtendedResponseDecorator.java
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ExtrasBundleActivator.java
    directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectFactory.java

Modified: directory/shared/branches/akarasulu/ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ExtendedResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ExtendedResponseDecorator.java?rev=1072852&r1=1072851&r2=1072852&view=diff
==============================================================================
--- directory/shared/branches/akarasulu/ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ExtendedResponseDecorator.java
(original)
+++ directory/shared/branches/akarasulu/ldap-codec/src/main/java/org/apache/directory/shared/ldap/codec/api/ExtendedResponseDecorator.java
Mon Feb 21 05:26:44 2011
@@ -48,7 +48,7 @@ public class ExtendedResponseDecorator<R
     /** The encoded extendedResponse length */
     private int extendedResponseLength;
 
-    private byte[] responseValue;
+    protected byte[] responseValue;
 
 
     /**

Modified: directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ExtrasBundleActivator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ExtrasBundleActivator.java?rev=1072852&r1=1072851&r2=1072852&view=diff
==============================================================================
--- directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ExtrasBundleActivator.java
(original)
+++ directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/ExtrasBundleActivator.java
Mon Feb 21 05:26:44 2011
@@ -36,7 +36,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.extras.controls.syncrepl_impl.SyncModifyDnFactory;
 import org.apache.directory.shared.ldap.extras.controls.syncrepl_impl.SyncRequestValueFactory;
 import org.apache.directory.shared.ldap.extras.controls.syncrepl_impl.SyncStateValueFactory;
-import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnect;
+import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse;
 import org.apache.directory.shared.ldap.extras.extended.GracefulShutdownRequest;
 import org.apache.directory.shared.ldap.extras.extended.CancelRequest;
 import org.apache.directory.shared.ldap.extras.extended.CertGenerationRequest;
@@ -116,7 +116,7 @@ public class ExtrasBundleActivator imple
         ExtendedRequestFactory<?,?> extReqfactory = new CancelFactory( codec );
         codec.registerExtendedRequest( extReqfactory );
         
-        extReqfactory = new CertGenerationFactory();
+        extReqfactory = new CertGenerationFactory( codec );
         codec.registerExtendedRequest( extReqfactory );
 
         extReqfactory = new GracefulShutdownFactory();
@@ -155,6 +155,6 @@ public class ExtrasBundleActivator imple
         codec.unregisterExtendedRequest( GracefulShutdownRequest.EXTENSION_OID );
         codec.unregisterExtendedRequest( StoredProcedureRequest.EXTENSION_OID );
         
-        codec.unregisterUnsolicitedResponse( GracefulDisconnect.EXTENSION_OID );
+        codec.unregisterUnsolicitedResponse( GracefulDisconnectResponse.EXTENSION_OID );
     }
 }

Added: directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponse.java?rev=1072852&view=auto
==============================================================================
--- directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponse.java
(added)
+++ directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponse.java
Mon Feb 21 05:26:44 2011
@@ -0,0 +1,80 @@
+/*
+ *  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.extras.extended;
+
+
+import org.apache.directory.shared.ldap.model.message.ExtendedResponse;
+import org.apache.directory.shared.ldap.model.message.Referral;
+
+
+/**
+ * An unsolicited notification, extended response, intended for notifying
+ * clients of upcoming disconnection due to intended service windows. Unlike the
+ * {@link org.apache.directory.shared.ldap.model.message.extended.NoticeOfDisconnect} this
response contains additional information about
+ * the amount of time the server will be offline and exactly when it intends to
+ * shutdown.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public interface GracefulDisconnectResponse extends ExtendedResponse
+{
+    /** The OID for the graceful disconnect extended operation response. */
+    public static final String EXTENSION_OID = "1.3.6.1.4.1.18060.0.1.5";
+
+
+    /**
+     * Gets the delay before disconnection, in seconds.
+     *
+     * @return the delay before disconnection
+     */
+    int getDelay();
+
+
+    /**
+     * Sets the delay befor disconnection, in seconds.
+     *
+     * @param delay the new delay before disconnection
+     */
+    void setDelay( int delay );
+
+
+    /**
+     * Gets the offline time after disconnection, in minutes.
+     *
+     * @return the offline time after disconnection
+     */
+    int getTimeOffline();
+
+
+    /**
+     * Sets the time offline after disconnection, in minutes.
+     *
+     * @param timeOffline the new time offline after disconnection
+     */
+    void setTimeOffline( int timeOffline );
+
+
+    /**
+     * Gets the replicated contexts.
+     *
+     * @return the replicated contexts
+     */
+    Referral getReplicatedContexts();
+}
\ No newline at end of file

Copied: directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponseImpl.java
(from r1072813, directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnect.java)
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponseImpl.java?p2=directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponseImpl.java&p1=directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnect.java&r1=1072813&r2=1072852&rev=1072852&view=diff
==============================================================================
--- directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnect.java
(original)
+++ directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/GracefulDisconnectResponseImpl.java
Mon Feb 21 05:26:44 2011
@@ -20,45 +20,27 @@
 package org.apache.directory.shared.ldap.extras.extended;
 
 
-import java.nio.ByteBuffer;
-import java.util.List;
-
-import org.apache.directory.shared.asn1.DecoderException;
-import org.apache.directory.shared.asn1.EncoderException;
-import org.apache.directory.shared.asn1.ber.Asn1Decoder;
 import org.apache.directory.shared.i18n.I18n;
-import org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnectContainer;
-import org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnectDecoder;
-import org.apache.directory.shared.ldap.model.exception.LdapURLEncodingException;
 import org.apache.directory.shared.ldap.model.message.ExtendedResponseImpl;
 import org.apache.directory.shared.ldap.model.message.Referral;
 import org.apache.directory.shared.ldap.model.message.ReferralImpl;
 import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
-import org.apache.directory.shared.ldap.model.filter.LdapURL;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 
 /**
  * An unsolicited notification, extended response, intended for notifying
- * clients of upcoming disconnection due to intended service windows. Unlike the
+ * clients of up coming disconnection due to intended service windows. Unlike the
  * {@link org.apache.directory.shared.ldap.model.message.extended.NoticeOfDisconnect} this
response contains additional information about
  * the amount of time the server will be offline and exactly when it intends to
  * shutdown.
  * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class GracefulDisconnect extends ExtendedResponseImpl
+public class GracefulDisconnectResponseImpl extends ExtendedResponseImpl implements GracefulDisconnectResponse
 {
     /** The serialVersionUID. */
     private static final long serialVersionUID = -4682291068700593492L;
 
-    /** The OID for the graceful disconnect extended operation response. */
-    public static final String EXTENSION_OID = "1.3.6.1.4.1.18060.0.1.5";
-
-    /** The logger. */
-    private static final Logger LOG = LoggerFactory.getLogger( GracefulDisconnect.class );
-
     /** Offline time after disconnection */
     private int timeOffline;
 
@@ -71,35 +53,10 @@ public class GracefulDisconnect extends 
 
     /**
      * Instantiates a new graceful disconnect.
-     *
-     * @param responseValue the response value
-     * @throws DecoderException if the response value can't be decoded
      */
-    public GracefulDisconnect( byte[] responseValue ) throws DecoderException
+    public GracefulDisconnectResponseImpl()
     {
         super( 0, EXTENSION_OID );
-
-        if ( responseValue != null )
-        {
-            this.responseValue = new byte[responseValue.length];
-            System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length
);
-        }
-        else
-        {
-            this.responseValue = null;
-        }
-
-        decodeValue();
-    }
-
-
-    /**
-     * Instantiates a new graceful disconnect.
-     */
-    public GracefulDisconnect()
-    {
-        super( 0, EXTENSION_OID );
-        this.responseValue = null;
     }
 
 
@@ -109,7 +66,7 @@ public class GracefulDisconnect extends 
      * @param timeOffline the offline time after disconnect, in minutes
      * @param delay the delay before disconnect, in seconds
      */
-    public GracefulDisconnect( int timeOffline, int delay )
+    public GracefulDisconnectResponseImpl( int timeOffline, int delay )
     {
         super( 0, EXTENSION_OID );
         responseName = EXTENSION_OID;
@@ -123,136 +80,6 @@ public class GracefulDisconnect extends 
         ldapResult.setErrorMessage( buf.toString() );
         ldapResult.setMatchedDn( null );
         ldapResult.setResultCode( ResultCodeEnum.UNAVAILABLE );
-
-        encodeResponse();
-    }
-
-
-    private void decodeValue() throws DecoderException
-    {
-        GracefulDisconnectDecoder decoder = new GracefulDisconnectDecoder();
-        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= null;
-
-        try
-        {
-            codec = (org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect)
decoder
-                .decode( responseValue );
-            this.timeOffline = codec.getTimeOffline();
-            this.delay = codec.getDelay();
-            ldapResult.setResultCode( ResultCodeEnum.SUCCESS );
-            List<LdapURL> contexts = codec.getReplicatedContexts();
-
-            for ( LdapURL ldapUrl : contexts )
-            {
-                replicatedContexts.addLdapUrl( ldapUrl.toString() );
-            }
-        }
-        catch ( DecoderException e )
-        {
-            LOG.error( I18n.err( I18n.ERR_04169 ), e );
-            throw e;
-        }
-    }
-
-
-    private void encodeResponse()
-    {
-        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= new org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect();
-        codec.setTimeOffline( this.timeOffline );
-        codec.setDelay( this.delay );
-
-        for ( String ldapUrlStr : replicatedContexts.getLdapUrls() )
-        {
-            LdapURL ldapUrl = null;
-
-            try
-            {
-                ldapUrl = new LdapURL( ldapUrlStr );
-            }
-            catch ( LdapURLEncodingException e )
-            {
-                LOG.error( I18n.err( I18n.ERR_04170, ldapUrlStr ), e );
-                continue;
-            }
-
-            codec.addReplicatedContexts( ldapUrl );
-        }
-
-        try
-        {
-            super.responseValue = codec.encode().array();
-        }
-        catch ( EncoderException e )
-        {
-            LOG.error( I18n.err( I18n.ERR_04171 ), e );
-            throw new RuntimeException( e );
-        }
-    }
-
-
-    // ------------------------------------------------------------------------
-    // ExtendedResponse Interface Method Implementations
-    // ------------------------------------------------------------------------
-
-    /**
-     * Gets the response OID specific encoded response values.
-     * 
-     * @return the response specific encoded response values.
-     */
-    public byte[] getResponseValue()
-    {
-        if ( responseValue == null )
-        {
-            encodeResponse();
-        }
-
-        final byte[] copy = new byte[responseValue.length];
-        System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
-        return copy;
-    }
-
-
-    /**
-     * Sets the response OID specific encoded response values.
-     * 
-     * @param responseValue the response specific encoded response values.
-     */
-    public void setResponseValue( byte[] responseValue )
-    {
-        if ( responseValue == null )
-        {
-            this.responseValue = null;
-            this.delay = 0;
-            this.timeOffline = 0;
-            this.replicatedContexts = new ReferralImpl();
-            return;
-        }
-        
-        ByteBuffer bb = ByteBuffer.wrap( responseValue );
-        GracefulDisconnectContainer container = new GracefulDisconnectContainer();
-        Asn1Decoder decoder = new Asn1Decoder();
-
-        try
-        {
-            decoder.decode( bb, container );
-        }
-        catch ( DecoderException e )
-        {
-            LOG.error( I18n.err( I18n.ERR_04172 ), e );
-        }
-
-        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= container
-            .getGracefulDisconnect();
-        this.delay = codec.getDelay();
-        this.timeOffline = codec.getTimeOffline();
-
-        for ( LdapURL ldapUrl : codec.getReplicatedContexts() )
-        {
-            replicatedContexts.addLdapUrl( ldapUrl.toString() );
-        }
-
-        this.responseValue = new byte[responseValue.length];
-        System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
     }
 
 
@@ -284,10 +111,9 @@ public class GracefulDisconnect extends 
     // Parameters of the Extended Response Value
     // -----------------------------------------------------------------------
 
-    /**
-     * Gets the delay before disconnection, in seconds.
-     *
-     * @return the delay before disconnection
+    
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse#getDelay()
      */
     public int getDelay()
     {
@@ -295,10 +121,8 @@ public class GracefulDisconnect extends 
     }
 
 
-    /**
-     * Sets the delay befor disconnection, in seconds.
-     *
-     * @param delay the new delay before disconnection
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse#setDelay(int)
      */
     public void setDelay( int delay )
     {
@@ -306,10 +130,8 @@ public class GracefulDisconnect extends 
     }
 
 
-    /**
-     * Gets the offline time after disconnection, in minutes.
-     *
-     * @return the offline time after disconnection
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse#getTimeOffline()
      */
     public int getTimeOffline()
     {
@@ -317,10 +139,8 @@ public class GracefulDisconnect extends 
     }
 
 
-    /**
-     * Sets the time offline after disconnection, in minutes.
-     *
-     * @param timeOffline the new time offline after disconnection
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse#setTimeOffline(int)
      */
     public void setTimeOffline( int timeOffline )
     {
@@ -328,10 +148,8 @@ public class GracefulDisconnect extends 
     }
 
 
-    /**
-     * Gets the replicated contexts.
-     *
-     * @return the replicated contexts
+    /* (non-Javadoc)
+     * @see org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse#getReplicatedContexts()
      */
     public Referral getReplicatedContexts()
     {

Modified: directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectFactory.java?rev=1072852&r1=1072851&r2=1072852&view=diff
==============================================================================
--- directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectFactory.java
(original)
+++ directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectFactory.java
Mon Feb 21 05:26:44 2011
@@ -22,8 +22,10 @@ package org.apache.directory.shared.ldap
 
 import org.apache.directory.shared.asn1.DecoderException;
 import org.apache.directory.shared.ldap.codec.api.ExtendedRequestFactory;
+import org.apache.directory.shared.ldap.codec.api.LdapCodecService;
 import org.apache.directory.shared.ldap.codec.api.UnsolicitedResponseFactory;
-import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnect;
+import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponseImpl;
+import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse;
 
 
 /**
@@ -32,28 +34,31 @@ import org.apache.directory.shared.ldap.
  *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class GracefulDisconnectFactory implements UnsolicitedResponseFactory<GracefulDisconnect>
+public class GracefulDisconnectFactory implements UnsolicitedResponseFactory<GracefulDisconnectResponse>
 {
+    private LdapCodecService codec;
+    
+    
     /**
      * {@inheritDoc}
      */
     public String getOid()
     {
-        return GracefulDisconnect.EXTENSION_OID;
+        return GracefulDisconnectResponse.EXTENSION_OID;
     }
 
     
     /**
      * {@inheritDoc}
      */
-    public GracefulDisconnect newRequest()
+    public GracefulDisconnectResponse newRequest()
     {
-        return new GracefulDisconnect();
+        return new GracefulDisconnectResponseDecorator( codec, new GracefulDisconnectResponseImpl()
);
     }
 
 
-    public GracefulDisconnect newResponse( byte[] encodedValue ) throws DecoderException
+    public GracefulDisconnectResponse newResponse( byte[] encodedValue ) throws DecoderException
     {
-        return new GracefulDisconnect( encodedValue );
+        return new GracefulDisconnectResponseDecorator( codec, encodedValue );
     }
 }

Added: directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectResponseDecorator.java?rev=1072852&view=auto
==============================================================================
--- directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectResponseDecorator.java
(added)
+++ directory/shared/branches/akarasulu/ldap-extras/codec/src/main/java/org/apache/directory/shared/ldap/extras/extended/ads_impl/GracefulDisconnectResponseDecorator.java
Mon Feb 21 05:26:44 2011
@@ -0,0 +1,256 @@
+/*
+ *   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.extras.extended.ads_impl;
+
+
+import java.nio.ByteBuffer;
+import java.util.List;
+
+import org.apache.directory.shared.asn1.DecoderException;
+import org.apache.directory.shared.asn1.EncoderException;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.api.ExtendedResponseDecorator;
+import org.apache.directory.shared.ldap.codec.api.LdapCodecService;
+import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponse;
+import org.apache.directory.shared.ldap.extras.extended.GracefulDisconnectResponseImpl;
+import org.apache.directory.shared.ldap.model.exception.LdapURLEncodingException;
+import org.apache.directory.shared.ldap.model.filter.LdapURL;
+import org.apache.directory.shared.ldap.model.message.Referral;
+import org.apache.directory.shared.ldap.model.message.ReferralImpl;
+import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A Decorator for CancelResponses.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class GracefulDisconnectResponseDecorator extends ExtendedResponseDecorator<GracefulDisconnectResponse>
implements GracefulDisconnectResponse
+{
+    /** The logger. */
+    private static final Logger LOG = LoggerFactory.getLogger( GracefulDisconnectResponseDecorator.class
);
+
+    
+    /**
+     * Creates a new instance of CancelResponseDecorator.
+     *
+     * @param codec
+     * @param decoratedMessage
+     */
+    public GracefulDisconnectResponseDecorator( LdapCodecService codec, GracefulDisconnectResponse
decoratedMessage )
+    {
+        super( codec, decoratedMessage );
+        responseValue = null;
+        encodeResponse();
+    }
+    
+    
+    /**
+     * Creates a new instance of CancelResponseDecorator.
+     *
+     * @param codec
+     * @param responseValue
+     */
+    public GracefulDisconnectResponseDecorator( LdapCodecService codec, byte[] responseValue
) throws DecoderException
+    {
+        super( codec, new GracefulDisconnectResponseImpl() );
+        this.responseValue = responseValue;
+        decodeValue();
+    }
+    
+    
+    private void decodeValue() throws DecoderException
+    {
+        GracefulDisconnectDecoder decoder = new GracefulDisconnectDecoder();
+        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= null;
+
+        try
+        {
+            codec = (org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect)
decoder
+                .decode( responseValue );
+            getDecorated().setTimeOffline( codec.getTimeOffline() );
+            getDecorated().setDelay( codec.getDelay() );
+            getDecorated().getLdapResult().setResultCode( ResultCodeEnum.SUCCESS );
+            List<LdapURL> contexts = codec.getReplicatedContexts();
+
+            for ( LdapURL ldapUrl : contexts )
+            {
+                getDecorated().getLdapResult().getReferral().addLdapUrl( ldapUrl.toString()
);
+            }
+        }
+        catch ( DecoderException e )
+        {
+            LOG.error( I18n.err( I18n.ERR_04169 ), e );
+            throw e;
+        }
+    }
+
+
+    private void encodeResponse()
+    {
+        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= 
+            new org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect();
+        codec.setTimeOffline( getDecorated().getTimeOffline() );
+        codec.setDelay( getDecorated().getDelay() );
+
+        for ( String ldapUrlStr : getDecorated().getLdapResult().getReferral().getLdapUrls()
)
+        {
+            LdapURL ldapUrl = null;
+
+            try
+            {
+                ldapUrl = new LdapURL( ldapUrlStr );
+            }
+            catch ( LdapURLEncodingException e )
+            {
+                LOG.error( I18n.err( I18n.ERR_04170, ldapUrlStr ), e );
+                continue;
+            }
+
+            codec.addReplicatedContexts( ldapUrl );
+        }
+
+        try
+        {
+            super.responseValue = codec.encode().array();
+        }
+        catch ( EncoderException e )
+        {
+            LOG.error( I18n.err( I18n.ERR_04171 ), e );
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    // ------------------------------------------------------------------------
+    // ExtendedResponse Interface Method Implementations
+    // ------------------------------------------------------------------------
+
+    
+    /**
+     * Gets the response OID specific encoded response values.
+     * 
+     * @return the response specific encoded response values.
+     */
+    public byte[] getResponseValue()
+    {
+        if ( responseValue == null )
+        {
+            encodeResponse();
+        }
+
+        final byte[] copy = new byte[responseValue.length];
+        System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
+        return copy;
+    }
+
+
+    /**
+     * Sets the response OID specific encoded response values.
+     * 
+     * @param responseValue the response specific encoded response values.
+     */
+    public void setResponseValue( byte[] responseValue )
+    {
+        if ( responseValue == null )
+        {
+            this.responseValue = null;
+            getDecorated().setDelay( 0 );
+            getDecorated().setTimeOffline( 0 );
+            getDecorated().getLdapResult().setReferral( new ReferralImpl() );
+            return;
+        }
+        
+        ByteBuffer bb = ByteBuffer.wrap( responseValue );
+        GracefulDisconnectContainer container = new GracefulDisconnectContainer();
+        Asn1Decoder decoder = new Asn1Decoder();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException e )
+        {
+            LOG.error( I18n.err( I18n.ERR_04172 ), e );
+        }
+
+        org.apache.directory.shared.ldap.extras.extended.ads_impl.GracefulDisconnect codec
= container
+            .getGracefulDisconnect();
+        
+        getDecorated().setDelay( codec.getDelay() );
+        getDecorated().setTimeOffline( codec.getTimeOffline() );
+
+        for ( LdapURL ldapUrl : codec.getReplicatedContexts() )
+        {
+            getDecorated().getLdapResult().getReferral().addLdapUrl( ldapUrl.toString() );
+        }
+
+        this.responseValue = new byte[responseValue.length];
+        System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length );
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public int getDelay()
+    {
+        return getDecorated().getDelay();
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void setDelay( int delay )
+    {
+        getDecorated().setDelay( delay );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public int getTimeOffline()
+    {
+        return getDecorated().getTimeOffline();
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setTimeOffline( int timeOffline )
+    {
+        getDecorated().setTimeOffline( timeOffline );
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public Referral getReplicatedContexts()
+    {
+        return getDecorated().getReplicatedContexts();
+    }
+}



Mime
View raw message