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    @SuppressWarnings("unchecked")
064    public PasswordModifyResponseGrammar()
065    {
066        setName( PasswordModifyResponseGrammar.class.getName() );
067
068        // Create the transitions table
069        super.transitions = new GrammarTransition[PasswordModifyResponseStatesEnum.LAST_PASSWORD_MODIFY_RESPONSE_STATE
070            .ordinal()][256];
071
072        /**
073         * Transition from init state to PasswordModify Response Value
074         * 
075         * PasswdModifyResponseValue ::= SEQUENCE {
076         *     ...
077         *     
078         * Creates the PasswdModifyResponse object
079         */
080        super.transitions[PasswordModifyResponseStatesEnum.START_STATE.ordinal()][UniversalTag.SEQUENCE.getValue()] =
081            new GrammarTransition<PasswordModifyResponseContainer>(
082                PasswordModifyResponseStatesEnum.START_STATE,
083                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
084                UniversalTag.SEQUENCE.getValue(), new GrammarAction<PasswordModifyResponseContainer>(
085                    "Init PasswordModifyResponse" )
086                {
087                    public void action( PasswordModifyResponseContainer container )
088                    {
089                        PasswordModifyResponseDecorator passwordModifyResponse = new PasswordModifyResponseDecorator(
090                            LdapApiServiceFactory.getSingleton(), new PasswordModifyResponseImpl() );
091                        container.setPasswordModifyResponse( passwordModifyResponse );
092
093                        // We may have nothing left
094                        container.setGrammarEndAllowed( true );
095                    }
096                } );
097
098        /**
099         * Transition from PasswordModify Response Value to genPassword
100         *
101         * PasswdModifyResponseValue ::= SEQUENCE {
102         *     genPassword    [0]  OCTET STRING OPTIONAL
103         *     ...
104         *     
105         * Set the userIdentity into the PasswdModifyResponset instance.
106         */
107        super.transitions[PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE.ordinal()][PasswordModifyResponseConstants.GEN_PASSWORD_TAG] =
108            new GrammarTransition<PasswordModifyResponseContainer>(
109                PasswordModifyResponseStatesEnum.PASSWORD_MODIFY_RESPONSE_SEQUENCE_STATE,
110                PasswordModifyResponseStatesEnum.GEN_PASSWORD_STATE,
111                PasswordModifyResponseConstants.GEN_PASSWORD_TAG,
112                new GrammarAction<PasswordModifyResponseContainer>( "Set PasswordModifyResponse user identity" )
113                {
114                    public void action( PasswordModifyResponseContainer container ) throws DecoderException
115                    {
116                        BerValue value = container.getCurrentTLV().getValue();
117
118                        byte[] genPassword = value.getData();
119
120                        if ( IS_DEBUG )
121                        {
122                            LOG.debug( "GenPassword = " + Strings.dumpBytes( genPassword ) );
123                        }
124
125                        if ( genPassword == null )
126                        {
127                            genPassword = Strings.EMPTY_BYTES;
128                        }
129
130                        ( ( PasswordModifyResponseDecorator ) container.getPwdModifyResponse() )
131                            .setGenPassword( genPassword );
132
133                        // We may have nothing left
134                        container.setGrammarEndAllowed( true );
135                    }
136                } );
137    }
138
139
140    /**
141     * This class is a singleton.
142     * 
143     * @return An instance on this grammar
144     */
145    public static Grammar<PasswordModifyResponseContainer> getInstance()
146    {
147        return instance;
148    }
149}