You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/06/14 15:19:19 UTC

svn commit: r954440 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/ test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/

Author: kayyagari
Date: Mon Jun 14 13:19:19 2010
New Revision: 954440

URL: http://svn.apache.org/viewvc?rev=954440&view=rev
Log:
o request and response controls of password policy
o associated test case (@Ignored at the moment)

Added:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyErrorEnum.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControl.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControlDecoder.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControl.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlContainer.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlDecoder.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlGrammar.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlStates.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTags.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTest.java

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyErrorEnum.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyErrorEnum.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyErrorEnum.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyErrorEnum.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,86 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+/**
+ *  constants representing PasswordPolicyErrorS as stated in the <a href="http://tools.ietf.org/html/draft-behera-ldap-password-policy-10">draft</a>
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum PasswordPolicyErrorEnum
+{
+    PASSWORD_EXPIRED(0), ACCOUNT_LOCKED(1), CHANGE_AFTER_RESET(2), PASSWORD_MOD_NOT_ALLOWED(3), MUST_SUPPLY_OLD_PASSWORD(
+        4), INSUFFICIENT_PASSWORD_QUALITY(5), PASSWORD_TOO_SHORT(6), PASSWORD_TOO_YOUNG(7), PASSWORD_IN_HISTORY(8);
+
+    private int value;
+
+
+    private PasswordPolicyErrorEnum( int value )
+    {
+        this.value = value;
+    }
+
+
+    public static PasswordPolicyErrorEnum get( int val )
+    {
+        switch ( val )
+        {
+            case 0:
+                return PASSWORD_EXPIRED;
+
+            case 1:
+                return ACCOUNT_LOCKED;
+
+            case 2:
+                return CHANGE_AFTER_RESET;
+
+            case 3:
+                return PASSWORD_MOD_NOT_ALLOWED;
+
+            case 4:
+                return MUST_SUPPLY_OLD_PASSWORD;
+
+            case 5:
+                return INSUFFICIENT_PASSWORD_QUALITY;
+
+            case 6:
+                return PASSWORD_TOO_SHORT;
+
+            case 7:
+                return PASSWORD_TOO_YOUNG;
+
+            case 8:
+                return PASSWORD_IN_HISTORY;
+
+            default:
+
+                throw new IllegalArgumentException( "unknown password policy error value " + val );
+        }
+    }
+
+
+    public int getValue()
+    {
+        return value;
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControl.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControl.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControl.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,49 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+import org.apache.directory.shared.ldap.codec.controls.AbstractControl;
+
+
+/**
+ * PasswordPolicyControl as stated in the <a href="http://tools.ietf.org/html/draft-behera-ldap-password-policy-10">draft</a>.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyRequestControl extends AbstractControl
+{
+    /** the password policy request control */
+    public static final String CONTROL_OID = "1.3.6.1.4.1.42.2.27.8.5.1";
+
+
+    public PasswordPolicyRequestControl()
+    {
+        super( CONTROL_OID );
+    }
+
+
+    @Override
+    public int computeLength()
+    {
+        return super.computeLength( 0 );
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControlDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControlDecoder.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControlDecoder.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyRequestControlDecoder.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,41 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+import org.apache.directory.shared.asn1.Asn1Object;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.ldap.codec.controls.ControlDecoder;
+import org.apache.directory.shared.ldap.message.control.Control;
+
+/**
+ * A decoder for PasswordPolicyRequestControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyRequestControlDecoder  implements ControlDecoder
+{
+
+    public Asn1Object decode( byte[] controlBytes, Control control ) throws DecoderException
+    {
+        return new PasswordPolicyRequestControl();
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControl.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControl.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControl.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,176 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.ber.tlv.TLV;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.codec.EncoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.apache.directory.shared.ldap.codec.controls.AbstractControl;
+
+
+/**
+ * PasswordPolicyResponseControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControl extends AbstractControl
+{
+
+    /** time before expiration of the password */
+    private int timeBeforeExpiration = -1;
+
+    /** number of remaining grace authentications */
+    private int graceAuthNsRemaining = -1;
+
+    /** number representing the password policy error */
+    private PasswordPolicyErrorEnum ppolicyError;
+
+    private int controlLen = 0;
+
+
+    public PasswordPolicyResponseControl()
+    {
+        super( PasswordPolicyRequestControl.CONTROL_OID );
+    }
+
+
+    @Override
+    public int computeLength()
+    {
+        if ( timeBeforeExpiration >= 0 )
+        {
+            controlLen = 1 + ( 2 * TLV.getNbBytes( timeBeforeExpiration ) );
+        }
+        else if ( graceAuthNsRemaining >= 0 )
+        {
+            controlLen = 1 + ( 2 * TLV.getNbBytes( graceAuthNsRemaining ) );
+        }
+
+        if ( ppolicyError != null )
+        {
+            controlLen += 1 + ( 2 * TLV.getNbBytes( ppolicyError.getValue() ) );
+        }
+
+        return super.computeLength( controlLen );
+    }
+
+
+    @Override
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
+        }
+
+        // Encode the Control envelop
+        super.encode( buffer );
+
+        // Encode the OCTET_STRING tag
+        buffer.put( UniversalTag.OCTET_STRING_TAG );
+        buffer.put( TLV.getBytes( valueLength ) );
+
+        if ( timeBeforeExpiration >= 0 )
+        {
+            buffer.put( ( byte ) PasswordPolicyResponseControlTags.TIME_BEFORE_EXPIRATION_TAG.getValue() );
+            buffer.put( TLV.getBytes( timeBeforeExpiration ) );
+        }
+        else if ( graceAuthNsRemaining >= 0 )
+        {
+            buffer.put( ( byte ) PasswordPolicyResponseControlTags.GRACE_AUTHNS_REMAINING_TAG.getValue() );
+            buffer.put( TLV.getBytes( graceAuthNsRemaining ) );
+        }
+
+        if ( ppolicyError != null )
+        {
+            buffer.put( UniversalTag.ENUMERATED_TAG );
+            buffer.put( TLV.getBytes( ppolicyError.getValue() ) );
+        }
+
+        return buffer;
+    }
+
+
+    public int getTimeBeforeExpiration()
+    {
+        return timeBeforeExpiration;
+    }
+
+
+    public void setTimeBeforeExpiration( int timeBeforeExpiration )
+    {
+        this.timeBeforeExpiration = timeBeforeExpiration;
+    }
+
+
+    public int getGraceAuthNsRemaining()
+    {
+        return graceAuthNsRemaining;
+    }
+
+
+    public void setGraceAuthNsRemaining( int graceAuthNsRemaining )
+    {
+        this.graceAuthNsRemaining = graceAuthNsRemaining;
+    }
+
+
+    public PasswordPolicyErrorEnum getPasswordPolicyError()
+    {
+        return ppolicyError;
+    }
+
+
+    public void setPasswordPolicyError( PasswordPolicyErrorEnum ppolicyError )
+    {
+        this.ppolicyError = ppolicyError;
+    }
+
+
+    @Override
+    public String toString()
+    {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append( "  PasswordPolicyResponse control :\n" );
+        sb.append( "   oid          : '" ).append( getOid() ).append( '\n' );
+        if ( timeBeforeExpiration >= 0 )
+        {
+            sb.append( "   timeBeforeExpiration          : '" ).append( timeBeforeExpiration ).append( '\n' );
+        }
+        else if ( graceAuthNsRemaining >= 0 )
+        {
+            sb.append( "   graceAuthNsRemaining          : '" ).append( graceAuthNsRemaining ).append( '\n' );
+        }
+
+        if ( ppolicyError != null )
+        {
+            sb.append( "   ppolicyError          : '" ).append( ppolicyError.toString() ).append( '\n' );
+        }
+
+        return sb.toString();
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlContainer.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlContainer.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlContainer.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlContainer.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,68 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import org.apache.directory.shared.asn1.ber.AbstractContainer;
+
+
+/**
+ * container for PasswordPolicyResponseControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControlContainer extends AbstractContainer
+{
+    private PasswordPolicyResponseControl control;
+
+
+    public PasswordPolicyResponseControlContainer()
+    {
+        super();
+        stateStack = new int[1];
+        grammar = PasswordPolicyResponseControlGrammar.getInstance();
+        states = PasswordPolicyResponseControlStates.getInstance();
+    }
+
+
+    public PasswordPolicyResponseControl getPasswordPolicyResponseControl()
+    {
+        return control;
+    }
+
+
+    public void setPasswordPolicyResponseControl( PasswordPolicyResponseControl control )
+    {
+        this.control = control;
+    }
+
+
+    /**
+     * clean the container
+     */
+    @Override
+    public void clean()
+    {
+        super.clean();
+        control = null;
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlDecoder.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlDecoder.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlDecoder.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,54 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.Asn1Object;
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.ldap.codec.controls.ControlDecoder;
+import org.apache.directory.shared.ldap.message.control.Control;
+
+
+/**
+ * Decoder for PasswordPolicyResponseControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControlDecoder extends Asn1Decoder implements ControlDecoder
+{
+    /** An instance of this decoder */
+    private static final Asn1Decoder decoder = new Asn1Decoder();
+
+
+    public Asn1Object decode( byte[] controlBytes, Control control ) throws DecoderException
+    {
+        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
+        PasswordPolicyResponseControlContainer container = new PasswordPolicyResponseControlContainer();
+        container.setPasswordPolicyResponseControl( ( PasswordPolicyResponseControl )control );
+
+        decoder.decode( bb, container );
+        return container.getPasswordPolicyResponseControl();
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlGrammar.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlGrammar.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlGrammar.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlGrammar.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,208 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import org.apache.directory.shared.asn1.ber.IAsn1Container;
+import org.apache.directory.shared.asn1.ber.grammar.AbstractGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarAction;
+import org.apache.directory.shared.asn1.ber.grammar.GrammarTransition;
+import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.IStates;
+import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+import org.apache.directory.shared.asn1.codec.DecoderException;
+import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.apache.directory.shared.asn1.util.IntegerDecoderException;
+import org.apache.directory.shared.i18n.I18n;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Grammar for decoding PasswordPolicyResponseControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControlGrammar extends AbstractGrammar
+{
+    /** the logger */
+    private static final Logger LOG = LoggerFactory.getLogger( PasswordPolicyResponseControlGrammar.class );
+
+    /** speedup for logger */
+    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
+
+    /** PasswordPolicyResponseControlGrammar singleton instance */
+    private static final PasswordPolicyResponseControlGrammar INSTANCE = new PasswordPolicyResponseControlGrammar();
+
+
+    private PasswordPolicyResponseControlGrammar()
+    {
+        name = PasswordPolicyResponseControlGrammar.class.getName();
+        statesEnum = PasswordPolicyResponseControlStates.getInstance();
+
+        super.transitions = new GrammarTransition[PasswordPolicyResponseControlStates.END_STATE][256];
+
+        super.transitions[IStates.INIT_GRAMMAR_STATE][UniversalTag.SEQUENCE_TAG] = new GrammarTransition(
+            IStates.INIT_GRAMMAR_STATE, PasswordPolicyResponseControlStates.START_STATE, UniversalTag.SEQUENCE_TAG,
+            new GrammarAction( "Initialization" )
+            {
+                public void action( IAsn1Container container ) throws DecoderException
+                {
+                    PasswordPolicyResponseControlContainer ppolicyRespContainer = ( PasswordPolicyResponseControlContainer ) container;
+
+                    // As all the values are optional or defaulted, we can end here
+                    ppolicyRespContainer.grammarEndAllowed( true );
+                }
+            } );
+
+        super.transitions[PasswordPolicyResponseControlStates.START_STATE][PasswordPolicyResponseControlTags.TIME_BEFORE_EXPIRATION_TAG
+            .getValue()] = new GrammarTransition( PasswordPolicyResponseControlStates.START_STATE,
+            PasswordPolicyResponseControlStates.PPOLICY_ERROR_STATE,
+            PasswordPolicyResponseControlTags.TIME_BEFORE_EXPIRATION_TAG.getValue(), new GrammarAction(
+                "set time before expiration value" )
+            {
+                public void action( IAsn1Container container ) throws DecoderException
+                {
+                    PasswordPolicyResponseControlContainer ppolicyRespContainer = ( PasswordPolicyResponseControlContainer ) container;
+                    Value value = ppolicyRespContainer.getCurrentTLV().getValue();
+                    try
+                    {
+                        int timeBeforeExp = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
+                        
+                        if( IS_DEBUG )
+                        {
+                            LOG.debug( "timeBeforeExpiration {}", timeBeforeExp );
+                        }
+                        
+                        ppolicyRespContainer.getPasswordPolicyResponseControl().setTimeBeforeExpiration( timeBeforeExp );
+                    }
+                    catch ( IntegerDecoderException e )
+                    {
+                        String msg = I18n.err( I18n.ERR_04028 );
+                        LOG.error( msg, e );
+                        throw new DecoderException( msg );
+                    }
+
+                    ppolicyRespContainer.grammarEndAllowed( true );
+                }
+            } );
+
+        super.transitions[PasswordPolicyResponseControlStates.PPOLICY_ERROR_STATE][UniversalTag.ENUMERATED_TAG] = new GrammarTransition(
+            PasswordPolicyResponseControlStates.PPOLICY_ERROR_STATE, PasswordPolicyResponseControlStates.END_STATE,
+            UniversalTag.ENUMERATED_TAG, new GrammarAction( "set ppolicy error value" )
+            {
+                public void action( IAsn1Container container ) throws DecoderException
+                {
+                    PasswordPolicyResponseControlContainer ppolicyRespContainer = ( PasswordPolicyResponseControlContainer ) container;
+
+                    setPasswordPolicyError( ppolicyRespContainer );
+                    
+                    ppolicyRespContainer.grammarEndAllowed( true );
+                }
+            } );
+
+        super.transitions[PasswordPolicyResponseControlStates.START_STATE][PasswordPolicyResponseControlTags.GRACE_AUTHNS_REMAINING_TAG
+            .getValue()] = new GrammarTransition( PasswordPolicyResponseControlStates.START_STATE,
+            PasswordPolicyResponseControlStates.PPOLICY_ERROR_STATE,
+            PasswordPolicyResponseControlTags.GRACE_AUTHNS_REMAINING_TAG.getValue(), new GrammarAction(
+                "set number of grace authentications remaining" )
+            {
+                public void action( IAsn1Container container ) throws DecoderException
+                {
+                    PasswordPolicyResponseControlContainer ppolicyRespContainer = ( PasswordPolicyResponseControlContainer ) container;
+                    Value value = ppolicyRespContainer.getCurrentTLV().getValue();
+                    try
+                    {
+                        int graceAuthNum = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
+                        
+                        if( IS_DEBUG )
+                        {
+                            LOG.debug( "graceAuthNsRemaining {}", graceAuthNum );
+                        }
+                        
+                        ppolicyRespContainer.getPasswordPolicyResponseControl().setGraceAuthNsRemaining( graceAuthNum );
+                    }
+                    catch ( IntegerDecoderException e )
+                    {
+                        String msg = I18n.err( I18n.ERR_04028 );
+                        LOG.error( msg, e );
+                        throw new DecoderException( msg );
+                    }
+
+                    ppolicyRespContainer.grammarEndAllowed( true );
+                }
+            } );
+
+        super.transitions[PasswordPolicyResponseControlStates.START_STATE][UniversalTag.ENUMERATED_TAG] = new GrammarTransition(
+            PasswordPolicyResponseControlStates.START_STATE, PasswordPolicyResponseControlStates.PPOLICY_ERROR_STATE,
+            UniversalTag.ENUMERATED_TAG, new GrammarAction( "set ppolicy error value" )
+            {
+                public void action( IAsn1Container container ) throws DecoderException
+                {
+                    PasswordPolicyResponseControlContainer ppolicyRespContainer = ( PasswordPolicyResponseControlContainer ) container;
+                    
+                    setPasswordPolicyError( ppolicyRespContainer );
+
+                    ppolicyRespContainer.grammarEndAllowed( true );
+                }
+            } );
+
+    }
+
+
+    /**
+     * read and set the Value of password policy error
+     *
+     * @param ppolicyRespContainer the container holding PasswordPolicyResponceControl
+     * @throws DecoderException
+     */
+    private void setPasswordPolicyError( PasswordPolicyResponseControlContainer ppolicyRespContainer ) throws DecoderException
+    {
+        Value value = ppolicyRespContainer.getCurrentTLV().getValue();
+        try
+        {
+            int errorNum = IntegerDecoder.parse( value,
+                PasswordPolicyErrorEnum.PASSWORD_EXPIRED.getValue(),
+                PasswordPolicyErrorEnum.PASSWORD_IN_HISTORY.getValue() );
+            
+            if( IS_DEBUG )
+            {
+                LOG.debug( "password policy error {}", errorNum );
+            }
+            
+            ppolicyRespContainer.getPasswordPolicyResponseControl().setPasswordPolicyError(
+                PasswordPolicyErrorEnum.get( errorNum ) );
+        }
+        catch ( IntegerDecoderException e )
+        {
+            String msg = I18n.err( I18n.ERR_04028 );
+            LOG.error( msg, e );
+            throw new DecoderException( msg );
+        }
+
+    }
+
+    public static IGrammar getInstance()
+    {
+        return INSTANCE;
+    }
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlStates.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlStates.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlStates.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlStates.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,104 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import org.apache.directory.shared.asn1.ber.grammar.IGrammar;
+import org.apache.directory.shared.asn1.ber.grammar.IStates;
+
+
+/**
+ * various states used in {@link PasswordPolicyResponseControlGrammar}.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControlStates implements IStates
+{
+
+    public static final int START_STATE = 0;
+
+    public static final int PPOLICY_TIME_BEFORE_EXPIRATION_STATE = 1;
+
+    public static final int PPOLICY_GRACE_AUTHNS_REMAINING_STATE = 2;
+
+    public static final int PPOLICY_ERROR_STATE = 3;
+
+    public static final int END_STATE = 4;
+
+    private static PasswordPolicyResponseControlStates instance = new PasswordPolicyResponseControlStates();
+
+    public static final String[] ppolicyStateString = new String[]
+        { 
+          "START_STATE",
+          "PPOLICY_TIME_BEFORE_EXPIRATION_STATE",
+          "PPOLICY_GRACE_AUTHNS_REMAINING_STATE",
+          "PPOLICY_ERROR_STATE"
+        };
+
+
+    private PasswordPolicyResponseControlStates()
+    {
+    }
+
+
+    /**
+     * Get an instance of this class
+     * 
+     * @return An instance on this class
+     */
+    public static IStates getInstance()
+    {
+        return instance;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getGrammarName( IGrammar grammar )
+    {
+        if( grammar instanceof PasswordPolicyResponseControlGrammar )
+        {
+            return "PASSWORD_POLICY_RESPONSE_CONTROL_GRAMMAR";
+        }
+        
+        return "UNKNOWN_GRAMMAR";
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getGrammarName( int grammar )
+    {
+        return "PASSWORD_POLICY_RESPONSE_CONTROL_GRAMMAR";
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getState( int state )
+    {
+        return ( ( state == GRAMMAR_END ) ? "PASSWORD_POLICY_RESPONSE_CONTROL_GRAMMAR" : ppolicyStateString[state] );
+    }
+
+}

Added: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTags.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTags.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTags.java (added)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTags.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,46 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+/**
+ * Tags used for decoding PasswordPolicyResponseControl.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public enum PasswordPolicyResponseControlTags
+{
+    TIME_BEFORE_EXPIRATION_TAG(0x0080), GRACE_AUTHNS_REMAINING_TAG(0x00A1);
+
+    /** Internal value for each tag */
+    private int value;
+
+
+    private PasswordPolicyResponseControlTags( int value )
+    {
+        this.value = value;
+    }
+
+
+    public int getValue()
+    {
+        return value;
+    }
+}

Added: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTest.java?rev=954440&view=auto
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTest.java (added)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/codec/controls/ppolicy/PasswordPolicyResponseControlTest.java Mon Jun 14 13:19:19 2010
@@ -0,0 +1,67 @@
+/*
+ *   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.codec.controls.ppolicy;
+
+
+import java.nio.ByteBuffer;
+
+import org.apache.directory.shared.asn1.ber.Asn1Decoder;
+import org.junit.Ignore;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+
+/**
+ * PasswordPolicyResponseControlTest.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class PasswordPolicyResponseControlTest
+{
+    @Test
+    @Ignore( "failing due to a unknown issue related to reading the data from PasswordPolicyResponseControlTags.GRACE_AUTHNS_REMAINING_TAG" )
+    public void testDecodeResp() throws Exception
+    {
+        Asn1Decoder decoder = new PasswordPolicyResponseControlDecoder();
+        ByteBuffer bb = ByteBuffer.allocate( 5 );
+
+        bb.put( new byte[]
+            { 
+             0x30, 0x03,
+             // for the below 0x0080 tag the data is coming correctly
+              //(byte)0x0080, 1, 1, //  
+             // FIXME if I give the below tag 0x00A1 the data is coming as empty array
+             // the below tag is PasswordPolicyResponseControlTags.GRACE_AUTHNS_REMAINING_TAG
+               ( byte ) 0x00A1, 0x01, 0 // 
+            } );
+
+        bb.flip();
+
+        PasswordPolicyResponseControlContainer container = new PasswordPolicyResponseControlContainer();
+        container.setPasswordPolicyResponseControl( new PasswordPolicyResponseControl() );
+
+        decoder.decode( bb, container );
+
+        PasswordPolicyResponseControl control = container.getPasswordPolicyResponseControl();
+        //assertEquals( 1, control.getTimeBeforeExpiration() );
+        assertEquals( 1, control.getGraceAuthNsRemaining() );
+    }
+}