directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1468874 - in /directory/shared/trunk/ldap/extras: codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/ codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/ codec/src/test/java/org/apache/d...
Date Wed, 17 Apr 2013 12:43:29 GMT
Author: elecharny
Date: Wed Apr 17 12:43:28 2013
New Revision: 1468874

URL: http://svn.apache.org/r1468874
Log:
Adding the codec and tests for PwdModifyResponse

Added:
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponse.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseConstants.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseContainer.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecoder.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecorator.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseGrammar.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseStatesEnum.java
    directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/PasswordModifyResponseTest.java
Modified:
    directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/PwdModifyResponseImpl.java
    directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyRequestStatesEnum.java

Modified: directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/PwdModifyResponseImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/PwdModifyResponseImpl.java?rev=1468874&r1=1468873&r2=1468874&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/PwdModifyResponseImpl.java
(original)
+++ directory/shared/trunk/ldap/extras/codec-api/src/main/java/org/apache/directory/api/ldap/extras/extended/PwdModifyResponseImpl.java
Wed Apr 17 12:43:28 2013
@@ -23,6 +23,7 @@ package org.apache.directory.api.ldap.ex
 import org.apache.directory.api.i18n.I18n;
 import org.apache.directory.api.ldap.model.message.ExtendedResponseImpl;
 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
+import org.apache.directory.api.util.Strings;
 
 
 /**
@@ -112,4 +113,27 @@ public class PwdModifyResponseImpl exten
     {
         this.genPassword = genPassword;
     }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "PwdModifyResponse :" );
+        sb.append( "\n    genPassword : " );
+
+        if ( genPassword != null )
+        {
+            sb.append( Strings.utf8ToString( genPassword ) );
+        }
+        else
+        {
+            sb.append( "null" );
+        }
+
+        return sb.toString();
+    }
 }

Modified: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyRequestStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyRequestStatesEnum.java?rev=1468874&r1=1468873&r2=1468874&view=diff
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyRequestStatesEnum.java
(original)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyRequestStatesEnum.java
Wed Apr 17 12:43:28 2013
@@ -62,7 +62,7 @@ public enum PasswordModifyRequestStatesE
      */
     public String getGrammarName( Grammar<PasswordModifyRequestContainer> grammar )
     {
-        return "PASSWORD_MODIFY_GRAMMER";
+        return "PASSWORD_MODIFY_REQUEST_GRAMMER";
     }
 
 
@@ -74,7 +74,7 @@ public enum PasswordModifyRequestStatesE
      */
     public String getGrammarName( int grammar )
     {
-        return "PASSWORD_MODIFY_GRAMMER";
+        return "PASSWORD_MODIFY_REQUEST_GRAMMER";
     }
 
 
