directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1587971 - in /directory/shared/trunk/ldap/extras: codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/ codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/ codec/src/test/java/org/apache/directory/api/...
Date Wed, 16 Apr 2014 16:47:19 GMT
Author: elecharny
Date: Wed Apr 16 16:47:19 2014
New Revision: 1587971

URL: http://svn.apache.org/r1587971
Log:
Added the implementatio of the AdDirSyncResponse codec

Added:
    directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncFlag.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseContainer.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseDecorator.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseFactory.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseGrammar.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseStatesEnum.java
    directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
Modified:
    directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponse.java
    directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseImpl.java

Added: directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncFlag.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncFlag.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncFlag.java
(added)
+++ directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncFlag.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,71 @@
+/*
+ *  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.api.ldap.extras.controls.ad;
+
+/**
+ * The flags used in the AdDirSync response.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum AdDirSyncFlag
+{
+    DEFAULT (0x0000),
+    LDAP_DIRSYNC_OBJECT_SECURITY (0x0001),
+    LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER (0x0800),
+    LDAP_DIRSYNC_PUBLIC_DATA_ONLY (0x2000),
+    LDAP_DIRSYNC_INCREMENTAL_VALUES (0x7FFFFFFF);
+
+    /** The interned value */
+    private int value;
+    
+    /** A private constructor that associates a value to each flag */
+    private AdDirSyncFlag( int value )
+    {
+        this.value = value;
+    }
+    
+    
+    /**
+     * @return The associated value of a given flag
+     */
+    public int getValue()
+    {
+        return value;
+    }
+    
+    
+    /**
+     * Get back the flag associated with a given value
+     * @param value The integer value
+     * @return The associated flag
+     */
+    public static AdDirSyncFlag getFlag( int value )
+    {
+        switch ( value )
+        {
+            case 0x0000 : return DEFAULT;
+            case 0x0001 : return LDAP_DIRSYNC_OBJECT_SECURITY;
+            case 0x0800 : return LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER;
+            case 0x2000 : return LDAP_DIRSYNC_PUBLIC_DATA_ONLY;
+            case 0x7FFFFFFF : return LDAP_DIRSYNC_INCREMENTAL_VALUES;
+            default : return null;
+        }
+    }
+}

Modified: directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponse.java?rev=1587971&r1=1587970&r2=1587971&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponse.java
(original)
+++ directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponse.java
Wed Apr 16 16:47:19 2014
@@ -22,10 +22,67 @@ package org.apache.directory.api.ldap.ex
 import org.apache.directory.api.ldap.model.message.Control;
 
 /**
+ * The AdDirSyncResponse control has the following value :
  * 
+ * <pre>
+ * Repl    Control ::= SEQUENCE {
+ *     controlType             1.2.840.113556.1.4.841
+ *     controlValue            replControlValue
+ *     criticality             TRUE
+ * }
+ * 
+ * realReplControlValue ::= SEQUENCE {
+ *     flag                    integer
+ *     maxReturnLength       integer
+ *     cookie                  OCTET STRING
+ * }
+ * </pre>
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
 public interface AdDirSyncResponse extends Control
 {
-
+    /** This control OID */
+    static final String OID = "1.2.840.113556.1.4.841";
+    
+    /**
+     * @return The flag returned by the server. One of :
+     * <ul>
+     * <li>LDAP_DIRSYNC_OBJECT_SECURITY (0x0001)</li>
+     * <li>LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER (0x0800)</li>
+     * <li>LDAP_DIRSYNC_PUBLIC_DATA_ONLY (0x2000)(</li>
+     * <li>LDAP_DIRSYNC_INCREMENTAL_VALUES (0x7FFFFFFF)</li>
+     * </ul>
+     */
+    AdDirSyncFlag getFlag();
+    
+    
+    /**
+     * @param flag The flag. 
+     */
+    void setFlag( AdDirSyncFlag flag );
+    
+    
+    /**
+     * @return The maximum length of attributes to be returned
+     */
+    int getMaxReturnLength();
+    
+    
+    /**
+     * @param maxReturnLength The maximum length of attributes to be returned
+     */
+    void setMaxReturnLength( int maxReturnLength );
+    
+    
+    /**
+     * @return The cookie used while processing the successive DirSync operations
+     */
+    byte[] getCookie();
+    
+    
+    /**
+     * @param cookie The cookie to send to the server. It's the value found in the response
control. Should be null
+     * for the first control.
+     */
+    void setCookie( byte[] cookie );
 }

