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.ber.grammar.AbstractGrammar;
024import org.apache.directory.api.asn1.ber.grammar.Grammar;
025import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
026import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.util.Strings;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034
035/**
036 * This class implements the PasswordModifyResponse extended operation's ASN.1 grammer. 
037 * All the actions are declared in this class. As it is a singleton, 
038 * these declaration are only done once. The grammar is :
039 * 
040 * <pre>
041 *  PasswdModifyResponseValue ::= SEQUENCE {
042 *      genPasswd       [0]     OCTET STRING OPTIONAL }
043 * </pre>
044 * 
045 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046 */
047
048public class PasswordModifyResponseGrammar extends AbstractGrammar<PasswordModifyResponseContainer>
049{
050
051    /** logger */
052    private static final Logger LOG = LoggerFactory.getLogger( PasswordModifyResponseGrammar.class );
053
054    /** The instance of grammar. PasswdModifyResponseGrammar is a singleton */
055    private static Grammar<PasswordModifyResponseContainer> instance = new PasswordModifyResponseGrammar();
056
057
058    /**
059     * Creates a new PasswordModifyResponseGrammar object.
060     */
061    @SuppressWarnings("unchecked")
062    public PasswordModifyResponseGrammar()
063    {
064        setName( PasswordModifyResponseGrammar.class.getName() );
065
066        // Create the transitions table
067        super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
068            .ordinal()][256];
069
070        /**
071         * Transition from init state to PasswordModify Response Value
072         * 
073         * PasswdModifyResponseValue ::= SEQUENCE {
074         *     ...
075         *     
076         * Creates the PasswdModifyResponse object
077         */
078        super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
079            new GrammarTransition<PasswordModifyResponseContainer>(
080                PasswordModifyResponseStatesEnum.START_STATE,
081                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
082                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
083                    "Init PasswordModifyResponse" )
084                {
085                    public void action( PasswordModifyResponseContainer container )
086                    {
087                        if ( container.getCurrentTLV().getLength() == 0 )
088                        { 
089                            // We may have nothing left
090                            container.setGrammarEndAllowed( true );
091                        }
092                    }
093                } );
094
095        /**
096         * Transition from PasswordModify Response Value to genPassword
097         *
098         * PasswdModifyResponseValue ::= SEQUENCE {
099         *     genPassword    [0]  OCTET STRING OPTIONAL
100         *     ...
101         *     
102         * Set the userIdentity into the PasswdModifyResponset instance.
103         */
104        super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
105            new GrammarTransition<PasswordModifyResponseContainer>(
106                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
107                PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
108                PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
109                new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
110                {
111                    public void action( PasswordModifyResponseContainer container )
112                    {
113                        BerValue value = container.getCurrentTLV().getValue();
114
115                        byte[] genPassword = value.getData();
116
117                        if ( LOG.isDebugEnabled() )
118                        {
119                            LOG.debug( I18n.msg( I18n.MSG_08205_GEN_PASSWORD ) );
120                        }
121
122                        if ( genPassword == null )
123                        {
124                            genPassword = Strings.EMPTY_BYTES;
125                        }
126
127                        container.getPwdModifyResponse().setGenPassword( genPassword );
128
129                        // We may have nothing left
130                        container.setGrammarEndAllowed( true );
131                    }
132                } );
133    }
134
135
136    /**
137     * This class is a singleton.
138     * 
139     * @return An instance on this grammar
140     */
141    public static Grammar<PasswordModifyResponseContainer> getInstance()
142    {
143        return instance;
144    }
145}