@@ -86,7 +86,7 @@ public enum PasswordModifyRequestStatesE
      */
     public String getState( int state )
     {
-        return ( ( state == END_STATE.ordinal() ) ? "PASSWORD_MODIFY_GRAMMER" : this.name()
);
+        return ( ( state == END_STATE.ordinal() ) ? "PASSWORD_MODIFY_REQUEST_GRAMMER" : this.name()
);
     }
 
 

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponse.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponse.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponse.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponse.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,117 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.AbstractAsn1Object;
+import org.apache.directory.api.asn1.EncoderException;
+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.ldap.extras.extended.PwdModifyResponse;
+
+
+/**
+ * A PasswordModifyResponse class, as described in RFC 3062
+ * 
+ * <pre>
+ *  PasswdModifyResponseValue ::= SEQUENCE {
+ *    genPasswd       [0]     OCTET STRING OPTIONAL }
+ * </pre>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordModifyResponse extends AbstractAsn1Object
+{
+    /** The encapsulated response */
+    private PwdModifyResponse pwdModifyResponse;
+
+
+    /**
+     * Creates an instance of a PasswordModifyResponse
+     * @param pwdModifyResponse The encapsulated response
+     */
+    public PasswordModifyResponse( PwdModifyResponse pwdModifyResponse )
+    {
+        this.pwdModifyResponse = pwdModifyResponse;
+    }
+
+    /** stores the length of the request*/
+    private int requestLength = 0;
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public int computeLength()
+    {
+        requestLength = 0;
+
+        if ( pwdModifyResponse.getGenPassword() != null )
+        {
+            int len = pwdModifyResponse.getGenPassword().length;
+            requestLength = 1 + BerValue.getNbBytes( len ) + len;
+        }
+
+        return 1 + BerValue.getNbBytes( requestLength ) + requestLength;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public ByteBuffer encode() throws EncoderException
+    {
+        ByteBuffer bb = ByteBuffer.allocate( computeLength() );
+
+        bb.put( UniversalTag.SEQUENCE.getValue() );
+        bb.put( BerValue.getBytes( requestLength ) );
+
+        if ( pwdModifyResponse.getGenPassword() != null )
+        {
+            byte[] userIdentity = pwdModifyResponse.getGenPassword();
+            bb.put( ( byte ) PasswordModifyRequestConstants.USER_IDENTITY_TAG );
+            bb.put( TLV.getBytes( userIdentity.length ) );
+            bb.put( userIdentity );
+        }
+
+        return bb;
+    }
+
+
+    /**
+     * @return The internal PwdModifyResponse object
+     */
+    public PwdModifyResponse getPwdModifyResponse()
+    {
+        return pwdModifyResponse;
+    }
+
+
+    /**
+     * @see Object#toString()
+     */
+    public String toString()
+    {
+        return pwdModifyResponse.toString();
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseConstants.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseConstants.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseConstants.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseConstants.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,40 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+/**
+ * PasswordModifyResponse extended operation constants
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public final class PasswordModifyResponseConstants
+{
+    /** This is the TAG used for the genPassword. It's a contextual primitive Tag */
+    public static final int GEN_PASSORWD_TAG = 0x80;
+
+
+    /**
+     * Private constructor.
+     */
+    private PasswordModifyResponseConstants()
+    {
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseContainer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseContainer.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseContainer.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseContainer.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,79 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+import org.apache.directory.api.asn1.ber.AbstractContainer;
+
+
+/**
+ * A container for PasswordModifyResponse codec.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordModifyResponseContainer extends AbstractContainer
+{
+    /** PasswordModifyResponse decorator*/
+    private PasswordModifyResponseDecorator pwdModifyResponse;
+
+
+    /**
+     * Creates a new PasswordModifyResponseContainer object. We will store one
+     * grammar, it's enough ...
+     */
+    public PasswordModifyResponseContainer()
+    {
+        super();
+        stateStack = new int[1];
+        grammar = PasswordModifyResponseGrammar.getInstance();
+        setTransition( PasswordModifyResponseStatesEnum.START_STATE );
+    }
+
+
+    /**
+     * @return Returns the PwdModifyResponset instance.
+     */
+    public PasswordModifyResponseDecorator getPasswordModifyResponse()
+    {
+        return pwdModifyResponse;
+    }
+
+
+    /**
+     * Set a PasswordModifyResponse Object into the container. It will be completed by
+     * the ldapDecoder.
+     * 
+     * @param pwdModifyResponse the PasswordModifyResponse to set.
+     */
+    public void setPasswordModifyResponse( PasswordModifyResponseDecorator pwdModifyResponse
)
+    {
+        this.pwdModifyResponse = pwdModifyResponse;
+    }
+
+
+    /**
+     * Clean the container for the next decoding.
+     */
+    public void clean()
+    {
+        super.clean();
+        pwdModifyResponse = null;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecoder.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecoder.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecoder.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,62 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.api.asn1.Asn1Object;
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.ber.Asn1Decoder;
+
+
+/**
+ * 
+ * A decoder for PasswordModifyRequest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordModifyResponseDecoder extends Asn1Decoder
+{
+    /** The decoder */
+    private static final Asn1Decoder decoder = new Asn1Decoder();
+
+
+    /**
+     * Decode a PDU which must contain a PwdModifyRequest extended operation.
+     * Note that the stream of bytes much contain a full PDU, not a partial one.
+     * 
+     * @param stream The bytes to be decoded
+     * @return a PwdModifyRequest object
+     * @throws org.apache.directory.api.asn1.DecoderException If the decoding failed
+     */
+    public Asn1Object decode( byte[] stream ) throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.wrap( stream );
+        PasswordModifyRequestContainer container = new PasswordModifyRequestContainer();
+        decoder.decode( bb, container );
+        PasswordModifyRequestDecorator passwordModifyRequestDecorator = container.getPasswordModifyRequest();
+
+        // Clean the container for the next decoding
+        container.clean();
+
+        return passwordModifyRequestDecorator.getPasswordModifyRequest();
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecorator.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecorator.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecorator.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseDecorator.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,127 @@
+/*
+ *   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.extended.ads_impl.pwdModify;
+
+
+import org.apache.directory.api.asn1.DecoderException;
+import org.apache.directory.api.asn1.EncoderException;
+import org.apache.directory.api.i18n.I18n;
+import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.extras.extended.PwdModifyResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * A Decorator for PasswordModifyResponse extended request.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordModifyResponseDecorator extends ExtendedResponseDecorator<PwdModifyResponse>
+    implements PwdModifyResponse
+{
+    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseDecorator.class
);
+
+    private PasswordModifyResponse passwordModifyResponse;
+
+
+    public PasswordModifyResponseDecorator( LdapApiService codec, PwdModifyResponse decoratedMessage
)
+    {
+        super( codec, decoratedMessage );
+        passwordModifyResponse = new PasswordModifyResponse( decoratedMessage );
+    }
+
+
+    public PasswordModifyResponse getPasswordModifyResponse()
+    {
+        return passwordModifyResponse;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setResponseValue( byte[] responseValue )
+    {
+        PasswordModifyResponseDecoder decoder = new PasswordModifyResponseDecoder();
+
+        try
+        {
+            passwordModifyResponse = ( PasswordModifyResponse ) decoder.decode( responseValue
);
+
+            if ( responseValue != null )
+            {
+                this.responseValue = new byte[responseValue.length];
+                System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length
);
+            }
+            else
+            {
+                this.responseValue = null;
+            }
+        }
+        catch ( DecoderException e )
+        {
+            LOG.error( I18n.err( I18n.ERR_04165 ), e );
+            throw new RuntimeException( e );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public byte[] getResponseValue()
+    {
+        if ( responseValue == null )
+        {
+            try
+            {
+                responseValue = passwordModifyResponse.encode().array();
+            }
+            catch ( EncoderException e )
+            {
+                LOG.error( I18n.err( I18n.ERR_04167 ), e );
+                throw new RuntimeException( e );
+            }
+        }
+
+        if ( responseValue == null )
+        {
+            return null;
+        }
+
+        final byte[] copy = new byte[responseValue.length];
+        System.arraycopy( responseValue, 0, copy, 0, responseValue.length );
+
+        return copy;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public byte[] getGenPassword()
+    {
+        return getDecorated().getGenPassword();
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseGrammar.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseGrammar.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseGrammar.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseGrammar.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,149 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+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.UniversalTag;
+import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
+import org.apache.directory.api.ldap.extras.extended.PwdModifyResponseImpl;
+import org.apache.directory.api.util.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class implements the PasswordModifyResponse extended operation's ASN.1 grammer. 
+ * All the actions are declared in this class. As it is a singleton, 
+ * these declaration are only done once. The grammar is :
+ * 
+ * <pre>
+ *  PasswdModifyResponseValue ::= SEQUENCE {
+ *      genPasswd       [0]     OCTET STRING OPTIONAL }
+ * </pre>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+
+public class PasswordModifyResponseGrammar extends AbstractGrammar<PasswordModifyResponseContainer>
+{
+
+    /** logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseGrammar.class
);
+
+    /** Speedup for logs */
+    static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** The instance of grammar. PasswdModifyResponseGrammar is a singleton */
+    private static Grammar<PasswordModifyResponseContainer> instance = new PasswordModifyResponseGrammar();
+
+
+    @SuppressWarnings("unchecked")
+    public PasswordModifyResponseGrammar()
+    {
+        setName( PasswordModifyResponseGrammar.class.getName() );
+
+        // Create the transitions table
+        super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
+            .ordinal()][256];
+
+        /**
+         * Transition from init state to PasswordModify Response Value
+         * 
+         * PasswdModifyResponseValue ::= SEQUENCE {
+         *     ...
+         *     
+         * Creates the PasswdModifyResponse object
+         */
+        super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()]
=
+            new GrammarTransition<PasswordModifyResponseContainer>(
+                PasswordModifyResponseStatesEnum.START_STATE,
+                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
+                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
+                    "Init PasswordModifyResponse" )
+                {
+                    public void action( PasswordModifyResponseContainer container )
+                    {
+                        PasswordModifyResponseDecorator passwordModifyResponse = new PasswordModifyResponseDecorator(
+                            LdapApiServiceFactory.getSingleton(), new PwdModifyResponseImpl()
);
+                        container.setPasswordModifyResponse( passwordModifyResponse );
+
+                        // We may have nothing left
+                        container.setGrammarEndAllowed( true );
+                    }
+                } );
+
+        /**
+         * Transition from PasswordModify Response Value to genPassword
+         *
+         * PasswdModifyResponseValue ::= SEQUENCE {
+         *     genPassword    [0]  OCTET STRING OPTIONAL
+         *     ...
+         *     
+         * Set the userIdentity into the PasswdModifyResponset instance.
+         */
+        super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSORWD_TAG]
=
+            new GrammarTransition<PasswordModifyResponseContainer>(
+                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
+                PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
+                PasswordModifyResponseConstants.GEN_PASSORWD_TAG,
+                new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse
user identity" )
+                {
+                    public void action( PasswordModifyResponseContainer container ) throws
DecoderException
+                    {
+                        BerValue value = container.getCurrentTLV().getValue();
+
+                        byte[] genPassword = value.getData();
+
+                        if ( IS_DEBUG )
+                        {
+                            LOG.debug( "GenPassword = " + Strings.dumpBytes( genPassword
) );
+                        }
+
+                        if ( genPassword == null )
+                        {
+                            genPassword = Strings.EMPTY_BYTES;
+                        }
+
+                        ( ( PwdModifyResponseImpl ) container.getPasswordModifyResponse().getDecorated()
)
+                            .setGenPassword( genPassword );
+
+                        // We may have nothing left
+                        container.setGrammarEndAllowed( true );
+                    }
+                } );
+    }
+
+
+    /**
+     * This class is a singleton.
+     * 
+     * @return An instance on this grammar
+     */
+    public static Grammar<PasswordModifyResponseContainer> getInstance()
+    {
+        return instance;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseStatesEnum.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseStatesEnum.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseStatesEnum.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/main/java/org/apache/directory/api/ldap/extras/extended/ads_impl/pwdModify/PasswordModifyResponseStatesEnum.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,103 @@
+/*
+ *  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.extended.ads_impl.pwdModify;
+
+
+import org.apache.directory.api.asn1.ber.grammar.Grammar;
+import org.apache.directory.api.asn1.ber.grammar.States;
+
+
+/**
+ * This class store the PasswordModifyResponse's grammar constants. It is also used
+ * for debugging purposes.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum PasswordModifyResponseStatesEnum implements States
+{
+
+    /** The END_STATE */
+    END_STATE,
+
+    /** start state*/
+    START_STATE,
+
+    /** sequence*/
+    PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
+
+    /** the generated password */
+    GEN_PASSWORD_STATE,
+
+    /** Last state */
+    LAST_PASSWORD_MODIFY_RESPONSE_STATE;
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar class
+     * @return The grammar name
+     */
+    public String getGrammarName( Grammar<PasswordModifyResponseContainer> grammar
)
+    {
+        return "PASSWORD_MODIFY_RESPONSE_GRAMMER";
+    }
+
+
+    /**
+     * Get the grammar name
+     * 
+     * @param grammar The grammar code
+     * @return The grammar name
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "PASSWORD_MODIFY_RESPONSE_GRAMMER";
+    }
+
+
+    /**
+     * 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() ) ? "PASSWORD_MODIFY_RESPONSE_GRAMMER" :
this.name() );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isEndState()
+    {
+        return this == END_STATE;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public PasswordModifyResponseStatesEnum getStartState()
+    {
+        return START_STATE;
+    }
+}

Added: directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/PasswordModifyResponseTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/PasswordModifyResponseTest.java?rev=1468874&view=auto
==============================================================================
--- directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/PasswordModifyResponseTest.java
(added)
+++ directory/shared/trunk/ldap/extras/codec/src/test/java/org/apache/directory/api/ldap/extras/extended/ads_impl/PasswordModifyResponseTest.java
Wed Apr 17 12:43:28 2013
@@ -0,0 +1,217 @@
+/*
+ *  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.extended.ads_impl;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+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.asn1.ber.Asn1Decoder;
+import org.apache.directory.api.ldap.extras.extended.PwdModifyResponse;
+import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyResponseContainer;
+import org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify.PasswordModifyResponseDecorator;
+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;
+
+
+/**
+ * Test the PasswordModifyReponse codec
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(ConcurrentJunitRunner.class)
+@Concurrency()
+public class PasswordModifyResponseTest
+{
+    /**
+     * Test the decoding of a PasswordModifyResponse with nothing in it
+     */
+    @Test
+    public void testDecodePasswordModifyResponseEmpty()
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x02 );
+        bb.put( new byte[]
+            { 0x30, 0x00, // PasswordModifyResponse ::= SEQUENCE {
+            } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+
+        PwdModifyResponse pwdModifyResponse = container.getPasswordModifyResponse();
+        assertNull( pwdModifyResponse.getGenPassword() );
+
+        // Check the length
+        assertEquals( 0x02, ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+            .computeLength() );
+
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb1 = ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+                .encode();
+
+            String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            fail( ee.getMessage() );
+        }
+    }
+
+
+    /**
+     * Test the decoding of a PasswordModifyResponse with an empty genPassword
+     */
+    @Test
+    public void testDecodePasswordModifyResponseUserIdentityNull()
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x04 );
+        bb.put( new byte[]
+            { 0x30, 0x02, // PasswordModifyResponse ::= SEQUENCE {
+                ( byte ) 0x80,
+                0x00 // genPassword    [0]  OCTET STRING OPTIONAL
+        } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+
+        PwdModifyResponse pwdModifyResponse = container.getPasswordModifyResponse();
+        assertNotNull( pwdModifyResponse.getGenPassword() );
+        assertEquals( 0, pwdModifyResponse.getGenPassword().length );
+
+        // Check the length
+        assertEquals( 0x04, ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+            .computeLength() );
+
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb1 = ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+                .encode();
+
+            String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            fail( ee.getMessage() );
+        }
+    }
+
+
+    /**
+     * Test the decoding of a PasswordModifyResponse with a genPassword
+     */
+    @Test
+    public void testDecodePasswordModifyResponseUserIdentityValue()
+    {
+        Asn1Decoder decoder = new Asn1Decoder();
+        ByteBuffer bb = ByteBuffer.allocate( 0x08 );
+        bb.put( new byte[]
+            { 0x30, 0x06, // PasswordModifyResponse ::= SEQUENCE {
+                ( byte ) 0x80,
+                0x04, // genPassword    [0]  OCTET STRING OPTIONAL
+                'a',
+                'b',
+                'c',
+                'd'
+        } );
+
+        String decodedPdu = Strings.dumpBytes( bb.array() );
+        bb.flip();
+
+        PasswordModifyResponseContainer container = new PasswordModifyResponseContainer();
+
+        try
+        {
+            decoder.decode( bb, container );
+        }
+        catch ( DecoderException de )
+        {
+            de.printStackTrace();
+            fail( de.getMessage() );
+        }
+
+        PwdModifyResponse pwdModifyResponse = container.getPasswordModifyResponse();
+        assertNotNull( pwdModifyResponse.getGenPassword() );
+        assertEquals( "abcd", Strings.utf8ToString( pwdModifyResponse.getGenPassword() )
);
+
+        // Check the length
+        assertEquals( 0x08, ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+            .computeLength() );
+
+        // Check the encoding
+        try
+        {
+            ByteBuffer bb1 = ( ( PasswordModifyResponseDecorator ) pwdModifyResponse ).getPasswordModifyResponse()
+                .encode();
+
+            String encodedPdu = Strings.dumpBytes( bb1.array() );
+
+            assertEquals( encodedPdu, decodedPdu );
+        }
+        catch ( EncoderException ee )
+        {
+            ee.printStackTrace();
+            fail( ee.getMessage() );
+        }
+    }
+}



Mime
View raw message