001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.extras.extended.ads_impl.pwdModify;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
025import org.apache.directory.api.asn1.ber.grammar.Grammar;
026import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
027import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
028import org.apache.directory.api.asn1.ber.tlv.BerValue;
029import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
030import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
031import org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl;
032import org.apache.directory.api.util.Strings;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * This class implements the PasswordModifyResponse extended operation's ASN.1 grammer. 
039 * All the actions are declared in this class. As it is a singleton, 
040 * these declaration are only done once. The grammar is :
041 * 
042 * <pre>
043 *  PasswdModifyResponseValue ::= SEQUENCE {
044 *      genPasswd       [0]     OCTET STRING OPTIONAL }
045 * </pre>
046 * 
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049
050public class PasswordModifyResponseGrammar extends AbstractGrammar<PasswordModifyResponseContainer>
051{
052
053    /** logger */
054    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseGrammar.class );
055
056    /** Speedup for logs */
057    static final boolean IS_DEBUG = LOG.isDebugEnabled();
058
059    /** The instance of grammar. PasswdModifyResponseGrammar is a singleton */
060    private static Grammar<PasswordModifyResponseContainer> instance = new PasswordModifyResponseGrammar();
061
062
063    /**
064     * Creates a new PasswordModifyResponseGrammar object.
065     */
066    @SuppressWarnings("unchecked")
067    public PasswordModifyResponseGrammar()
068    {
069        setName( PasswordModifyResponseGrammar.class.getName() );
070
071        // Create the transitions table
072        super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
073            .ordinal()][256];
074
075        /**
076         * Transition from init state to PasswordModify Response Value
077         * 
078         * PasswdModifyResponseValue ::= SEQUENCE {
079         *     ...
080         *     
081         * Creates the PasswdModifyResponse object
082         */
083        super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
084            new GrammarTransition<PasswordModifyResponseContainer>(
085                PasswordModifyResponseStatesEnum.START_STATE,
086                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
087                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
088                    "Init PasswordModifyResponse" )
089                {
090                    public void action( PasswordModifyResponseContainer container )
091                    {
092                        PasswordModifyResponseDecorator passwordModifyResponse = new PasswordModifyResponseDecorator(
093                            LdapApiServiceFactory.getSingleton(), new PasswordModifyResponseImpl() );
094                        container.setPasswordModifyResponse( passwordModifyResponse );
095
096                        // We may have nothing left
097                        container.setGrammarEndAllowed( true );
098                    }
099                } );
100
101        /**
102         * Transition from PasswordModify Response Value to genPassword
103         *
104         * PasswdModifyResponseValue ::= SEQUENCE {
105         *     genPassword    [0]  OCTET STRING OPTIONAL
106         *     ...
107         *     
108         * Set the userIdentity into the PasswdModifyResponset instance.
109         */
110        super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
111            new GrammarTransition<PasswordModifyResponseContainer>(
112                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
113                PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
114                PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
115                new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
116                {
117                    public void action( PasswordModifyResponseContainer container ) throws DecoderException
118                    {
119                        BerValue value = container.getCurrentTLV().getValue();
120
121                        byte[] genPassword = value.getData();
122
123                        if ( IS_DEBUG )
124                        {
125                            LOG.debug( "GenPassword = " + Strings.dumpBytes( genPassword ) );
126                        }
127
128                        if ( genPassword == null )
129                        {
130                            genPassword = Strings.EMPTY_BYTES;
131                        }
132
133                        container.getPwdModifyResponse().setGenPassword( genPassword );
134
135                        // We may have nothing left
136                        container.setGrammarEndAllowed( true );
137                    }
138                } );
139    }
140
141
142    /**
143     * This class is a singleton.
144     * 
145     * @return An instance on this grammar
146     */
147    public static Grammar<PasswordModifyResponseContainer> getInstance()
148    {
149        return instance;
150    }
151}