Modified: directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseImpl.java?rev=1587971&r1=1587970&r2=1587971&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseImpl.java
(original)
+++ directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseImpl.java
Wed Apr 16 16:47:19 2014
@@ -19,11 +19,92 @@
  */
 package org.apache.directory.api.ldap.extras.controls.ad;
 
+import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
+import org.apache.directory.api.util.Strings;
+
 /**
+ * The class implementing the AdDirSyncResponse
  * 
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class AdDirSyncResponseImpl
+public class AdDirSyncResponseImpl extends AbstractControl implements AdDirSyncResponse
 {
+    /** A flag used to indicate that there are more data to return */
+    AdDirSyncFlag flag = AdDirSyncFlag.DEFAULT;
+    
+    /** The maximum number of attributes to return */
+    int maxReturnLength = 0;
+    
+    /** The DirSync cookie */
+    private byte[] cookie;
+
+    /**
+     * Default constructor for this control
+     */
+    public AdDirSyncResponseImpl()
+    {
+        super( OID, Boolean.TRUE );
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public AdDirSyncFlag getFlag()
+    {
+        return flag;
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setFlag( AdDirSyncFlag flag )
+    {
+        this.flag = flag;
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public int getMaxReturnLength()
+    {
+        return maxReturnLength;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setMaxReturnLength( int maxReturnLength )
+    {
+        this.maxReturnLength = maxReturnLength;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] getCookie()
+    {
+        return cookie;
+    }
+
 
+    /**
+     * {@inheritDoc}
+     */
+    public void setCookie( byte[] cookie )
+    {
+        if ( cookie != null )
+        {
+            this.cookie = new byte[cookie.length];
+            System.arraycopy( cookie, 0, this.cookie, 0,cookie.length );
+        }
+        else
+        {
+            this.cookie = Strings.EMPTY_BYTES;
+        }
+    }
 }

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseContainer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseContainer.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseContainer.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseContainer.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,107 @@
+/*
+ *  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.api.ldap.extras.controls.ad;
+
+
+import org.apache.directory.api.asn1.ber.AbstractContainer;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+
+
+/**
+ * 
+ * ASN.1 container for AD DirSyncResponse control.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AdDirSyncResponseContainer extends AbstractContainer
+{
+    /** adDirSync */
+    private AdDirSyncResponse control;
+
+    private LdapApiService codec;
+
+
+    /**
+     * 
+     * Creates a new AdDirSyncResponseControl Container object.
+     *
+     */
+    public AdDirSyncResponseContainer( LdapApiService codec )
+    {
+        super();
+        this.codec = codec;
+        this.control = new AdDirSyncResponseDecorator( codec );
+        grammar = AdDirSyncResponseGrammar.getInstance();
+        setTransition( AdDirSyncResponseStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * 
+     * Creates a new AdDirSyncResponseControl object.
+     *
+     */
+    public AdDirSyncResponseContainer( LdapApiService codec, AdDirSyncResponse control )
+    {
+        super();
+        this.codec = codec;
+        this.control = control;
+        grammar = AdDirSyncResponseGrammar.getInstance();
+        setTransition( AdDirSyncResponseStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return the AdDirSyncResponseControlCodec object
+     */
+    public AdDirSyncResponse getAdDirSyncResponseControl()
+    {
+        return control;
+    }
+
+
+    /**
+     * Set a AdDirSyncResponseControlCodec Object into the container. It will be completed
+     * by the ldapDecoder.
+     * 
+     * @param control the AdDirSyncResponseControlCodec to set.
+     */
+    public void setAdDirSyncControl( AdDirSyncResponse control )
+    {
+        this.control = control;
+    }
+
+
+    public LdapApiService getCodecService()
+    {
+        return codec;
+    }
+
+
+    /**
+     * clean the container
+     */
+    @Override
+    public void clean()
+    {
+        super.clean();
+        control = null;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseDecorator.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseDecorator.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseDecorator.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,252 @@
+/*
+ *  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.api.ldap.extras.controls.ad;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.Asn1Object;
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.asn1.ber.Asn1Decoder;
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
+import org.apache.directory.api.asn1.ber.tlv.TLV;
+import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.api.i18n.I18n;
+import org.apache.directory.api.ldap.codec.api.ControlDecorator;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.util.Strings;
+
+/**
+ * A decorator around AdDirSyncResponse control. It will encode and decode this control.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class AdDirSyncResponseDecorator extends ControlDecorator<AdDirSyncResponse>
implements AdDirSyncResponse
+{
+    /** The global length for this control */
+    private int adDirSyncResponseLength;
+
+    /** An instance of this decoder */
+    private static final Asn1Decoder decoder = new Asn1Decoder();
+
+
+    /**
+     * Creates a new instance of AdDirSyncResponseControlCodec.
+     */
+    public AdDirSyncResponseDecorator( LdapApiService codec )
+    {
+        super( codec, new AdDirSyncResponseImpl() );
+    }
+
+
+    /**
+     * Creates a new instance of AdDirSyncResponseDecorator.
+     *
+     * @param codec The LDAP codec
+     * @param control The control to be decorated
+     */
+    public AdDirSyncResponseDecorator( LdapApiService codec, AdDirSyncResponse control )
+    {
+        super( codec, control );
+    }
+
+
+    /**
+     * Compute the AdDirSyncResponse length.
+     * 0x30 L1
+     * |
+     * +--> 0x02 0x0(1-4) nnn  (Flag)
+     * +--> 0x02 0x0(1-4) nnn  (maxReturnLength)
+     * +--> 0x04 L2 xkcd!!!... (cookie)
+     */
+    @Override
+    public int computeLength()
+    {
+        // the parentFirst flag length
+        int flagValue = getFlag().getValue();
+        adDirSyncResponseLength = 1 + TLV.getNbBytes( flagValue ) + BerValue.getNbBytes(
flagValue );
+
+        // the maxReturnLength length
+        adDirSyncResponseLength += 1 + TLV.getNbBytes( getMaxReturnLength() ) + BerValue.getNbBytes(
getMaxReturnLength() );
+
+        // cookie's length
+        byte[] cookie = getCookie();
+        
+        if ( cookie == null )
+        {
+            adDirSyncResponseLength += 1 + 1;
+        }
+        else
+        {
+            adDirSyncResponseLength += 1 + TLV.getNbBytes( cookie.length ) + cookie.length;
+        }
+
+        valueLength = 1 + TLV.getNbBytes( adDirSyncResponseLength ) + adDirSyncResponseLength;
+
+        // Call the super class to compute the global control length
+        return valueLength;
+    }
+
+
+    /**
+     * Encode the AdDirSyncResponse control
+     *
+     * @param buffer The encoded sink
+     * @return A ByteBuffer that contains the encoded PDU
+     * @throws EncoderException If anything goes wrong while encoding.
+     */
+    @Override
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
+        }
+
+        // Encode the SEQ
+        buffer.put( UniversalTag.SEQUENCE.getValue() );
+        buffer.put( TLV.getBytes( adDirSyncResponseLength ) );
+
+        // Encode the ParentFirst flag
+        BerValue.encode( buffer, getFlag().getValue() );
+
+        // Encode the MaxReturnLength
+        BerValue.encode( buffer, getMaxReturnLength() );
+        
+        // Encode the cookie
+        BerValue.encode( buffer, getCookie() );
+
+        return buffer;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public byte[] getValue()
+    {
+        if ( value == null )
+        {
+            try
+            {
+                computeLength();
+                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
+
+                // Encode the SEQ
+                buffer.put( UniversalTag.SEQUENCE.getValue() );
+                buffer.put( TLV.getBytes( adDirSyncResponseLength ) );
+
+                // Encode the ParentFirst flag
+                BerValue.encode( buffer, getFlag().getValue() );
+
+                // Encode the MaxReturnLength
+                BerValue.encode( buffer, getMaxReturnLength() );
+                
+                // Encode the cookie
+                BerValue.encode( buffer, getCookie() );
+
+                value = buffer.array();
+            }
+            catch ( Exception e )
+            {
+                return null;
+            }
+        }
+
+        return value;
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public AdDirSyncFlag getFlag()
+    {
+        return getDecorated().getFlag();
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void setFlag( AdDirSyncFlag flag )
+    {
+        getDecorated().setFlag( flag );
+    }
+    
+    
+    /**
+     * {@inheritDoc}
+     */
+    public int getMaxReturnLength()
+    {
+        return getDecorated().getMaxReturnLength();
+    }
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public void setMaxReturnLength( int maxReturnLength )
+    {
+        getDecorated().setMaxReturnLength( maxReturnLength );
+    }
+    
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] getCookie()
+    {
+        return getDecorated().getCookie();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void setCookie( byte[] cookie )
+    {
+        // Copy the bytes
+        if ( !Strings.isEmpty( cookie ) )
+        {
+            byte[] copy = new byte[cookie.length];
+            System.arraycopy( cookie, 0, copy, 0, cookie.length );
+            getDecorated().setCookie( copy );
+        }
+        else
+        {
+            getDecorated().setCookie( null );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
+        AdDirSyncResponseContainer container = new AdDirSyncResponseContainer( getCodecService(),
this );
+        decoder.decode( bb, container );
+        return this;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseFactory.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseFactory.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseFactory.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,76 @@
+/*
+ *   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.api.ldap.extras.controls.ad;
+
+
+import org.apache.directory.api.ldap.codec.api.CodecControl;
+import org.apache.directory.api.ldap.codec.api.ControlFactory;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+
+
+/**
+ * A {@link ControlFactory} which creates {@link AdDirSync} controls.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class AdDirSyncResponseFactory implements ControlFactory<AdDirSyncResponse>
+{
+    /** The codec for this factory */
+    private LdapApiService codec;
+
+
+    /**
+     * Creates a new instance of AdDirSyncResponseFactory.
+     *
+     * @param codec The codec for this factory.
+     */
+    public AdDirSyncResponseFactory( LdapApiService codec )
+    {
+        this.codec = codec;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getOid()
+    {
+        return AdDirSyncResponse.OID;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public CodecControl<AdDirSyncResponse> newCodecControl()
+    {
+        return new AdDirSyncResponseDecorator( codec );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public CodecControl<AdDirSyncResponse> newCodecControl( AdDirSyncResponse control
)
+    {
+        return new AdDirSyncResponseDecorator( codec, control );
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseGrammar.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseGrammar.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseGrammar.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseGrammar.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,223 @@
+/*
+ *  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.api.ldap.extras.controls.ad;
+
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.api.asn1.ber.grammar.Grammar;
+import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.api.asn1.ber.tlv.BerValue;
+import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
+import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
+import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.api.util.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * 
+ * Implementation of AdDirSyncResponse Control. All the actions are declared in
+ * this class. As it is a singleton, these declaration are only done once.
+ *
+ *  The decoded grammar is as follows :
+ *  
+ *  <pre>
+ * realReplControlValue ::= SEQUENCE {
+ *     flag                  integer
+ *     maxReturnLength       integer
+ *     cookie                OCTET STRING
+ * }
+ * </pre> 
+ *  
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class AdDirSyncResponseGrammar extends AbstractGrammar<AdDirSyncResponseContainer>
+{
+
+    /** the logger */
+    private static final Logger LOG = LoggerFactory.getLogger( AdDirSyncResponseGrammar.class
);
+
+    /** speedup for logger */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** AdDirSyncControlGrammar singleton instance */
+    private static final AdDirSyncResponseGrammar INSTANCE = new AdDirSyncResponseGrammar();
+
+
+    /**
+     * 
+     * Creates a new instance of AdDirSyncResponseControlGrammar.
+     *
+     */
+    @SuppressWarnings("unchecked")
+    private AdDirSyncResponseGrammar()
+    {
+        setName( AdDirSyncResponseGrammar.class.getName() );
+
+        super.transitions = new GrammarTransition[AdDirSyncResponseStatesEnum.LAST_AD_DIR_SYNC_RESPONSE_STATE.ordinal()][256];
+
+        /** 
+         * Transition from initial state to AdDirSyncResponse sequence
+         * AdDirSyncResponse ::= SEQUENCE {
+         *     ...
+         *     
+         * Initialize the adDirSyncResponse object
+         */
+        super.transitions[AdDirSyncResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
= 
+            new GrammarTransition<AdDirSyncResponseContainer>(
+            AdDirSyncResponseStatesEnum.START_STATE, AdDirSyncResponseStatesEnum.AD_DIR_SYNC_RESPONSE_SEQUENCE_STATE,
+            UniversalTag.SEQUENCE.getValue(),
+            new GrammarAction<AdDirSyncResponseContainer>( "Initialization" )
+            {
+                public void action( AdDirSyncResponseContainer container ) throws DecoderException
+                {
+                }
+            } );
+
+        
+        /**
+         * transition from start to flag
+         * realReplControlValue ::= SEQUENCE {
+         *     flag            integer
+         *    ....
+         * }
+         */
+        super.transitions[AdDirSyncResponseStatesEnum.AD_DIR_SYNC_RESPONSE_SEQUENCE_STATE.ordinal()][UniversalTag.INTEGER
+            .getValue()] =
+            new GrammarTransition<AdDirSyncResponseContainer>( AdDirSyncResponseStatesEnum.AD_DIR_SYNC_RESPONSE_SEQUENCE_STATE,
+                AdDirSyncResponseStatesEnum.FLAG_STATE, UniversalTag.INTEGER.getValue(),
+                new GrammarAction<AdDirSyncResponseContainer>( "Set AdDirSyncResponseControl
parentFirst" )
+                {
+                    public void action( AdDirSyncResponseContainer container ) throws DecoderException
+                    {
+                        BerValue value = container.getCurrentTLV().getValue();
+
+                        try
+                        {
+                            int flagValue = IntegerDecoder.parse( value );
+                            
+                            AdDirSyncFlag flag = AdDirSyncFlag.getFlag( flagValue );
+                            
+                            if ( flag == null )
+                            {
+                                String msg = "Error while decoding the AdDirSync flag, unknown
value : " + flagValue;
+                                LOG.error( msg );
+                                throw new DecoderException( msg );
+                            }
+                            
+                            if ( IS_DEBUG )
+                            {
+                                LOG.debug( "flag = {}", flagValue );
+                            }
+                            
+                            container.getAdDirSyncResponseControl().setFlag( flag );
+                        }
+                        catch ( IntegerDecoderException ide )
+                        {
+                            String msg = "Error while decoding the AdDirSyncResponse flag
: " + ide.getMessage();
+                            LOG.error( msg, ide );
+                            throw new DecoderException( msg );
+                        }
+                    }
+                } );
+
+        
+        /**
+         * transition from flag to maxReturnLength
+         * realReplControlValue ::= SEQUENCE {
+         *     flag                    integer
+         *     maxReturnLength         integer
+         *    ....
+         * }
+         */
+        super.transitions[AdDirSyncResponseStatesEnum.FLAG_STATE.ordinal()][UniversalTag.INTEGER
+            .getValue()] =
+            new GrammarTransition<AdDirSyncResponseContainer>( AdDirSyncResponseStatesEnum.FLAG_STATE,
+                AdDirSyncResponseStatesEnum.MAX_RETURN_LENGTH_STATE, UniversalTag.INTEGER.getValue(),
+                new GrammarAction<AdDirSyncResponseContainer>( "Set AdDirSyncResponseControl
maxReturnLength" )
+                {
+                    public void action( AdDirSyncResponseContainer container ) throws DecoderException
+                    {
+                        BerValue value = container.getCurrentTLV().getValue();
+
+                        try
+                        {
+                            int maxReturnLength = IntegerDecoder.parse( value );
+                            
+                            if ( IS_DEBUG )
+                            {
+                                LOG.debug( "maxReturnLength = {}", maxReturnLength );
+                            }
+                            
+                            container.getAdDirSyncResponseControl().setMaxReturnLength( maxReturnLength
);
+                        }
+                        catch ( IntegerDecoderException ide )
+                        {
+                            String msg = "Error while decoding the AdDirSyncResponse maxReturnLength
: " + ide.getMessage();
+                            LOG.error( msg, ide );
+                            throw new DecoderException( msg );
+                        }
+                    }
+                } );
+        
+        
+        /**
+         * transition from maxReturnLength to cookie
+         *     ...
+         *     maxReturnLength         integer
+         *     cookie                  OCTET STRING
+         * }
+         */
+        super.transitions[AdDirSyncResponseStatesEnum.MAX_RETURN_LENGTH_STATE.ordinal()][UniversalTag.OCTET_STRING
+            .getValue()] =
+            new GrammarTransition<AdDirSyncResponseContainer>( AdDirSyncResponseStatesEnum.MAX_RETURN_LENGTH_STATE,
+                AdDirSyncResponseStatesEnum.COOKIE_STATE, UniversalTag.OCTET_STRING.getValue(),
+                new GrammarAction<AdDirSyncResponseContainer>( "Set AdDirSyncResponseControl
cookie" )
+                {
+                    public void action( AdDirSyncResponseContainer container ) throws DecoderException
+                    {
+                        BerValue value = container.getCurrentTLV().getValue();
+
+                        byte[] cookie = value.getData();
+
+                        if ( IS_DEBUG )
+                        {
+                            LOG.debug( "cookie = {}", Strings.dumpBytes( cookie ) );
+                        }
+
+                        container.getAdDirSyncResponseControl().setCookie( cookie );
+
+                        container.setGrammarEndAllowed( true );
+                    }
+                } );
+    }
+
+
+    /**
+     * @return the singleton instance of the AdDirSyncResponseControlGrammar
+     */
+    public static Grammar<AdDirSyncResponseContainer> getInstance()
+    {
+        return INSTANCE;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseStatesEnum.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseStatesEnum.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseStatesEnum.java
Wed Apr 16 16:47:19 2014
@@ -0,0 +1,113 @@
+/*
+ *  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.api.ldap.extras.controls.ad;
+
+
+import org.apache.directory.api.asn1.ber.grammar.Grammar;
+import org.apache.directory.api.asn1.ber.grammar.States;
+
+
+/**
+ * ASN.1 grammar constants of AdDirSyncResponse Control.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum AdDirSyncResponseStatesEnum implements States
+{
+
+    /** The END_STATE */
+    END_STATE,
+
+    /***/
+    START_STATE,
+
+    /** sequence start state */
+    AD_DIR_SYNC_RESPONSE_SEQUENCE_STATE,
+
+    /** flag state */
+    FLAG_STATE,
+
+    /** maxReturnLength value state */
+    MAX_RETURN_LENGTH_STATE,
+
+    /** cookie value state */
+    COOKIE_STATE,
+
+    /** terminal state */
+    LAST_AD_DIR_SYNC_RESPONSE_STATE;
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "AD_DIR_SYNC_RESPONSE_GRAMMAR";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar<AdDirSyncResponseContainer> grammar )
+    {
+        if ( grammar instanceof AdDirSyncResponseGrammar )
+        {
+            return "AD_DIR_SYNC_RESPONSE_GRAMMAR";
+        }
+
+        return "UNKNOWN GRAMMAR";
+    }
+
+
+    /**
+     * Get the string representing the state
+     * 
+     * @param state The state number
+     * @return The String representing the state
+     */
+    public String getState( int state )
+    {
+        return ( ( state == END_STATE.ordinal() ) ? "AD_DIR_SYNC_RESPONSE_GRAMMAR" : this.name()
);
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == END_STATE;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public AdDirSyncResponseStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java?rev=1587971&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/controls/ad/AdDirSyncResponseControlTest.java
Wed Apr 16 16:47:19 2014
@@ -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.api.ldap.extras.controls.ad;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.ldap.extras.AbstractCodecServiceTest;
+import org.apache.directory.api.util.Strings;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import com.mycila.junit.concurrent.Concurrency;
+import com.mycila.junit.concurrent.ConcurrentJunitRunner;
+
+
+/**
+ * 
+ * TestCase for AdDirSyncResponseControlCodec .
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class AdDirSyncResponseControlTest extends AbstractCodecServiceTest
+{
+    @Test
+    public void testAdDirSyncResponseControl() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x0E );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x0C,
+                  0x02, 0x01, 0x01,  // flag (LDAP_DIRSYNC_OBJECT_SECURITY)
+                  0x02, 0x01, 0x00,  // maxReturnLength (no limit)
+                  0x04, 0x04, 'x', 'k', 'c', 'd' // the cookie 
+        } );
+
+        bb.flip();
+
+        AdDirSyncResponse decorator = new AdDirSyncResponseDecorator( codec );
+
+        AdDirSyncResponse adDirSyncResponse = ( AdDirSyncResponse ) ( ( AdDirSyncResponseDecorator
) decorator ).decode( bb.array() );
+
+        assertEquals( AdDirSyncFlag.LDAP_DIRSYNC_OBJECT_SECURITY, adDirSyncResponse.getFlag()
);
+        assertEquals( 0, adDirSyncResponse.getMaxReturnLength() );
+        assertEquals( "xkcd", Strings.utf8ToString( adDirSyncResponse.getCookie() ) );
+
+        // test encoding
+        try
+        {
+            ByteBuffer buffer = ( ( AdDirSyncResponseDecorator ) adDirSyncResponse ).encode(
ByteBuffer
+                .allocate( ( ( AdDirSyncResponseDecorator ) adDirSyncResponse ).computeLength()
) );
+            String expected = Strings.dumpBytes( bb.array() );
+            String decoded = Strings.dumpBytes( buffer.array() );
+            assertEquals( expected, decoded );
+        }
+        catch ( EncoderException e )
+        {
+            fail( e.getMessage() );
+        }
+    }
+
+
+    @Test
+    public void testAdDirSyncResponseControlNoCookie() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x0A );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x08,
+                  0x02, 0x01, 0x01,  // flag (LDAP_DIRSYNC_OBJECT_SECURITY)
+                  0x02, 0x01, 0x00,  // maxReturnLength (no limit)
+                  0x04, 0x00         // the cookie 
+        } );
+
+        bb.flip();
+
+        AdDirSyncResponse decorator = new AdDirSyncResponseDecorator( codec );
+
+        AdDirSyncResponse adDirSyncResponse = ( AdDirSyncResponse ) ( ( AdDirSyncResponseDecorator
) decorator ).decode( bb.array() );
+
+        assertEquals( AdDirSyncFlag.LDAP_DIRSYNC_OBJECT_SECURITY, adDirSyncResponse.getFlag()
);
+        assertEquals( 0, adDirSyncResponse.getMaxReturnLength() );
+        assertEquals( "", Strings.utf8ToString( adDirSyncResponse.getCookie() ) );
+
+        // test encoding
+        try
+        {
+            ByteBuffer buffer = ( ( AdDirSyncResponseDecorator ) adDirSyncResponse ).encode(
ByteBuffer
+                .allocate( ( ( AdDirSyncResponseDecorator ) adDirSyncResponse ).computeLength()
) );
+            String expected = Strings.dumpBytes( bb.array() );
+            String decoded = Strings.dumpBytes( buffer.array() );
+            assertEquals( expected, decoded );
+        }
+        catch ( EncoderException e )
+        {
+            fail( e.getMessage() );
+        }
+    }
+    
+    
+    @Test
+    public void testAdDirSyncResponseControlAbsentCookie() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x08 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x06,
+                  0x02, 0x01, 0x01,  // parentFirst (LDAP_DIRSYNC_ANCESTORS_FIRST_ORDER)
+                  0x02, 0x01, 0x00   // maxReturnLength (no limit)
+        } );
+
+        bb.flip();
+
+        AdDirSyncResponse decorator = new AdDirSyncResponseDecorator( codec );
+
+        try
+        {
+            ( ( AdDirSyncResponseDecorator ) decorator ).decode( bb.array() );
+            fail();
+        }
+        catch ( DecoderException de )
+        {
+            // expected
+        }
+    }
+    
+    
+    @Test
+    public void testAdDirSyncResponseControlAbsentParentFirst() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x07 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x05,
+                  0x02, 0x01, 0x00,  // maxReturnLength (no limit)
+                  0x04, 0x00         // cookie
+        } );
+
+        bb.flip();
+
+        AdDirSyncResponse decorator = new AdDirSyncResponseDecorator( codec );
+
+        try
+        {
+            ( ( AdDirSyncResponseDecorator ) decorator ).decode( bb.array() );
+            fail();
+        }
+        catch ( DecoderException de )
+        {
+            // expected
+        }
+    }
+    
+    
+    @Test
+    public void testAdDirSyncResponseControlEmpty() throws Exception
+    {
+        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
+
+        bb.put( new byte[]
+            {
+                0x30, 0x00,
+        } );
+
+        bb.flip();
+
+        AdDirSyncResponse decorator = new AdDirSyncResponseDecorator( codec );
+
+        try
+        {
+            ( ( AdDirSyncResponseDecorator ) decorator ).decode( bb.array() );
+            fail();
+        }
+        catch ( DecoderException de )
+        {
+            // expected
+        }
+    }
+}



Mime
View raw message